blob: 7b7b61795995afa2e32456280744fbd8aebc64e7 [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;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040059import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020061import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
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 Goyal83a8f042015-05-19 12:52:12 -070099import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale2df0622015-04-24 11:27:00 -0700105import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700106import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700107import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700108import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700109import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700110
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.io.DataInputStream;
112import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700114import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700118import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800119import java.lang.reflect.InvocationTargetException;
120import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700121import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700122import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700123import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700124import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700125import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700126import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700127import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000128import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130/**
131 * Default launcher application.
132 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100133public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700134 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800135 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
136 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700137 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700138 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Winson Chung83f59ab2015-05-05 17:21:58 -0700140 // Temporary flag
141 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
142
Daniel Sandlerf061f822013-06-27 13:59:36 -0400143 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700144 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700145 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400146 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700147 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700148
Dan Sandlerd5024042014-01-09 15:01:33 -0500149 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
150
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700154 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
Michael Jurka8b805b12012-04-18 14:23:14 -0700156 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700157 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700158
Mathew Inwood876a8462013-06-14 14:12:41 +0100159 /**
160 * IntentStarter uses request codes starting with this. This must be greater than all activity
161 * request codes used internally.
162 */
163 protected static final int REQUEST_LAST = 100;
164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
166
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800167 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 // To turn on these properties, type
170 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800171 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Winson Chung2672ff92012-05-04 16:22:30 -0700173 // The Intent extra that defines whether to ignore the launch animation
174 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400175 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: int
178 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700179 // Type: int
180 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700182 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
183 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
185 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700186 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700188 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700189 // Type: int
190 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
191 // Type: int
192 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
193 // Type: parcelable
194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000195 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000197 // Type: int[]
198 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Adam Cohen432609a2014-03-13 17:03:22 -0700200 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800201 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
202
Adam Cohen3ed316a2014-07-23 18:21:20 -0700203 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
204 static final String ACTION_FIRST_LOAD_COMPLETE =
205 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
206
Adam Cohen39a06042013-07-19 14:30:12 -0700207 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700208 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700209
Sunny Goyal594d76d2014-11-06 10:12:54 -0800210 private static final String QSB_WIDGET_ID = "qsb_widget_id";
211 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
212
Winson Chunga6945242014-01-08 14:04:34 -0800213 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
214
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700215 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800216 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700219
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700220 private boolean mIsSafeModeEnabled;
221
Adam Cohenc2d6e892014-10-16 09:49:24 -0700222 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
223 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700224 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700225
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700227 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
228 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700229 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000231 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
232 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
233
Winson Chunga2413752012-04-03 14:22:34 -0700234 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700235 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
236 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700237 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700238
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800239 private final BroadcastReceiver mCloseSystemDialogsReceiver
240 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800241 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 private LayoutInflater mInflater;
244
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700246 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800247 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700248 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800249 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700250 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700251
Sunny Goyalffe83f12014-08-14 17:39:34 -0700252 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700253 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700254
Adam Cohen091440a2015-03-18 14:16:05 -0700255 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800256 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800257 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700258
Michael Jurka0280c3b2010-09-17 15:00:07 -0700259 private int[] mTmpAddItemCellCoordinates = new int[2];
260
Winson Chung3d503fb2011-07-13 17:25:49 -0700261 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800262 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700263
Michael Jurka838a4ca2011-02-07 13:33:06 -0800264 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700265
Winson Chungc51db6a2011-10-05 11:44:49 -0700266 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700267
268 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700269 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700270
271 // Main container view for the widget tray screen.
272 private WidgetsContainerView mWidgetsView;
273
Winson Chungf0ea4d32011-06-06 14:27:16 -0700274 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800275 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700278 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
279 // scroll issues (because the workspace may not have been measured yet) and extra work.
280 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
281 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282
283 private SpannableStringBuilder mDefaultKeySsb = null;
284
Adam Cohen091440a2015-03-18 14:16:05 -0700285 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800287 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 private boolean mRestoring;
289 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700290 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291
Michael Jurka1e2f4652013-07-08 18:03:46 -0700292 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700293 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 private Bundle mSavedInstanceState;
296
Joe Onorato9c1289c2009-08-17 11:03:03 -0400297 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800298 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700299 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800300 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700301 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800302 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400303
Adam Cohen091440a2015-03-18 14:16:05 -0700304 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700305
Sunny Goyale2df0622015-04-24 11:27:00 -0700306 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700307
Adam Cohen61f560d2013-09-30 15:58:20 -0700308 private View.OnTouchListener mHapticFeedbackTouchListener;
309
Adam Cohended9f8d2010-11-03 13:25:16 -0700310 // Related to the auto-advancing of widgets
311 private final int ADVANCE_MSG = 1;
312 private final int mAdvanceInterval = 20000;
313 private final int mAdvanceStagger = 250;
314 private long mAutoAdvanceSentTime;
315 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700316 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700317 new HashMap<View, AppWidgetProviderInfo>();
318
Winson Chung400438b2011-01-16 17:53:48 -0800319 // Determines how long to wait after a rotation before restoring the screen orientation to
320 // match the sensor state.
321 private final int mRestoreScreenOrientationDelay = 500;
322
Adam Cohen091440a2015-03-18 14:16:05 -0700323 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700324
Adam Cohen1462de32012-07-24 22:34:36 -0700325 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700326 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700327
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700328 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700329 static Date sDateStamp = new Date();
330 static DateFormat sDateFormat =
331 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
332 static long sRunStart = System.currentTimeMillis();
333 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700334
Winson Chung46353de2012-02-16 14:05:10 -0800335 // We only want to get the SharedPreferences once since it does an FS stat each time we get
336 // it from the context.
337 private SharedPreferences mSharedPrefs;
338
Winson Chungf0c6ae02012-03-21 16:10:31 -0700339 // Holds the page that we need to animate to, and the icon views that we need to animate up
340 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700341 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700342 private Bitmap mFolderIconBitmap;
343 private Canvas mFolderIconCanvas;
344 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700345
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700346 private DeviceProfile mDeviceProfile;
347
Winson Chung13eb5272015-05-11 16:30:13 -0700348 // This is set to the view that launched the activity that navigated the user away from
349 // launcher. Since there is no callback for when the activity has finished launching, enable
350 // the press state and keep this reference to reset the press state when we return to launcher.
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 Goyaldfaccf62015-05-11 16:30:44 -0700427 mDeviceProfile = 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();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700435 mIconCache.flushInvalidIcons(mDeviceProfile);
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();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700461 mDeviceProfile.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.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700478 mModel.startLoader(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
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700482 mModel.startLoader(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 ||
Hyunyoung Songb7900832015-05-15 16:29:45 -0700807 requestCode == REQUEST_CREATE_APPWIDGET || requestCode == REQUEST_CREATE_SHORTCUT);
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 Chung13eb5272015-05-11 16:30:13 -07001022 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001023 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001024 // view after launching an app, as they may be depending on the UI to be static to
1025 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001026 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -07001027 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001028 } else if (mOnResumeState == State.WIDGETS) {
1029 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001030 }
1031 mOnResumeState = State.NONE;
1032
Winson Chungcd99cd32015-04-29 11:03:24 -07001033 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001034 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1035 // Otherwise, notify the callbacks if we are in all apps mode
1036 if (mState == State.APPS) {
1037 if (mLauncherCallbacks != null) {
1038 mLauncherCallbacks.onAllAppsShown();
1039 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001040 }
1041 }
1042
Craig Mautner360310b2012-10-26 15:13:08 -07001043 // Background was set to gradient in onPause(), restore to black if in all apps.
1044 setWorkspaceBackground(mState == State.WORKSPACE);
1045
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001046 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001047 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001048 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001049 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001050 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001051 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001053 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001054 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1055 // execute them here
1056 long startTimeCallbacks = 0;
1057 if (DEBUG_RESUME_TIME) {
1058 startTimeCallbacks = System.currentTimeMillis();
1059 }
1060
Michael Jurka1e2f4652013-07-08 18:03:46 -07001061 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1062 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001063 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001064 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001065 if (DEBUG_RESUME_TIME) {
1066 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1067 (System.currentTimeMillis() - startTimeCallbacks));
1068 }
Michael Jurka447bf842013-05-15 14:52:15 +02001069 }
Michael Jurka54554252013-08-01 12:52:23 +02001070 if (mOnResumeCallbacks.size() > 0) {
1071 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1072 mOnResumeCallbacks.get(i).run();
1073 }
1074 mOnResumeCallbacks.clear();
1075 }
Winson Chunge4e50662012-01-23 14:45:13 -08001076
1077 // Reset the pressed state of icons that were locked in the press state while activities
1078 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001079 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001080 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001081 mWaitingForResume.setStayPressed(false);
1082 }
Winson Chung82963d52013-10-09 11:20:57 -07001083
Adam Cohen06dff352012-06-01 17:17:08 -07001084 // It is possible that widgets can receive updates while launcher is not in the foreground.
1085 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1086 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1087 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001088 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001089 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001090
Michael Jurka447bf842013-05-15 14:52:15 +02001091 if (DEBUG_RESUME_TIME) {
1092 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1093 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001094
1095 if (mWorkspace.getCustomContentCallbacks() != null) {
1096 // If we are resuming and the custom content is the current page, we call onShow().
1097 // It is also poassible that onShow will instead be called slightly after first layout
1098 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1099 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001100 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001101 }
1102 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001103 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001104 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001105
Sunny Goyal756adbc2015-04-16 15:20:51 -07001106 if (!isWorkspaceLoading()) {
1107 // Process any items that were added while Launcher was away.
1108 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1109 }
Adam Cohen9211d422014-10-07 18:14:53 -07001110
1111 if (mLauncherCallbacks != null) {
1112 mLauncherCallbacks.onResume();
1113 }
Adam Cohen06dff352012-06-01 17:17:08 -07001114 }
1115
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001117 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001118 // Ensure that items added to Launcher are queued until Launcher returns
1119 InstallShortcutReceiver.enableInstallQueue();
1120
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001121 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001122 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001123 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001124 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001125
1126 // We call onHide() aggressively. The custom content callbacks should be able to
1127 // debounce excess onHide calls.
1128 if (mWorkspace.getCustomContentCallbacks() != null) {
1129 mWorkspace.getCustomContentCallbacks().onHide();
1130 }
Romain Guycbb89e42009-06-08 15:52:54 -07001131
Adam Cohen9211d422014-10-07 18:14:53 -07001132 if (mLauncherCallbacks != null) {
1133 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001134 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001135 }
1136
1137 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001138 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1139 // by a onResume or by scrolling otherwise.
1140 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001141
1142 // Custom content is completely hidden
1143 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001144
1145 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1146 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001147
1148 // Indicates whether the user is allowed to scroll away from the custom content.
1149 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001150 }
1151
Adam Cohenc2d6e892014-10-16 09:49:24 -07001152 public interface LauncherOverlay {
1153
1154 /**
1155 * Touch interaction leading to overscroll has begun
1156 */
1157 public void onScrollInteractionBegin();
1158
1159 /**
1160 * Touch interaction related to overscroll has ended
1161 */
1162 public void onScrollInteractionEnd();
1163
1164 /**
1165 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1166 * screen (or in the case of RTL, the rightmost screen).
1167 */
1168 public void onScrollChange(int progress, boolean rtl);
1169
1170 /**
1171 * Screen has stopped scrolling
1172 */
1173 public void onScrollSettled();
1174
1175 /**
1176 * This method can be called by the Launcher in order to force the LauncherOverlay
1177 * to exit fully immersive mode.
1178 */
1179 public void forceExitFullImmersion();
1180 }
1181
Winson Chung0f785722015-04-08 10:27:49 -07001182 public interface LauncherAppsCallbacks {
1183 /**
1184 * Updates launcher to the available space that AllApps can take so as not to overlap with
1185 * any other views.
1186 */
1187 public void onAllAppsBoundsChanged(Rect bounds);
1188
1189 /**
1190 * Called to dismiss all apps if it is showing.
1191 */
1192 public void dismissAllApps();
1193 }
1194
Adam Cohenc2d6e892014-10-16 09:49:24 -07001195 public interface LauncherOverlayCallbacks {
1196 /**
1197 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1198 * however it doesn't modify any state within the launcher.
1199 */
1200 public boolean canEnterFullImmersion();
1201
1202 /**
1203 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1204 * eg. by occupying the full screen and handling all touch events.
1205 *
1206 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1207 * case, Launcher will modify any necessary state and assumes the overlay is
1208 * handling all interaction. If false, the LauncherOverlay should cancel any
1209 *
1210 */
1211 public boolean enterFullImmersion();
1212
1213 /**
1214 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1215 * full control over UI and state.
1216 */
1217 public void exitFullImmersion();
1218 }
1219
1220 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1221
1222 @Override
1223 public boolean canEnterFullImmersion() {
1224 return mState == State.WORKSPACE;
1225 }
1226
1227 @Override
1228 public boolean enterFullImmersion() {
1229 if (mState == State.WORKSPACE) {
1230 // When fully immersed, disregard any touches which fall through.
1231 mDragLayer.setBlockTouch(true);
1232 return true;
1233 }
1234 return false;
1235 }
1236
1237 @Override
1238 public void exitFullImmersion() {
1239 mDragLayer.setBlockTouch(false);
1240 }
1241 }
1242
Jorim Jaggid017f882014-01-14 17:08:48 -08001243 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001244 if (mLauncherCallbacks != null) {
1245 return mLauncherCallbacks.hasSettings();
1246 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001247 return false;
1248 }
1249
Adam Cohen9211d422014-10-07 18:14:53 -07001250 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001251 CustomContentCallbacks callbacks, String description) {
1252 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001253 }
1254
Adam Cohenedb40762013-07-18 16:45:45 -07001255 // The custom content needs to offset its content to account for the QSB
1256 public int getTopOffsetForCustomContent() {
1257 return mWorkspace.getPaddingTop();
1258 }
1259
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001260 @Override
1261 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001262 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001263 if (mModel.isCurrentCallbacks(this)) {
1264 mModel.stopLoader();
1265 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001266 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1267
Romain Guy13c2e7b2010-03-10 19:45:00 -08001268 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001269 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001270
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001271 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001272 @Override
1273 public void onWindowFocusChanged(boolean hasFocus) {
1274 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001275 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001276
1277 if (mLauncherCallbacks != null) {
1278 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1279 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001280 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 private boolean acceptFilter() {
1283 final InputMethodManager inputManager = (InputMethodManager)
1284 getSystemService(Context.INPUT_METHOD_SERVICE);
1285 return !inputManager.isFullscreenMode();
1286 }
1287
1288 @Override
1289 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001290 final int uniChar = event.getUnicodeChar();
1291 final boolean handled = super.onKeyDown(keyCode, event);
1292 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1293 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1295 keyCode, event);
1296 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001297 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001298 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001299 // showSearchDialog()
1300 // If there are multiple keystrokes before the search dialog takes focus,
1301 // onSearchRequested() will be called for every keystroke,
1302 // but it is idempotent, so it's fine.
1303 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
1305 }
1306
Joe Onorato8a9625e2010-01-28 15:55:35 -08001307 // Eat the long press event so the keyboard doesn't come up.
1308 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1309 return true;
1310 }
1311
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 return handled;
1313 }
1314
Karl Rosaen138a0412009-04-23 19:00:21 -07001315 private String getTypedText() {
1316 return mDefaultKeySsb.toString();
1317 }
1318
1319 private void clearTypedText() {
1320 mDefaultKeySsb.clear();
1321 mDefaultKeySsb.clearSpans();
1322 Selection.setSelection(mDefaultKeySsb, 0);
1323 }
1324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001326 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1327 * State
1328 */
1329 private static State intToState(int stateOrdinal) {
1330 State state = State.WORKSPACE;
1331 final State[] stateValues = State.values();
1332 for (int i = 0; i < stateValues.length; i++) {
1333 if (stateValues[i].ordinal() == stateOrdinal) {
1334 state = stateValues[i];
1335 break;
1336 }
1337 }
1338 return state;
1339 }
1340
1341 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001342 * Restores the previous state, if it exists.
1343 *
1344 * @param savedState The previous state.
1345 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001346 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 private void restoreState(Bundle savedState) {
1348 if (savedState == null) {
1349 return;
1350 }
1351
Michael Jurka883f55b2010-10-21 15:47:14 -07001352 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001353 if (state == State.APPS || state == State.WIDGETS) {
1354 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001355 }
1356
Adam Cohen21cd0022013-10-09 18:57:02 -07001357 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1358 PagedView.INVALID_RESTORE_PAGE);
1359 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001360 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 }
1362
Winson Chung3d503fb2011-07-13 17:25:49 -07001363 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001364 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001365
Winson Chung3d503fb2011-07-13 17:25:49 -07001366 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1367 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001368 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001369 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1370 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001371 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1372 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001373 AppWidgetProviderInfo info = savedState.getParcelable(
1374 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001375 mPendingAddWidgetInfo = info == null ?
1376 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1377
Adam Cohen4637b5a2013-11-04 18:21:24 -08001378 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001379 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 mRestoring = true;
1381 }
1382
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001383 mItemIdToViewId = (HashMap<Integer, Integer>)
1384 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
1386
1387 /**
1388 * Finds all the views we need and configure them properly.
1389 */
1390 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001391 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001392
Craig Mautner360310b2012-10-26 15:13:08 -07001393 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001394 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001395 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1396 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001397 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001398 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001399
John Spurlock77e1f472013-09-11 10:09:51 -04001400 mLauncherView.setSystemUiVisibility(
1401 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001402 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403
Winson Chung4c98d922011-05-31 16:50:48 -07001404 // Setup the drag layer
1405 mDragLayer.setup(this, dragController);
1406
Winson Chung3d503fb2011-07-13 17:25:49 -07001407 // Setup the hotseat
1408 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1409 if (mHotseat != null) {
1410 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001411 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001412 }
1413
Jorim Jaggid017f882014-01-14 17:08:48 -08001414 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001415 View widgetButton = findViewById(R.id.widget_button);
1416 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001417 @Override
1418 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001419 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001420 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001421 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001422 }
1423 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001424 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1425
1426 View wallpaperButton = findViewById(R.id.wallpaper_button);
1427 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001428 @Override
1429 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001430 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001431 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001432 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001433 }
1434 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001435 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1436
1437 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001438 if (hasSettings()) {
1439 settingsButton.setOnClickListener(new OnClickListener() {
1440 @Override
1441 public void onClick(View arg0) {
1442 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001443 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001444 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001445 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001446 });
1447 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1448 } else {
1449 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001450 }
1451
Adam Cohendbdff6b2013-09-18 19:09:15 -07001452 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001453
Winson Chung4c98d922011-05-31 16:50:48 -07001454 // Setup the workspace
1455 mWorkspace.setHapticFeedbackEnabled(false);
1456 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001457 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001458 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001459
Winson Chungf0ea4d32011-06-06 14:27:16 -07001460 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001461 mSearchDropTargetBar = (SearchDropTargetBar)
1462 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001463
Winson Chungb745afb2015-03-02 11:51:23 -08001464 // Setup Apps
1465 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001466 if (isAllAppsSearchOverridden()) {
1467 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001468 }
Winson Chungb745afb2015-03-02 11:51:23 -08001469
Winson Chungf0ea4d32011-06-06 14:27:16 -07001470 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001471 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001472
Winson Chung3d503fb2011-07-13 17:25:49 -07001473 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001474 dragController.setDragScoller(mWorkspace);
1475 dragController.setScrollView(mDragLayer);
1476 dragController.setMoveTarget(mWorkspace);
1477 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001478 if (mSearchDropTargetBar != null) {
1479 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001480 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001481 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001482
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001483 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001484 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001485 mWeightWatcher = new WeightWatcher(this);
1486 mWeightWatcher.setAlpha(0.5f);
1487 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001488 new FrameLayout.LayoutParams(
1489 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001490 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001491 Gravity.BOTTOM)
1492 );
Adam Cohen39a06042013-07-19 14:30:12 -07001493
1494 boolean show = shouldShowWeightWatcher();
1495 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001496 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 }
1498
1499 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001500 * Sets the all apps button. This method is called from {@link Hotseat}.
1501 */
1502 public void setAllAppsButton(View allAppsButton) {
1503 mAllAppsButton = allAppsButton;
1504 }
1505
1506 public View getAllAppsButton() {
1507 return mAllAppsButton;
1508 }
1509
1510 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 * Creates a view representing a shortcut.
1512 *
1513 * @param info The data structure describing the shortcut.
1514 *
1515 * @return A View inflated from R.layout.application.
1516 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001517 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001518 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
1520
1521 /**
1522 * Creates a view representing a shortcut inflated from the specified resource.
1523 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 * @param parent The group the shortcut belongs to.
1525 * @param info The data structure describing the shortcut.
1526 *
1527 * @return A View inflated from layoutResId.
1528 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001529 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
1530 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.application,
1531 parent, false);
1532 favorite.applyFromShortcutInfo(info, mIconCache);
1533 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001535 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 return favorite;
1537 }
1538
1539 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 * Add a shortcut to the workspace.
1541 *
1542 * @param data The intent describing the shortcut.
1543 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001545 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001546 int cellY) {
1547 int[] cellXY = mTmpAddItemCellCoordinates;
1548 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001549 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001550
Michael Jurkad3ef3062010-11-23 16:23:58 -08001551 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001552
Sunny Goyal2350bc92014-10-14 16:42:54 -07001553 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001554 if (info == null) {
1555 return;
1556 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001557 final View view = createShortcut(info);
1558
Adam Cohenfbba09b2011-07-18 21:43:05 -07001559 // First we check if we already know the exact location where we want to add this item.
1560 if (cellX >= 0 && cellY >= 0) {
1561 cellXY[0] = cellX;
1562 cellXY[1] = cellY;
1563 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001564
1565 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001566 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001567 true, null,null)) {
1568 return;
1569 }
1570 DragObject dragObject = new DragObject();
1571 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001572 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1573 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001574 return;
1575 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001576 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001577 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001578 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001579 foundCellSpan = (result != null);
1580 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001581 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001582 }
1583
1584 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001585 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001586 return;
1587 }
1588
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001589 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590
1591 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001592 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001593 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 }
1595 }
1596
Adam Cohen2f093b62012-04-30 18:59:53 -07001597 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1598 int minHeight) {
1599 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001600 // We want to account for the extra amount of padding that we are adding to the widget
1601 // to ensure that it gets the full amount of space that it has requested
1602 int requiredWidth = minWidth + padding.left + padding.right;
1603 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001604 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001605 }
1606
Adam Cohen2f093b62012-04-30 18:59:53 -07001607 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1608 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001609 }
1610
Adam Cohen2f093b62012-04-30 18:59:53 -07001611 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1612 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001613 }
1614
Adam Cohen2f093b62012-04-30 18:59:53 -07001615 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1616 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001617 }
1618
Adam Cohen2f093b62012-04-30 18:59:53 -07001619 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1620 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001621 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001622 }
1623
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001625 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001627 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 */
Adam Cohen091440a2015-03-18 14:16:05 -07001629 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001630 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1631
1632 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001633 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001634 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1635 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001636 }
Romain Guycbb89e42009-06-08 15:52:54 -07001637
Adam Cohen59400422014-03-05 18:07:04 -08001638 if (appWidgetInfo.isCustomWidget) {
1639 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001640 }
1641
Adam Cohen59400422014-03-05 18:07:04 -08001642 LauncherAppWidgetInfo launcherInfo;
1643 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1644 launcherInfo.spanX = info.spanX;
1645 launcherInfo.spanY = info.spanY;
1646 launcherInfo.minSpanX = info.minSpanX;
1647 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001648 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001649
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001651 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652
1653 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001654 if (hostView == null) {
1655 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001656 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1657 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001658 } else {
1659 // The AppWidgetHostView has already been inflated and instantiated
1660 launcherInfo.hostView = hostView;
1661 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001663 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001664 launcherInfo.notifyWidgetSizeChanged(this);
1665
Adam Cohen59400422014-03-05 18:07:04 -08001666 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1667 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001668
1669 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001671 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 }
Romain Guycbb89e42009-06-08 15:52:54 -07001673
Adam Cohended9f8d2010-11-03 13:25:16 -07001674 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1675 @Override
1676 public void onReceive(Context context, Intent intent) {
1677 final String action = intent.getAction();
1678 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1679 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001680 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001681 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001682
Winson Chungc100e8e2011-08-09 16:02:43 -07001683 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001684 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001685 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001686 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001687 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001688 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1690 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001691 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001692 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1693 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001694 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001695 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1696 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1697 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001698 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001699 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1700 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001701 }
1702 }
1703 };
1704
1705 @Override
1706 public void onAttachedToWindow() {
1707 super.onAttachedToWindow();
1708
1709 // Listen for broadcasts related to user-presence
1710 final IntentFilter filter = new IntentFilter();
1711 filter.addAction(Intent.ACTION_SCREEN_OFF);
1712 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001713 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001714 if (ENABLE_DEBUG_INTENTS) {
1715 filter.addAction(DebugIntents.DELETE_DATABASE);
1716 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1717 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001719 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001720 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001721 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001722 mVisible = true;
1723 }
1724
Adam Cohen0b395352014-06-09 22:54:36 +00001725 /**
1726 * Sets up transparent navigation and status bars in LMP.
1727 * This method is a no-op for other platform versions.
1728 */
Sunny Goyald0091012015-01-20 15:55:34 -08001729 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001730 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001731 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001732 Window window = getWindow();
1733 window.getAttributes().systemUiVisibility |=
1734 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1735 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1736 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1737 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1738 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1739 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1740 window.setStatusBarColor(Color.TRANSPARENT);
1741 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001742 }
1743 }
1744
Adam Cohended9f8d2010-11-03 13:25:16 -07001745 @Override
1746 public void onDetachedFromWindow() {
1747 super.onDetachedFromWindow();
1748 mVisible = false;
1749
Adam Cohend113e0c2010-11-11 10:48:05 -08001750 if (mAttached) {
1751 unregisterReceiver(mReceiver);
1752 mAttached = false;
1753 }
Winson Chungb745afb2015-03-02 11:51:23 -08001754 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 }
1756
1757 public void onWindowVisibilityChanged(int visibility) {
1758 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001759 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001760 // The following code used to be in onResume, but it turns out onResume is called when
1761 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1762 // is a more appropriate event to handle
1763 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001764 if (!mWorkspaceLoading) {
1765 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001766 // We want to let Launcher draw itself at least once before we force it to build
1767 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001768 // apps is nice and speedy.
1769 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001770 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001771 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001772 if (mStarted) return;
1773 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001774 // We delay the layer building a bit in order to give
1775 // other message processing a time to run. In particular
1776 // this avoids a delay in hiding the IME if it was
1777 // currently shown, because doing that may involve
1778 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001779 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001780 final ViewTreeObserver.OnDrawListener listener = this;
1781 mWorkspace.post(new Runnable() {
1782 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001783 if (mWorkspace != null &&
1784 mWorkspace.getViewTreeObserver() != null) {
1785 mWorkspace.getViewTreeObserver().
1786 removeOnDrawListener(listener);
1787 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001788 }
1789 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001790 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001791 }
1792 });
1793 }
1794 clearTypedText();
1795 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001796 }
1797
Adam Cohen091440a2015-03-18 14:16:05 -07001798 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001799 mHandler.removeMessages(ADVANCE_MSG);
1800 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1801 mHandler.sendMessageDelayed(msg, delay);
1802 mAutoAdvanceSentTime = System.currentTimeMillis();
1803 }
1804
Adam Cohen091440a2015-03-18 14:16:05 -07001805 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001806 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1807 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1808 mAutoAdvanceRunning = autoAdvanceRunning;
1809 if (autoAdvanceRunning) {
1810 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1811 sendAdvanceMessage(delay);
1812 } else {
1813 if (!mWidgetsToAdvance.isEmpty()) {
1814 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1815 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1816 }
1817 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001818 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001819 }
1820 }
1821 }
1822
1823 private final Handler mHandler = new Handler() {
1824 @Override
1825 public void handleMessage(Message msg) {
1826 if (msg.what == ADVANCE_MSG) {
1827 int i = 0;
1828 for (View key: mWidgetsToAdvance.keySet()) {
1829 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1830 final int delay = mAdvanceStagger * i;
1831 if (v instanceof Advanceable) {
1832 postDelayed(new Runnable() {
1833 public void run() {
1834 ((Advanceable) v).advance();
1835 }
1836 }, delay);
1837 }
1838 i++;
1839 }
1840 sendAdvanceMessage(mAdvanceInterval);
1841 }
1842 }
1843 };
1844
1845 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001846 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001847 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1848 if (v instanceof Advanceable) {
1849 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001850 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001851 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001852 }
1853 }
1854
1855 void removeWidgetToAutoAdvance(View hostView) {
1856 if (mWidgetsToAdvance.containsKey(hostView)) {
1857 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001858 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001859 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001860 }
1861
Joe Onorato9c1289c2009-08-17 11:03:03 -04001862 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001863 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001864 launcherInfo.hostView = null;
1865 }
1866
Hyunyoung Song3f471442015-04-08 19:01:34 -07001867 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001868 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1869 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001870 }
1871
Winson Chunga6945242014-01-08 14:04:34 -08001872 public DragLayer getDragLayer() {
1873 return mDragLayer;
1874 }
1875
Winson Chungb745afb2015-03-02 11:51:23 -08001876 public AppsContainerView getAppsView() {
1877 return mAppsView;
1878 }
1879
Hyunyoung Song3f471442015-04-08 19:01:34 -07001880 public WidgetsContainerView getWidgetsView() {
1881 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001882 }
1883
Winson Chunga6945242014-01-08 14:04:34 -08001884 public Workspace getWorkspace() {
1885 return mWorkspace;
1886 }
1887
1888 public Hotseat getHotseat() {
1889 return mHotseat;
1890 }
1891
Jorim Jaggid017f882014-01-14 17:08:48 -08001892 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001893 return mOverviewPanel;
1894 }
1895
1896 public SearchDropTargetBar getSearchBar() {
1897 return mSearchDropTargetBar;
1898 }
1899
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001900 public LauncherAppWidgetHost getAppWidgetHost() {
1901 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 }
Romain Guycbb89e42009-06-08 15:52:54 -07001903
Winson Chunga9abd0e2010-10-27 17:18:37 -07001904 public LauncherModel getModel() {
1905 return mModel;
1906 }
1907
Winson Chunga6945242014-01-08 14:04:34 -08001908 protected SharedPreferences getSharedPrefs() {
1909 return mSharedPrefs;
1910 }
1911
Bjorn Bringertc459e522013-06-07 19:36:01 +01001912 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001913 getWindow().closeAllPanels();
1914
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001915 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001916 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001917 }
1918
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 @Override
1920 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001921 long startTime = 0;
1922 if (DEBUG_RESUME_TIME) {
1923 startTime = System.currentTimeMillis();
1924 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 super.onNewIntent(intent);
1926
1927 // Close the menu
1928 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001929 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001930 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001931
Adam Cohened307df2013-10-02 09:37:31 -07001932 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1933 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1934 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001935
Adam Cohen6fecd412013-10-02 17:41:50 -07001936 if (mWorkspace == null) {
1937 // Can be cases where mWorkspace is null, this prevents a NPE
1938 return;
1939 }
1940 Folder openFolder = mWorkspace.getOpenFolder();
1941 // In all these cases, only animate if we're already on home
1942 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001943
1944 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1945 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001946 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001947 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001948 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001949 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001950
Adam Cohen6fecd412013-10-02 17:41:50 -07001951 closeFolder();
1952 exitSpringLoadedDragMode();
1953
1954 // If we are already on home, then just animate back to the workspace,
1955 // otherwise, just wait until onResume to set the state back to Workspace
1956 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001957 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001958 } else {
1959 mOnResumeState = State.WORKSPACE;
1960 }
1961
1962 final View v = getWindow().peekDecorView();
1963 if (v != null && v.getWindowToken() != null) {
1964 InputMethodManager imm = (InputMethodManager)getSystemService(
1965 INPUT_METHOD_SERVICE);
1966 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1967 }
1968
Winson Chungb745afb2015-03-02 11:51:23 -08001969 // Reset the apps view
1970 if (!alreadyOnHome && mAppsView != null) {
1971 mAppsView.scrollToTop();
1972 }
1973
Hyunyoung Song3f471442015-04-08 19:01:34 -07001974 // Reset the widgets view
1975 if (!alreadyOnHome && mWidgetsView != null) {
1976 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001977 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001978
Adam Cohen9211d422014-10-07 18:14:53 -07001979 if (mLauncherCallbacks != null) {
1980 mLauncherCallbacks.onHomeIntent();
1981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 }
Adam Cohened307df2013-10-02 09:37:31 -07001983
Michael Jurka447bf842013-05-15 14:52:15 +02001984 if (DEBUG_RESUME_TIME) {
1985 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987
Adam Cohen9211d422014-10-07 18:14:53 -07001988 if (mLauncherCallbacks != null) {
1989 mLauncherCallbacks.onNewIntent(intent);
1990 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001991 }
1992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001994 public void onRestoreInstanceState(Bundle state) {
1995 super.onRestoreInstanceState(state);
1996 for (int page: mSynchronouslyBoundPages) {
1997 mWorkspace.restoreInstanceStateForChild(page);
1998 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 }
2000
2001 @Override
2002 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002003 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002004 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2005 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002006 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002007 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008
Michael Jurka883f55b2010-10-21 15:47:14 -07002009 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002010 // We close any open folder since it will not be re-opened, and we need to make sure
2011 // this state is reflected.
2012 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013
Adam Cohendcd297f2013-06-18 13:13:40 -07002014 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002015 mWaitingForResult) {
2016 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002017 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002018 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2019 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002020 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2021 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2022 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002023 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 }
2025
Hyunyoung Song3f471442015-04-08 19:01:34 -07002026 // Save the current widgets tray?
2027 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002028 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002029
2030 if (mLauncherCallbacks != null) {
2031 mLauncherCallbacks.onSaveInstanceState(outState);
2032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 }
2034
2035 @Override
2036 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002038
Winson Chunge7a03942011-08-05 15:05:12 -07002039 // Remove all pending runnables
2040 mHandler.removeMessages(ADVANCE_MSG);
2041 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002042 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002043
Winson Chungcd2b0142011-06-08 16:02:26 -07002044 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002045 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002046
2047 // It's possible to receive onDestroy after a new Launcher activity has
2048 // been created. In this case, don't interfere with the new Launcher.
2049 if (mModel.isCurrentCallbacks(this)) {
2050 mModel.stopLoader();
2051 app.setLauncher(null);
2052 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002055 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002057 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002059 mAppWidgetHost = null;
2060
2061 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062
2063 TextKeyListener.getInstance().release();
2064
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002065 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002066 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002067
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002068 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002069 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002070 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002071 mWorkspace = null;
2072 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002073
Michael Jurka2ecf9952012-06-18 12:52:28 -07002074 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002075
2076 if (mLauncherCallbacks != null) {
2077 mLauncherCallbacks.onDestroy();
2078 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 }
2080
Adam Cohena9cf38f2011-05-02 15:36:58 -07002081 public DragController getDragController() {
2082 return mDragController;
2083 }
2084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 @Override
2086 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002087 onStartForResult(requestCode);
2088 super.startActivityForResult(intent, requestCode);
2089 }
2090
2091 @Override
2092 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2093 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2094 onStartForResult(requestCode);
2095 try {
2096 super.startIntentSenderForResult(intent, requestCode,
2097 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2098 } catch (IntentSender.SendIntentException e) {
2099 throw new ActivityNotFoundException();
2100 }
2101 }
2102
2103 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002104 if (requestCode >= 0) {
2105 setWaitingForResult(true);
2106 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 }
2108
Winson Chung70d72102011-08-12 11:24:35 -07002109 /**
2110 * Indicates that we want global search for this activity by setting the globalSearch
2111 * argument for {@link #startSearch} to true.
2112 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002114 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002116
Karl Rosaen138a0412009-04-23 19:00:21 -07002117 if (initialQuery == null) {
2118 // Use any text typed in the launcher as the initial query
2119 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 if (appSearchData == null) {
2122 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002123 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 }
Winson Chungadf0c182012-08-23 14:59:07 -07002125 Rect sourceBounds = new Rect();
2126 if (mSearchDropTargetBar != null) {
2127 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2128 }
Romain Guycbb89e42009-06-08 15:52:54 -07002129
Ian Parkinson047036e2014-09-01 15:40:53 +01002130 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002131 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002132 if (clearTextImmediately) {
2133 clearTypedText();
2134 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002135
2136 // We need to show the workspace after starting the search
2137 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002138 }
2139
Ian Parkinson047036e2014-09-01 15:40:53 +01002140 /**
2141 * Start a text search.
2142 *
2143 * @return {@code true} if the search will start immediately, so any further keypresses
2144 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2145 * to buffer keypresses.
2146 */
2147 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002148 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002149 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2150 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2151 sourceBounds);
2152 }
2153
Michael Jurkaa33411c2012-06-14 16:18:21 -07002154 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002155 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002156 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002157 }
2158
2159 /**
2160 * Starts the global search activity. This code is a copied from SearchManager
2161 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002162 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002163 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002164 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002165 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2166 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2167 if (globalSearchActivity == null) {
2168 Log.w(TAG, "No global search activity found.");
2169 return;
2170 }
2171 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2172 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2173 intent.setComponent(globalSearchActivity);
2174 // Make sure that we have a Bundle to put source in
2175 if (appSearchData == null) {
2176 appSearchData = new Bundle();
2177 } else {
2178 appSearchData = new Bundle(appSearchData);
2179 }
Adam Cohen9211d422014-10-07 18:14:53 -07002180 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002181 if (!appSearchData.containsKey("source")) {
2182 appSearchData.putString("source", getPackageName());
2183 }
2184 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2185 if (!TextUtils.isEmpty(initialQuery)) {
2186 intent.putExtra(SearchManager.QUERY, initialQuery);
2187 }
2188 if (selectInitialQuery) {
2189 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2190 }
2191 intent.setSourceBounds(sourceBounds);
2192 try {
2193 startActivity(intent);
2194 } catch (ActivityNotFoundException ex) {
2195 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 }
2198
Yura4f93ec62014-02-04 14:15:21 +00002199 public boolean isOnCustomContent() {
2200 return mWorkspace.isOnOrMovingToCustomContent();
2201 }
2202
Winson Chung70d72102011-08-12 11:24:35 -07002203 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002204 public boolean onPrepareOptionsMenu(Menu menu) {
2205 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002206 if (!isOnCustomContent()) {
2207 // Close any open folders
2208 closeFolder();
2209 // Stop resizing any widgets
2210 mWorkspace.exitWidgetResizeMode();
2211 if (!mWorkspace.isInOverviewMode()) {
2212 // Show the overview mode
2213 showOverviewMode(true);
2214 } else {
2215 showWorkspace(true);
2216 }
Winson Chunge0298742014-01-17 12:03:00 -08002217 }
Adam Cohen9211d422014-10-07 18:14:53 -07002218 if (mLauncherCallbacks != null) {
2219 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2220 }
2221
Michael Jurkab94f3f82013-09-11 18:08:54 +02002222 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002223 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002225 @Override
2226 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002227 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002228 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002229 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002230 }
2231
Joe Onorato9c1289c2009-08-17 11:03:03 -04002232 public boolean isWorkspaceLocked() {
2233 return mWorkspaceLoading || mWaitingForResult;
2234 }
2235
Adam Cohen517a7f52014-03-01 12:12:59 -08002236 public boolean isWorkspaceLoading() {
2237 return mWorkspaceLoading;
2238 }
2239
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002240 private void setWorkspaceLoading(boolean value) {
2241 boolean isLocked = isWorkspaceLocked();
2242 mWorkspaceLoading = value;
2243 if (isLocked != isWorkspaceLocked()) {
2244 onWorkspaceLockedChanged();
2245 }
2246 }
2247
2248 private void setWaitingForResult(boolean value) {
2249 boolean isLocked = isWorkspaceLocked();
2250 mWaitingForResult = value;
2251 if (isLocked != isWorkspaceLocked()) {
2252 onWorkspaceLockedChanged();
2253 }
2254 }
2255
Adam Cohen9211d422014-10-07 18:14:53 -07002256 protected void onWorkspaceLockedChanged() {
2257 if (mLauncherCallbacks != null) {
2258 mLauncherCallbacks.onWorkspaceLockedChanged();
2259 }
2260 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002261
Michael Jurka0280c3b2010-09-17 15:00:07 -07002262 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002263 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002264 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002265 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2266 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002267 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002268 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002269 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002270
Sunny Goyale6b63a32015-01-16 16:14:29 -08002271 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002272 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002273 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2274 }
2275
Sunny Goyale6b63a32015-01-16 16:14:29 -08002276 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002277 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2278 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002279 if (appWidgetInfo.configure != null) {
2280 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002281 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002282
Bjorn Bringert7984c942009-12-09 15:38:25 +00002283 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002284 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2285 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002288 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002289 Runnable onComplete = new Runnable() {
2290 @Override
2291 public void run() {
2292 // Exit spring loaded mode if necessary after adding the widget
2293 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2294 null);
2295 }
2296 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002297 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002298 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002299 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002300 }
2301 }
Romain Guycbb89e42009-06-08 15:52:54 -07002302
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002303 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002304 // Close any folders that may be open.
2305 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002306 mWorkspace.moveToCustomContentScreen(animate);
2307 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002308
2309 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2310 int[] cell, int spanX, int spanY) {
2311 switch (info.itemType) {
2312 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2313 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2314 int span[] = new int[2];
2315 span[0] = spanX;
2316 span[1] = spanY;
2317 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2318 container, screenId, cell, span);
2319 break;
2320 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2321 processShortcutFromDrop(info.componentName, container, screenId, cell);
2322 break;
2323 default:
2324 throw new IllegalStateException("Unknown item type: " + info.itemType);
2325 }
2326 }
2327
Adam Cohenfbba09b2011-07-18 21:43:05 -07002328 /**
2329 * Process a shortcut drop.
2330 *
2331 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002333 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002334 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002335 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2336 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002337 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002338 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002339 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002340 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002341
2342 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002343 mPendingAddInfo.cellX = cell[0];
2344 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002345 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002346
2347 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2348 createShortcutIntent.setComponent(componentName);
2349 processShortcut(createShortcutIntent);
2350 }
2351
Adam Cohenfbba09b2011-07-18 21:43:05 -07002352 /**
2353 * Process a widget drop.
2354 *
2355 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002356 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002357 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002358 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002359 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2360 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002361 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002362 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002363 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002364 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002365 mPendingAddInfo.minSpanX = info.minSpanX;
2366 mPendingAddInfo.minSpanY = info.minSpanY;
2367
Adam Cohenfbba09b2011-07-18 21:43:05 -07002368 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002369 mPendingAddInfo.cellX = cell[0];
2370 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002371 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002372 if (span != null) {
2373 mPendingAddInfo.spanX = span[0];
2374 mPendingAddInfo.spanY = span[1];
2375 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002376
Adam Cohened66b2b2012-01-23 17:28:51 -08002377 AppWidgetHostView hostView = info.boundWidget;
2378 int appWidgetId;
2379 if (hostView != null) {
2380 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002381 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002382 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002383 // In this case, we either need to start an activity to get permission to bind
2384 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002385 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002386 Bundle options = info.bindOptions;
2387
Sunny Goyalffe83f12014-08-14 17:39:34 -07002388 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2389 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002390 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002391 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002392 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002393 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002394 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2395 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2396 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002397 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2398 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002399 // TODO: we need to make sure that this accounts for the options bundle.
2400 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002401 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2402 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002403 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002404 }
2405
Joe Onoratodeb98af2010-02-19 14:59:39 -08002406 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002407 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 }
2409
Winson Chung24ab2f12010-09-16 14:10:47 -07002410 void processWallpaper(Intent intent) {
2411 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2412 }
2413
Adam Cohendcd297f2013-06-18 13:13:40 -07002414 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002415 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002416 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 folderInfo.title = getText(R.string.folder_name);
2418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002420 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2421 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002422 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423
2424 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002425 FolderIcon newFolder =
2426 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002427 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002428 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002429 // Force measure the new folder icon
2430 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2431 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002432 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433 }
Romain Guycbb89e42009-06-08 15:52:54 -07002434
Joe Onorato9c1289c2009-08-17 11:03:03 -04002435 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002436 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002437 }
2438
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002439 /**
2440 * Registers various content observers. The current implementation registers
2441 * only a favorites observer to keep track of the favorites applications.
2442 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002443 private void registerContentObservers() {
2444 ContentResolver resolver = getContentResolver();
2445 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2446 true, mWidgetObserver);
2447 }
2448
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 @Override
2450 public boolean dispatchKeyEvent(KeyEvent event) {
2451 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2452 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002454 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002455 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002456 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002457 dumpState();
2458 return true;
2459 }
2460 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002461 }
2462 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2463 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002464 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 return true;
2466 }
2467 }
2468
2469 return super.dispatchKeyEvent(event);
2470 }
2471
Joe Onorato88ec0992009-11-19 13:16:06 -08002472 @Override
2473 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002474 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2475 return;
2476 }
2477
Winson Chung3d9490a2015-03-24 17:38:42 -07002478 LauncherAccessibilityDelegate delegate =
2479 LauncherAppState.getInstance().getAccessibilityDelegate();
2480 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002481 return;
2482 }
2483
Winson Chungb745afb2015-03-02 11:51:23 -08002484 if (isAppsViewVisible()) {
2485 showWorkspace(true);
2486 } else if (isWidgetsViewVisible()) {
2487 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002488 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002489 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002490 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002491 Folder openFolder = mWorkspace.getOpenFolder();
2492 if (openFolder.isEditingName()) {
2493 openFolder.dismissEditingName();
2494 } else {
2495 closeFolder();
2496 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002497 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002498 mWorkspace.exitWidgetResizeMode();
2499
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002500 // Back button is a no-op here, but give at least some feedback for the button press
2501 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002502 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002503 }
2504
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002506 * Re-listen when widgets are reset.
2507 */
Adam Cohen091440a2015-03-18 14:16:05 -07002508 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002509 if (mAppWidgetHost != null) {
2510 mAppWidgetHost.startListening();
2511 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002512 }
2513
2514 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 * Launches the intent referred by the clicked shortcut.
2516 *
2517 * @param v The view representing the clicked shortcut.
2518 */
2519 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002520 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2521 // view has detached (it's possible for this to happen if the view is removed mid touch).
2522 if (v.getWindowToken() == null) {
2523 return;
2524 }
2525
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002526 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002527 return;
2528 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002529
Adam Cohen1697b792013-09-17 19:08:21 -07002530 if (v instanceof Workspace) {
2531 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002532 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002533 }
2534 return;
2535 }
2536
2537 if (v instanceof CellLayout) {
2538 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002539 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002540 }
2541 }
2542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002544 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002545 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002546 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002547 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002548 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002549 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002550 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002551 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002552 } else if (tag instanceof AppInfo) {
2553 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002554 } else if (tag instanceof LauncherAppWidgetInfo) {
2555 if (v instanceof PendingAppWidgetHostView) {
2556 onClickPendingWidget((PendingAppWidgetHostView) v);
2557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 }
2559 }
2560
Sunny Goyal508da152014-08-14 10:53:27 -07002561 public void onClickPagedViewIcon(View v) {
2562 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002563 if (mLauncherCallbacks != null) {
2564 mLauncherCallbacks.onClickPagedViewIcon(v);
2565 }
Sunny Goyal508da152014-08-14 10:53:27 -07002566 }
2567
Sunny Goyal70660032015-05-14 00:07:08 -07002568 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002569 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002570 return false;
2571 }
2572
Michael Jurkaaf442092010-06-10 17:01:57 -07002573 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002574 * Event handler for the app widget view which has not fully restored.
2575 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002576 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002577 if (mIsSafeModeEnabled) {
2578 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2579 return;
2580 }
2581
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002582 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002583 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002584 int widgetId = info.appWidgetId;
2585 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2586 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002587 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2588 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002589 mPendingAddInfo.copyFrom(info);
2590 mPendingAddWidgetId = widgetId;
2591
Sunny Goyalffe83f12014-08-14 17:39:34 -07002592 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2593 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002594 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002595 } else if (info.installProgress < 0) {
2596 // The install has not been queued
2597 final String packageName = info.providerName.getPackageName();
2598 showBrokenAppInstallDialog(packageName,
2599 new DialogInterface.OnClickListener() {
2600 public void onClick(DialogInterface dialog, int id) {
2601 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2602 }
2603 });
2604 } else {
2605 // Download has started.
2606 final String packageName = info.providerName.getPackageName();
2607 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002608 }
2609 }
2610
2611 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002612 * Event handler for the "grid" button that appears on the home screen, which
2613 * enters all apps mode.
2614 *
2615 * @param v The view that was clicked.
2616 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002617 protected void onClickAllAppsButton(View v) {
2618 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002619 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002620 showWorkspace(true);
2621 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002622 // Try and refresh the set of predicted apps before we enter launcher
2623 showAppsView(true /* animated */, false /* resetListToTop */,
2624 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 }
2626 }
2627
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002628 private void showBrokenAppInstallDialog(final String packageName,
2629 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002630 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002631 .setTitle(R.string.abandoned_promises_title)
2632 .setMessage(R.string.abandoned_promise_explanation)
2633 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2634 .setNeutralButton(R.string.abandoned_clean_this,
2635 new DialogInterface.OnClickListener() {
2636 public void onClick(DialogInterface dialog, int id) {
2637 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2638 mWorkspace.removeAbandonedPromise(packageName, user);
2639 }
2640 })
2641 .create().show();
2642 return;
2643 }
2644
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002645 /**
2646 * Event handler for an app shortcut click.
2647 *
2648 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2649 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002650 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002651 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2652 Object tag = v.getTag();
2653 if (!(tag instanceof ShortcutInfo)) {
2654 throw new IllegalArgumentException("Input must be a Shortcut");
2655 }
2656
2657 // Open shortcut
2658 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002659
2660 if (shortcut.isDisabled != 0) {
2661 int error = R.string.activity_not_available;
2662 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2663 error = R.string.safemode_shortcut_error;
2664 }
2665 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2666 return;
2667 }
2668
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002669 final Intent intent = shortcut.intent;
2670
2671 // Check for special shortcuts
2672 if (intent.getComponent() != null) {
2673 final String shortcutClass = intent.getComponent().getClassName();
2674
2675 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2676 MemoryDumpActivity.startDump(this);
2677 return;
2678 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2679 toggleShowWeightWatcher();
2680 return;
2681 }
2682 }
2683
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002685 if ((v instanceof BubbleTextView)
2686 && shortcut.isPromise()
2687 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002688 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002689 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002690 new DialogInterface.OnClickListener() {
2691 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002692 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002693 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002694 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002695 return;
2696 }
2697
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002698 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002699 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002700
2701 if (mLauncherCallbacks != null) {
2702 mLauncherCallbacks.onClickAppShortcut(v);
2703 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002704 }
2705
Adam Cohen091440a2015-03-18 14:16:05 -07002706 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002707 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002708 final ShortcutInfo shortcut;
2709 final Intent intent;
2710 if (tag instanceof ShortcutInfo) {
2711 shortcut = (ShortcutInfo) tag;
2712 intent = shortcut.intent;
2713 int[] pos = new int[2];
2714 v.getLocationOnScreen(pos);
2715 intent.setSourceBounds(new Rect(pos[0], pos[1],
2716 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002717
Sunny Goyal508da152014-08-14 10:53:27 -07002718 } else if (tag instanceof AppInfo) {
2719 shortcut = null;
2720 intent = ((AppInfo) tag).intent;
2721 } else {
2722 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2723 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002724
2725 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002726 mStats.recordLaunch(intent, shortcut);
2727
2728 if (success && v instanceof BubbleTextView) {
2729 mWaitingForResume = (BubbleTextView) v;
2730 mWaitingForResume.setStayPressed(true);
2731 }
2732 }
2733
2734 /**
2735 * Event handler for a folder icon click.
2736 *
2737 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2738 */
2739 protected void onClickFolderIcon(View v) {
2740 if (LOGD) Log.d(TAG, "onClickFolder");
2741 if (!(v instanceof FolderIcon)){
2742 throw new IllegalArgumentException("Input must be a FolderIcon");
2743 }
2744
2745 FolderIcon folderIcon = (FolderIcon) v;
2746 final FolderInfo info = folderIcon.getFolderInfo();
2747 Folder openFolder = mWorkspace.getFolderForTag(info);
2748
2749 // If the folder info reports that the associated folder is open, then verify that
2750 // it is actually opened. There have been a few instances where this gets out of sync.
2751 if (info.opened && openFolder == null) {
2752 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2753 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2754 info.opened = false;
2755 }
2756
2757 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2758 // Close any open folder
2759 closeFolder();
2760 // Open the requested folder
2761 openFolder(folderIcon);
2762 } else {
2763 // Find the open folder...
2764 int folderScreen;
2765 if (openFolder != null) {
2766 folderScreen = mWorkspace.getPageForView(openFolder);
2767 // .. and close it
2768 closeFolder(openFolder);
2769 if (folderScreen != mWorkspace.getCurrentPage()) {
2770 // Close any folder open on the current screen
2771 closeFolder();
2772 // Pull the folder onto this screen
2773 openFolder(folderIcon);
2774 }
2775 }
2776 }
Adam Cohen9211d422014-10-07 18:14:53 -07002777
2778 if (mLauncherCallbacks != null) {
2779 mLauncherCallbacks.onClickFolderIcon(v);
2780 }
Michael Jurka5130e402011-10-13 04:55:35 -07002781 }
2782
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002783 /**
2784 * Event handler for the (Add) Widgets button that appears after a long press
2785 * on the home screen.
2786 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002787 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002788 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002789 if (mIsSafeModeEnabled) {
2790 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2791 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002792 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002793 if (mLauncherCallbacks != null) {
2794 mLauncherCallbacks.onClickAddWidgetButton(view);
2795 }
Adam Cohen9211d422014-10-07 18:14:53 -07002796 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002797 }
2798
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002799 /**
2800 * Event handler for the wallpaper picker button that appears after a long press
2801 * on the home screen.
2802 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002803 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002804 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002805 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2806 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002807
2808 if (mLauncherCallbacks != null) {
2809 mLauncherCallbacks.onClickWallpaperPicker(v);
2810 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002811 }
2812
2813 /**
2814 * Event handler for a click on the settings button that appears after a long press
2815 * on the home screen.
2816 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002817 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002818 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002819 if (mLauncherCallbacks != null) {
2820 mLauncherCallbacks.onClickSettingsButton(v);
2821 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002822 }
2823
Michael Jurka5130e402011-10-13 04:55:35 -07002824 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002825 // Provide the same haptic feedback that the system offers for virtual keys.
2826 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002827 }
2828
Adam Cohen61f560d2013-09-30 15:58:20 -07002829 public void performHapticFeedbackOnTouchDown(View v) {
2830 // Provide the same haptic feedback that the system offers for virtual keys.
2831 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2832 }
2833
2834 public View.OnTouchListener getHapticFeedbackTouchListener() {
2835 if (mHapticFeedbackTouchListener == null) {
2836 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002837 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002838 @Override
2839 public boolean onTouch(View v, MotionEvent event) {
2840 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2841 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2842 }
2843 return false;
2844 }
2845 };
2846 }
2847 return mHapticFeedbackTouchListener;
2848 }
2849
Adam Cohen9211d422014-10-07 18:14:53 -07002850 public void onDragStarted(View view) {
2851 if (isOnCustomContent()) {
2852 // Custom content screen doesn't participate in drag and drop. If on custom
2853 // content screen, move to default.
2854 moveWorkspaceToDefaultScreen();
2855 }
2856
2857 if (mLauncherCallbacks != null) {
2858 mLauncherCallbacks.onDragStarted(view);
2859 }
2860 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002861
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002862 /**
2863 * Called when the user stops interacting with the launcher.
2864 * This implies that the user is now on the homescreen and is not doing housekeeping.
2865 */
Adam Cohen9211d422014-10-07 18:14:53 -07002866 protected void onInteractionEnd() {
2867 if (mLauncherCallbacks != null) {
2868 mLauncherCallbacks.onInteractionEnd();
2869 }
2870 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002871
2872 /**
2873 * Called when the user starts interacting with the launcher.
2874 * The possible interactions are:
2875 * - open all apps
2876 * - reorder an app shortcut, or a widget
2877 * - open the overview mode.
2878 * This is a good time to stop doing things that only make sense
2879 * when the user is on the homescreen and not doing housekeeping.
2880 */
Adam Cohen9211d422014-10-07 18:14:53 -07002881 protected void onInteractionBegin() {
2882 if (mLauncherCallbacks != null) {
2883 mLauncherCallbacks.onInteractionBegin();
2884 }
2885 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002886
Winson Chungcd99cd32015-04-29 11:03:24 -07002887 /** Updates the interaction state. */
2888 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002889 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002890 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002891 boolean fromStateWithOverlay;
2892 boolean toStateWithOverlay;
2893 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2894 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2895 toStateWithOverlay = toState != Workspace.State.NORMAL;
2896 } else {
2897 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2898 fromState != Workspace.State.NORMAL_HIDDEN;
2899 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2900 toState != Workspace.State.NORMAL_HIDDEN;
2901 }
2902 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002903 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002904 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002905 onInteractionEnd();
2906 }
2907 }
2908
Kenny Guyf07af7b2014-07-31 11:39:16 +01002909 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002910 try {
2911 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002912 launcherApps.showAppDetailsForProfile(componentName, user);
2913 } catch (SecurityException e) {
2914 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2915 Log.e(TAG, "Launcher does not have permission to launch settings");
2916 } catch (ActivityNotFoundException e) {
2917 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2918 Log.e(TAG, "Unable to launch settings");
2919 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002920 }
2921
Michael Jurka1e2f4652013-07-08 18:03:46 -07002922 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002923 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2924 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002925 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002926 // System applications cannot be installed. For now, show a toast explaining that.
2927 // We may give them the option of disabling apps this way.
2928 int messageId = R.string.uninstall_system_app_text;
2929 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002930 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002931 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002932 String packageName = componentName.getPackageName();
2933 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002934 Intent intent = new Intent(
2935 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002936 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2937 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002938 if (user != null) {
2939 user.addToIntent(intent, Intent.EXTRA_USER);
2940 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002941 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002942 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002943 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002944 }
2945
Michael Jurka86a720e2012-05-09 11:23:23 -07002946 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002947 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002949 // Only launch using the new animation if the shortcut has not opted out (this is a
2950 // private contract between launcher and may be ignored in the future).
2951 boolean useLaunchAnimation = (v != null) &&
2952 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002953 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2954 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002955
Kenny Guy1317e2d2014-05-08 18:52:50 +01002956 UserHandleCompat user = null;
2957 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2958 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2959 user = userManager.getUserForSerialNumber(serialNumber);
2960 }
2961
2962 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002963 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002964 ActivityOptions opts = null;
2965 if (sClipRevealMethod != null) {
2966 // TODO: call method directly when Launcher3 can depend on M APIs
2967 int left = 0, top = 0;
2968 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2969 if (v instanceof TextView) {
2970 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002971 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2972 if (icon != null) {
2973 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002974 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002975 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002976 width = bounds.width();
2977 height = bounds.height();
2978 }
2979 }
2980 try {
2981 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2982 left, top, width, height);
2983 } catch (IllegalAccessException e) {
2984 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2985 sClipRevealMethod = null;
2986 } catch (InvocationTargetException e) {
2987 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2988 sClipRevealMethod = null;
2989 }
2990 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002991 if (opts == null && !Utilities.isLmpOrAbove()) {
2992 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002993 v.getMeasuredWidth(), v.getMeasuredHeight());
2994 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002995 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002996 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002997
2998 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2999 // Could be launching some bookkeeping activity
3000 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003001 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003002 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003003 launcherApps.startActivityForProfile(intent.getComponent(), user,
3004 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003005 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003006 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003007 } catch (SecurityException e) {
3008 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003009 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003010 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003011 "or use the exported attribute for this activity. "
3012 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003013 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003014 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003016
Michael Jurka86a720e2012-05-09 11:23:23 -07003017 boolean startActivitySafely(View v, Intent intent, Object tag) {
3018 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003019 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3020 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3021 return false;
3022 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003023 try {
3024 success = startActivity(v, intent, tag);
3025 } catch (ActivityNotFoundException e) {
3026 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3027 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3028 }
3029 return success;
3030 }
3031
Adam Cohen268c4752012-06-06 17:47:33 -07003032 /**
3033 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3034 * in the DragLayer in the exact absolute location of the original FolderIcon.
3035 */
3036 private void copyFolderIconToImage(FolderIcon fi) {
3037 final int width = fi.getMeasuredWidth();
3038 final int height = fi.getMeasuredHeight();
3039
3040 // Lazy load ImageView, Bitmap and Canvas
3041 if (mFolderIconImageView == null) {
3042 mFolderIconImageView = new ImageView(this);
3043 }
3044 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3045 mFolderIconBitmap.getHeight() != height) {
3046 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3047 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3048 }
3049
3050 DragLayer.LayoutParams lp;
3051 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3052 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3053 } else {
3054 lp = new DragLayer.LayoutParams(width, height);
3055 }
3056
Adam Cohen307fe232012-08-16 17:55:58 -07003057 // The layout from which the folder is being opened may be scaled, adjust the starting
3058 // view size by this scale factor.
3059 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003060 lp.customPosition = true;
3061 lp.x = mRectForFolderAnimation.left;
3062 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003063 lp.width = (int) (scale * width);
3064 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003065
3066 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3067 fi.draw(mFolderIconCanvas);
3068 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003069 if (fi.getFolder() != null) {
3070 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3071 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003072 }
Adam Cohen268c4752012-06-06 17:47:33 -07003073 // Just in case this image view is still in the drag layer from a previous animation,
3074 // we remove it and re-add it.
3075 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3076 mDragLayer.removeView(mFolderIconImageView);
3077 }
3078 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003079 if (fi.getFolder() != null) {
3080 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003081 }
Adam Cohen268c4752012-06-06 17:47:33 -07003082 }
3083
Adam Cohen2801caf2011-05-13 20:57:39 -07003084 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003085 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003086 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3087 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3088 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3089
Adam Cohenc51934b2011-07-26 21:07:43 -07003090 FolderInfo info = (FolderInfo) fi.getTag();
3091 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3092 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003093 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3094 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003095 }
3096
Adam Cohen268c4752012-06-06 17:47:33 -07003097 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3098 copyFolderIconToImage(fi);
3099 fi.setVisibility(View.INVISIBLE);
3100
Michael Jurka2ecf9952012-06-18 12:52:28 -07003101 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003102 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003103 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003104 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3105 }
3106 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003107 oa.start();
3108 }
3109
Adam Cohen268c4752012-06-06 17:47:33 -07003110 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003111 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003112 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3113 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3114 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3115
Adam Cohen268c4752012-06-06 17:47:33 -07003116 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003117
Adam Cohen268c4752012-06-06 17:47:33 -07003118 // We remove and re-draw the FolderIcon in-case it has changed
3119 mDragLayer.removeView(mFolderIconImageView);
3120 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003121 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003122 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003123 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003124 oa.addListener(new AnimatorListenerAdapter() {
3125 @Override
3126 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003127 if (cl != null) {
3128 cl.clearFolderLeaveBehind();
3129 // Remove the ImageView copy of the FolderIcon and make the original visible.
3130 mDragLayer.removeView(mFolderIconImageView);
3131 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003132 }
3133 }
3134 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003135 oa.start();
3136 }
3137
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003138 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003139 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003140 * is animated relative to the specified View. If the View is null, no animation
3141 * is played.
3142 *
3143 * @param folderInfo The FolderInfo describing the folder to open.
3144 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003145 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003146 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003147 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3148 if (openFolder != null && openFolder != folder) {
3149 // Close any open folder before opening a folder.
3150 closeFolder();
3151 }
3152
Adam Cohena9cf38f2011-05-02 15:36:58 -07003153 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003154
Adam Cohena9cf38f2011-05-02 15:36:58 -07003155 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156
Sunny Goyalf4066152015-04-15 09:42:19 -07003157 // While the folder is open, the position of the icon cannot change.
3158 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3159
Adam Cohen4554ee12011-08-03 16:13:21 -07003160 // Just verify that the folder hasn't already been added to the DragLayer.
3161 // There was a one-off crash where the folder had a parent already.
3162 if (folder.getParent() == null) {
3163 mDragLayer.addView(folder);
3164 mDragController.addDropTarget((DropTarget) folder);
3165 } else {
3166 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3167 folder.getParent() + ").");
3168 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003169 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003170 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003171
3172 // Notify the accessibility manager that this folder "window" has appeared and occluded
3173 // the workspace items
3174 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3175 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003176 }
3177
3178 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003179 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003180 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003181 if (folder.isEditingName()) {
3182 folder.dismissEditingName();
3183 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003184 closeFolder(folder);
3185 }
3186 }
3187
Sunny Goyal83a8f042015-05-19 12:52:12 -07003188 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003189 folder.getInfo().opened = false;
3190
3191 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3192 if (parent != null) {
3193 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3194 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003195 if (fi != null) {
3196 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3197 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003198 }
3199 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003200
3201 // Notify the accessibility manager that this folder "window" has disappeard and no
3202 // longer occludeds the workspace items
3203 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003204 }
3205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003207 if (!isDraggingEnabled()) return false;
3208 if (isWorkspaceLocked()) return false;
3209 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003210
Adam Cohen1697b792013-09-17 19:08:21 -07003211 if (v instanceof Workspace) {
3212 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003213 if (!mWorkspace.isTouchActive()) {
3214 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003215 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3216 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3217 return true;
3218 } else {
3219 return false;
3220 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003221 } else {
3222 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003223 }
Adam Cohen1697b792013-09-17 19:08:21 -07003224 }
3225
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003226 CellLayout.CellInfo longClickCellInfo = null;
3227 View itemUnderLongClick = null;
3228 if (v.getTag() instanceof ItemInfo) {
3229 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003230 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003231 itemUnderLongClick = longClickCellInfo.cell;
3232 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003233 }
3234
Winson Chung3d503fb2011-07-13 17:25:49 -07003235 // The hotseat touch handling does not go through Workspace, and we always allow long press
3236 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003237 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003238 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003239 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003240 // User long pressed on empty space
3241 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3242 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003243 if (mWorkspace.isInOverviewMode()) {
3244 mWorkspace.startReordering(v);
3245 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003246 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003247 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003248 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003249 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3250 mHotseat.getOrderInHotseat(
3251 longClickCellInfo.cellX,
3252 longClickCellInfo.cellY));
3253 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003254 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003255 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003256 }
3257 }
3258 }
3259 return true;
3260 }
3261
Winson Chung3d503fb2011-07-13 17:25:49 -07003262 boolean isHotseatLayout(View layout) {
3263 return mHotseat != null && layout != null &&
3264 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3265 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003266
Winson Chung3d503fb2011-07-13 17:25:49 -07003267 /**
3268 * Returns the CellLayout of the specified container at the specified screen.
3269 */
Sunny Goyal92820592015-03-02 11:31:35 -08003270 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003271 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3272 if (mHotseat != null) {
3273 return mHotseat.getLayout();
3274 } else {
3275 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003276 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003277 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003278 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003279 }
3280 }
3281
Winson Chungb745afb2015-03-02 11:51:23 -08003282 /**
3283 * For overridden classes.
3284 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003285 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003286 return isAppsViewVisible();
3287 }
3288
3289 public boolean isAppsViewVisible() {
3290 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3291 }
3292
3293 public boolean isWidgetsViewVisible() {
3294 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003295 }
3296
Craig Mautner360310b2012-10-26 15:13:08 -07003297 private void setWorkspaceBackground(boolean workspace) {
3298 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003299 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003300 }
3301
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003302 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003303 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3304 int curflags = getWindow().getAttributes().flags
3305 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3306 if (wpflags != curflags) {
3307 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3308 }
Craig Mautner360310b2012-10-26 15:13:08 -07003309 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003310 }
3311
Michael Jurkae326f182011-11-21 14:05:46 -08003312 @Override
3313 public void onTrimMemory(int level) {
3314 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003315 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3316 // The widget preview db can result in holding onto over
3317 // 3MB of memory for caching which isn't necessary.
3318 SQLiteDatabase.releaseMemory();
3319
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003320 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003321 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003322 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003323 if (mLauncherCallbacks != null) {
3324 mLauncherCallbacks.onTrimMemory(level);
3325 }
Michael Jurkae326f182011-11-21 14:05:46 -08003326 }
3327
Winson Chungb745afb2015-03-02 11:51:23 -08003328 @Override
3329 public void onStateTransitionHideSearchBar() {
3330 // Hide the search bar
3331 if (mSearchDropTargetBar != null) {
3332 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3333 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003334 }
3335
Winson Chungb745afb2015-03-02 11:51:23 -08003336 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003337 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3338 true);
Adam Cohened307df2013-10-02 09:37:31 -07003339 }
3340
Sunny Goyal83a8f042015-05-19 12:52:12 -07003341 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003342 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3343 onCompleteRunnable, true);
3344 }
3345
3346 protected void showWorkspace(int snapToPage, boolean animated) {
3347 showWorkspace(snapToPage, animated, null, true);
3348 }
3349
3350 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3351 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003352 boolean changed = mState != State.WORKSPACE ||
3353 mWorkspace.getState() != Workspace.State.NORMAL;
3354 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003355 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003356 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003357 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003358 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003359
Winson Chungc7d2b602012-05-16 17:02:20 -07003360 // Show the search bar (only animate if we were showing the drop target bar in spring
3361 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003362 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003363 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003364 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003365
Michael Jurkab3e22d92011-10-31 15:58:33 -07003366 // Set focus to the AppsCustomize button
3367 if (mAllAppsButton != null) {
3368 mAllAppsButton.requestFocus();
3369 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003370 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003371
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003372 // Change the state *after* we've called all the transition code
3373 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003374
Winson Chung5fb63472011-02-02 17:03:37 -08003375 // Resume the auto-advance of widgets
3376 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003377 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003378
Winson Chung0f785722015-04-08 10:27:49 -07003379 if (changed) {
3380 // Send an accessibility event to announce the context change
3381 getWindow().getDecorView()
3382 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003383 if (notifyLauncherCallbacks) {
3384 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003385 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003386 mLauncherCallbacks.onAllAppsHidden();
3387 }
3388 }
Winson Chung0f785722015-04-08 10:27:49 -07003389 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003390 }
3391
Adam Cohened307df2013-10-02 09:37:31 -07003392 void showOverviewMode(boolean animated) {
3393 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003394 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003395 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3396 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003397 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003398 }
3399
Winson Chungb745afb2015-03-02 11:51:23 -08003400 /**
3401 * Shows the apps view.
3402 */
Winson Chung4ac30062015-05-08 17:34:17 -07003403 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003404 if (resetListToTop) {
3405 mAppsView.scrollToTop();
3406 }
Winson Chung4ac30062015-05-08 17:34:17 -07003407 if (updatePredictedApps) {
3408 tryAndUpdatePredictedApps();
3409 }
Winson Chungb745afb2015-03-02 11:51:23 -08003410 showAppsOrWidgets(animated, State.APPS);
3411 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003412
Winson Chungb745afb2015-03-02 11:51:23 -08003413 /**
3414 * Shows the widgets view.
3415 */
3416 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003417 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003418 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003419 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003420 }
Winson Chungb745afb2015-03-02 11:51:23 -08003421 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003422
3423 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003424 @Override
3425 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003426 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003427 }
3428 });
Winson Chungb745afb2015-03-02 11:51:23 -08003429 }
3430
3431 /**
3432 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003433 *
3434 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003435 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003436 // TODO: calling method should use the return value so that when {@code false} is returned
3437 // the workspace transition doesn't fall into invalid state.
3438 private boolean showAppsOrWidgets(boolean animated, State toState) {
3439 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3440 mState != State.WIDGETS_SPRING_LOADED) {
3441 return false;
3442 }
3443 if (toState != State.APPS && toState != State.WIDGETS) {
3444 return false;
3445 }
Winson Chungb745afb2015-03-02 11:51:23 -08003446
3447 if (toState == State.APPS) {
3448 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003449 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003450 mLauncherCallbacks.onAllAppsShown();
3451 }
Winson Chungb745afb2015-03-02 11:51:23 -08003452 } else {
3453 mStateTransitionAnimation.startAnimationToWidgets(animated);
3454 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003455
Michael Jurkab3e22d92011-10-31 15:58:33 -07003456 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003457 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003458
3459 // Pause the auto-advance of widgets until we are out of AllApps
3460 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003461 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003462 closeFolder();
3463
3464 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003465 getWindow().getDecorView()
3466 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003467 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003468 }
3469
Winson Chungcd99cd32015-04-29 11:03:24 -07003470 /**
3471 * Updates the workspace and interaction state on state change, and return the animation to this
3472 * new state.
3473 */
3474 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3475 boolean animated, HashMap<View, Integer> layerViews) {
3476 Workspace.State fromState = mWorkspace.getState();
3477 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3478 updateInteraction(fromState, toState);
3479 return anim;
3480 }
3481
Hyunyoung Song3f471442015-04-08 19:01:34 -07003482 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003483 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003484 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3485 mState == State.WIDGETS_SPRING_LOADED) {
3486 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003487 }
Winson Chungb745afb2015-03-02 11:51:23 -08003488
3489 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003490 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3491 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003492 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003493 }
Adam Cohen7777d962011-08-18 18:58:38 -07003494
Hyunyoung Song3f471442015-04-08 19:01:34 -07003495 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003496 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003497 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003498
Winson Chungcd99cd32015-04-29 11:03:24 -07003499 if (successfulDrop) {
3500 // We need to trigger all apps hidden to notify search to update itself before the
3501 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003502 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003503 mLauncherCallbacks.onAllAppsHidden();
3504 }
3505 }
3506
Winson Chunge7a03942011-08-05 15:05:12 -07003507 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003508 @Override
3509 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003510 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003511 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3512 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003513 // Before we show workspace, hide all apps again because
3514 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3515 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003516 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003517 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003518 } else {
3519 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003520 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003521 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003522 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003523 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003524
Michael Jurkad3ef3062010-11-23 16:23:58 -08003525 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003526 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003527 showAppsView(true /* animated */, false /* resetListToTop */,
3528 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003529 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003530 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003531 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003532 }
3533
Winson Chung4ac30062015-05-08 17:34:17 -07003534 /**
3535 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3536 * resumed.
3537 */
3538 private void tryAndUpdatePredictedApps() {
3539 if (mLauncherCallbacks != null) {
3540 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3541 if (!apps.isEmpty()) {
3542 mAppsView.setPredictedApps(apps);
3543 }
3544 }
3545 }
3546
Michael Jurkab3e22d92011-10-31 15:58:33 -07003547 void lockAllApps() {
3548 // TODO
3549 }
3550
3551 void unlockAllApps() {
3552 // TODO
3553 }
3554
Sunny Goyal594d76d2014-11-06 10:12:54 -08003555 protected void disableVoiceButtonProxy(boolean disable) {
3556 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003557 }
3558
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003559 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003560 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3561 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003562 }
3563
Adam Cohen24ce0b32014-01-14 16:18:14 -08003564 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003565 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3566 if (searchProvider == null) {
3567 return null;
3568 }
3569
3570 Bundle opts = new Bundle();
3571 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003572 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003573
3574 SharedPreferences sp = getSharedPreferences(
3575 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3576 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003577 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003578 if (!searchProvider.provider.flattenToString().equals(
3579 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003580 || (widgetInfo == null)
3581 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003582 // A valid widget is not already bound.
3583 if (widgetId > -1) {
3584 mAppWidgetHost.deleteAppWidgetId(widgetId);
3585 widgetId = -1;
3586 }
3587
3588 // Try to bind a new widget
3589 widgetId = mAppWidgetHost.allocateAppWidgetId();
3590
3591 if (!AppWidgetManagerCompat.getInstance(this)
3592 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3593 mAppWidgetHost.deleteAppWidgetId(widgetId);
3594 widgetId = -1;
3595 }
3596
3597 sp.edit()
3598 .putInt(QSB_WIDGET_ID, widgetId)
3599 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3600 .commit();
3601 }
3602
3603 if (widgetId != -1) {
3604 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3605 mQsb.updateAppWidgetOptions(opts);
3606 mQsb.setPadding(0, 0, 0, 0);
3607 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003608 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003609 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003610 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003611 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003612 }
3613
Sunny Goyal22235bc2015-04-03 09:23:43 -07003614 private void reinflateQSBIfNecessary() {
3615 if (mQsb instanceof LauncherAppWidgetHostView &&
3616 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3617 mSearchDropTargetBar.removeView(mQsb);
3618 mQsb = null;
3619 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3620 }
3621 }
3622
Michael Jurkad7c28052012-04-27 15:43:36 -07003623 @Override
3624 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003625 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003626 final List<CharSequence> text = event.getText();
3627 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003628 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003629 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003630 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003631 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003632 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003633 } else {
3634 text.add(getString(R.string.all_apps_home_button_label));
3635 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003636 return result;
3637 }
3638
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003639 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003640 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003641 */
Adam Cohen091440a2015-03-18 14:16:05 -07003642 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003643 @Override
3644 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003645 closeSystemDialogs();
3646 }
3647 }
3648
3649 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003650 * Receives notifications whenever the appwidgets are reset.
3651 */
3652 private class AppWidgetResetObserver extends ContentObserver {
3653 public AppWidgetResetObserver() {
3654 super(new Handler());
3655 }
3656
3657 @Override
3658 public void onChange(boolean selfChange) {
3659 onAppWidgetReset();
3660 }
3661 }
3662
3663 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003664 * If the activity is currently paused, signal that we need to run the passed Runnable
3665 * in onResume.
3666 *
3667 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003668 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3669 * wrong when we're not running, and if the activity comes back to what the configuration was
3670 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003671 *
3672 * Implementation of the method from LauncherModel.Callbacks.
3673 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003674 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003675 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003676 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003677 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003678 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003679 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003680 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003681 }
3682 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003683 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003684 return true;
3685 } else {
3686 return false;
3687 }
3688 }
3689
Michael Jurkac402cd92013-05-20 15:49:32 +02003690 private boolean waitUntilResume(Runnable run) {
3691 return waitUntilResume(run, false);
3692 }
3693
Michael Jurka1e2f4652013-07-08 18:03:46 -07003694 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003695 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003696 }
3697
Michael Jurka7607c2f2013-04-03 14:33:19 -07003698 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003699 * If the activity is currently paused, signal that we need to re-run the loader
3700 * in onResume.
3701 *
3702 * This needs to be called from incoming places where resources might have been loaded
3703 * while we are paused. That is becaues the Configuration might be wrong
3704 * when we're not running, and if it comes back to what it was when we
3705 * were paused, we are not restarted.
3706 *
3707 * Implementation of the method from LauncherModel.Callbacks.
3708 *
3709 * @return true if we are currently paused. The caller might be able to
3710 * skip some work in that case since we will come back again.
3711 */
3712 public boolean setLoadOnResume() {
3713 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003714 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003715 mOnResumeNeedsLoad = true;
3716 return true;
3717 } else {
3718 return false;
3719 }
3720 }
3721
3722 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003724 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003725 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003726 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003727 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003728 } else {
3729 return SCREEN_COUNT / 2;
3730 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003732
Joe Onorato9c1289c2009-08-17 11:03:03 -04003733 /**
3734 * Refreshes the shortcuts shown on the workspace.
3735 *
3736 * Implementation of the method from LauncherModel.Callbacks.
3737 */
3738 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003739 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003740
Michael Jurka7607c2f2013-04-03 14:33:19 -07003741 // If we're starting binding all over again, clear any bind calls we'd postponed in
3742 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3743 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003744 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003745
Winson Chungd64d1762013-08-20 14:37:16 -07003746 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003747 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003748 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003749
Michael Jurka05bf6442011-11-30 20:28:53 -08003750 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003751 if (mHotseat != null) {
3752 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003753 }
3754 }
3755
Adam Cohendcd297f2013-06-18 13:13:40 -07003756 @Override
3757 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003758 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003759
Adam Cohen5084cba2013-09-03 12:01:16 -07003760 // If there are no screens, we need to have an empty screen
3761 if (orderedScreenIds.size() == 0) {
3762 mWorkspace.addExtraEmptyScreen();
3763 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003764
3765 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003766 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003767 if (hasCustomContentToLeft()) {
3768 mWorkspace.createCustomContentContainer();
3769 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003770 }
Winson Chung64359a52013-07-08 17:17:08 -07003771 }
3772
3773 @Override
3774 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003775 // Log to disk
3776 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3777 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3778 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003779 int count = orderedScreenIds.size();
3780 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003781 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003782 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003783 }
3784
Adam Cohen39a06042013-07-19 14:30:12 -07003785 private boolean shouldShowWeightWatcher() {
3786 String spKey = LauncherAppState.getSharedPreferencesKey();
3787 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003788 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003789
3790 return show;
3791 }
3792
3793 private void toggleShowWeightWatcher() {
3794 String spKey = LauncherAppState.getSharedPreferencesKey();
3795 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3796 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3797
3798 show = !show;
3799
3800 SharedPreferences.Editor editor = sp.edit();
3801 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3802 editor.commit();
3803
3804 if (mWeightWatcher != null) {
3805 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3806 }
3807 }
3808
Winson Chungd64d1762013-08-20 14:37:16 -07003809 public void bindAppsAdded(final ArrayList<Long> newScreens,
3810 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003811 final ArrayList<ItemInfo> addAnimated,
3812 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003813 Runnable r = new Runnable() {
3814 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003815 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003816 }
3817 };
3818 if (waitUntilResume(r)) {
3819 return;
3820 }
3821
Winson Chungd64d1762013-08-20 14:37:16 -07003822 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003823 if (newScreens != null) {
3824 bindAddScreens(newScreens);
3825 }
Winson Chungd64d1762013-08-20 14:37:16 -07003826
3827 // We add the items without animation on non-visible pages, and with
3828 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003829 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003830 bindItems(addNotAnimated, 0,
3831 addNotAnimated.size(), false);
3832 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003833 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003834 bindItems(addAnimated, 0,
3835 addAnimated.size(), true);
3836 }
Winson Chungc58497e2013-09-03 17:48:37 -07003837
Winson Chung87412982013-10-03 18:34:14 -07003838 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003839 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003840
Winson Chungb745afb2015-03-02 11:51:23 -08003841 if (addedApps != null && mAppsView != null) {
3842 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003843 }
Winson Chungd64d1762013-08-20 14:37:16 -07003844 }
3845
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003846 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003847 * Bind the items start-end from the list.
3848 *
3849 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003850 */
Winson Chung64359a52013-07-08 17:17:08 -07003851 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3852 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003853 Runnable r = new Runnable() {
3854 public void run() {
3855 bindItems(shortcuts, start, end, forceAnimateIcons);
3856 }
3857 };
3858 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003859 return;
3860 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003861
Winson Chungf0c6ae02012-03-21 16:10:31 -07003862 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003863 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3864 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003865 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003866 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003867 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003868 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003870
3871 // Short circuit if we are loading dock items for a configuration which has no dock
3872 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3873 mHotseat == null) {
3874 continue;
3875 }
3876
Joe Onorato9c1289c2009-08-17 11:03:03 -04003877 switch (item.itemType) {
3878 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3879 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003880 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003881 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003882
Winson Chung64359a52013-07-08 17:17:08 -07003883 /*
3884 * TODO: FIX collision case
3885 */
Winson Chungce376632013-07-11 16:12:41 -07003886 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3887 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3888 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003889 View v = cl.getChildAt(item.cellX, item.cellY);
3890 Object tag = v.getTag();
3891 String desc = "Collision while binding workspace item: " + item
3892 + ". Collides with " + tag;
3893 if (LauncherAppState.isDogfoodBuild()) {
3894 throw (new RuntimeException(desc));
3895 } else {
3896 Log.d(TAG, desc);
3897 }
Winson Chungce376632013-07-11 16:12:41 -07003898 }
Winson Chung64359a52013-07-08 17:17:08 -07003899 }
3900
Adam Cohendcd297f2013-06-18 13:13:40 -07003901 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3902 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003903 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003904 // Animate all the applications up now
3905 shortcut.setAlpha(0f);
3906 shortcut.setScaleX(0f);
3907 shortcut.setScaleY(0f);
3908 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003909 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003910 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003911 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003912 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003913 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003914 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003915 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003916 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3917 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003918 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003919 default:
3920 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003922 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003923
Winson Chung997a9232013-07-24 15:33:46 -07003924 if (animateIcons) {
3925 // Animate to the correct page
3926 if (newShortcutsScreenId > -1) {
3927 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003928 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003929 final Runnable startBounceAnimRunnable = new Runnable() {
3930 public void run() {
3931 anim.playTogether(bounceAnims);
3932 anim.start();
3933 }
3934 };
Winson Chung997a9232013-07-24 15:33:46 -07003935 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003936 // We post the animation slightly delayed to prevent slowdowns
3937 // when we are loading right after we return to launcher.
3938 mWorkspace.postDelayed(new Runnable() {
3939 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003940 if (mWorkspace != null) {
3941 mWorkspace.snapToPage(newScreenIndex);
3942 mWorkspace.postDelayed(startBounceAnimRunnable,
3943 NEW_APPS_ANIMATION_DELAY);
3944 }
Winson Chung94d67682013-09-25 16:29:40 -07003945 }
3946 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003947 } else {
3948 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003949 }
3950 }
Winson Chung64359a52013-07-08 17:17:08 -07003951 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003952 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003953 }
3954
Joe Onorato9c1289c2009-08-17 11:03:03 -04003955 /**
3956 * Implementation of the method from LauncherModel.Callbacks.
3957 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003958 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003959 Runnable r = new Runnable() {
3960 public void run() {
3961 bindFolders(folders);
3962 }
3963 };
3964 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003965 return;
3966 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003967 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003968 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003969
3970 /**
3971 * Add the views for a widget to the workspace.
3972 *
3973 * Implementation of the method from LauncherModel.Callbacks.
3974 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003975 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003976 Runnable r = new Runnable() {
3977 public void run() {
3978 bindAppWidget(item);
3979 }
3980 };
3981 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003982 return;
3983 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003984
Daniel Sandler843e8602010-06-07 14:59:01 -04003985 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3986 if (DEBUG_WIDGETS) {
3987 Log.d(TAG, "bindAppWidget: " + item);
3988 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003989 final Workspace workspace = mWorkspace;
3990
Adam Cohen59400422014-03-05 18:07:04 -08003991 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003992 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003993
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003994 if (!mIsSafeModeEnabled
3995 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3996 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003997 if (appWidgetInfo == null) {
3998 if (DEBUG_WIDGETS) {
3999 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4000 + " belongs to component " + item.providerName
4001 + ", as the povider is null");
4002 }
4003 LauncherModel.deleteItemFromDatabase(this, item);
4004 return;
4005 }
4006 // Note: This assumes that the id remap broadcast is received before this step.
4007 // If that is not the case, the id remap will be ignored and user may see the
4008 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004009 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004010 pendingInfo.spanX = item.spanX;
4011 pendingInfo.spanY = item.spanY;
4012 pendingInfo.minSpanX = item.minSpanX;
4013 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004014 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004015 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004016
Sunny Goyalff572272014-07-23 13:58:07 -07004017 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004018 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4019 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004020
4021 // TODO consider showing a permission dialog when the widget is clicked.
4022 if (!success) {
4023 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4024 if (DEBUG_WIDGETS) {
4025 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4026 + " belongs to component " + item.providerName
4027 + ", as the launcher is unable to bing a new widget id");
4028 }
4029 LauncherModel.deleteItemFromDatabase(this, item);
4030 return;
4031 }
4032
4033 item.appWidgetId = newWidgetId;
4034
4035 // If the widget has a configure activity, it is still needs to set it up, otherwise
4036 // the widget is ready to go.
4037 item.restoreStatus = (appWidgetInfo.configure == null)
4038 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4039 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4040
4041 LauncherModel.updateItemInDatabase(this, item);
4042 }
4043
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004044 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004045 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004046 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004047 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4048 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004049 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004050
Sunny Goyal651077b2014-06-30 14:15:31 -07004051 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4052 } else {
4053 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004054 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4055 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004056 view.updateIcon(mIconCache);
4057 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004058 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004059 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004060 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061
Joe Onorato9c1289c2009-08-17 11:03:03 -04004062 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004063 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004064
Adam Cohendcd297f2013-06-18 13:13:40 -07004065 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004067 if (!item.isCustomWidget()) {
4068 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4069 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004070
Joe Onorato9c1289c2009-08-17 11:03:03 -04004071 workspace.requestLayout();
4072
Daniel Sandler843e8602010-06-07 14:59:01 -04004073 if (DEBUG_WIDGETS) {
4074 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4075 + (SystemClock.uptimeMillis()-start) + "ms");
4076 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004077 }
4078
Sunny Goyalff572272014-07-23 13:58:07 -07004079 /**
4080 * Restores a pending widget.
4081 *
4082 * @param appWidgetId The app widget id
4083 * @param cellInfo The position on screen where to create the widget.
4084 */
4085 private void completeRestoreAppWidget(final int appWidgetId) {
4086 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4087 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4088 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4089 return;
4090 }
4091
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004092 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004093 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4094
4095 mWorkspace.reinflateWidgetsIfNecessary();
4096 LauncherModel.updateItemInDatabase(this, info);
4097 }
4098
Adam Cohen1462de32012-07-24 22:34:36 -07004099 public void onPageBoundSynchronously(int page) {
4100 mSynchronouslyBoundPages.add(page);
4101 }
4102
Joe Onorato9c1289c2009-08-17 11:03:03 -04004103 /**
4104 * Callback saying that there aren't any more items to bind.
4105 *
4106 * Implementation of the method from LauncherModel.Callbacks.
4107 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004108 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004109 Runnable r = new Runnable() {
4110 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004111 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004112 }
4113 };
4114 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004115 return;
4116 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004117 if (mSavedState != null) {
4118 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004119 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004120 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004121 mSavedState = null;
4122 }
4123
Adam Cohen1462de32012-07-24 22:34:36 -07004124 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004125
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004126 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004127 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004128
4129 // If we received the result of any pending adds while the loader was running (e.g. the
4130 // widget configuration forced an orientation change), process them now.
4131 if (sPendingAddItem != null) {
4132 final long screenId = completeAdd(sPendingAddItem);
4133
4134 // TODO: this moves the user to the page where the pending item was added. Ideally,
4135 // the screen would be guaranteed to exist after bind, and the page would be set through
4136 // the workspace restore process.
4137 mWorkspace.post(new Runnable() {
4138 @Override
4139 public void run() {
4140 mWorkspace.snapToScreenId(screenId);
4141 }
4142 });
4143 sPendingAddItem = null;
4144 }
4145
Sunny Goyal756adbc2015-04-16 15:20:51 -07004146 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004147
4148 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004149 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004150 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004151 }
4152
Adam Cohen3ed316a2014-07-23 18:21:20 -07004153 private void sendLoadingCompleteBroadcastIfNecessary() {
4154 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4155 String permission =
4156 getResources().getString(R.string.receive_first_load_broadcast_permission);
4157 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4158 sendBroadcast(intent, permission);
4159 SharedPreferences.Editor editor = mSharedPrefs.edit();
4160 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4161 editor.apply();
4162 }
4163 }
4164
Winson Chunga0b7e862013-09-05 16:03:15 -07004165 public boolean isAllAppsButtonRank(int rank) {
4166 if (mHotseat != null) {
4167 return mHotseat.isAllAppsButtonRank(rank);
4168 }
4169 return false;
4170 }
4171
Winson Chunga2413752012-04-03 14:22:34 -07004172 private boolean canRunNewAppsAnimation() {
4173 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4174 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4175 }
4176
Winson Chungc9168342013-06-26 14:54:55 -07004177 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4178 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4179 PropertyValuesHolder.ofFloat("alpha", 1f),
4180 PropertyValuesHolder.ofFloat("scaleX", 1f),
4181 PropertyValuesHolder.ofFloat("scaleY", 1f));
4182 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4183 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4184 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4185 return bounceAnim;
4186 }
4187
Adam Cohen27772732013-11-11 14:00:56 +00004188 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004189 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004190 }
4191
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004192 protected Rect getSearchBarBounds() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004193 return mDeviceProfile.getSearchBarBounds();
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004194 }
4195
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004196 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004197 if (mSearchDropTargetBar == null) {
4198 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004199 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004200 if (mQsb != null) {
4201 mSearchDropTargetBar.removeView(mQsb);
4202 mQsb = null;
4203 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004204 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004205 }
4206
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004207 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004208 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4209 * multiple calls to bind the same list.)
4210 */
4211 @Thunk ArrayList<AppInfo> mTmpAppsList;
4212 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4213 public void run() {
4214 bindAllApplications(mTmpAppsList);
4215 mTmpAppsList = null;
4216 }
4217 };
4218
4219 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004220 * Add the icons for all apps.
4221 *
4222 * Implementation of the method from LauncherModel.Callbacks.
4223 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004224 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004225 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4226 mTmpAppsList = apps;
4227 return;
4228 }
4229
Winson Chungb745afb2015-03-02 11:51:23 -08004230 if (mAppsView != null) {
4231 mAppsView.setApps(apps);
4232 }
Adam Cohen9211d422014-10-07 18:14:53 -07004233 if (mLauncherCallbacks != null) {
4234 mLauncherCallbacks.bindAllApplications(apps);
4235 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004236 }
4237
4238 /**
4239 * A package was updated.
4240 *
4241 * Implementation of the method from LauncherModel.Callbacks.
4242 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004243 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004244 Runnable r = new Runnable() {
4245 public void run() {
4246 bindAppsUpdated(apps);
4247 }
4248 };
4249 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004250 return;
4251 }
4252
Winson Chungb745afb2015-03-02 11:51:23 -08004253 if (mAppsView != null) {
4254 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004255 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004256 }
4257
Sunny Goyal4390ace2014-10-13 11:33:11 -07004258 @Override
4259 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4260 Runnable r = new Runnable() {
4261 public void run() {
4262 bindWidgetsRestored(widgets);
4263 }
4264 };
4265 if (waitUntilResume(r)) {
4266 return;
4267 }
4268 mWorkspace.widgetsRestored(widgets);
4269 }
4270
Joe Onorato9c1289c2009-08-17 11:03:03 -04004271 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004272 * Some shortcuts were updated in the background.
4273 *
4274 * Implementation of the method from LauncherModel.Callbacks.
4275 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004276 @Override
4277 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4278 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004279 Runnable r = new Runnable() {
4280 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004281 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004282 }
4283 };
4284 if (waitUntilResume(r)) {
4285 return;
4286 }
4287
Sunny Goyal4390ace2014-10-13 11:33:11 -07004288 if (!updated.isEmpty()) {
4289 mWorkspace.updateShortcuts(updated);
4290 }
4291
4292 if (!removed.isEmpty()) {
4293 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4294 for (ShortcutInfo si : removed) {
4295 removedComponents.add(si.getTargetComponent());
4296 }
4297 mWorkspace.removeItemsByComponentName(removedComponents, user);
4298 // Notify the drag controller
4299 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004300 }
4301 }
4302
4303 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004304 * Update the state of a package, typically related to install state.
4305 *
4306 * Implementation of the method from LauncherModel.Callbacks.
4307 */
Sunny Goyale755d462014-07-22 13:48:29 -07004308 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004309 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4310 Runnable r = new Runnable() {
4311 public void run() {
4312 bindRestoreItemsChange(updates);
4313 }
4314 };
4315 if (waitUntilResume(r)) {
4316 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004317 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004318
Sunny Goyal756adbc2015-04-16 15:20:51 -07004319 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004320 }
4321
4322 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004323 * A package was uninstalled. We take both the super set of packageNames
4324 * in addition to specific applications to remove, the reason being that
4325 * this can be called when a package is updated as well. In that scenario,
4326 * we only remove specific components from the workspace, where as
4327 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004328 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004329 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004330 * Implementation of the method from LauncherModel.Callbacks.
4331 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004332 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004333 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004334 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004335 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004336 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004337 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004338 }
Winson Chungd64d1762013-08-20 14:37:16 -07004339 };
4340 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004341 return;
4342 }
4343
Sunny Goyal1a745e82014-10-02 15:58:31 -07004344 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004345 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4346 for (AppInfo info : appInfos) {
4347 removedComponents.add(info.componentName);
4348 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004349 if (!packageNames.isEmpty()) {
4350 mWorkspace.removeItemsByPackageName(packageNames, user);
4351 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004352 if (!removedComponents.isEmpty()) {
4353 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004354 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004355 // Notify the drag controller
4356 mDragController.onAppsRemoved(packageNames, removedComponents);
4357
Sunny Goyal1a745e82014-10-02 15:58:31 -07004358 } else {
4359 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004360 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004361
Winson Chungdf95eb12013-10-16 14:57:07 -07004362 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004363 if (mAppsView != null) {
4364 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004365 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004366 }
Joe Onoratobe386092009-11-17 17:32:16 -08004367
Adam Cohen091440a2015-03-18 14:16:05 -07004368 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004369 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004370 public void run() {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004371 bindAllPackages(mWidgetsAndShortcuts);
Winson Chung83892cc2013-05-01 16:53:33 -07004372 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004373 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004374
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004375 @Override
4376 public void bindAllPackages(final ArrayList<Object> widgetsAndShortcuts) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004377 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4378 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004379 return;
4380 }
4381
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004382 if (mWidgetsView != null && widgetsAndShortcuts != null) {
4383 mWidgetsView.addWidgets(widgetsAndShortcuts, getPackageManager());
4384 mWidgetsAndShortcuts = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004385 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004386 }
4387
Winson Chung400438b2011-01-16 17:53:48 -08004388 private int mapConfigurationOriActivityInfoOri(int configOri) {
4389 final Display d = getWindowManager().getDefaultDisplay();
4390 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4391 switch (d.getRotation()) {
4392 case Surface.ROTATION_0:
4393 case Surface.ROTATION_180:
4394 // We are currently in the same basic orientation as the natural orientation
4395 naturalOri = configOri;
4396 break;
4397 case Surface.ROTATION_90:
4398 case Surface.ROTATION_270:
4399 // We are currently in the other basic orientation to the natural orientation
4400 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4401 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4402 break;
4403 }
4404
4405 int[] oriMap = {
4406 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4407 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4408 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4409 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4410 };
4411 // Since the map starts at portrait, we need to offset if this device's natural orientation
4412 // is landscape.
4413 int indexOffset = 0;
4414 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4415 indexOffset = 1;
4416 }
4417 return oriMap[(d.getRotation() + indexOffset) % 4];
4418 }
Adam Cohen446e9402011-09-15 18:21:21 -07004419
Winson Chung4b919f82012-05-01 10:44:08 -07004420 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004421 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004422 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4423 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4424 .getConfiguration().orientation));
4425 } else {
4426 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4427 }
Winson Chung4b919f82012-05-01 10:44:08 -07004428 }
4429 }
4430 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004431 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004432 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004433 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004434 } else {
4435 mHandler.postDelayed(new Runnable() {
4436 public void run() {
4437 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4438 }
4439 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004440 }
Winson Chung4b919f82012-05-01 10:44:08 -07004441 }
Winson Chung400438b2011-01-16 17:53:48 -08004442 }
4443
Winson Chunge43a1e72014-01-15 10:33:02 -08004444 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004445 if (mLauncherCallbacks != null) {
4446 return mLauncherCallbacks.isLauncherPreinstalled();
4447 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004448 PackageManager pm = getPackageManager();
4449 try {
4450 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4451 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4452 return true;
4453 } else {
4454 return false;
4455 }
4456 } catch (NameNotFoundException e) {
4457 e.printStackTrace();
4458 return false;
4459 }
4460 }
4461
Adam Cohenea90f832014-05-21 15:03:34 -07004462 /**
4463 * This method indicates whether or not we should suggest default wallpaper dimensions
4464 * when our wallpaper cropper was not yet used to set a wallpaper.
4465 */
4466 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004467 if (mLauncherCallbacks != null) {
4468 return mLauncherCallbacks.overrideWallpaperDimensions();
4469 }
Adam Cohenea90f832014-05-21 15:03:34 -07004470 return true;
4471 }
4472
Adam Cohen432609a2014-03-13 17:03:22 -07004473 /**
4474 * To be overridden by subclasses to indicate that there is an activity to launch
4475 * before showing the standard launcher experience.
4476 */
4477 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004478 if (mLauncherCallbacks != null) {
4479 return mLauncherCallbacks.hasFirstRunActivity();
4480 }
Adam Cohen432609a2014-03-13 17:03:22 -07004481 return false;
4482 }
4483
4484 /**
4485 * To be overridden by subclasses to launch any first run activity
4486 */
4487 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004488 if (mLauncherCallbacks != null) {
4489 return mLauncherCallbacks.getFirstRunActivity();
4490 }
Adam Cohen432609a2014-03-13 17:03:22 -07004491 return null;
4492 }
4493
Winson Chung2826a402015-04-17 16:18:53 -07004494 /**
4495 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004496 */
4497 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004498 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4499 return false;
4500 }
4501
Winson Chung2826a402015-04-17 16:18:53 -07004502 if (mLauncherCallbacks != null) {
4503 return mLauncherCallbacks.overrideAllAppsSearch();
4504 }
4505 return false;
4506 }
4507
Winson Chunga6945242014-01-08 14:04:34 -08004508 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004509 return !ActivityManager.isRunningInTestHarness() &&
4510 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004511 }
4512
Adam Cohen4a9423d2014-03-28 14:22:31 -07004513 protected boolean hasRunFirstRunActivity() {
4514 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4515 }
4516
Adam Cohen432609a2014-03-13 17:03:22 -07004517 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004518 if (shouldRunFirstRunActivity() &&
4519 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004520 Intent firstRunIntent = getFirstRunActivity();
4521 if (firstRunIntent != null) {
4522 startActivity(firstRunIntent);
4523 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004524 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004525 }
4526 }
Adam Cohen432609a2014-03-13 17:03:22 -07004527 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004528 }
4529
4530 private void markFirstRunActivityShown() {
4531 SharedPreferences.Editor editor = mSharedPrefs.edit();
4532 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4533 editor.apply();
4534 }
4535
Adam Cohen432609a2014-03-13 17:03:22 -07004536 /**
4537 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4538 * screen that must be displayed and dismissed.
4539 */
4540 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004541 if (mLauncherCallbacks != null) {
4542 return mLauncherCallbacks.hasDismissableIntroScreen();
4543 }
Adam Cohen432609a2014-03-13 17:03:22 -07004544 return false;
4545 }
4546
4547 /**
4548 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4549 */
4550 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004551 if (mLauncherCallbacks != null) {
4552 return mLauncherCallbacks.getIntroScreen();
4553 }
Adam Cohen432609a2014-03-13 17:03:22 -07004554 return null;
4555 }
4556
4557 /**
4558 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4559 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4560 */
4561 private boolean shouldShowIntroScreen() {
4562 return hasDismissableIntroScreen() &&
4563 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4564 }
4565
4566 protected void showIntroScreen() {
4567 View introScreen = getIntroScreen();
4568 changeWallpaperVisiblity(false);
4569 if (introScreen != null) {
4570 mDragLayer.showOverlayView(introScreen);
4571 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004572 if (mLauncherOverlayContainer != null) {
4573 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4574 }
Adam Cohen432609a2014-03-13 17:03:22 -07004575 }
4576
4577 public void dismissIntroScreen() {
4578 markIntroScreenDismissed();
4579 if (showFirstRunActivity()) {
4580 // We delay hiding the intro view until the first run activity is showing. This
4581 // avoids a blip.
4582 mWorkspace.postDelayed(new Runnable() {
4583 @Override
4584 public void run() {
4585 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004586 if (mLauncherOverlayContainer != null) {
4587 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4588 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004589 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004590 }
4591 }, ACTIVITY_START_DELAY);
4592 } else {
4593 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004594 if (mLauncherOverlayContainer != null) {
4595 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4596 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004597 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004598 }
4599 changeWallpaperVisiblity(true);
4600 }
4601
4602 private void markIntroScreenDismissed() {
4603 SharedPreferences.Editor editor = mSharedPrefs.edit();
4604 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4605 editor.apply();
4606 }
4607
Adam Cohen091440a2015-03-18 14:16:05 -07004608 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004609 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4610 // on the device, then we always show the first run cling experience (or if there is no
4611 // launcher2). Otherwise, we prompt the user upon started for migration
4612 LauncherClings launcherClings = new LauncherClings(this);
4613 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4614 if (mModel.canMigrateFromOldLauncherDb(this)) {
4615 launcherClings.showMigrationCling();
4616 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004617 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004618 }
4619 }
4620 }
4621
Winson Chunga6945242014-01-08 14:04:34 -08004622 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004623 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4624 if (mHotseat != null) mHotseat.setAlpha(1f);
4625 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4626 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004627 }
4628
4629 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004630 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4631 if (mHotseat != null) mHotseat.setAlpha(0f);
4632 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4633 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004634 }
4635
Mathew Inwood72fbec12013-11-19 15:45:07 +00004636 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004637 // Called from search suggestion, not supported in other profiles.
4638 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4639 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4640 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4641 myUser);
4642 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004643 return null;
4644 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004645 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004646 }
4647
4648 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4649 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004650 // Called from search suggestion, not supported in other profiles.
4651 return createShortcutDragInfo(shortcutIntent, caption, icon,
4652 UserHandleCompat.myUserHandle());
4653 }
4654
4655 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4656 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004657 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004658 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004659 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004660 }
4661
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004662 protected void moveWorkspaceToDefaultScreen() {
4663 mWorkspace.moveToDefaultScreen(false);
4664 }
4665
Mathew Inwood72fbec12013-11-19 15:45:07 +00004666 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4667 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004668 mWorkspace.onExternalDragStartedWithItem(dragView);
4669 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004670 }
4671
Anjali Koppalf5d29132014-02-28 13:33:27 -08004672 @Override
4673 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004674 if (mLauncherCallbacks != null) {
4675 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4676 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004677 }
4678
Winson Chung80baf5a2010-08-09 16:03:15 -07004679 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004680 * Prints out out state for debugging.
4681 */
4682 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004683 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004684 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004685 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4686 Log.d(TAG, "mRestoring=" + mRestoring);
4687 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4688 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004689 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004690 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004691 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004692
Daniel Sandler325dc232013-06-05 22:57:57 -04004693 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004694 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004695
4696 @Override
4697 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4698 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004699 synchronized (sDumpLogs) {
4700 writer.println(" ");
4701 writer.println("Debug logs: ");
4702 for (int i = 0; i < sDumpLogs.size(); i++) {
4703 writer.println(" " + sDumpLogs.get(i));
4704 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004705 }
Adam Cohen9211d422014-10-07 18:14:53 -07004706 if (mLauncherCallbacks != null) {
4707 mLauncherCallbacks.dump(prefix, fd, writer, args);
4708 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004709 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004710
4711 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004712 if (DEBUG_DUMP_LOG) {
4713 synchronized (sDumpLogs) {
4714 Log.d(TAG, "");
4715 Log.d(TAG, "*********************");
4716 Log.d(TAG, "Launcher debug logs: ");
4717 for (int i = 0; i < sDumpLogs.size(); i++) {
4718 Log.d(TAG, " " + sDumpLogs.get(i));
4719 }
4720 Log.d(TAG, "*********************");
4721 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004722 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004723 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004724 }
4725
4726 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004727 addDumpLog(tag, log, null, debugLog);
4728 }
4729
4730 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004731 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004732 if (e != null) {
4733 Log.d(tag, log, e);
4734 } else {
4735 Log.d(tag, log);
4736 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004737 }
Winson Chungede41292013-09-19 16:27:36 -07004738 if (DEBUG_DUMP_LOG) {
4739 sDateStamp.setTime(System.currentTimeMillis());
4740 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004741 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4742 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004743 }
Winson Chungede41292013-09-19 16:27:36 -07004744 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004745 }
4746
Adam Cohen59400422014-03-05 18:07:04 -08004747 public static CustomAppWidget getCustomAppWidget(String name) {
4748 return sCustomAppWidgets.get(name);
4749 }
4750
4751 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4752 return sCustomAppWidgets;
4753 }
4754
Winson Chungede41292013-09-19 16:27:36 -07004755 public void dumpLogsToLocalData() {
4756 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004757 new AsyncTask<Void, Void, Void>() {
4758 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004759 boolean success = false;
4760 sDateStamp.setTime(sRunStart);
4761 String FILENAME = sDateStamp.getMonth() + "-"
4762 + sDateStamp.getDay() + "_"
4763 + sDateStamp.getHours() + "-"
4764 + sDateStamp.getMinutes() + "_"
4765 + sDateStamp.getSeconds() + ".txt";
4766
4767 FileOutputStream fos = null;
4768 File outFile = null;
4769 try {
4770 outFile = new File(getFilesDir(), FILENAME);
4771 outFile.createNewFile();
4772 fos = new FileOutputStream(outFile);
4773 } catch (Exception e) {
4774 e.printStackTrace();
4775 }
4776 if (fos != null) {
4777 PrintWriter writer = new PrintWriter(fos);
4778
4779 writer.println(" ");
4780 writer.println("Debug logs: ");
4781 synchronized (sDumpLogs) {
4782 for (int i = 0; i < sDumpLogs.size(); i++) {
4783 writer.println(" " + sDumpLogs.get(i));
4784 }
4785 }
4786 writer.close();
4787 }
4788 try {
4789 if (fos != null) {
4790 fos.close();
4791 success = true;
4792 }
4793 } catch (IOException e) {
4794 e.printStackTrace();
4795 }
Michael Jurka43467462013-11-14 12:03:58 +01004796 return null;
Winson Chungede41292013-09-19 16:27:36 -07004797 }
Michael Jurka43467462013-11-14 12:03:58 +01004798 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004799 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004800 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004801}
Michael Jurka2763be32011-02-24 11:19:57 -08004802
Michael Jurkaabded662011-03-04 12:06:57 -08004803interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004804 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004805 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004806 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004807 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004808 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004809}
Dan Sandlerd5024042014-01-09 15:01:33 -05004810
4811interface DebugIntents {
4812 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4813 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004814}