blob: 51ba2dfc908288ad68bc16d973a4e2b8d58efa78 [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 Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale2df0622015-04-24 11:27:00 -0700104import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700105import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700106import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700107import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700108import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700109
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.io.DataInputStream;
111import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700113import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800118import java.lang.reflect.InvocationTargetException;
119import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700120import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700122import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700125import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700126import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000127import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129/**
130 * Default launcher application.
131 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100132public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700133 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800134 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
135 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700136 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700137 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Winson Chung83f59ab2015-05-05 17:21:58 -0700139 // Temporary flag
140 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
141
Daniel Sandlerf061f822013-06-27 13:59:36 -0400142 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700143 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700144 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400145 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700146 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700147
Dan Sandlerd5024042014-01-09 15:01:33 -0500148 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700153 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Michael Jurka8b805b12012-04-18 14:23:14 -0700155 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700156 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700157
Mathew Inwood876a8462013-06-14 14:12:41 +0100158 /**
159 * IntentStarter uses request codes starting with this. This must be greater than all activity
160 * request codes used internally.
161 */
162 protected static final int REQUEST_LAST = 100;
163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
165
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800166 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 // To turn on these properties, type
169 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800170 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Winson Chung2672ff92012-05-04 16:22:30 -0700172 // The Intent extra that defines whether to ignore the launch animation
173 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400174 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
177 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700178 // Type: int
179 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
182 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
184 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700185 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700188 // Type: int
189 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: int[]
197 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
Adam Cohen432609a2014-03-13 17:03:22 -0700199 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800200 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
201
Adam Cohen3ed316a2014-07-23 18:21:20 -0700202 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
203 static final String ACTION_FIRST_LOAD_COMPLETE =
204 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
205
Adam Cohen39a06042013-07-19 14:30:12 -0700206 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700207 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700208
Sunny Goyal594d76d2014-11-06 10:12:54 -0800209 private static final String QSB_WIDGET_ID = "qsb_widget_id";
210 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
211
Winson Chunga6945242014-01-08 14:04:34 -0800212 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
213
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700214 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800215 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700216 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700218
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700219 private boolean mIsSafeModeEnabled;
220
Adam Cohenc2d6e892014-10-16 09:49:24 -0700221 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
222 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700223 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700224
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700226 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
227 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700228 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000230 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
231 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
232
Winson Chunga2413752012-04-03 14:22:34 -0700233 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700234 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
235 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700236 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700237
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800238 private final BroadcastReceiver mCloseSystemDialogsReceiver
239 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800240 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
241
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 private LayoutInflater mInflater;
243
Adam Cohen091440a2015-03-18 14:16:05 -0700244 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700245 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800246 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700247 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800248 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700249 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700250
Sunny Goyalffe83f12014-08-14 17:39:34 -0700251 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700252 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700253
Adam Cohen091440a2015-03-18 14:16:05 -0700254 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800255 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800256 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700257
Michael Jurka0280c3b2010-09-17 15:00:07 -0700258 private int[] mTmpAddItemCellCoordinates = new int[2];
259
Winson Chung3d503fb2011-07-13 17:25:49 -0700260 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800261 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700262
Michael Jurka838a4ca2011-02-07 13:33:06 -0800263 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700264
Winson Chungc51db6a2011-10-05 11:44:49 -0700265 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700266
267 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700268 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700269
270 // Main container view for the widget tray screen.
271 private WidgetsContainerView mWidgetsView;
272
Winson Chungf0ea4d32011-06-06 14:27:16 -0700273 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800274 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700277 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
278 // scroll issues (because the workspace may not have been measured yet) and extra work.
279 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
280 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
282 private SpannableStringBuilder mDefaultKeySsb = null;
283
Adam Cohen091440a2015-03-18 14:16:05 -0700284 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800286 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 private boolean mRestoring;
288 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700289 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290
Michael Jurka1e2f4652013-07-08 18:03:46 -0700291 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700292 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 private Bundle mSavedInstanceState;
295
Joe Onorato9c1289c2009-08-17 11:03:03 -0400296 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800297 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700298 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800299 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700300 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800301 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302
Adam Cohen091440a2015-03-18 14:16:05 -0700303 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700304
Sunny Goyale2df0622015-04-24 11:27:00 -0700305 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700306
Adam Cohen61f560d2013-09-30 15:58:20 -0700307 private View.OnTouchListener mHapticFeedbackTouchListener;
308
Adam Cohended9f8d2010-11-03 13:25:16 -0700309 // Related to the auto-advancing of widgets
310 private final int ADVANCE_MSG = 1;
311 private final int mAdvanceInterval = 20000;
312 private final int mAdvanceStagger = 250;
313 private long mAutoAdvanceSentTime;
314 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700315 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700316 new HashMap<View, AppWidgetProviderInfo>();
317
Winson Chung400438b2011-01-16 17:53:48 -0800318 // Determines how long to wait after a rotation before restoring the screen orientation to
319 // match the sensor state.
320 private final int mRestoreScreenOrientationDelay = 500;
321
Adam Cohen091440a2015-03-18 14:16:05 -0700322 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700323
Adam Cohen1462de32012-07-24 22:34:36 -0700324 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700325 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700326
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700327 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700328 static Date sDateStamp = new Date();
329 static DateFormat sDateFormat =
330 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
331 static long sRunStart = System.currentTimeMillis();
332 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700333
Winson Chung46353de2012-02-16 14:05:10 -0800334 // We only want to get the SharedPreferences once since it does an FS stat each time we get
335 // it from the context.
336 private SharedPreferences mSharedPrefs;
337
Winson Chungf0c6ae02012-03-21 16:10:31 -0700338 // Holds the page that we need to animate to, and the icon views that we need to animate up
339 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700340 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700341 private Bitmap mFolderIconBitmap;
342 private Canvas mFolderIconCanvas;
343 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700344
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700345 private DeviceProfile mDeviceProfile;
346
Winson Chung13eb5272015-05-11 16:30:13 -0700347 // This is set to the view that launched the activity that navigated the user away from
348 // launcher. Since there is no callback for when the activity has finished launching, enable
349 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800350 private BubbleTextView mWaitingForResume;
351
Adam Cohen59400422014-03-05 18:07:04 -0800352 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
353 new HashMap<String, CustomAppWidget>();
354
355 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
356 static {
357 if (ENABLE_CUSTOM_WIDGET_TEST) {
358 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
359 }
360 }
361
Chet Haasea8f996d2015-02-17 12:56:04 -0800362 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
363 private static Method sClipRevealMethod = null;
364 static {
365 Class<?> activityOptionsClass = ActivityOptions.class;
366 try {
367 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
368 View.class, int.class, int.class, int.class, int.class);
369 } catch (Exception e) {
370 // Earlier version
371 }
372 }
373
Adam Cohen091440a2015-03-18 14:16:05 -0700374 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700375 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700376 if (mWorkspace != null) {
377 mWorkspace.buildPageHardwareLayers();
378 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700379 }
380 };
381
Adam Cohendb364c32014-05-20 14:23:40 -0700382 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800383
Adam Cohen091440a2015-03-18 14:16:05 -0700384 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800385 int requestCode;
386 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700387 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700388 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800389 int cellX;
390 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700391 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800392 }
393
Daniel Sandlerff02d492013-08-05 02:12:05 -0400394 private Stats mStats;
395
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700396 FocusIndicatorView mFocusHandler;
397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 @Override
399 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700400 if (DEBUG_STRICT_MODE) {
401 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
402 .detectDiskReads()
403 .detectDiskWrites()
404 .detectNetwork() // or .detectAll() for all detectable problems
405 .penaltyLog()
406 .build());
407 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
408 .detectLeakedSqlLiteObjects()
409 .detectLeakedClosableObjects()
410 .penaltyLog()
411 .penaltyDeath()
412 .build());
413 }
414
Adam Cohen9211d422014-10-07 18:14:53 -0700415 if (mLauncherCallbacks != null) {
416 mLauncherCallbacks.preOnCreate();
417 }
418
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400420
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400421 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400422 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700423 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700424
Winson Chung5f8afe62013-08-12 16:19:28 -0700425 // Lazy-initialize the dynamic grid
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700426 mDeviceProfile = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700427
428 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400429 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700430 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700431 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800432 mModel = app.setLauncher(this);
433 mIconCache = app.getIconCache();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700434 mIconCache.flushInvalidIcons(mDeviceProfile);
Joe Onorato41a12d22009-10-31 18:30:00 -0400435 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800437 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700438
Daniel Sandlerff02d492013-08-05 02:12:05 -0400439 mStats = new Stats(this);
440
Sunny Goyalffe83f12014-08-14 17:39:34 -0700441 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700442
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700443 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
444 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700445
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700446 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
447 // this also ensures that any synchronous binding below doesn't re-trigger another
448 // LauncherModel load.
449 mPaused = false;
450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200452 android.os.Debug.startMethodTracing(
453 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 }
455
456 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700458
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700460 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800462 registerContentObservers();
463
Joe Onorato7c312c12009-08-13 21:36:53 -0700464 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 mSavedState = savedInstanceState;
467 restoreState(mSavedState);
468
469 if (PROFILE_STARTUP) {
470 android.os.Debug.stopMethodTracing();
471 }
472
473 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700474 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700475 // If the user leaves launcher, then we should just load items asynchronously when
476 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700477 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 } else {
479 // We only load the page synchronously if the user rotates (or triggers a
480 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700481 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 }
484
485 // For handling default keys
486 mDefaultKeySsb = new SpannableStringBuilder();
487 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800488
489 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
490 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800491
Adam Cohenf9426d52012-06-04 17:26:21 -0700492 // On large interfaces, we want the screen to auto-rotate based on the current orientation
493 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700494
Adam Cohen9211d422014-10-07 18:14:53 -0700495 if (mLauncherCallbacks != null) {
496 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700497 if (mLauncherCallbacks.hasLauncherOverlay()) {
498 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700499 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
500 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
501 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700502 mWorkspace.setLauncherOverlay(mLauncherOverlay);
503 }
Adam Cohen9211d422014-10-07 18:14:53 -0700504 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700505
506 if (shouldShowIntroScreen()) {
507 showIntroScreen();
508 } else {
509 showFirstRunActivity();
510 showFirstRunClings();
511 }
Adam Cohen9211d422014-10-07 18:14:53 -0700512 }
513
514 private LauncherCallbacks mLauncherCallbacks;
515
516 public void onPostCreate(Bundle savedInstanceState) {
517 super.onPostCreate(savedInstanceState);
518 if (mLauncherCallbacks != null) {
519 mLauncherCallbacks.onPostCreate(savedInstanceState);
520 }
521 }
522
523 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
524 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700525 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
526 @Override
527 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700528 if (LOGD) {
529 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
530 }
Winson Chung94804152015-05-08 13:06:44 -0700531 mAppsView.setFixedBounds(bounds);
532 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700533 }
534
535 @Override
536 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700537 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
538 // Dismiss All Apps if we aren't already paused/invisible
539 if (!mPaused) {
540 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
541 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
542 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700543 }
Winson Chung0f785722015-04-08 10:27:49 -0700544 }
545 });
Adam Cohen9211d422014-10-07 18:14:53 -0700546 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700547 }
548
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700549 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700550 public void onLauncherProviderChange() {
551 if (mLauncherCallbacks != null) {
552 mLauncherCallbacks.onLauncherProviderChange();
553 }
554 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700555
Adam Cohen9211d422014-10-07 18:14:53 -0700556 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700557 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700558 if (mLauncherCallbacks != null) {
559 return mLauncherCallbacks.hasCustomContentToLeft();
560 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700561 return false;
562 }
563
Dave Hawkeya8881582013-09-17 15:55:33 -0600564 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500565 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600566 * {@link #addToCustomContentPage}. This will only be invoked if
567 * {@link #hasCustomContentToLeft()} is {@code true}.
568 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500569 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700570 if (mLauncherCallbacks != null) {
571 mLauncherCallbacks.populateCustomContentContainer();
572 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600573 }
574
575 /**
576 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
577 * ensure the custom content page is added or removed if necessary.
578 */
579 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600580 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600581 // Not bound yet, wait for bindScreens to be called.
582 return;
583 }
584
585 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
586 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500587 mWorkspace.createCustomContentContainer();
588 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600589 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
590 mWorkspace.removeCustomContentPage();
591 }
592 }
593
Adam Cohen091440a2015-03-18 14:16:05 -0700594 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700595 if (sLocaleConfiguration == null) {
596 new AsyncTask<Void, Void, LocaleConfiguration>() {
597 @Override
598 protected LocaleConfiguration doInBackground(Void... unused) {
599 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
600 readConfiguration(Launcher.this, localeConfiguration);
601 return localeConfiguration;
602 }
603
604 @Override
605 protected void onPostExecute(LocaleConfiguration result) {
606 sLocaleConfiguration = result;
607 checkForLocaleChange(); // recursive, but now with a locale configuration
608 }
609 }.execute();
610 return;
611 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700612
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 final Configuration configuration = getResources().getConfiguration();
614
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700615 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 final String locale = configuration.locale.toString();
617
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700618 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 final int mcc = configuration.mcc;
620
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700621 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 final int mnc = configuration.mnc;
623
Romain Guy84f296c2009-11-04 15:00:44 -0800624 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625
Romain Guy84f296c2009-11-04 15:00:44 -0800626 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700627 sLocaleConfiguration.locale = locale;
628 sLocaleConfiguration.mcc = mcc;
629 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700630
Joe Onorato0589f0f2010-02-08 13:44:00 -0800631 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700632
633 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100634 new AsyncTask<Void, Void, Void>() {
635 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700636 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100637 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700638 }
Michael Jurka43467462013-11-14 12:03:58 +0100639 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700640 }
641 }
642
Adam Cohen091440a2015-03-18 14:16:05 -0700643 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700644 public String locale;
645 public int mcc = -1;
646 public int mnc = -1;
647 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700648
Adam Cohen091440a2015-03-18 14:16:05 -0700649 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700650 DataInputStream in = null;
651 try {
Helena Josol28db2802014-10-09 17:04:09 +0100652 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700653 configuration.locale = in.readUTF();
654 configuration.mcc = in.readInt();
655 configuration.mnc = in.readInt();
656 } catch (FileNotFoundException e) {
657 // Ignore
658 } catch (IOException e) {
659 // Ignore
660 } finally {
661 if (in != null) {
662 try {
663 in.close();
664 } catch (IOException e) {
665 // Ignore
666 }
667 }
668 }
669 }
670
Adam Cohen091440a2015-03-18 14:16:05 -0700671 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700672 DataOutputStream out = null;
673 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100674 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100675 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700676 out.writeUTF(configuration.locale);
677 out.writeInt(configuration.mcc);
678 out.writeInt(configuration.mnc);
679 out.flush();
680 } catch (FileNotFoundException e) {
681 // Ignore
682 } catch (IOException e) {
683 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100684 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700685 } finally {
686 if (out != null) {
687 try {
688 out.close();
689 } catch (IOException e) {
690 // Ignore
691 }
692 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 }
694 }
695
Anton Hanssona2f665f2013-09-26 12:18:32 +0100696 public Stats getStats() {
697 return mStats;
698 }
699
Bjorn Bringertc459e522013-06-07 19:36:01 +0100700 public LayoutInflater getInflater() {
701 return mInflater;
702 }
703
Hyunyoung Song3f471442015-04-08 19:01:34 -0700704 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700705 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
706 // that is subsequently removed from the workspace in startBinding().
707 return !mModel.isLoadingWorkspace();
708 }
709
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800710 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000711 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100712 if (Build.VERSION.SDK_INT >= 17) {
713 return View.generateViewId();
714 } else {
715 // View.generateViewId() is not available. The following fallback logic is a copy
716 // of its implementation.
717 for (;;) {
718 final int result = sNextGeneratedId.get();
719 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
720 int newValue = result + 1;
721 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
722 if (sNextGeneratedId.compareAndSet(result, newValue)) {
723 return result;
724 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000725 }
726 }
727 }
728
729 public int getViewIdForItem(ItemInfo info) {
730 // This cast is safe given the > 2B range for int.
731 int itemId = (int) info.id;
732 if (mItemIdToViewId.containsKey(itemId)) {
733 return mItemIdToViewId.get(itemId);
734 }
735 int viewId = generateViewId();
736 mItemIdToViewId.put(itemId, viewId);
737 return viewId;
738 }
739
740 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700741 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
742 * a configuration step, this allows the proper animations to run after other transitions.
743 */
Adam Cohendb364c32014-05-20 14:23:40 -0700744 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700745 long screenId = args.screenId;
746 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
747 // When the screen id represents an actual screen (as opposed to a rank) we make sure
748 // that the drop page actually exists.
749 screenId = ensurePendingDropLayoutExists(args.screenId);
750 }
Adam Cohendb364c32014-05-20 14:23:40 -0700751
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800752 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800753 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700754 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700755 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700756 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800757 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700758 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700759 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700760 case REQUEST_RECONFIGURE_APPWIDGET:
761 completeRestoreAppWidget(args.appWidgetId);
762 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800763 }
Michael Jurka27614d22012-04-02 06:40:22 -0700764 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
765 // if you turned the screen off and then back while in All Apps, Launcher would not
766 // return to the workspace. Clearing mAddInfo.container here fixes this issue
767 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700768 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800769 }
770
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800771 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700773 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700774 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700775 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800776 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700777
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 Runnable exitSpringLoaded = new Runnable() {
779 @Override
780 public void run() {
781 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
782 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
783 }
784 };
785
Michael Jurka8b805b12012-04-18 14:23:14 -0700786 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700787 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700788 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
789 if (resultCode == RESULT_CANCELED) {
790 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700791 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700792 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700793 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800794 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700795 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700796 }
797 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200798 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
799 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700800 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200801 }
802 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700803 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200804
Adam Cohened66b2b2012-01-23 17:28:51 -0800805 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Hyunyoung Songb7900832015-05-15 16:29:45 -0700806 requestCode == REQUEST_CREATE_APPWIDGET || requestCode == REQUEST_CREATE_SHORTCUT);
Romain Guyaad5ef42009-06-10 02:48:37 -0700807
Adam Cohendb364c32014-05-20 14:23:40 -0700808 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800809 // We have special handling for widgets
810 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800811 final int appWidgetId;
812 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
813 : -1;
814 if (widgetId < 0) {
815 appWidgetId = pendingAddWidgetId;
816 } else {
817 appWidgetId = widgetId;
818 }
819
Adam Cohenad4e15c2013-10-17 16:21:35 -0700820 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800821 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700822 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
823 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700824 result = RESULT_CANCELED;
825 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700826 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700827 @Override
828 public void run() {
829 exitSpringLoadedDragModeDelayed(false, 0, null);
830 }
831 };
Adam Cohendb364c32014-05-20 14:23:40 -0700832 if (workspaceLocked) {
833 // No need to remove the empty screen if we're mid-binding, as the
834 // the bind will not add the empty screen.
835 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
836 } else {
837 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
838 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
839 }
Winson Chung5aaab772012-04-27 15:24:02 -0700840 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700841 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700842 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
843 // When the screen id represents an actual screen (as opposed to a rank)
844 // we make sure that the drop page actually exists.
845 mPendingAddInfo.screenId =
846 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
847 }
Adam Cohendb364c32014-05-20 14:23:40 -0700848 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
849
850 dropLayout.setDropPending(true);
851 final Runnable onComplete = new Runnable() {
852 @Override
853 public void run() {
854 completeTwoStageWidgetDrop(resultCode, appWidgetId);
855 dropLayout.setDropPending(false);
856 }
857 };
858 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
859 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
860 } else {
861 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
862 mPendingAddInfo);
863 sPendingAddItem = args;
864 }
Winson Chung5aaab772012-04-27 15:24:02 -0700865 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800866 return;
867 }
868
Sunny Goyalff572272014-07-23 13:58:07 -0700869 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
870 if (resultCode == RESULT_OK) {
871 // Update the widget view.
872 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
873 pendingAddWidgetId, mPendingAddInfo);
874 if (workspaceLocked) {
875 sPendingAddItem = args;
876 } else {
877 completeAdd(args);
878 }
879 }
880 // Leave the widget in the pending state if the user canceled the configure.
881 return;
882 }
883
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 // The pattern used here is that a user PICKs a specific application,
885 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
888 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700889 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700890 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
891 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800892 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700893 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800894 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700895 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700896 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
897 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 }
Adam Cohen00074722013-10-11 17:46:09 -0700899 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700900 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700901 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800903 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800904
905 }
906
907 @Override
908 protected void onActivityResult(
909 final int requestCode, final int resultCode, final Intent data) {
910 handleActivityResult(requestCode, resultCode, data);
911 if (mLauncherCallbacks != null) {
912 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
913 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 }
915
Adam Cohendb364c32014-05-20 14:23:40 -0700916 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
917 appWidgetId, ItemInfo info) {
918 PendingAddArguments args = new PendingAddArguments();
919 args.requestCode = requestCode;
920 args.intent = data;
921 args.container = info.container;
922 args.screenId = info.screenId;
923 args.cellX = info.cellX;
924 args.cellY = info.cellY;
925 args.appWidgetId = appWidgetId;
926 return args;
927 }
928
929 /**
930 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
931 *
932 * @param screenId the screen id to check
933 * @return the new screen, or screenId if it exists
934 */
935 private long ensurePendingDropLayoutExists(long screenId) {
936 CellLayout dropLayout =
937 (CellLayout) mWorkspace.getScreenWithId(screenId);
938 if (dropLayout == null) {
939 // it's possible that the add screen was removed because it was
940 // empty and a re-bind occurred
941 mWorkspace.addExtraEmptyScreen();
942 return mWorkspace.commitExtraEmptyScreen();
943 } else {
944 return screenId;
945 }
946 }
947
Adam Cohen091440a2015-03-18 14:16:05 -0700948 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700949 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700950 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800951 Runnable onCompleteRunnable = null;
952 int animationType = 0;
953
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700954 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800955 if (resultCode == RESULT_OK) {
956 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
957 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700958 mPendingAddWidgetInfo);
959 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800960 onCompleteRunnable = new Runnable() {
961 @Override
962 public void run() {
963 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700964 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700965 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
966 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800967 }
968 };
969 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800970 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800971 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800972 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700973 if (mDragLayer.getAnimatedView() != null) {
974 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
975 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
976 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700977 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700978 // The animated view may be null in the case of a rotation during widget configuration
979 onCompleteRunnable.run();
980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 }
982
983 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700984 protected void onStop() {
985 super.onStop();
986 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700987
988 if (mLauncherCallbacks != null) {
989 mLauncherCallbacks.onStop();
990 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700991 }
992
993 @Override
994 protected void onStart() {
995 super.onStart();
996 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700997
998 if (mLauncherCallbacks != null) {
999 mLauncherCallbacks.onStart();
1000 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001001 }
1002
1003 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001005 long startTime = 0;
1006 if (DEBUG_RESUME_TIME) {
1007 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001008 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001009 }
Adam Cohen9211d422014-10-07 18:14:53 -07001010
1011 if (mLauncherCallbacks != null) {
1012 mLauncherCallbacks.preOnResume();
1013 }
1014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001016
Winson Chung4a2afa32012-07-19 14:53:05 -07001017 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001018 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001019 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001020 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001021 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001022 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001023 // view after launching an app, as they may be depending on the UI to be static to
1024 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001025 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -07001026 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001027 } else if (mOnResumeState == State.WIDGETS) {
1028 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001029 }
1030 mOnResumeState = State.NONE;
1031
Winson Chungcd99cd32015-04-29 11:03:24 -07001032 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001033 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1034 // Otherwise, notify the callbacks if we are in all apps mode
1035 if (mState == State.APPS) {
1036 if (mLauncherCallbacks != null) {
1037 mLauncherCallbacks.onAllAppsShown();
1038 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001039 }
1040 }
1041
Craig Mautner360310b2012-10-26 15:13:08 -07001042 // Background was set to gradient in onPause(), restore to black if in all apps.
1043 setWorkspaceBackground(mState == State.WORKSPACE);
1044
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001045 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001046 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001047 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001048 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001049 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001050 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001052 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001053 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1054 // execute them here
1055 long startTimeCallbacks = 0;
1056 if (DEBUG_RESUME_TIME) {
1057 startTimeCallbacks = System.currentTimeMillis();
1058 }
1059
Michael Jurka1e2f4652013-07-08 18:03:46 -07001060 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1061 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001062 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001063 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001064 if (DEBUG_RESUME_TIME) {
1065 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1066 (System.currentTimeMillis() - startTimeCallbacks));
1067 }
Michael Jurka447bf842013-05-15 14:52:15 +02001068 }
Michael Jurka54554252013-08-01 12:52:23 +02001069 if (mOnResumeCallbacks.size() > 0) {
1070 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1071 mOnResumeCallbacks.get(i).run();
1072 }
1073 mOnResumeCallbacks.clear();
1074 }
Winson Chunge4e50662012-01-23 14:45:13 -08001075
1076 // Reset the pressed state of icons that were locked in the press state while activities
1077 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001078 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001079 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001080 mWaitingForResume.setStayPressed(false);
1081 }
Winson Chung82963d52013-10-09 11:20:57 -07001082
Adam Cohen06dff352012-06-01 17:17:08 -07001083 // It is possible that widgets can receive updates while launcher is not in the foreground.
1084 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1085 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1086 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001087 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001088 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001089
Michael Jurka447bf842013-05-15 14:52:15 +02001090 if (DEBUG_RESUME_TIME) {
1091 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1092 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001093
1094 if (mWorkspace.getCustomContentCallbacks() != null) {
1095 // If we are resuming and the custom content is the current page, we call onShow().
1096 // It is also poassible that onShow will instead be called slightly after first layout
1097 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1098 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001099 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001100 }
1101 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001102 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001103 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001104
Sunny Goyal756adbc2015-04-16 15:20:51 -07001105 if (!isWorkspaceLoading()) {
1106 // Process any items that were added while Launcher was away.
1107 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1108 }
Adam Cohen9211d422014-10-07 18:14:53 -07001109
1110 if (mLauncherCallbacks != null) {
1111 mLauncherCallbacks.onResume();
1112 }
Adam Cohen06dff352012-06-01 17:17:08 -07001113 }
1114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001116 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001117 // Ensure that items added to Launcher are queued until Launcher returns
1118 InstallShortcutReceiver.enableInstallQueue();
1119
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001120 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001121 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001122 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001123 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001124
1125 // We call onHide() aggressively. The custom content callbacks should be able to
1126 // debounce excess onHide calls.
1127 if (mWorkspace.getCustomContentCallbacks() != null) {
1128 mWorkspace.getCustomContentCallbacks().onHide();
1129 }
Romain Guycbb89e42009-06-08 15:52:54 -07001130
Adam Cohen9211d422014-10-07 18:14:53 -07001131 if (mLauncherCallbacks != null) {
1132 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001133 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001134 }
1135
1136 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001137 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1138 // by a onResume or by scrolling otherwise.
1139 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001140
1141 // Custom content is completely hidden
1142 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001143
1144 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1145 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001146
1147 // Indicates whether the user is allowed to scroll away from the custom content.
1148 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001149 }
1150
Adam Cohenc2d6e892014-10-16 09:49:24 -07001151 public interface LauncherOverlay {
1152
1153 /**
1154 * Touch interaction leading to overscroll has begun
1155 */
1156 public void onScrollInteractionBegin();
1157
1158 /**
1159 * Touch interaction related to overscroll has ended
1160 */
1161 public void onScrollInteractionEnd();
1162
1163 /**
1164 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1165 * screen (or in the case of RTL, the rightmost screen).
1166 */
1167 public void onScrollChange(int progress, boolean rtl);
1168
1169 /**
1170 * Screen has stopped scrolling
1171 */
1172 public void onScrollSettled();
1173
1174 /**
1175 * This method can be called by the Launcher in order to force the LauncherOverlay
1176 * to exit fully immersive mode.
1177 */
1178 public void forceExitFullImmersion();
1179 }
1180
Winson Chung0f785722015-04-08 10:27:49 -07001181 public interface LauncherAppsCallbacks {
1182 /**
1183 * Updates launcher to the available space that AllApps can take so as not to overlap with
1184 * any other views.
1185 */
1186 public void onAllAppsBoundsChanged(Rect bounds);
1187
1188 /**
1189 * Called to dismiss all apps if it is showing.
1190 */
1191 public void dismissAllApps();
1192 }
1193
Adam Cohenc2d6e892014-10-16 09:49:24 -07001194 public interface LauncherOverlayCallbacks {
1195 /**
1196 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1197 * however it doesn't modify any state within the launcher.
1198 */
1199 public boolean canEnterFullImmersion();
1200
1201 /**
1202 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1203 * eg. by occupying the full screen and handling all touch events.
1204 *
1205 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1206 * case, Launcher will modify any necessary state and assumes the overlay is
1207 * handling all interaction. If false, the LauncherOverlay should cancel any
1208 *
1209 */
1210 public boolean enterFullImmersion();
1211
1212 /**
1213 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1214 * full control over UI and state.
1215 */
1216 public void exitFullImmersion();
1217 }
1218
1219 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1220
1221 @Override
1222 public boolean canEnterFullImmersion() {
1223 return mState == State.WORKSPACE;
1224 }
1225
1226 @Override
1227 public boolean enterFullImmersion() {
1228 if (mState == State.WORKSPACE) {
1229 // When fully immersed, disregard any touches which fall through.
1230 mDragLayer.setBlockTouch(true);
1231 return true;
1232 }
1233 return false;
1234 }
1235
1236 @Override
1237 public void exitFullImmersion() {
1238 mDragLayer.setBlockTouch(false);
1239 }
1240 }
1241
Jorim Jaggid017f882014-01-14 17:08:48 -08001242 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001243 if (mLauncherCallbacks != null) {
1244 return mLauncherCallbacks.hasSettings();
1245 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001246 return false;
1247 }
1248
Adam Cohen9211d422014-10-07 18:14:53 -07001249 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001250 CustomContentCallbacks callbacks, String description) {
1251 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001252 }
1253
Adam Cohenedb40762013-07-18 16:45:45 -07001254 // The custom content needs to offset its content to account for the QSB
1255 public int getTopOffsetForCustomContent() {
1256 return mWorkspace.getPaddingTop();
1257 }
1258
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001259 @Override
1260 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001261 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001262 if (mModel.isCurrentCallbacks(this)) {
1263 mModel.stopLoader();
1264 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001265 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1266
Romain Guy13c2e7b2010-03-10 19:45:00 -08001267 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001268 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001269
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001270 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001271 @Override
1272 public void onWindowFocusChanged(boolean hasFocus) {
1273 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001274 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001275
1276 if (mLauncherCallbacks != null) {
1277 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1278 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001279 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 private boolean acceptFilter() {
1282 final InputMethodManager inputManager = (InputMethodManager)
1283 getSystemService(Context.INPUT_METHOD_SERVICE);
1284 return !inputManager.isFullscreenMode();
1285 }
1286
1287 @Override
1288 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001289 final int uniChar = event.getUnicodeChar();
1290 final boolean handled = super.onKeyDown(keyCode, event);
1291 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1292 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1294 keyCode, event);
1295 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001296 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001297 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001298 // showSearchDialog()
1299 // If there are multiple keystrokes before the search dialog takes focus,
1300 // onSearchRequested() will be called for every keystroke,
1301 // but it is idempotent, so it's fine.
1302 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 }
1304 }
1305
Joe Onorato8a9625e2010-01-28 15:55:35 -08001306 // Eat the long press event so the keyboard doesn't come up.
1307 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1308 return true;
1309 }
1310
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 return handled;
1312 }
1313
Karl Rosaen138a0412009-04-23 19:00:21 -07001314 private String getTypedText() {
1315 return mDefaultKeySsb.toString();
1316 }
1317
1318 private void clearTypedText() {
1319 mDefaultKeySsb.clear();
1320 mDefaultKeySsb.clearSpans();
1321 Selection.setSelection(mDefaultKeySsb, 0);
1322 }
1323
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001325 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1326 * State
1327 */
1328 private static State intToState(int stateOrdinal) {
1329 State state = State.WORKSPACE;
1330 final State[] stateValues = State.values();
1331 for (int i = 0; i < stateValues.length; i++) {
1332 if (stateValues[i].ordinal() == stateOrdinal) {
1333 state = stateValues[i];
1334 break;
1335 }
1336 }
1337 return state;
1338 }
1339
1340 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 * Restores the previous state, if it exists.
1342 *
1343 * @param savedState The previous state.
1344 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001345 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 private void restoreState(Bundle savedState) {
1347 if (savedState == null) {
1348 return;
1349 }
1350
Michael Jurka883f55b2010-10-21 15:47:14 -07001351 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001352 if (state == State.APPS || state == State.WIDGETS) {
1353 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001354 }
1355
Adam Cohen21cd0022013-10-09 18:57:02 -07001356 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1357 PagedView.INVALID_RESTORE_PAGE);
1358 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001359 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361
Winson Chung3d503fb2011-07-13 17:25:49 -07001362 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001363 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001364
Winson Chung3d503fb2011-07-13 17:25:49 -07001365 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1366 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001367 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001368 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1369 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001370 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1371 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001372 AppWidgetProviderInfo info = savedState.getParcelable(
1373 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001374 mPendingAddWidgetInfo = info == null ?
1375 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1376
Adam Cohen4637b5a2013-11-04 18:21:24 -08001377 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001378 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 mRestoring = true;
1380 }
1381
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001382 mItemIdToViewId = (HashMap<Integer, Integer>)
1383 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
1385
1386 /**
1387 * Finds all the views we need and configure them properly.
1388 */
1389 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001390 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001391
Craig Mautner360310b2012-10-26 15:13:08 -07001392 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001393 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001394 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1395 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001396 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001397 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001398
John Spurlock77e1f472013-09-11 10:09:51 -04001399 mLauncherView.setSystemUiVisibility(
1400 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001401 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402
Winson Chung4c98d922011-05-31 16:50:48 -07001403 // Setup the drag layer
1404 mDragLayer.setup(this, dragController);
1405
Winson Chung3d503fb2011-07-13 17:25:49 -07001406 // Setup the hotseat
1407 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1408 if (mHotseat != null) {
1409 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001410 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001411 }
1412
Jorim Jaggid017f882014-01-14 17:08:48 -08001413 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001414 View widgetButton = findViewById(R.id.widget_button);
1415 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001416 @Override
1417 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001418 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001419 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001420 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001421 }
1422 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001423 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1424
1425 View wallpaperButton = findViewById(R.id.wallpaper_button);
1426 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001427 @Override
1428 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001429 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001430 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001431 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001432 }
1433 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001434 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1435
1436 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001437 if (hasSettings()) {
1438 settingsButton.setOnClickListener(new OnClickListener() {
1439 @Override
1440 public void onClick(View arg0) {
1441 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001442 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001443 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001444 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001445 });
1446 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1447 } else {
1448 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001449 }
1450
Adam Cohendbdff6b2013-09-18 19:09:15 -07001451 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001452
Winson Chung4c98d922011-05-31 16:50:48 -07001453 // Setup the workspace
1454 mWorkspace.setHapticFeedbackEnabled(false);
1455 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001456 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001457 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001458
Winson Chungf0ea4d32011-06-06 14:27:16 -07001459 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001460 mSearchDropTargetBar = (SearchDropTargetBar)
1461 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001462
Winson Chungb745afb2015-03-02 11:51:23 -08001463 // Setup Apps
1464 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001465 if (isAllAppsSearchOverridden()) {
1466 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001467 }
Winson Chungb745afb2015-03-02 11:51:23 -08001468
Winson Chungf0ea4d32011-06-06 14:27:16 -07001469 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001470 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001471
Winson Chung3d503fb2011-07-13 17:25:49 -07001472 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001473 dragController.setDragScoller(mWorkspace);
1474 dragController.setScrollView(mDragLayer);
1475 dragController.setMoveTarget(mWorkspace);
1476 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001477 if (mSearchDropTargetBar != null) {
1478 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001479 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001480 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001481
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001482 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001483 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001484 mWeightWatcher = new WeightWatcher(this);
1485 mWeightWatcher.setAlpha(0.5f);
1486 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001487 new FrameLayout.LayoutParams(
1488 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001489 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001490 Gravity.BOTTOM)
1491 );
Adam Cohen39a06042013-07-19 14:30:12 -07001492
1493 boolean show = shouldShowWeightWatcher();
1494 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 }
1497
1498 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001499 * Sets the all apps button. This method is called from {@link Hotseat}.
1500 */
1501 public void setAllAppsButton(View allAppsButton) {
1502 mAllAppsButton = allAppsButton;
1503 }
1504
1505 public View getAllAppsButton() {
1506 return mAllAppsButton;
1507 }
1508
1509 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 * Creates a view representing a shortcut.
1511 *
1512 * @param info The data structure describing the shortcut.
1513 *
1514 * @return A View inflated from R.layout.application.
1515 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001516 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001517 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 }
1519
1520 /**
1521 * Creates a view representing a shortcut inflated from the specified resource.
1522 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 * @param parent The group the shortcut belongs to.
1524 * @param info The data structure describing the shortcut.
1525 *
1526 * @return A View inflated from layoutResId.
1527 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001528 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
1529 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.application,
1530 parent, false);
1531 favorite.applyFromShortcutInfo(info, mIconCache);
1532 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001534 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 return favorite;
1536 }
1537
1538 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 * Add a shortcut to the workspace.
1540 *
1541 * @param data The intent describing the shortcut.
1542 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001545 int cellY) {
1546 int[] cellXY = mTmpAddItemCellCoordinates;
1547 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001549
Michael Jurkad3ef3062010-11-23 16:23:58 -08001550 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001551
Sunny Goyal2350bc92014-10-14 16:42:54 -07001552 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001553 if (info == null) {
1554 return;
1555 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001556 final View view = createShortcut(info);
1557
Adam Cohenfbba09b2011-07-18 21:43:05 -07001558 // First we check if we already know the exact location where we want to add this item.
1559 if (cellX >= 0 && cellY >= 0) {
1560 cellXY[0] = cellX;
1561 cellXY[1] = cellY;
1562 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001563
1564 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001565 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001566 true, null,null)) {
1567 return;
1568 }
1569 DragObject dragObject = new DragObject();
1570 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001571 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1572 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001573 return;
1574 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001575 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001576 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001577 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001578 foundCellSpan = (result != null);
1579 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001580 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001581 }
1582
1583 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001584 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001585 return;
1586 }
1587
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001588 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589
1590 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001591 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001592 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
1594 }
1595
Adam Cohen2f093b62012-04-30 18:59:53 -07001596 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1597 int minHeight) {
1598 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001599 // We want to account for the extra amount of padding that we are adding to the widget
1600 // to ensure that it gets the full amount of space that it has requested
1601 int requiredWidth = minWidth + padding.left + padding.right;
1602 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001603 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001604 }
1605
Adam Cohen2f093b62012-04-30 18:59:53 -07001606 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1607 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001608 }
1609
Adam Cohen2f093b62012-04-30 18:59:53 -07001610 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1611 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001612 }
1613
Adam Cohen2f093b62012-04-30 18:59:53 -07001614 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1615 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001616 }
1617
Adam Cohen2f093b62012-04-30 18:59:53 -07001618 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1619 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001620 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001621 }
1622
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001624 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001626 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 */
Adam Cohen091440a2015-03-18 14:16:05 -07001628 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001629 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1630
1631 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001632 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001633 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1634 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001635 }
Romain Guycbb89e42009-06-08 15:52:54 -07001636
Adam Cohen59400422014-03-05 18:07:04 -08001637 if (appWidgetInfo.isCustomWidget) {
1638 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001639 }
1640
Adam Cohen59400422014-03-05 18:07:04 -08001641 LauncherAppWidgetInfo launcherInfo;
1642 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1643 launcherInfo.spanX = info.spanX;
1644 launcherInfo.spanY = info.spanY;
1645 launcherInfo.minSpanX = info.minSpanX;
1646 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001647 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001650 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651
1652 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001653 if (hostView == null) {
1654 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001655 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1656 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001657 } else {
1658 // The AppWidgetHostView has already been inflated and instantiated
1659 launcherInfo.hostView = hostView;
1660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001662 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001663 launcherInfo.notifyWidgetSizeChanged(this);
1664
Adam Cohen59400422014-03-05 18:07:04 -08001665 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1666 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001667
1668 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001670 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 }
Romain Guycbb89e42009-06-08 15:52:54 -07001672
Adam Cohended9f8d2010-11-03 13:25:16 -07001673 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1674 @Override
1675 public void onReceive(Context context, Intent intent) {
1676 final String action = intent.getAction();
1677 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1678 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001679 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001680 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001681
Winson Chungc100e8e2011-08-09 16:02:43 -07001682 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001683 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001684 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001685 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001686 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001687 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001688 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1689 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001690 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001691 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1692 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001693 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001694 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1695 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1696 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001697 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001698 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1699 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001700 }
1701 }
1702 };
1703
1704 @Override
1705 public void onAttachedToWindow() {
1706 super.onAttachedToWindow();
1707
1708 // Listen for broadcasts related to user-presence
1709 final IntentFilter filter = new IntentFilter();
1710 filter.addAction(Intent.ACTION_SCREEN_OFF);
1711 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001712 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001713 if (ENABLE_DEBUG_INTENTS) {
1714 filter.addAction(DebugIntents.DELETE_DATABASE);
1715 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1716 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001718 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001719 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001720 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 mVisible = true;
1722 }
1723
Adam Cohen0b395352014-06-09 22:54:36 +00001724 /**
1725 * Sets up transparent navigation and status bars in LMP.
1726 * This method is a no-op for other platform versions.
1727 */
Sunny Goyald0091012015-01-20 15:55:34 -08001728 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001729 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001730 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001731 Window window = getWindow();
1732 window.getAttributes().systemUiVisibility |=
1733 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1734 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1735 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1736 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1737 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1738 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1739 window.setStatusBarColor(Color.TRANSPARENT);
1740 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001741 }
1742 }
1743
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 @Override
1745 public void onDetachedFromWindow() {
1746 super.onDetachedFromWindow();
1747 mVisible = false;
1748
Adam Cohend113e0c2010-11-11 10:48:05 -08001749 if (mAttached) {
1750 unregisterReceiver(mReceiver);
1751 mAttached = false;
1752 }
Winson Chungb745afb2015-03-02 11:51:23 -08001753 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 }
1755
1756 public void onWindowVisibilityChanged(int visibility) {
1757 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001758 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001759 // The following code used to be in onResume, but it turns out onResume is called when
1760 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1761 // is a more appropriate event to handle
1762 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001763 if (!mWorkspaceLoading) {
1764 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001765 // We want to let Launcher draw itself at least once before we force it to build
1766 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001767 // apps is nice and speedy.
1768 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001769 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001770 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001771 if (mStarted) return;
1772 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001773 // We delay the layer building a bit in order to give
1774 // other message processing a time to run. In particular
1775 // this avoids a delay in hiding the IME if it was
1776 // currently shown, because doing that may involve
1777 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001778 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001779 final ViewTreeObserver.OnDrawListener listener = this;
1780 mWorkspace.post(new Runnable() {
1781 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001782 if (mWorkspace != null &&
1783 mWorkspace.getViewTreeObserver() != null) {
1784 mWorkspace.getViewTreeObserver().
1785 removeOnDrawListener(listener);
1786 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001787 }
1788 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001789 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001790 }
1791 });
1792 }
1793 clearTypedText();
1794 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001795 }
1796
Adam Cohen091440a2015-03-18 14:16:05 -07001797 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001798 mHandler.removeMessages(ADVANCE_MSG);
1799 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1800 mHandler.sendMessageDelayed(msg, delay);
1801 mAutoAdvanceSentTime = System.currentTimeMillis();
1802 }
1803
Adam Cohen091440a2015-03-18 14:16:05 -07001804 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001805 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1806 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1807 mAutoAdvanceRunning = autoAdvanceRunning;
1808 if (autoAdvanceRunning) {
1809 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1810 sendAdvanceMessage(delay);
1811 } else {
1812 if (!mWidgetsToAdvance.isEmpty()) {
1813 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1814 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1815 }
1816 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001817 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001818 }
1819 }
1820 }
1821
1822 private final Handler mHandler = new Handler() {
1823 @Override
1824 public void handleMessage(Message msg) {
1825 if (msg.what == ADVANCE_MSG) {
1826 int i = 0;
1827 for (View key: mWidgetsToAdvance.keySet()) {
1828 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1829 final int delay = mAdvanceStagger * i;
1830 if (v instanceof Advanceable) {
1831 postDelayed(new Runnable() {
1832 public void run() {
1833 ((Advanceable) v).advance();
1834 }
1835 }, delay);
1836 }
1837 i++;
1838 }
1839 sendAdvanceMessage(mAdvanceInterval);
1840 }
1841 }
1842 };
1843
1844 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001845 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001846 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1847 if (v instanceof Advanceable) {
1848 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001849 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001850 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001851 }
1852 }
1853
1854 void removeWidgetToAutoAdvance(View hostView) {
1855 if (mWidgetsToAdvance.containsKey(hostView)) {
1856 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001857 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001858 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001859 }
1860
Joe Onorato9c1289c2009-08-17 11:03:03 -04001861 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001862 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001863 launcherInfo.hostView = null;
1864 }
1865
Hyunyoung Song3f471442015-04-08 19:01:34 -07001866 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001867 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1868 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001869 }
1870
Winson Chunga6945242014-01-08 14:04:34 -08001871 public DragLayer getDragLayer() {
1872 return mDragLayer;
1873 }
1874
Winson Chungb745afb2015-03-02 11:51:23 -08001875 public AppsContainerView getAppsView() {
1876 return mAppsView;
1877 }
1878
Hyunyoung Song3f471442015-04-08 19:01:34 -07001879 public WidgetsContainerView getWidgetsView() {
1880 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001881 }
1882
Winson Chunga6945242014-01-08 14:04:34 -08001883 public Workspace getWorkspace() {
1884 return mWorkspace;
1885 }
1886
1887 public Hotseat getHotseat() {
1888 return mHotseat;
1889 }
1890
Jorim Jaggid017f882014-01-14 17:08:48 -08001891 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001892 return mOverviewPanel;
1893 }
1894
1895 public SearchDropTargetBar getSearchBar() {
1896 return mSearchDropTargetBar;
1897 }
1898
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001899 public LauncherAppWidgetHost getAppWidgetHost() {
1900 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 }
Romain Guycbb89e42009-06-08 15:52:54 -07001902
Winson Chunga9abd0e2010-10-27 17:18:37 -07001903 public LauncherModel getModel() {
1904 return mModel;
1905 }
1906
Winson Chunga6945242014-01-08 14:04:34 -08001907 protected SharedPreferences getSharedPrefs() {
1908 return mSharedPrefs;
1909 }
1910
Bjorn Bringertc459e522013-06-07 19:36:01 +01001911 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001912 getWindow().closeAllPanels();
1913
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001914 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001915 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001916 }
1917
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 @Override
1919 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001920 long startTime = 0;
1921 if (DEBUG_RESUME_TIME) {
1922 startTime = System.currentTimeMillis();
1923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 super.onNewIntent(intent);
1925
1926 // Close the menu
1927 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001928 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001929 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001930
Adam Cohened307df2013-10-02 09:37:31 -07001931 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1932 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1933 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001934
Adam Cohen6fecd412013-10-02 17:41:50 -07001935 if (mWorkspace == null) {
1936 // Can be cases where mWorkspace is null, this prevents a NPE
1937 return;
1938 }
1939 Folder openFolder = mWorkspace.getOpenFolder();
1940 // In all these cases, only animate if we're already on home
1941 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001942
1943 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1944 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001945 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001946 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001947 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001948 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001949
Adam Cohen6fecd412013-10-02 17:41:50 -07001950 closeFolder();
1951 exitSpringLoadedDragMode();
1952
1953 // If we are already on home, then just animate back to the workspace,
1954 // otherwise, just wait until onResume to set the state back to Workspace
1955 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001956 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001957 } else {
1958 mOnResumeState = State.WORKSPACE;
1959 }
1960
1961 final View v = getWindow().peekDecorView();
1962 if (v != null && v.getWindowToken() != null) {
1963 InputMethodManager imm = (InputMethodManager)getSystemService(
1964 INPUT_METHOD_SERVICE);
1965 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1966 }
1967
Winson Chungb745afb2015-03-02 11:51:23 -08001968 // Reset the apps view
1969 if (!alreadyOnHome && mAppsView != null) {
1970 mAppsView.scrollToTop();
1971 }
1972
Hyunyoung Song3f471442015-04-08 19:01:34 -07001973 // Reset the widgets view
1974 if (!alreadyOnHome && mWidgetsView != null) {
1975 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001976 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001977
Adam Cohen9211d422014-10-07 18:14:53 -07001978 if (mLauncherCallbacks != null) {
1979 mLauncherCallbacks.onHomeIntent();
1980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 }
Adam Cohened307df2013-10-02 09:37:31 -07001982
Michael Jurka447bf842013-05-15 14:52:15 +02001983 if (DEBUG_RESUME_TIME) {
1984 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1985 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986
Adam Cohen9211d422014-10-07 18:14:53 -07001987 if (mLauncherCallbacks != null) {
1988 mLauncherCallbacks.onNewIntent(intent);
1989 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001990 }
1991
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001993 public void onRestoreInstanceState(Bundle state) {
1994 super.onRestoreInstanceState(state);
1995 for (int page: mSynchronouslyBoundPages) {
1996 mWorkspace.restoreInstanceStateForChild(page);
1997 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 }
1999
2000 @Override
2001 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002002 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002003 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2004 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002005 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002006 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007
Michael Jurka883f55b2010-10-21 15:47:14 -07002008 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002009 // We close any open folder since it will not be re-opened, and we need to make sure
2010 // this state is reflected.
2011 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012
Adam Cohendcd297f2013-06-18 13:13:40 -07002013 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002014 mWaitingForResult) {
2015 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002016 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002017 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2018 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002019 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2020 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2021 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002022 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 }
2024
Hyunyoung Song3f471442015-04-08 19:01:34 -07002025 // Save the current widgets tray?
2026 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002027 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002028
2029 if (mLauncherCallbacks != null) {
2030 mLauncherCallbacks.onSaveInstanceState(outState);
2031 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
2033
2034 @Override
2035 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002037
Winson Chunge7a03942011-08-05 15:05:12 -07002038 // Remove all pending runnables
2039 mHandler.removeMessages(ADVANCE_MSG);
2040 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002041 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002042
Winson Chungcd2b0142011-06-08 16:02:26 -07002043 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002044 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002045
2046 // It's possible to receive onDestroy after a new Launcher activity has
2047 // been created. In this case, don't interfere with the new Launcher.
2048 if (mModel.isCurrentCallbacks(this)) {
2049 mModel.stopLoader();
2050 app.setLauncher(null);
2051 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002054 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002056 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002058 mAppWidgetHost = null;
2059
2060 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061
2062 TextKeyListener.getInstance().release();
2063
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002064 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002065 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002066
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002067 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002068 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002069 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002070 mWorkspace = null;
2071 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002072
Michael Jurka2ecf9952012-06-18 12:52:28 -07002073 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002074
2075 if (mLauncherCallbacks != null) {
2076 mLauncherCallbacks.onDestroy();
2077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 }
2079
Adam Cohena9cf38f2011-05-02 15:36:58 -07002080 public DragController getDragController() {
2081 return mDragController;
2082 }
2083
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 @Override
2085 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002086 onStartForResult(requestCode);
2087 super.startActivityForResult(intent, requestCode);
2088 }
2089
2090 @Override
2091 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2092 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2093 onStartForResult(requestCode);
2094 try {
2095 super.startIntentSenderForResult(intent, requestCode,
2096 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2097 } catch (IntentSender.SendIntentException e) {
2098 throw new ActivityNotFoundException();
2099 }
2100 }
2101
2102 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002103 if (requestCode >= 0) {
2104 setWaitingForResult(true);
2105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 }
2107
Winson Chung70d72102011-08-12 11:24:35 -07002108 /**
2109 * Indicates that we want global search for this activity by setting the globalSearch
2110 * argument for {@link #startSearch} to true.
2111 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002113 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002115
Karl Rosaen138a0412009-04-23 19:00:21 -07002116 if (initialQuery == null) {
2117 // Use any text typed in the launcher as the initial query
2118 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002119 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 if (appSearchData == null) {
2121 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002122 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 }
Winson Chungadf0c182012-08-23 14:59:07 -07002124 Rect sourceBounds = new Rect();
2125 if (mSearchDropTargetBar != null) {
2126 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2127 }
Romain Guycbb89e42009-06-08 15:52:54 -07002128
Ian Parkinson047036e2014-09-01 15:40:53 +01002129 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002130 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002131 if (clearTextImmediately) {
2132 clearTypedText();
2133 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002134
2135 // We need to show the workspace after starting the search
2136 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002137 }
2138
Ian Parkinson047036e2014-09-01 15:40:53 +01002139 /**
2140 * Start a text search.
2141 *
2142 * @return {@code true} if the search will start immediately, so any further keypresses
2143 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2144 * to buffer keypresses.
2145 */
2146 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002147 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002148 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2149 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2150 sourceBounds);
2151 }
2152
Michael Jurkaa33411c2012-06-14 16:18:21 -07002153 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002154 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002155 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002156 }
2157
2158 /**
2159 * Starts the global search activity. This code is a copied from SearchManager
2160 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002161 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002162 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002163 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002164 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2165 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2166 if (globalSearchActivity == null) {
2167 Log.w(TAG, "No global search activity found.");
2168 return;
2169 }
2170 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2171 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2172 intent.setComponent(globalSearchActivity);
2173 // Make sure that we have a Bundle to put source in
2174 if (appSearchData == null) {
2175 appSearchData = new Bundle();
2176 } else {
2177 appSearchData = new Bundle(appSearchData);
2178 }
Adam Cohen9211d422014-10-07 18:14:53 -07002179 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002180 if (!appSearchData.containsKey("source")) {
2181 appSearchData.putString("source", getPackageName());
2182 }
2183 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2184 if (!TextUtils.isEmpty(initialQuery)) {
2185 intent.putExtra(SearchManager.QUERY, initialQuery);
2186 }
2187 if (selectInitialQuery) {
2188 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2189 }
2190 intent.setSourceBounds(sourceBounds);
2191 try {
2192 startActivity(intent);
2193 } catch (ActivityNotFoundException ex) {
2194 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2195 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 }
2197
Yura4f93ec62014-02-04 14:15:21 +00002198 public boolean isOnCustomContent() {
2199 return mWorkspace.isOnOrMovingToCustomContent();
2200 }
2201
Winson Chung70d72102011-08-12 11:24:35 -07002202 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002203 public boolean onPrepareOptionsMenu(Menu menu) {
2204 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002205 if (!isOnCustomContent()) {
2206 // Close any open folders
2207 closeFolder();
2208 // Stop resizing any widgets
2209 mWorkspace.exitWidgetResizeMode();
2210 if (!mWorkspace.isInOverviewMode()) {
2211 // Show the overview mode
2212 showOverviewMode(true);
2213 } else {
2214 showWorkspace(true);
2215 }
Winson Chunge0298742014-01-17 12:03:00 -08002216 }
Adam Cohen9211d422014-10-07 18:14:53 -07002217 if (mLauncherCallbacks != null) {
2218 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2219 }
2220
Michael Jurkab94f3f82013-09-11 18:08:54 +02002221 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002224 @Override
2225 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002226 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002227 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002228 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002229 }
2230
Joe Onorato9c1289c2009-08-17 11:03:03 -04002231 public boolean isWorkspaceLocked() {
2232 return mWorkspaceLoading || mWaitingForResult;
2233 }
2234
Adam Cohen517a7f52014-03-01 12:12:59 -08002235 public boolean isWorkspaceLoading() {
2236 return mWorkspaceLoading;
2237 }
2238
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002239 private void setWorkspaceLoading(boolean value) {
2240 boolean isLocked = isWorkspaceLocked();
2241 mWorkspaceLoading = value;
2242 if (isLocked != isWorkspaceLocked()) {
2243 onWorkspaceLockedChanged();
2244 }
2245 }
2246
2247 private void setWaitingForResult(boolean value) {
2248 boolean isLocked = isWorkspaceLocked();
2249 mWaitingForResult = value;
2250 if (isLocked != isWorkspaceLocked()) {
2251 onWorkspaceLockedChanged();
2252 }
2253 }
2254
Adam Cohen9211d422014-10-07 18:14:53 -07002255 protected void onWorkspaceLockedChanged() {
2256 if (mLauncherCallbacks != null) {
2257 mLauncherCallbacks.onWorkspaceLockedChanged();
2258 }
2259 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002260
Michael Jurka0280c3b2010-09-17 15:00:07 -07002261 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002262 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002263 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002264 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2265 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002266 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002267 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002268 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002269
Sunny Goyale6b63a32015-01-16 16:14:29 -08002270 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002271 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002272 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2273 }
2274
Sunny Goyale6b63a32015-01-16 16:14:29 -08002275 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002276 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2277 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002278 if (appWidgetInfo.configure != null) {
2279 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002280 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002281
Bjorn Bringert7984c942009-12-09 15:38:25 +00002282 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002283 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2284 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2285
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002287 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002288 Runnable onComplete = new Runnable() {
2289 @Override
2290 public void run() {
2291 // Exit spring loaded mode if necessary after adding the widget
2292 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2293 null);
2294 }
2295 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002296 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002297 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002298 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 }
2300 }
Romain Guycbb89e42009-06-08 15:52:54 -07002301
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002302 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002303 // Close any folders that may be open.
2304 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002305 mWorkspace.moveToCustomContentScreen(animate);
2306 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002307
2308 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2309 int[] cell, int spanX, int spanY) {
2310 switch (info.itemType) {
2311 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2312 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2313 int span[] = new int[2];
2314 span[0] = spanX;
2315 span[1] = spanY;
2316 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2317 container, screenId, cell, span);
2318 break;
2319 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2320 processShortcutFromDrop(info.componentName, container, screenId, cell);
2321 break;
2322 default:
2323 throw new IllegalStateException("Unknown item type: " + info.itemType);
2324 }
2325 }
2326
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 /**
2328 * Process a shortcut drop.
2329 *
2330 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002332 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002333 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002334 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2335 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002336 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002338 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002339 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002340
2341 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002342 mPendingAddInfo.cellX = cell[0];
2343 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002344 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002345
2346 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2347 createShortcutIntent.setComponent(componentName);
2348 processShortcut(createShortcutIntent);
2349 }
2350
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351 /**
2352 * Process a widget drop.
2353 *
2354 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002355 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002356 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002357 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002358 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2359 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002360 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002361 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002362 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002363 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002364 mPendingAddInfo.minSpanX = info.minSpanX;
2365 mPendingAddInfo.minSpanY = info.minSpanY;
2366
Adam Cohenfbba09b2011-07-18 21:43:05 -07002367 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002368 mPendingAddInfo.cellX = cell[0];
2369 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002370 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002371 if (span != null) {
2372 mPendingAddInfo.spanX = span[0];
2373 mPendingAddInfo.spanY = span[1];
2374 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002375
Adam Cohened66b2b2012-01-23 17:28:51 -08002376 AppWidgetHostView hostView = info.boundWidget;
2377 int appWidgetId;
2378 if (hostView != null) {
2379 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002380 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002381 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002382 // In this case, we either need to start an activity to get permission to bind
2383 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002384 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002385 Bundle options = info.bindOptions;
2386
Sunny Goyalffe83f12014-08-14 17:39:34 -07002387 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2388 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002389 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002390 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002391 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002392 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002393 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2394 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2395 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002396 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2397 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002398 // TODO: we need to make sure that this accounts for the options bundle.
2399 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002400 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2401 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002402 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002403 }
2404
Joe Onoratodeb98af2010-02-19 14:59:39 -08002405 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002406 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 }
2408
Winson Chung24ab2f12010-09-16 14:10:47 -07002409 void processWallpaper(Intent intent) {
2410 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2411 }
2412
Adam Cohendcd297f2013-06-18 13:13:40 -07002413 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002414 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002415 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 folderInfo.title = getText(R.string.folder_name);
2417
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002419 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2420 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002421 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422
2423 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002424 FolderIcon newFolder =
2425 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002426 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002427 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002428 // Force measure the new folder icon
2429 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2430 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002431 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 }
Romain Guycbb89e42009-06-08 15:52:54 -07002433
Joe Onorato9c1289c2009-08-17 11:03:03 -04002434 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002435 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002436 }
2437
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002438 /**
2439 * Registers various content observers. The current implementation registers
2440 * only a favorites observer to keep track of the favorites applications.
2441 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002442 private void registerContentObservers() {
2443 ContentResolver resolver = getContentResolver();
2444 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2445 true, mWidgetObserver);
2446 }
2447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448 @Override
2449 public boolean dispatchKeyEvent(KeyEvent event) {
2450 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2451 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002453 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002454 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002455 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002456 dumpState();
2457 return true;
2458 }
2459 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002460 }
2461 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2462 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002463 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 return true;
2465 }
2466 }
2467
2468 return super.dispatchKeyEvent(event);
2469 }
2470
Joe Onorato88ec0992009-11-19 13:16:06 -08002471 @Override
2472 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002473 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2474 return;
2475 }
2476
Winson Chung3d9490a2015-03-24 17:38:42 -07002477 LauncherAccessibilityDelegate delegate =
2478 LauncherAppState.getInstance().getAccessibilityDelegate();
2479 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002480 return;
2481 }
2482
Winson Chungb745afb2015-03-02 11:51:23 -08002483 if (isAppsViewVisible()) {
2484 showWorkspace(true);
2485 } else if (isWidgetsViewVisible()) {
2486 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002487 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002488 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002489 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002490 Folder openFolder = mWorkspace.getOpenFolder();
2491 if (openFolder.isEditingName()) {
2492 openFolder.dismissEditingName();
2493 } else {
2494 closeFolder();
2495 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002496 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002497 mWorkspace.exitWidgetResizeMode();
2498
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002499 // Back button is a no-op here, but give at least some feedback for the button press
2500 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002501 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002502 }
2503
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002504 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002505 * Re-listen when widgets are reset.
2506 */
Adam Cohen091440a2015-03-18 14:16:05 -07002507 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002508 if (mAppWidgetHost != null) {
2509 mAppWidgetHost.startListening();
2510 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002511 }
2512
2513 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002514 * Launches the intent referred by the clicked shortcut.
2515 *
2516 * @param v The view representing the clicked shortcut.
2517 */
2518 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002519 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2520 // view has detached (it's possible for this to happen if the view is removed mid touch).
2521 if (v.getWindowToken() == null) {
2522 return;
2523 }
2524
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002525 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002526 return;
2527 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002528
Adam Cohen1697b792013-09-17 19:08:21 -07002529 if (v instanceof Workspace) {
2530 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002531 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002532 }
2533 return;
2534 }
2535
2536 if (v instanceof CellLayout) {
2537 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002538 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002539 }
2540 }
2541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002542 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002543 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002544 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002546 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002547 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002548 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002549 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002550 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002551 } else if (tag instanceof AppInfo) {
2552 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002553 } else if (tag instanceof LauncherAppWidgetInfo) {
2554 if (v instanceof PendingAppWidgetHostView) {
2555 onClickPendingWidget((PendingAppWidgetHostView) v);
2556 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 }
2558 }
2559
Sunny Goyal508da152014-08-14 10:53:27 -07002560 public void onClickPagedViewIcon(View v) {
2561 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002562 if (mLauncherCallbacks != null) {
2563 mLauncherCallbacks.onClickPagedViewIcon(v);
2564 }
Sunny Goyal508da152014-08-14 10:53:27 -07002565 }
2566
Sunny Goyal70660032015-05-14 00:07:08 -07002567 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002568 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002569 return false;
2570 }
2571
Michael Jurkaaf442092010-06-10 17:01:57 -07002572 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002573 * Event handler for the app widget view which has not fully restored.
2574 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002575 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002576 if (mIsSafeModeEnabled) {
2577 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2578 return;
2579 }
2580
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002581 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002582 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002583 int widgetId = info.appWidgetId;
2584 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2585 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002586 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2587 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002588 mPendingAddInfo.copyFrom(info);
2589 mPendingAddWidgetId = widgetId;
2590
Sunny Goyalffe83f12014-08-14 17:39:34 -07002591 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2592 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002593 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002594 } else if (info.installProgress < 0) {
2595 // The install has not been queued
2596 final String packageName = info.providerName.getPackageName();
2597 showBrokenAppInstallDialog(packageName,
2598 new DialogInterface.OnClickListener() {
2599 public void onClick(DialogInterface dialog, int id) {
2600 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2601 }
2602 });
2603 } else {
2604 // Download has started.
2605 final String packageName = info.providerName.getPackageName();
2606 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002607 }
2608 }
2609
2610 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002611 * Event handler for the "grid" button that appears on the home screen, which
2612 * enters all apps mode.
2613 *
2614 * @param v The view that was clicked.
2615 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002616 protected void onClickAllAppsButton(View v) {
2617 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002618 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 showWorkspace(true);
2620 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002621 // Try and refresh the set of predicted apps before we enter launcher
2622 showAppsView(true /* animated */, false /* resetListToTop */,
2623 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624 }
2625 }
2626
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002627 private void showBrokenAppInstallDialog(final String packageName,
2628 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002629 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002630 .setTitle(R.string.abandoned_promises_title)
2631 .setMessage(R.string.abandoned_promise_explanation)
2632 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2633 .setNeutralButton(R.string.abandoned_clean_this,
2634 new DialogInterface.OnClickListener() {
2635 public void onClick(DialogInterface dialog, int id) {
2636 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2637 mWorkspace.removeAbandonedPromise(packageName, user);
2638 }
2639 })
2640 .create().show();
2641 return;
2642 }
2643
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002644 /**
2645 * Event handler for an app shortcut click.
2646 *
2647 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2648 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002650 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2651 Object tag = v.getTag();
2652 if (!(tag instanceof ShortcutInfo)) {
2653 throw new IllegalArgumentException("Input must be a Shortcut");
2654 }
2655
2656 // Open shortcut
2657 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002658
2659 if (shortcut.isDisabled != 0) {
2660 int error = R.string.activity_not_available;
2661 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2662 error = R.string.safemode_shortcut_error;
2663 }
2664 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2665 return;
2666 }
2667
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002668 final Intent intent = shortcut.intent;
2669
2670 // Check for special shortcuts
2671 if (intent.getComponent() != null) {
2672 final String shortcutClass = intent.getComponent().getClassName();
2673
2674 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2675 MemoryDumpActivity.startDump(this);
2676 return;
2677 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2678 toggleShowWeightWatcher();
2679 return;
2680 }
2681 }
2682
Chris Wren40c5ed32014-06-24 18:24:23 -04002683 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002684 if ((v instanceof BubbleTextView)
2685 && shortcut.isPromise()
2686 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002687 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002688 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002689 new DialogInterface.OnClickListener() {
2690 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002691 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002692 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002693 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002694 return;
2695 }
2696
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002697 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002698 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002699
2700 if (mLauncherCallbacks != null) {
2701 mLauncherCallbacks.onClickAppShortcut(v);
2702 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002703 }
2704
Adam Cohen091440a2015-03-18 14:16:05 -07002705 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002706 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002707 final ShortcutInfo shortcut;
2708 final Intent intent;
2709 if (tag instanceof ShortcutInfo) {
2710 shortcut = (ShortcutInfo) tag;
2711 intent = shortcut.intent;
2712 int[] pos = new int[2];
2713 v.getLocationOnScreen(pos);
2714 intent.setSourceBounds(new Rect(pos[0], pos[1],
2715 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002716
Sunny Goyal508da152014-08-14 10:53:27 -07002717 } else if (tag instanceof AppInfo) {
2718 shortcut = null;
2719 intent = ((AppInfo) tag).intent;
2720 } else {
2721 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2722 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002723
2724 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002725 mStats.recordLaunch(intent, shortcut);
2726
2727 if (success && v instanceof BubbleTextView) {
2728 mWaitingForResume = (BubbleTextView) v;
2729 mWaitingForResume.setStayPressed(true);
2730 }
2731 }
2732
2733 /**
2734 * Event handler for a folder icon click.
2735 *
2736 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2737 */
2738 protected void onClickFolderIcon(View v) {
2739 if (LOGD) Log.d(TAG, "onClickFolder");
2740 if (!(v instanceof FolderIcon)){
2741 throw new IllegalArgumentException("Input must be a FolderIcon");
2742 }
2743
2744 FolderIcon folderIcon = (FolderIcon) v;
2745 final FolderInfo info = folderIcon.getFolderInfo();
2746 Folder openFolder = mWorkspace.getFolderForTag(info);
2747
2748 // If the folder info reports that the associated folder is open, then verify that
2749 // it is actually opened. There have been a few instances where this gets out of sync.
2750 if (info.opened && openFolder == null) {
2751 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2752 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2753 info.opened = false;
2754 }
2755
2756 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2757 // Close any open folder
2758 closeFolder();
2759 // Open the requested folder
2760 openFolder(folderIcon);
2761 } else {
2762 // Find the open folder...
2763 int folderScreen;
2764 if (openFolder != null) {
2765 folderScreen = mWorkspace.getPageForView(openFolder);
2766 // .. and close it
2767 closeFolder(openFolder);
2768 if (folderScreen != mWorkspace.getCurrentPage()) {
2769 // Close any folder open on the current screen
2770 closeFolder();
2771 // Pull the folder onto this screen
2772 openFolder(folderIcon);
2773 }
2774 }
2775 }
Adam Cohen9211d422014-10-07 18:14:53 -07002776
2777 if (mLauncherCallbacks != null) {
2778 mLauncherCallbacks.onClickFolderIcon(v);
2779 }
Michael Jurka5130e402011-10-13 04:55:35 -07002780 }
2781
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002782 /**
2783 * Event handler for the (Add) Widgets button that appears after a long press
2784 * on the home screen.
2785 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002786 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002787 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002788 if (mIsSafeModeEnabled) {
2789 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2790 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002791 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002792 if (mLauncherCallbacks != null) {
2793 mLauncherCallbacks.onClickAddWidgetButton(view);
2794 }
Adam Cohen9211d422014-10-07 18:14:53 -07002795 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002796 }
2797
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002798 /**
2799 * Event handler for the wallpaper picker button that appears after a long press
2800 * on the home screen.
2801 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002802 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002803 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002804 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2805 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002806
2807 if (mLauncherCallbacks != null) {
2808 mLauncherCallbacks.onClickWallpaperPicker(v);
2809 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002810 }
2811
2812 /**
2813 * Event handler for a click on the settings button that appears after a long press
2814 * on the home screen.
2815 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002816 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002817 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002818 if (mLauncherCallbacks != null) {
2819 mLauncherCallbacks.onClickSettingsButton(v);
2820 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002821 }
2822
Michael Jurka5130e402011-10-13 04:55:35 -07002823 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002824 // Provide the same haptic feedback that the system offers for virtual keys.
2825 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002826 }
2827
Adam Cohen61f560d2013-09-30 15:58:20 -07002828 public void performHapticFeedbackOnTouchDown(View v) {
2829 // Provide the same haptic feedback that the system offers for virtual keys.
2830 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2831 }
2832
2833 public View.OnTouchListener getHapticFeedbackTouchListener() {
2834 if (mHapticFeedbackTouchListener == null) {
2835 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002836 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002837 @Override
2838 public boolean onTouch(View v, MotionEvent event) {
2839 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2840 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2841 }
2842 return false;
2843 }
2844 };
2845 }
2846 return mHapticFeedbackTouchListener;
2847 }
2848
Adam Cohen9211d422014-10-07 18:14:53 -07002849 public void onDragStarted(View view) {
2850 if (isOnCustomContent()) {
2851 // Custom content screen doesn't participate in drag and drop. If on custom
2852 // content screen, move to default.
2853 moveWorkspaceToDefaultScreen();
2854 }
2855
2856 if (mLauncherCallbacks != null) {
2857 mLauncherCallbacks.onDragStarted(view);
2858 }
2859 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002860
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002861 /**
2862 * Called when the user stops interacting with the launcher.
2863 * This implies that the user is now on the homescreen and is not doing housekeeping.
2864 */
Adam Cohen9211d422014-10-07 18:14:53 -07002865 protected void onInteractionEnd() {
2866 if (mLauncherCallbacks != null) {
2867 mLauncherCallbacks.onInteractionEnd();
2868 }
2869 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002870
2871 /**
2872 * Called when the user starts interacting with the launcher.
2873 * The possible interactions are:
2874 * - open all apps
2875 * - reorder an app shortcut, or a widget
2876 * - open the overview mode.
2877 * This is a good time to stop doing things that only make sense
2878 * when the user is on the homescreen and not doing housekeeping.
2879 */
Adam Cohen9211d422014-10-07 18:14:53 -07002880 protected void onInteractionBegin() {
2881 if (mLauncherCallbacks != null) {
2882 mLauncherCallbacks.onInteractionBegin();
2883 }
2884 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002885
Winson Chungcd99cd32015-04-29 11:03:24 -07002886 /** Updates the interaction state. */
2887 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002888 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002889 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002890 boolean fromStateWithOverlay;
2891 boolean toStateWithOverlay;
2892 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2893 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2894 toStateWithOverlay = toState != Workspace.State.NORMAL;
2895 } else {
2896 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2897 fromState != Workspace.State.NORMAL_HIDDEN;
2898 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2899 toState != Workspace.State.NORMAL_HIDDEN;
2900 }
2901 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002902 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002903 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002904 onInteractionEnd();
2905 }
2906 }
2907
Kenny Guyf07af7b2014-07-31 11:39:16 +01002908 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002909 try {
2910 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002911 launcherApps.showAppDetailsForProfile(componentName, user);
2912 } catch (SecurityException e) {
2913 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2914 Log.e(TAG, "Launcher does not have permission to launch settings");
2915 } catch (ActivityNotFoundException e) {
2916 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2917 Log.e(TAG, "Unable to launch settings");
2918 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002919 }
2920
Michael Jurka1e2f4652013-07-08 18:03:46 -07002921 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002922 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2923 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002924 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002925 // System applications cannot be installed. For now, show a toast explaining that.
2926 // We may give them the option of disabling apps this way.
2927 int messageId = R.string.uninstall_system_app_text;
2928 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002929 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002930 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002931 String packageName = componentName.getPackageName();
2932 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002933 Intent intent = new Intent(
2934 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002935 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2936 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002937 if (user != null) {
2938 user.addToIntent(intent, Intent.EXTRA_USER);
2939 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002940 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002941 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002942 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002943 }
2944
Michael Jurka86a720e2012-05-09 11:23:23 -07002945 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002946 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002947 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002948 // Only launch using the new animation if the shortcut has not opted out (this is a
2949 // private contract between launcher and may be ignored in the future).
2950 boolean useLaunchAnimation = (v != null) &&
2951 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002952 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2953 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002954
Kenny Guy1317e2d2014-05-08 18:52:50 +01002955 UserHandleCompat user = null;
2956 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2957 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2958 user = userManager.getUserForSerialNumber(serialNumber);
2959 }
2960
2961 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002962 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002963 ActivityOptions opts = null;
2964 if (sClipRevealMethod != null) {
2965 // TODO: call method directly when Launcher3 can depend on M APIs
2966 int left = 0, top = 0;
2967 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2968 if (v instanceof TextView) {
2969 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002970 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2971 if (icon != null) {
2972 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002973 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002974 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002975 width = bounds.width();
2976 height = bounds.height();
2977 }
2978 }
2979 try {
2980 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2981 left, top, width, height);
2982 } catch (IllegalAccessException e) {
2983 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2984 sClipRevealMethod = null;
2985 } catch (InvocationTargetException e) {
2986 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2987 sClipRevealMethod = null;
2988 }
2989 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002990 if (opts == null && !Utilities.isLmpOrAbove()) {
2991 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002992 v.getMeasuredWidth(), v.getMeasuredHeight());
2993 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002994 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002995 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002996
2997 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2998 // Could be launching some bookkeeping activity
2999 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003000 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003001 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003002 launcherApps.startActivityForProfile(intent.getComponent(), user,
3003 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003004 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003005 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 } catch (SecurityException e) {
3007 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003008 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003010 "or use the exported attribute for this activity. "
3011 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003012 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003013 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003014 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003015
Michael Jurka86a720e2012-05-09 11:23:23 -07003016 boolean startActivitySafely(View v, Intent intent, Object tag) {
3017 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003018 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3019 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3020 return false;
3021 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003022 try {
3023 success = startActivity(v, intent, tag);
3024 } catch (ActivityNotFoundException e) {
3025 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3026 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3027 }
3028 return success;
3029 }
3030
Adam Cohen268c4752012-06-06 17:47:33 -07003031 /**
3032 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3033 * in the DragLayer in the exact absolute location of the original FolderIcon.
3034 */
3035 private void copyFolderIconToImage(FolderIcon fi) {
3036 final int width = fi.getMeasuredWidth();
3037 final int height = fi.getMeasuredHeight();
3038
3039 // Lazy load ImageView, Bitmap and Canvas
3040 if (mFolderIconImageView == null) {
3041 mFolderIconImageView = new ImageView(this);
3042 }
3043 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3044 mFolderIconBitmap.getHeight() != height) {
3045 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3046 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3047 }
3048
3049 DragLayer.LayoutParams lp;
3050 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3051 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3052 } else {
3053 lp = new DragLayer.LayoutParams(width, height);
3054 }
3055
Adam Cohen307fe232012-08-16 17:55:58 -07003056 // The layout from which the folder is being opened may be scaled, adjust the starting
3057 // view size by this scale factor.
3058 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003059 lp.customPosition = true;
3060 lp.x = mRectForFolderAnimation.left;
3061 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003062 lp.width = (int) (scale * width);
3063 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003064
3065 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3066 fi.draw(mFolderIconCanvas);
3067 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003068 if (fi.getFolder() != null) {
3069 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3070 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003071 }
Adam Cohen268c4752012-06-06 17:47:33 -07003072 // Just in case this image view is still in the drag layer from a previous animation,
3073 // we remove it and re-add it.
3074 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3075 mDragLayer.removeView(mFolderIconImageView);
3076 }
3077 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003078 if (fi.getFolder() != null) {
3079 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003080 }
Adam Cohen268c4752012-06-06 17:47:33 -07003081 }
3082
Adam Cohen2801caf2011-05-13 20:57:39 -07003083 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003084 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003085 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3086 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3087 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3088
Adam Cohenc51934b2011-07-26 21:07:43 -07003089 FolderInfo info = (FolderInfo) fi.getTag();
3090 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3091 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003092 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3093 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003094 }
3095
Adam Cohen268c4752012-06-06 17:47:33 -07003096 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3097 copyFolderIconToImage(fi);
3098 fi.setVisibility(View.INVISIBLE);
3099
Michael Jurka2ecf9952012-06-18 12:52:28 -07003100 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003101 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003102 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003103 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3104 }
3105 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003106 oa.start();
3107 }
3108
Adam Cohen268c4752012-06-06 17:47:33 -07003109 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003110 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003111 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3112 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3113 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3114
Adam Cohen268c4752012-06-06 17:47:33 -07003115 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003116
Adam Cohen268c4752012-06-06 17:47:33 -07003117 // We remove and re-draw the FolderIcon in-case it has changed
3118 mDragLayer.removeView(mFolderIconImageView);
3119 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003120 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003121 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003122 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003123 oa.addListener(new AnimatorListenerAdapter() {
3124 @Override
3125 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003126 if (cl != null) {
3127 cl.clearFolderLeaveBehind();
3128 // Remove the ImageView copy of the FolderIcon and make the original visible.
3129 mDragLayer.removeView(mFolderIconImageView);
3130 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003131 }
3132 }
3133 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003134 oa.start();
3135 }
3136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003138 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003139 * is animated relative to the specified View. If the View is null, no animation
3140 * is played.
3141 *
3142 * @param folderInfo The FolderInfo describing the folder to open.
3143 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003144 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003145 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003146 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3147 if (openFolder != null && openFolder != folder) {
3148 // Close any open folder before opening a folder.
3149 closeFolder();
3150 }
3151
Adam Cohena9cf38f2011-05-02 15:36:58 -07003152 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153
Adam Cohena9cf38f2011-05-02 15:36:58 -07003154 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155
Sunny Goyalf4066152015-04-15 09:42:19 -07003156 // While the folder is open, the position of the icon cannot change.
3157 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3158
Adam Cohen4554ee12011-08-03 16:13:21 -07003159 // Just verify that the folder hasn't already been added to the DragLayer.
3160 // There was a one-off crash where the folder had a parent already.
3161 if (folder.getParent() == null) {
3162 mDragLayer.addView(folder);
3163 mDragController.addDropTarget((DropTarget) folder);
3164 } else {
3165 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3166 folder.getParent() + ").");
3167 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003168 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003169 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003170
3171 // Notify the accessibility manager that this folder "window" has appeared and occluded
3172 // the workspace items
3173 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3174 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003175 }
3176
3177 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003178 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003179 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003180 if (folder.isEditingName()) {
3181 folder.dismissEditingName();
3182 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003183 closeFolder(folder);
3184 }
3185 }
3186
3187 void closeFolder(Folder folder) {
3188 folder.getInfo().opened = false;
3189
3190 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3191 if (parent != null) {
3192 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3193 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003194 if (fi != null) {
3195 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3196 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003197 }
3198 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003199
3200 // Notify the accessibility manager that this folder "window" has disappeard and no
3201 // longer occludeds the workspace items
3202 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203 }
3204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003205 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003206 if (!isDraggingEnabled()) return false;
3207 if (isWorkspaceLocked()) return false;
3208 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003209
Adam Cohen1697b792013-09-17 19:08:21 -07003210 if (v instanceof Workspace) {
3211 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003212 if (!mWorkspace.isTouchActive()) {
3213 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003214 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3215 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3216 return true;
3217 } else {
3218 return false;
3219 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003220 } else {
3221 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003222 }
Adam Cohen1697b792013-09-17 19:08:21 -07003223 }
3224
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003225 CellLayout.CellInfo longClickCellInfo = null;
3226 View itemUnderLongClick = null;
3227 if (v.getTag() instanceof ItemInfo) {
3228 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003229 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003230 itemUnderLongClick = longClickCellInfo.cell;
3231 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003232 }
3233
Winson Chung3d503fb2011-07-13 17:25:49 -07003234 // The hotseat touch handling does not go through Workspace, and we always allow long press
3235 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003236 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003237 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003238 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003239 // User long pressed on empty space
3240 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3241 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003242 if (mWorkspace.isInOverviewMode()) {
3243 mWorkspace.startReordering(v);
3244 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003245 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003246 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003247 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003248 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3249 mHotseat.getOrderInHotseat(
3250 longClickCellInfo.cellX,
3251 longClickCellInfo.cellY));
3252 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003253 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003254 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003255 }
3256 }
3257 }
3258 return true;
3259 }
3260
Winson Chung3d503fb2011-07-13 17:25:49 -07003261 boolean isHotseatLayout(View layout) {
3262 return mHotseat != null && layout != null &&
3263 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3264 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003265
Winson Chung3d503fb2011-07-13 17:25:49 -07003266 /**
3267 * Returns the CellLayout of the specified container at the specified screen.
3268 */
Sunny Goyal92820592015-03-02 11:31:35 -08003269 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003270 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3271 if (mHotseat != null) {
3272 return mHotseat.getLayout();
3273 } else {
3274 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003275 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003276 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003277 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003278 }
3279 }
3280
Winson Chungb745afb2015-03-02 11:51:23 -08003281 /**
3282 * For overridden classes.
3283 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003284 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003285 return isAppsViewVisible();
3286 }
3287
3288 public boolean isAppsViewVisible() {
3289 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3290 }
3291
3292 public boolean isWidgetsViewVisible() {
3293 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003294 }
3295
Craig Mautner360310b2012-10-26 15:13:08 -07003296 private void setWorkspaceBackground(boolean workspace) {
3297 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003298 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003299 }
3300
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003301 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003302 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3303 int curflags = getWindow().getAttributes().flags
3304 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3305 if (wpflags != curflags) {
3306 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3307 }
Craig Mautner360310b2012-10-26 15:13:08 -07003308 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003309 }
3310
Michael Jurkae326f182011-11-21 14:05:46 -08003311 @Override
3312 public void onTrimMemory(int level) {
3313 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003314 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3315 // The widget preview db can result in holding onto over
3316 // 3MB of memory for caching which isn't necessary.
3317 SQLiteDatabase.releaseMemory();
3318
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003319 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003320 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003321 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003322 if (mLauncherCallbacks != null) {
3323 mLauncherCallbacks.onTrimMemory(level);
3324 }
Michael Jurkae326f182011-11-21 14:05:46 -08003325 }
3326
Winson Chungb745afb2015-03-02 11:51:23 -08003327 @Override
3328 public void onStateTransitionHideSearchBar() {
3329 // Hide the search bar
3330 if (mSearchDropTargetBar != null) {
3331 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3332 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003333 }
3334
Winson Chungb745afb2015-03-02 11:51:23 -08003335 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003336 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3337 true);
Adam Cohened307df2013-10-02 09:37:31 -07003338 }
3339
Winson Chungdc61c4d2015-04-20 18:26:57 -07003340 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3341 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3342 onCompleteRunnable, true);
3343 }
3344
3345 protected void showWorkspace(int snapToPage, boolean animated) {
3346 showWorkspace(snapToPage, animated, null, true);
3347 }
3348
3349 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3350 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003351 boolean changed = mState != State.WORKSPACE ||
3352 mWorkspace.getState() != Workspace.State.NORMAL;
3353 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003354 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003355 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003356 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003357 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003358
Winson Chungc7d2b602012-05-16 17:02:20 -07003359 // Show the search bar (only animate if we were showing the drop target bar in spring
3360 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003361 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003362 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003363 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003364
Michael Jurkab3e22d92011-10-31 15:58:33 -07003365 // Set focus to the AppsCustomize button
3366 if (mAllAppsButton != null) {
3367 mAllAppsButton.requestFocus();
3368 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003369 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003370
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003371 // Change the state *after* we've called all the transition code
3372 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003373
Winson Chung5fb63472011-02-02 17:03:37 -08003374 // Resume the auto-advance of widgets
3375 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003376 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003377
Winson Chung0f785722015-04-08 10:27:49 -07003378 if (changed) {
3379 // Send an accessibility event to announce the context change
3380 getWindow().getDecorView()
3381 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003382 if (notifyLauncherCallbacks) {
3383 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003384 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003385 mLauncherCallbacks.onAllAppsHidden();
3386 }
3387 }
Winson Chung0f785722015-04-08 10:27:49 -07003388 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003389 }
3390
Adam Cohened307df2013-10-02 09:37:31 -07003391 void showOverviewMode(boolean animated) {
3392 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003393 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003394 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3395 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003396 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003397 }
3398
Winson Chungb745afb2015-03-02 11:51:23 -08003399 /**
3400 * Shows the apps view.
3401 */
Winson Chung4ac30062015-05-08 17:34:17 -07003402 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003403 if (resetListToTop) {
3404 mAppsView.scrollToTop();
3405 }
Winson Chung4ac30062015-05-08 17:34:17 -07003406 if (updatePredictedApps) {
3407 tryAndUpdatePredictedApps();
3408 }
Winson Chungb745afb2015-03-02 11:51:23 -08003409 showAppsOrWidgets(animated, State.APPS);
3410 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003411
Winson Chungb745afb2015-03-02 11:51:23 -08003412 /**
3413 * Shows the widgets view.
3414 */
3415 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003416 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003417 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003418 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003419 }
Winson Chungb745afb2015-03-02 11:51:23 -08003420 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003421
3422 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003423 @Override
3424 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003425 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003426 }
3427 });
Winson Chungb745afb2015-03-02 11:51:23 -08003428 }
3429
3430 /**
3431 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003432 *
3433 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003434 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003435 // TODO: calling method should use the return value so that when {@code false} is returned
3436 // the workspace transition doesn't fall into invalid state.
3437 private boolean showAppsOrWidgets(boolean animated, State toState) {
3438 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3439 mState != State.WIDGETS_SPRING_LOADED) {
3440 return false;
3441 }
3442 if (toState != State.APPS && toState != State.WIDGETS) {
3443 return false;
3444 }
Winson Chungb745afb2015-03-02 11:51:23 -08003445
3446 if (toState == State.APPS) {
3447 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003448 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003449 mLauncherCallbacks.onAllAppsShown();
3450 }
Winson Chungb745afb2015-03-02 11:51:23 -08003451 } else {
3452 mStateTransitionAnimation.startAnimationToWidgets(animated);
3453 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003454
Michael Jurkab3e22d92011-10-31 15:58:33 -07003455 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003456 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003457
3458 // Pause the auto-advance of widgets until we are out of AllApps
3459 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003460 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003461 closeFolder();
3462
3463 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003464 getWindow().getDecorView()
3465 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003466 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003467 }
3468
Winson Chungcd99cd32015-04-29 11:03:24 -07003469 /**
3470 * Updates the workspace and interaction state on state change, and return the animation to this
3471 * new state.
3472 */
3473 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3474 boolean animated, HashMap<View, Integer> layerViews) {
3475 Workspace.State fromState = mWorkspace.getState();
3476 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3477 updateInteraction(fromState, toState);
3478 return anim;
3479 }
3480
Hyunyoung Song3f471442015-04-08 19:01:34 -07003481 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003482 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003483 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3484 mState == State.WIDGETS_SPRING_LOADED) {
3485 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003486 }
Winson Chungb745afb2015-03-02 11:51:23 -08003487
3488 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003489 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3490 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003491 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003492 }
Adam Cohen7777d962011-08-18 18:58:38 -07003493
Hyunyoung Song3f471442015-04-08 19:01:34 -07003494 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003495 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003496 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003497
Winson Chungcd99cd32015-04-29 11:03:24 -07003498 if (successfulDrop) {
3499 // We need to trigger all apps hidden to notify search to update itself before the
3500 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003501 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003502 mLauncherCallbacks.onAllAppsHidden();
3503 }
3504 }
3505
Winson Chunge7a03942011-08-05 15:05:12 -07003506 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003507 @Override
3508 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003509 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003510 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3511 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003512 // Before we show workspace, hide all apps again because
3513 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3514 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003515 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003516 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003517 } else {
3518 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003519 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003520 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003521 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003522 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003523
Michael Jurkad3ef3062010-11-23 16:23:58 -08003524 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003525 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003526 showAppsView(true /* animated */, false /* resetListToTop */,
3527 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003528 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003529 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003530 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003531 }
3532
Winson Chung4ac30062015-05-08 17:34:17 -07003533 /**
3534 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3535 * resumed.
3536 */
3537 private void tryAndUpdatePredictedApps() {
3538 if (mLauncherCallbacks != null) {
3539 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3540 if (!apps.isEmpty()) {
3541 mAppsView.setPredictedApps(apps);
3542 }
3543 }
3544 }
3545
Michael Jurkab3e22d92011-10-31 15:58:33 -07003546 void lockAllApps() {
3547 // TODO
3548 }
3549
3550 void unlockAllApps() {
3551 // TODO
3552 }
3553
Sunny Goyal594d76d2014-11-06 10:12:54 -08003554 protected void disableVoiceButtonProxy(boolean disable) {
3555 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003556 }
3557
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003558 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003559 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3560 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003561 }
3562
Adam Cohen24ce0b32014-01-14 16:18:14 -08003563 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003564 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3565 if (searchProvider == null) {
3566 return null;
3567 }
3568
3569 Bundle opts = new Bundle();
3570 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003571 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003572
3573 SharedPreferences sp = getSharedPreferences(
3574 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3575 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003576 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003577 if (!searchProvider.provider.flattenToString().equals(
3578 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003579 || (widgetInfo == null)
3580 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003581 // A valid widget is not already bound.
3582 if (widgetId > -1) {
3583 mAppWidgetHost.deleteAppWidgetId(widgetId);
3584 widgetId = -1;
3585 }
3586
3587 // Try to bind a new widget
3588 widgetId = mAppWidgetHost.allocateAppWidgetId();
3589
3590 if (!AppWidgetManagerCompat.getInstance(this)
3591 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3592 mAppWidgetHost.deleteAppWidgetId(widgetId);
3593 widgetId = -1;
3594 }
3595
3596 sp.edit()
3597 .putInt(QSB_WIDGET_ID, widgetId)
3598 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3599 .commit();
3600 }
3601
3602 if (widgetId != -1) {
3603 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3604 mQsb.updateAppWidgetOptions(opts);
3605 mQsb.setPadding(0, 0, 0, 0);
3606 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003607 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003608 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003609 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003610 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003611 }
3612
Sunny Goyal22235bc2015-04-03 09:23:43 -07003613 private void reinflateQSBIfNecessary() {
3614 if (mQsb instanceof LauncherAppWidgetHostView &&
3615 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3616 mSearchDropTargetBar.removeView(mQsb);
3617 mQsb = null;
3618 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3619 }
3620 }
3621
Michael Jurkad7c28052012-04-27 15:43:36 -07003622 @Override
3623 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003624 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003625 final List<CharSequence> text = event.getText();
3626 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003627 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003628 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003629 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003630 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003631 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003632 } else {
3633 text.add(getString(R.string.all_apps_home_button_label));
3634 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003635 return result;
3636 }
3637
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003638 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003639 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003640 */
Adam Cohen091440a2015-03-18 14:16:05 -07003641 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003642 @Override
3643 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003644 closeSystemDialogs();
3645 }
3646 }
3647
3648 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003649 * Receives notifications whenever the appwidgets are reset.
3650 */
3651 private class AppWidgetResetObserver extends ContentObserver {
3652 public AppWidgetResetObserver() {
3653 super(new Handler());
3654 }
3655
3656 @Override
3657 public void onChange(boolean selfChange) {
3658 onAppWidgetReset();
3659 }
3660 }
3661
3662 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003663 * If the activity is currently paused, signal that we need to run the passed Runnable
3664 * in onResume.
3665 *
3666 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003667 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3668 * wrong when we're not running, and if the activity comes back to what the configuration was
3669 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003670 *
3671 * Implementation of the method from LauncherModel.Callbacks.
3672 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003673 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003674 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003675 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003676 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003677 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003678 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003679 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003680 }
3681 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003682 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003683 return true;
3684 } else {
3685 return false;
3686 }
3687 }
3688
Michael Jurkac402cd92013-05-20 15:49:32 +02003689 private boolean waitUntilResume(Runnable run) {
3690 return waitUntilResume(run, false);
3691 }
3692
Michael Jurka1e2f4652013-07-08 18:03:46 -07003693 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003694 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003695 }
3696
Michael Jurka7607c2f2013-04-03 14:33:19 -07003697 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003698 * If the activity is currently paused, signal that we need to re-run the loader
3699 * in onResume.
3700 *
3701 * This needs to be called from incoming places where resources might have been loaded
3702 * while we are paused. That is becaues the Configuration might be wrong
3703 * when we're not running, and if it comes back to what it was when we
3704 * were paused, we are not restarted.
3705 *
3706 * Implementation of the method from LauncherModel.Callbacks.
3707 *
3708 * @return true if we are currently paused. The caller might be able to
3709 * skip some work in that case since we will come back again.
3710 */
3711 public boolean setLoadOnResume() {
3712 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003713 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003714 mOnResumeNeedsLoad = true;
3715 return true;
3716 } else {
3717 return false;
3718 }
3719 }
3720
3721 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003722 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003723 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003725 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003726 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003727 } else {
3728 return SCREEN_COUNT / 2;
3729 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003731
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 /**
3733 * Refreshes the shortcuts shown on the workspace.
3734 *
3735 * Implementation of the method from LauncherModel.Callbacks.
3736 */
3737 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003738 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003739
Michael Jurka7607c2f2013-04-03 14:33:19 -07003740 // If we're starting binding all over again, clear any bind calls we'd postponed in
3741 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3742 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003743 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003744
Winson Chungd64d1762013-08-20 14:37:16 -07003745 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003746 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003747 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003748
Michael Jurka05bf6442011-11-30 20:28:53 -08003749 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003750 if (mHotseat != null) {
3751 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003752 }
3753 }
3754
Adam Cohendcd297f2013-06-18 13:13:40 -07003755 @Override
3756 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003757 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003758
Adam Cohen5084cba2013-09-03 12:01:16 -07003759 // If there are no screens, we need to have an empty screen
3760 if (orderedScreenIds.size() == 0) {
3761 mWorkspace.addExtraEmptyScreen();
3762 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003763
3764 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003765 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003766 if (hasCustomContentToLeft()) {
3767 mWorkspace.createCustomContentContainer();
3768 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003769 }
Winson Chung64359a52013-07-08 17:17:08 -07003770 }
3771
3772 @Override
3773 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003774 // Log to disk
3775 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3776 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3777 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003778 int count = orderedScreenIds.size();
3779 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003780 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003781 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003782 }
3783
Adam Cohen39a06042013-07-19 14:30:12 -07003784 private boolean shouldShowWeightWatcher() {
3785 String spKey = LauncherAppState.getSharedPreferencesKey();
3786 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003787 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003788
3789 return show;
3790 }
3791
3792 private void toggleShowWeightWatcher() {
3793 String spKey = LauncherAppState.getSharedPreferencesKey();
3794 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3795 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3796
3797 show = !show;
3798
3799 SharedPreferences.Editor editor = sp.edit();
3800 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3801 editor.commit();
3802
3803 if (mWeightWatcher != null) {
3804 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3805 }
3806 }
3807
Winson Chungd64d1762013-08-20 14:37:16 -07003808 public void bindAppsAdded(final ArrayList<Long> newScreens,
3809 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003810 final ArrayList<ItemInfo> addAnimated,
3811 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003812 Runnable r = new Runnable() {
3813 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003814 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003815 }
3816 };
3817 if (waitUntilResume(r)) {
3818 return;
3819 }
3820
Winson Chungd64d1762013-08-20 14:37:16 -07003821 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003822 if (newScreens != null) {
3823 bindAddScreens(newScreens);
3824 }
Winson Chungd64d1762013-08-20 14:37:16 -07003825
3826 // We add the items without animation on non-visible pages, and with
3827 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003828 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003829 bindItems(addNotAnimated, 0,
3830 addNotAnimated.size(), false);
3831 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003832 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003833 bindItems(addAnimated, 0,
3834 addAnimated.size(), true);
3835 }
Winson Chungc58497e2013-09-03 17:48:37 -07003836
Winson Chung87412982013-10-03 18:34:14 -07003837 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003838 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003839
Winson Chungb745afb2015-03-02 11:51:23 -08003840 if (addedApps != null && mAppsView != null) {
3841 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003842 }
Winson Chungd64d1762013-08-20 14:37:16 -07003843 }
3844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003845 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003846 * Bind the items start-end from the list.
3847 *
3848 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003849 */
Winson Chung64359a52013-07-08 17:17:08 -07003850 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3851 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003852 Runnable r = new Runnable() {
3853 public void run() {
3854 bindItems(shortcuts, start, end, forceAnimateIcons);
3855 }
3856 };
3857 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003858 return;
3859 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003860
Winson Chungf0c6ae02012-03-21 16:10:31 -07003861 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003862 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3863 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003864 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003865 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003866 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003867 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003868 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003869
3870 // Short circuit if we are loading dock items for a configuration which has no dock
3871 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3872 mHotseat == null) {
3873 continue;
3874 }
3875
Joe Onorato9c1289c2009-08-17 11:03:03 -04003876 switch (item.itemType) {
3877 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3878 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003879 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003880 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003881
Winson Chung64359a52013-07-08 17:17:08 -07003882 /*
3883 * TODO: FIX collision case
3884 */
Winson Chungce376632013-07-11 16:12:41 -07003885 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3886 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3887 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003888 View v = cl.getChildAt(item.cellX, item.cellY);
3889 Object tag = v.getTag();
3890 String desc = "Collision while binding workspace item: " + item
3891 + ". Collides with " + tag;
3892 if (LauncherAppState.isDogfoodBuild()) {
3893 throw (new RuntimeException(desc));
3894 } else {
3895 Log.d(TAG, desc);
3896 }
Winson Chungce376632013-07-11 16:12:41 -07003897 }
Winson Chung64359a52013-07-08 17:17:08 -07003898 }
3899
Adam Cohendcd297f2013-06-18 13:13:40 -07003900 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3901 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003902 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003903 // Animate all the applications up now
3904 shortcut.setAlpha(0f);
3905 shortcut.setScaleX(0f);
3906 shortcut.setScaleY(0f);
3907 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003908 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003909 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003910 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003911 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003912 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003913 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003914 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003915 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3916 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003917 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003918 default:
3919 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003920 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003921 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003922
Winson Chung997a9232013-07-24 15:33:46 -07003923 if (animateIcons) {
3924 // Animate to the correct page
3925 if (newShortcutsScreenId > -1) {
3926 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003927 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003928 final Runnable startBounceAnimRunnable = new Runnable() {
3929 public void run() {
3930 anim.playTogether(bounceAnims);
3931 anim.start();
3932 }
3933 };
Winson Chung997a9232013-07-24 15:33:46 -07003934 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003935 // We post the animation slightly delayed to prevent slowdowns
3936 // when we are loading right after we return to launcher.
3937 mWorkspace.postDelayed(new Runnable() {
3938 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003939 if (mWorkspace != null) {
3940 mWorkspace.snapToPage(newScreenIndex);
3941 mWorkspace.postDelayed(startBounceAnimRunnable,
3942 NEW_APPS_ANIMATION_DELAY);
3943 }
Winson Chung94d67682013-09-25 16:29:40 -07003944 }
3945 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003946 } else {
3947 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003948 }
3949 }
Winson Chung64359a52013-07-08 17:17:08 -07003950 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003951 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003952 }
3953
Joe Onorato9c1289c2009-08-17 11:03:03 -04003954 /**
3955 * Implementation of the method from LauncherModel.Callbacks.
3956 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003957 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003958 Runnable r = new Runnable() {
3959 public void run() {
3960 bindFolders(folders);
3961 }
3962 };
3963 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003964 return;
3965 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003966 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003967 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003968
3969 /**
3970 * Add the views for a widget to the workspace.
3971 *
3972 * Implementation of the method from LauncherModel.Callbacks.
3973 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003974 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003975 Runnable r = new Runnable() {
3976 public void run() {
3977 bindAppWidget(item);
3978 }
3979 };
3980 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003981 return;
3982 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003983
Daniel Sandler843e8602010-06-07 14:59:01 -04003984 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3985 if (DEBUG_WIDGETS) {
3986 Log.d(TAG, "bindAppWidget: " + item);
3987 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003988 final Workspace workspace = mWorkspace;
3989
Adam Cohen59400422014-03-05 18:07:04 -08003990 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003991 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003992
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003993 if (!mIsSafeModeEnabled
3994 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3995 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003996 if (appWidgetInfo == null) {
3997 if (DEBUG_WIDGETS) {
3998 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3999 + " belongs to component " + item.providerName
4000 + ", as the povider is null");
4001 }
4002 LauncherModel.deleteItemFromDatabase(this, item);
4003 return;
4004 }
4005 // Note: This assumes that the id remap broadcast is received before this step.
4006 // If that is not the case, the id remap will be ignored and user may see the
4007 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004008 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004009 pendingInfo.spanX = item.spanX;
4010 pendingInfo.spanY = item.spanY;
4011 pendingInfo.minSpanX = item.minSpanX;
4012 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004013 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004014 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004015
Sunny Goyalff572272014-07-23 13:58:07 -07004016 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004017 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4018 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004019
4020 // TODO consider showing a permission dialog when the widget is clicked.
4021 if (!success) {
4022 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4023 if (DEBUG_WIDGETS) {
4024 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4025 + " belongs to component " + item.providerName
4026 + ", as the launcher is unable to bing a new widget id");
4027 }
4028 LauncherModel.deleteItemFromDatabase(this, item);
4029 return;
4030 }
4031
4032 item.appWidgetId = newWidgetId;
4033
4034 // If the widget has a configure activity, it is still needs to set it up, otherwise
4035 // the widget is ready to go.
4036 item.restoreStatus = (appWidgetInfo.configure == null)
4037 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4038 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4039
4040 LauncherModel.updateItemInDatabase(this, item);
4041 }
4042
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004043 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004044 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004045 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004046 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4047 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004048 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004049
Sunny Goyal651077b2014-06-30 14:15:31 -07004050 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4051 } else {
4052 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004053 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4054 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004055 view.updateIcon(mIconCache);
4056 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004057 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004058 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004059 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004060
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004062 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004063
Adam Cohendcd297f2013-06-18 13:13:40 -07004064 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004066 if (!item.isCustomWidget()) {
4067 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4068 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004069
Joe Onorato9c1289c2009-08-17 11:03:03 -04004070 workspace.requestLayout();
4071
Daniel Sandler843e8602010-06-07 14:59:01 -04004072 if (DEBUG_WIDGETS) {
4073 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4074 + (SystemClock.uptimeMillis()-start) + "ms");
4075 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004076 }
4077
Sunny Goyalff572272014-07-23 13:58:07 -07004078 /**
4079 * Restores a pending widget.
4080 *
4081 * @param appWidgetId The app widget id
4082 * @param cellInfo The position on screen where to create the widget.
4083 */
4084 private void completeRestoreAppWidget(final int appWidgetId) {
4085 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4086 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4087 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4088 return;
4089 }
4090
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004091 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004092 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4093
4094 mWorkspace.reinflateWidgetsIfNecessary();
4095 LauncherModel.updateItemInDatabase(this, info);
4096 }
4097
Adam Cohen1462de32012-07-24 22:34:36 -07004098 public void onPageBoundSynchronously(int page) {
4099 mSynchronouslyBoundPages.add(page);
4100 }
4101
Joe Onorato9c1289c2009-08-17 11:03:03 -04004102 /**
4103 * Callback saying that there aren't any more items to bind.
4104 *
4105 * Implementation of the method from LauncherModel.Callbacks.
4106 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004107 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004108 Runnable r = new Runnable() {
4109 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004110 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004111 }
4112 };
4113 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004114 return;
4115 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004116 if (mSavedState != null) {
4117 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004118 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004119 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004120 mSavedState = null;
4121 }
4122
Adam Cohen1462de32012-07-24 22:34:36 -07004123 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004124
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004125 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004126 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004127
4128 // If we received the result of any pending adds while the loader was running (e.g. the
4129 // widget configuration forced an orientation change), process them now.
4130 if (sPendingAddItem != null) {
4131 final long screenId = completeAdd(sPendingAddItem);
4132
4133 // TODO: this moves the user to the page where the pending item was added. Ideally,
4134 // the screen would be guaranteed to exist after bind, and the page would be set through
4135 // the workspace restore process.
4136 mWorkspace.post(new Runnable() {
4137 @Override
4138 public void run() {
4139 mWorkspace.snapToScreenId(screenId);
4140 }
4141 });
4142 sPendingAddItem = null;
4143 }
4144
Sunny Goyal756adbc2015-04-16 15:20:51 -07004145 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004146
4147 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004148 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004149 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004150 }
4151
Adam Cohen3ed316a2014-07-23 18:21:20 -07004152 private void sendLoadingCompleteBroadcastIfNecessary() {
4153 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4154 String permission =
4155 getResources().getString(R.string.receive_first_load_broadcast_permission);
4156 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4157 sendBroadcast(intent, permission);
4158 SharedPreferences.Editor editor = mSharedPrefs.edit();
4159 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4160 editor.apply();
4161 }
4162 }
4163
Winson Chunga0b7e862013-09-05 16:03:15 -07004164 public boolean isAllAppsButtonRank(int rank) {
4165 if (mHotseat != null) {
4166 return mHotseat.isAllAppsButtonRank(rank);
4167 }
4168 return false;
4169 }
4170
Winson Chunga2413752012-04-03 14:22:34 -07004171 private boolean canRunNewAppsAnimation() {
4172 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4173 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4174 }
4175
Winson Chungc9168342013-06-26 14:54:55 -07004176 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4177 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4178 PropertyValuesHolder.ofFloat("alpha", 1f),
4179 PropertyValuesHolder.ofFloat("scaleX", 1f),
4180 PropertyValuesHolder.ofFloat("scaleY", 1f));
4181 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4182 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4183 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4184 return bounceAnim;
4185 }
4186
Adam Cohen27772732013-11-11 14:00:56 +00004187 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004188 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004189 }
4190
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004191 protected Rect getSearchBarBounds() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004192 return mDeviceProfile.getSearchBarBounds();
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004193 }
4194
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004195 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004196 if (mSearchDropTargetBar == null) {
4197 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004198 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004199 if (mQsb != null) {
4200 mSearchDropTargetBar.removeView(mQsb);
4201 mQsb = null;
4202 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004203 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004204 }
4205
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004206 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004207 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4208 * multiple calls to bind the same list.)
4209 */
4210 @Thunk ArrayList<AppInfo> mTmpAppsList;
4211 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4212 public void run() {
4213 bindAllApplications(mTmpAppsList);
4214 mTmpAppsList = null;
4215 }
4216 };
4217
4218 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004219 * Add the icons for all apps.
4220 *
4221 * Implementation of the method from LauncherModel.Callbacks.
4222 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004223 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004224 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4225 mTmpAppsList = apps;
4226 return;
4227 }
4228
Winson Chungb745afb2015-03-02 11:51:23 -08004229 if (mAppsView != null) {
4230 mAppsView.setApps(apps);
4231 }
Adam Cohen9211d422014-10-07 18:14:53 -07004232 if (mLauncherCallbacks != null) {
4233 mLauncherCallbacks.bindAllApplications(apps);
4234 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004235 }
4236
4237 /**
4238 * A package was updated.
4239 *
4240 * Implementation of the method from LauncherModel.Callbacks.
4241 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004242 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004243 Runnable r = new Runnable() {
4244 public void run() {
4245 bindAppsUpdated(apps);
4246 }
4247 };
4248 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004249 return;
4250 }
4251
Winson Chungb745afb2015-03-02 11:51:23 -08004252 if (mAppsView != null) {
4253 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004254 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004255 }
4256
Sunny Goyal4390ace2014-10-13 11:33:11 -07004257 @Override
4258 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4259 Runnable r = new Runnable() {
4260 public void run() {
4261 bindWidgetsRestored(widgets);
4262 }
4263 };
4264 if (waitUntilResume(r)) {
4265 return;
4266 }
4267 mWorkspace.widgetsRestored(widgets);
4268 }
4269
Joe Onorato9c1289c2009-08-17 11:03:03 -04004270 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004271 * Some shortcuts were updated in the background.
4272 *
4273 * Implementation of the method from LauncherModel.Callbacks.
4274 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004275 @Override
4276 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4277 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004278 Runnable r = new Runnable() {
4279 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004280 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004281 }
4282 };
4283 if (waitUntilResume(r)) {
4284 return;
4285 }
4286
Sunny Goyal4390ace2014-10-13 11:33:11 -07004287 if (!updated.isEmpty()) {
4288 mWorkspace.updateShortcuts(updated);
4289 }
4290
4291 if (!removed.isEmpty()) {
4292 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4293 for (ShortcutInfo si : removed) {
4294 removedComponents.add(si.getTargetComponent());
4295 }
4296 mWorkspace.removeItemsByComponentName(removedComponents, user);
4297 // Notify the drag controller
4298 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004299 }
4300 }
4301
4302 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004303 * Update the state of a package, typically related to install state.
4304 *
4305 * Implementation of the method from LauncherModel.Callbacks.
4306 */
Sunny Goyale755d462014-07-22 13:48:29 -07004307 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004308 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4309 Runnable r = new Runnable() {
4310 public void run() {
4311 bindRestoreItemsChange(updates);
4312 }
4313 };
4314 if (waitUntilResume(r)) {
4315 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004316 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004317
Sunny Goyal756adbc2015-04-16 15:20:51 -07004318 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004319 }
4320
4321 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004322 * A package was uninstalled. We take both the super set of packageNames
4323 * in addition to specific applications to remove, the reason being that
4324 * this can be called when a package is updated as well. In that scenario,
4325 * we only remove specific components from the workspace, where as
4326 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004327 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004328 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004329 * Implementation of the method from LauncherModel.Callbacks.
4330 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004331 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004332 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004333 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004334 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004335 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004336 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004337 }
Winson Chungd64d1762013-08-20 14:37:16 -07004338 };
4339 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004340 return;
4341 }
4342
Sunny Goyal1a745e82014-10-02 15:58:31 -07004343 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004344 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4345 for (AppInfo info : appInfos) {
4346 removedComponents.add(info.componentName);
4347 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004348 if (!packageNames.isEmpty()) {
4349 mWorkspace.removeItemsByPackageName(packageNames, user);
4350 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004351 if (!removedComponents.isEmpty()) {
4352 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004353 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004354 // Notify the drag controller
4355 mDragController.onAppsRemoved(packageNames, removedComponents);
4356
Sunny Goyal1a745e82014-10-02 15:58:31 -07004357 } else {
4358 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004359 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004360
Winson Chungdf95eb12013-10-16 14:57:07 -07004361 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004362 if (mAppsView != null) {
4363 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004364 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004365 }
Joe Onoratobe386092009-11-17 17:32:16 -08004366
Adam Cohen091440a2015-03-18 14:16:05 -07004367 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004368 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004369 public void run() {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004370 bindAllPackages(mWidgetsAndShortcuts);
Winson Chung83892cc2013-05-01 16:53:33 -07004371 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004372 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004373
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004374 @Override
4375 public void bindAllPackages(final ArrayList<Object> widgetsAndShortcuts) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004376 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4377 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004378 return;
4379 }
4380
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004381 if (mWidgetsView != null && widgetsAndShortcuts != null) {
4382 mWidgetsView.addWidgets(widgetsAndShortcuts, getPackageManager());
4383 mWidgetsAndShortcuts = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004384 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004385 }
4386
Winson Chung400438b2011-01-16 17:53:48 -08004387 private int mapConfigurationOriActivityInfoOri(int configOri) {
4388 final Display d = getWindowManager().getDefaultDisplay();
4389 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4390 switch (d.getRotation()) {
4391 case Surface.ROTATION_0:
4392 case Surface.ROTATION_180:
4393 // We are currently in the same basic orientation as the natural orientation
4394 naturalOri = configOri;
4395 break;
4396 case Surface.ROTATION_90:
4397 case Surface.ROTATION_270:
4398 // We are currently in the other basic orientation to the natural orientation
4399 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4400 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4401 break;
4402 }
4403
4404 int[] oriMap = {
4405 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4406 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4407 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4408 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4409 };
4410 // Since the map starts at portrait, we need to offset if this device's natural orientation
4411 // is landscape.
4412 int indexOffset = 0;
4413 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4414 indexOffset = 1;
4415 }
4416 return oriMap[(d.getRotation() + indexOffset) % 4];
4417 }
Adam Cohen446e9402011-09-15 18:21:21 -07004418
Winson Chung4b919f82012-05-01 10:44:08 -07004419 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004420 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004421 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4422 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4423 .getConfiguration().orientation));
4424 } else {
4425 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4426 }
Winson Chung4b919f82012-05-01 10:44:08 -07004427 }
4428 }
4429 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004430 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004431 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004432 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004433 } else {
4434 mHandler.postDelayed(new Runnable() {
4435 public void run() {
4436 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4437 }
4438 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004439 }
Winson Chung4b919f82012-05-01 10:44:08 -07004440 }
Winson Chung400438b2011-01-16 17:53:48 -08004441 }
4442
Winson Chunge43a1e72014-01-15 10:33:02 -08004443 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004444 if (mLauncherCallbacks != null) {
4445 return mLauncherCallbacks.isLauncherPreinstalled();
4446 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004447 PackageManager pm = getPackageManager();
4448 try {
4449 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4450 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4451 return true;
4452 } else {
4453 return false;
4454 }
4455 } catch (NameNotFoundException e) {
4456 e.printStackTrace();
4457 return false;
4458 }
4459 }
4460
Adam Cohenea90f832014-05-21 15:03:34 -07004461 /**
4462 * This method indicates whether or not we should suggest default wallpaper dimensions
4463 * when our wallpaper cropper was not yet used to set a wallpaper.
4464 */
4465 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004466 if (mLauncherCallbacks != null) {
4467 return mLauncherCallbacks.overrideWallpaperDimensions();
4468 }
Adam Cohenea90f832014-05-21 15:03:34 -07004469 return true;
4470 }
4471
Adam Cohen432609a2014-03-13 17:03:22 -07004472 /**
4473 * To be overridden by subclasses to indicate that there is an activity to launch
4474 * before showing the standard launcher experience.
4475 */
4476 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004477 if (mLauncherCallbacks != null) {
4478 return mLauncherCallbacks.hasFirstRunActivity();
4479 }
Adam Cohen432609a2014-03-13 17:03:22 -07004480 return false;
4481 }
4482
4483 /**
4484 * To be overridden by subclasses to launch any first run activity
4485 */
4486 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004487 if (mLauncherCallbacks != null) {
4488 return mLauncherCallbacks.getFirstRunActivity();
4489 }
Adam Cohen432609a2014-03-13 17:03:22 -07004490 return null;
4491 }
4492
Winson Chung2826a402015-04-17 16:18:53 -07004493 /**
4494 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004495 */
4496 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004497 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4498 return false;
4499 }
4500
Winson Chung2826a402015-04-17 16:18:53 -07004501 if (mLauncherCallbacks != null) {
4502 return mLauncherCallbacks.overrideAllAppsSearch();
4503 }
4504 return false;
4505 }
4506
Winson Chunga6945242014-01-08 14:04:34 -08004507 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004508 return !ActivityManager.isRunningInTestHarness() &&
4509 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004510 }
4511
Adam Cohen4a9423d2014-03-28 14:22:31 -07004512 protected boolean hasRunFirstRunActivity() {
4513 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4514 }
4515
Adam Cohen432609a2014-03-13 17:03:22 -07004516 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004517 if (shouldRunFirstRunActivity() &&
4518 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004519 Intent firstRunIntent = getFirstRunActivity();
4520 if (firstRunIntent != null) {
4521 startActivity(firstRunIntent);
4522 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004523 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004524 }
4525 }
Adam Cohen432609a2014-03-13 17:03:22 -07004526 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004527 }
4528
4529 private void markFirstRunActivityShown() {
4530 SharedPreferences.Editor editor = mSharedPrefs.edit();
4531 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4532 editor.apply();
4533 }
4534
Adam Cohen432609a2014-03-13 17:03:22 -07004535 /**
4536 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4537 * screen that must be displayed and dismissed.
4538 */
4539 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004540 if (mLauncherCallbacks != null) {
4541 return mLauncherCallbacks.hasDismissableIntroScreen();
4542 }
Adam Cohen432609a2014-03-13 17:03:22 -07004543 return false;
4544 }
4545
4546 /**
4547 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4548 */
4549 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004550 if (mLauncherCallbacks != null) {
4551 return mLauncherCallbacks.getIntroScreen();
4552 }
Adam Cohen432609a2014-03-13 17:03:22 -07004553 return null;
4554 }
4555
4556 /**
4557 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4558 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4559 */
4560 private boolean shouldShowIntroScreen() {
4561 return hasDismissableIntroScreen() &&
4562 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4563 }
4564
4565 protected void showIntroScreen() {
4566 View introScreen = getIntroScreen();
4567 changeWallpaperVisiblity(false);
4568 if (introScreen != null) {
4569 mDragLayer.showOverlayView(introScreen);
4570 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004571 if (mLauncherOverlayContainer != null) {
4572 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4573 }
Adam Cohen432609a2014-03-13 17:03:22 -07004574 }
4575
4576 public void dismissIntroScreen() {
4577 markIntroScreenDismissed();
4578 if (showFirstRunActivity()) {
4579 // We delay hiding the intro view until the first run activity is showing. This
4580 // avoids a blip.
4581 mWorkspace.postDelayed(new Runnable() {
4582 @Override
4583 public void run() {
4584 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004585 if (mLauncherOverlayContainer != null) {
4586 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4587 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004588 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004589 }
4590 }, ACTIVITY_START_DELAY);
4591 } else {
4592 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004593 if (mLauncherOverlayContainer != null) {
4594 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4595 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004596 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004597 }
4598 changeWallpaperVisiblity(true);
4599 }
4600
4601 private void markIntroScreenDismissed() {
4602 SharedPreferences.Editor editor = mSharedPrefs.edit();
4603 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4604 editor.apply();
4605 }
4606
Adam Cohen091440a2015-03-18 14:16:05 -07004607 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004608 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4609 // on the device, then we always show the first run cling experience (or if there is no
4610 // launcher2). Otherwise, we prompt the user upon started for migration
4611 LauncherClings launcherClings = new LauncherClings(this);
4612 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4613 if (mModel.canMigrateFromOldLauncherDb(this)) {
4614 launcherClings.showMigrationCling();
4615 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004616 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004617 }
4618 }
4619 }
4620
Winson Chunga6945242014-01-08 14:04:34 -08004621 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004622 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4623 if (mHotseat != null) mHotseat.setAlpha(1f);
4624 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4625 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004626 }
4627
4628 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004629 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4630 if (mHotseat != null) mHotseat.setAlpha(0f);
4631 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4632 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004633 }
4634
Mathew Inwood72fbec12013-11-19 15:45:07 +00004635 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004636 // Called from search suggestion, not supported in other profiles.
4637 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4638 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4639 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4640 myUser);
4641 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004642 return null;
4643 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004644 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004645 }
4646
4647 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4648 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004649 // Called from search suggestion, not supported in other profiles.
4650 return createShortcutDragInfo(shortcutIntent, caption, icon,
4651 UserHandleCompat.myUserHandle());
4652 }
4653
4654 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4655 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004656 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004657 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004658 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004659 }
4660
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004661 protected void moveWorkspaceToDefaultScreen() {
4662 mWorkspace.moveToDefaultScreen(false);
4663 }
4664
Mathew Inwood72fbec12013-11-19 15:45:07 +00004665 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4666 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004667 mWorkspace.onExternalDragStartedWithItem(dragView);
4668 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004669 }
4670
Anjali Koppalf5d29132014-02-28 13:33:27 -08004671 @Override
4672 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004673 if (mLauncherCallbacks != null) {
4674 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4675 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004676 }
4677
Winson Chung80baf5a2010-08-09 16:03:15 -07004678 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004679 * Prints out out state for debugging.
4680 */
4681 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004682 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004683 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004684 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4685 Log.d(TAG, "mRestoring=" + mRestoring);
4686 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4687 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004688 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004689 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004690 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004691
Daniel Sandler325dc232013-06-05 22:57:57 -04004692 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004693 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004694
4695 @Override
4696 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4697 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004698 synchronized (sDumpLogs) {
4699 writer.println(" ");
4700 writer.println("Debug logs: ");
4701 for (int i = 0; i < sDumpLogs.size(); i++) {
4702 writer.println(" " + sDumpLogs.get(i));
4703 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004704 }
Adam Cohen9211d422014-10-07 18:14:53 -07004705 if (mLauncherCallbacks != null) {
4706 mLauncherCallbacks.dump(prefix, fd, writer, args);
4707 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004708 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004709
4710 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004711 if (DEBUG_DUMP_LOG) {
4712 synchronized (sDumpLogs) {
4713 Log.d(TAG, "");
4714 Log.d(TAG, "*********************");
4715 Log.d(TAG, "Launcher debug logs: ");
4716 for (int i = 0; i < sDumpLogs.size(); i++) {
4717 Log.d(TAG, " " + sDumpLogs.get(i));
4718 }
4719 Log.d(TAG, "*********************");
4720 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004721 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004722 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004723 }
4724
4725 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004726 addDumpLog(tag, log, null, debugLog);
4727 }
4728
4729 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004730 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004731 if (e != null) {
4732 Log.d(tag, log, e);
4733 } else {
4734 Log.d(tag, log);
4735 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004736 }
Winson Chungede41292013-09-19 16:27:36 -07004737 if (DEBUG_DUMP_LOG) {
4738 sDateStamp.setTime(System.currentTimeMillis());
4739 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004740 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4741 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004742 }
Winson Chungede41292013-09-19 16:27:36 -07004743 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004744 }
4745
Adam Cohen59400422014-03-05 18:07:04 -08004746 public static CustomAppWidget getCustomAppWidget(String name) {
4747 return sCustomAppWidgets.get(name);
4748 }
4749
4750 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4751 return sCustomAppWidgets;
4752 }
4753
Winson Chungede41292013-09-19 16:27:36 -07004754 public void dumpLogsToLocalData() {
4755 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004756 new AsyncTask<Void, Void, Void>() {
4757 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004758 boolean success = false;
4759 sDateStamp.setTime(sRunStart);
4760 String FILENAME = sDateStamp.getMonth() + "-"
4761 + sDateStamp.getDay() + "_"
4762 + sDateStamp.getHours() + "-"
4763 + sDateStamp.getMinutes() + "_"
4764 + sDateStamp.getSeconds() + ".txt";
4765
4766 FileOutputStream fos = null;
4767 File outFile = null;
4768 try {
4769 outFile = new File(getFilesDir(), FILENAME);
4770 outFile.createNewFile();
4771 fos = new FileOutputStream(outFile);
4772 } catch (Exception e) {
4773 e.printStackTrace();
4774 }
4775 if (fos != null) {
4776 PrintWriter writer = new PrintWriter(fos);
4777
4778 writer.println(" ");
4779 writer.println("Debug logs: ");
4780 synchronized (sDumpLogs) {
4781 for (int i = 0; i < sDumpLogs.size(); i++) {
4782 writer.println(" " + sDumpLogs.get(i));
4783 }
4784 }
4785 writer.close();
4786 }
4787 try {
4788 if (fos != null) {
4789 fos.close();
4790 success = true;
4791 }
4792 } catch (IOException e) {
4793 e.printStackTrace();
4794 }
Michael Jurka43467462013-11-14 12:03:58 +01004795 return null;
Winson Chungede41292013-09-19 16:27:36 -07004796 }
Michael Jurka43467462013-11-14 12:03:58 +01004797 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004798 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004799 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004800}
Michael Jurka2763be32011-02-24 11:19:57 -08004801
Michael Jurkaabded662011-03-04 12:06:57 -08004802interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004803 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004804 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004805 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004806 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004807 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004808}
Dan Sandlerd5024042014-01-09 15:01:33 -05004809
4810interface DebugIntents {
4811 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4812 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004813}