blob: 59ca97865405f38c533f1d6cd2e78df0e4b04a19 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040059import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020061import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070085import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070087import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
92import android.widget.FrameLayout;
93import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080094import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070096
Sunny Goyal651077b2014-06-30 14:15:31 -070097import com.android.launcher3.DropTarget.DragObject;
98import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyal83a8f042015-05-19 12:52:12 -070099import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700105import com.android.launcher3.model.WidgetsModel;
Sunny Goyale2df0622015-04-24 11:27:00 -0700106import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700107import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700108import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700109import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700110import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700111
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.io.DataInputStream;
113import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700115import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700119import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800120import java.lang.reflect.InvocationTargetException;
121import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700124import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700127import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000129import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131/**
132 * Default launcher application.
133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100134public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
137 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700138 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700139 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Winson Chung83f59ab2015-05-05 17:21:58 -0700141 // Temporary flag
142 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
143
Daniel Sandlerf061f822013-06-27 13:59:36 -0400144 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700145 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700146 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400147 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700148 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700149
Dan Sandlerd5024042014-01-09 15:01:33 -0500150 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700155 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Michael Jurka8b805b12012-04-18 14:23:14 -0700157 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700158 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700159
Mathew Inwood876a8462013-06-14 14:12:41 +0100160 /**
161 * IntentStarter uses request codes starting with this. This must be greater than all activity
162 * request codes used internally.
163 */
164 protected static final int REQUEST_LAST = 100;
165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
167
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800168 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Michael Jurka0a457bf2012-11-19 14:05:05 -0800170 // To turn on these properties, type
171 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800172 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Winson Chung2672ff92012-05-04 16:22:30 -0700174 // The Intent extra that defines whether to ignore the launch animation
175 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400176 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: int
179 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700180 // Type: int
181 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
184 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700189 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
192 // Type: int
193 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
194 // Type: parcelable
195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000198 // Type: int[]
199 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Adam Cohen432609a2014-03-13 17:03:22 -0700201 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800202 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
203
Adam Cohen3ed316a2014-07-23 18:21:20 -0700204 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
205 static final String ACTION_FIRST_LOAD_COMPLETE =
206 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
207
Adam Cohen39a06042013-07-19 14:30:12 -0700208 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700209 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700210
Sunny Goyal594d76d2014-11-06 10:12:54 -0800211 private static final String QSB_WIDGET_ID = "qsb_widget_id";
212 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
213
Winson Chunga6945242014-01-08 14:04:34 -0800214 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
215
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700216 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800217 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700220
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700221 private boolean mIsSafeModeEnabled;
222
Adam Cohenc2d6e892014-10-16 09:49:24 -0700223 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
224 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700225 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700226
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700228 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
229 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700230 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000232 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
233 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
234
Winson Chunga2413752012-04-03 14:22:34 -0700235 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700236 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
237 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700238 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700239
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800240 private final BroadcastReceiver mCloseSystemDialogsReceiver
241 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800242 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 private LayoutInflater mInflater;
245
Adam Cohen091440a2015-03-18 14:16:05 -0700246 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700247 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800248 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700249 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800250 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700251 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700252
Sunny Goyalffe83f12014-08-14 17:39:34 -0700253 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700254 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700255
Adam Cohen091440a2015-03-18 14:16:05 -0700256 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800257 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800258 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700259
Michael Jurka0280c3b2010-09-17 15:00:07 -0700260 private int[] mTmpAddItemCellCoordinates = new int[2];
261
Winson Chung3d503fb2011-07-13 17:25:49 -0700262 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800263 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700264
Michael Jurka838a4ca2011-02-07 13:33:06 -0800265 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700266
Winson Chungc51db6a2011-10-05 11:44:49 -0700267 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700268
269 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700270 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700271
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700272 // Main container view and the model for the widget tray screen.
273 @Thunk WidgetsContainerView mWidgetsView;
274 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700275
Winson Chungf0ea4d32011-06-06 14:27:16 -0700276 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800277 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700280 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
281 // scroll issues (because the workspace may not have been measured yet) and extra work.
282 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
283 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284
285 private SpannableStringBuilder mDefaultKeySsb = null;
286
Adam Cohen091440a2015-03-18 14:16:05 -0700287 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800289 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 private boolean mRestoring;
291 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700292 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293
Michael Jurka1e2f4652013-07-08 18:03:46 -0700294 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700295 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
296
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 private Bundle mSavedInstanceState;
298
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800300 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700301 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800302 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700303 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305
Adam Cohen091440a2015-03-18 14:16:05 -0700306 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700307
Sunny Goyale2df0622015-04-24 11:27:00 -0700308 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700309
Adam Cohen61f560d2013-09-30 15:58:20 -0700310 private View.OnTouchListener mHapticFeedbackTouchListener;
311
Adam Cohended9f8d2010-11-03 13:25:16 -0700312 // Related to the auto-advancing of widgets
313 private final int ADVANCE_MSG = 1;
314 private final int mAdvanceInterval = 20000;
315 private final int mAdvanceStagger = 250;
316 private long mAutoAdvanceSentTime;
317 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700318 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700319 new HashMap<View, AppWidgetProviderInfo>();
320
Winson Chung400438b2011-01-16 17:53:48 -0800321 // Determines how long to wait after a rotation before restoring the screen orientation to
322 // match the sensor state.
323 private final int mRestoreScreenOrientationDelay = 500;
324
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700326
Adam Cohen1462de32012-07-24 22:34:36 -0700327 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700328 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700329
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700330 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700331 static Date sDateStamp = new Date();
332 static DateFormat sDateFormat =
333 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
334 static long sRunStart = System.currentTimeMillis();
335 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700336
Winson Chung46353de2012-02-16 14:05:10 -0800337 // We only want to get the SharedPreferences once since it does an FS stat each time we get
338 // it from the context.
339 private SharedPreferences mSharedPrefs;
340
Winson Chungf0c6ae02012-03-21 16:10:31 -0700341 // Holds the page that we need to animate to, and the icon views that we need to animate up
342 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700343 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700344 private Bitmap mFolderIconBitmap;
345 private Canvas mFolderIconCanvas;
346 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700347
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700348 private DeviceProfile mDeviceProfile;
349
Winson Chung13eb5272015-05-11 16:30:13 -0700350 // This is set to the view that launched the activity that navigated the user away from
351 // launcher. Since there is no callback for when the activity has finished launching, enable
352 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800353 private BubbleTextView mWaitingForResume;
354
Adam Cohen59400422014-03-05 18:07:04 -0800355 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
356 new HashMap<String, CustomAppWidget>();
357
358 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
359 static {
360 if (ENABLE_CUSTOM_WIDGET_TEST) {
361 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
362 }
363 }
364
Chet Haasea8f996d2015-02-17 12:56:04 -0800365 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
366 private static Method sClipRevealMethod = null;
367 static {
368 Class<?> activityOptionsClass = ActivityOptions.class;
369 try {
370 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
371 View.class, int.class, int.class, int.class, int.class);
372 } catch (Exception e) {
373 // Earlier version
374 }
375 }
376
Adam Cohen091440a2015-03-18 14:16:05 -0700377 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700378 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700379 if (mWorkspace != null) {
380 mWorkspace.buildPageHardwareLayers();
381 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700382 }
383 };
384
Adam Cohendb364c32014-05-20 14:23:40 -0700385 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800386
Adam Cohen091440a2015-03-18 14:16:05 -0700387 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800388 int requestCode;
389 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700390 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700391 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800392 int cellX;
393 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700394 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800395 }
396
Daniel Sandlerff02d492013-08-05 02:12:05 -0400397 private Stats mStats;
398
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700399 FocusIndicatorView mFocusHandler;
400
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 @Override
402 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700403 if (DEBUG_STRICT_MODE) {
404 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
405 .detectDiskReads()
406 .detectDiskWrites()
407 .detectNetwork() // or .detectAll() for all detectable problems
408 .penaltyLog()
409 .build());
410 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
411 .detectLeakedSqlLiteObjects()
412 .detectLeakedClosableObjects()
413 .penaltyLog()
414 .penaltyDeath()
415 .build());
416 }
417
Adam Cohen9211d422014-10-07 18:14:53 -0700418 if (mLauncherCallbacks != null) {
419 mLauncherCallbacks.preOnCreate();
420 }
421
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400423
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400424 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400425 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700426 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700427
Winson Chung5f8afe62013-08-12 16:19:28 -0700428 // Lazy-initialize the dynamic grid
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700429 mDeviceProfile = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700430
431 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400432 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700433 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700434 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800435 mModel = app.setLauncher(this);
436 mIconCache = app.getIconCache();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700437 mIconCache.flushInvalidIcons(mDeviceProfile);
Joe Onorato41a12d22009-10-31 18:30:00 -0400438 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800440 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700441
Daniel Sandlerff02d492013-08-05 02:12:05 -0400442 mStats = new Stats(this);
443
Sunny Goyalffe83f12014-08-14 17:39:34 -0700444 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700445
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700446 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
447 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700448
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700449 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
450 // this also ensures that any synchronous binding below doesn't re-trigger another
451 // LauncherModel load.
452 mPaused = false;
453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200455 android.os.Debug.startMethodTracing(
456 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
458
459 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700463 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800465 registerContentObservers();
466
Joe Onorato7c312c12009-08-13 21:36:53 -0700467 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700468
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 mSavedState = savedInstanceState;
470 restoreState(mSavedState);
471
472 if (PROFILE_STARTUP) {
473 android.os.Debug.stopMethodTracing();
474 }
475
476 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700477 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 // If the user leaves launcher, then we should just load items asynchronously when
479 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700480 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700481 } else {
482 // We only load the page synchronously if the user rotates (or triggers a
483 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700484 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700485 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 }
487
488 // For handling default keys
489 mDefaultKeySsb = new SpannableStringBuilder();
490 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800491
492 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
493 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800494
Adam Cohenf9426d52012-06-04 17:26:21 -0700495 // On large interfaces, we want the screen to auto-rotate based on the current orientation
496 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700497
Adam Cohen9211d422014-10-07 18:14:53 -0700498 if (mLauncherCallbacks != null) {
499 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700500 if (mLauncherCallbacks.hasLauncherOverlay()) {
501 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700502 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
503 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
504 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700505 mWorkspace.setLauncherOverlay(mLauncherOverlay);
506 }
Adam Cohen9211d422014-10-07 18:14:53 -0700507 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700508
509 if (shouldShowIntroScreen()) {
510 showIntroScreen();
511 } else {
512 showFirstRunActivity();
513 showFirstRunClings();
514 }
Adam Cohen9211d422014-10-07 18:14:53 -0700515 }
516
517 private LauncherCallbacks mLauncherCallbacks;
518
519 public void onPostCreate(Bundle savedInstanceState) {
520 super.onPostCreate(savedInstanceState);
521 if (mLauncherCallbacks != null) {
522 mLauncherCallbacks.onPostCreate(savedInstanceState);
523 }
524 }
525
526 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
527 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700528 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
529 @Override
530 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700531 if (LOGD) {
532 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
533 }
Winson Chung94804152015-05-08 13:06:44 -0700534 mAppsView.setFixedBounds(bounds);
535 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700536 }
537
538 @Override
539 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700540 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
541 // Dismiss All Apps if we aren't already paused/invisible
542 if (!mPaused) {
543 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
544 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
545 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700546 }
Winson Chung0f785722015-04-08 10:27:49 -0700547 }
548 });
Adam Cohen9211d422014-10-07 18:14:53 -0700549 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700550 }
551
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700552 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700553 public void onLauncherProviderChange() {
554 if (mLauncherCallbacks != null) {
555 mLauncherCallbacks.onLauncherProviderChange();
556 }
557 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700558
Adam Cohen9211d422014-10-07 18:14:53 -0700559 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700560 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700561 if (mLauncherCallbacks != null) {
562 return mLauncherCallbacks.hasCustomContentToLeft();
563 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700564 return false;
565 }
566
Dave Hawkeya8881582013-09-17 15:55:33 -0600567 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500568 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600569 * {@link #addToCustomContentPage}. This will only be invoked if
570 * {@link #hasCustomContentToLeft()} is {@code true}.
571 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500572 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700573 if (mLauncherCallbacks != null) {
574 mLauncherCallbacks.populateCustomContentContainer();
575 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600576 }
577
578 /**
579 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
580 * ensure the custom content page is added or removed if necessary.
581 */
582 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600583 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600584 // Not bound yet, wait for bindScreens to be called.
585 return;
586 }
587
588 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
589 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500590 mWorkspace.createCustomContentContainer();
591 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600592 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
593 mWorkspace.removeCustomContentPage();
594 }
595 }
596
Adam Cohen091440a2015-03-18 14:16:05 -0700597 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700598 if (sLocaleConfiguration == null) {
599 new AsyncTask<Void, Void, LocaleConfiguration>() {
600 @Override
601 protected LocaleConfiguration doInBackground(Void... unused) {
602 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
603 readConfiguration(Launcher.this, localeConfiguration);
604 return localeConfiguration;
605 }
606
607 @Override
608 protected void onPostExecute(LocaleConfiguration result) {
609 sLocaleConfiguration = result;
610 checkForLocaleChange(); // recursive, but now with a locale configuration
611 }
612 }.execute();
613 return;
614 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700615
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 final Configuration configuration = getResources().getConfiguration();
617
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700618 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 final String locale = configuration.locale.toString();
620
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700621 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 final int mcc = configuration.mcc;
623
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700624 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 final int mnc = configuration.mnc;
626
Romain Guy84f296c2009-11-04 15:00:44 -0800627 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628
Romain Guy84f296c2009-11-04 15:00:44 -0800629 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700630 sLocaleConfiguration.locale = locale;
631 sLocaleConfiguration.mcc = mcc;
632 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700633
Joe Onorato0589f0f2010-02-08 13:44:00 -0800634 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700635
636 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100637 new AsyncTask<Void, Void, Void>() {
638 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700639 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100640 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700641 }
Michael Jurka43467462013-11-14 12:03:58 +0100642 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700643 }
644 }
645
Adam Cohen091440a2015-03-18 14:16:05 -0700646 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700647 public String locale;
648 public int mcc = -1;
649 public int mnc = -1;
650 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700651
Adam Cohen091440a2015-03-18 14:16:05 -0700652 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700653 DataInputStream in = null;
654 try {
Helena Josol28db2802014-10-09 17:04:09 +0100655 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700656 configuration.locale = in.readUTF();
657 configuration.mcc = in.readInt();
658 configuration.mnc = in.readInt();
659 } catch (FileNotFoundException e) {
660 // Ignore
661 } catch (IOException e) {
662 // Ignore
663 } finally {
664 if (in != null) {
665 try {
666 in.close();
667 } catch (IOException e) {
668 // Ignore
669 }
670 }
671 }
672 }
673
Adam Cohen091440a2015-03-18 14:16:05 -0700674 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700675 DataOutputStream out = null;
676 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100677 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100678 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700679 out.writeUTF(configuration.locale);
680 out.writeInt(configuration.mcc);
681 out.writeInt(configuration.mnc);
682 out.flush();
683 } catch (FileNotFoundException e) {
684 // Ignore
685 } catch (IOException e) {
686 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100687 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700688 } finally {
689 if (out != null) {
690 try {
691 out.close();
692 } catch (IOException e) {
693 // Ignore
694 }
695 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 }
697 }
698
Anton Hanssona2f665f2013-09-26 12:18:32 +0100699 public Stats getStats() {
700 return mStats;
701 }
702
Bjorn Bringertc459e522013-06-07 19:36:01 +0100703 public LayoutInflater getInflater() {
704 return mInflater;
705 }
706
Hyunyoung Song3f471442015-04-08 19:01:34 -0700707 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700708 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
709 // that is subsequently removed from the workspace in startBinding().
710 return !mModel.isLoadingWorkspace();
711 }
712
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800713 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000714 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100715 if (Build.VERSION.SDK_INT >= 17) {
716 return View.generateViewId();
717 } else {
718 // View.generateViewId() is not available. The following fallback logic is a copy
719 // of its implementation.
720 for (;;) {
721 final int result = sNextGeneratedId.get();
722 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
723 int newValue = result + 1;
724 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
725 if (sNextGeneratedId.compareAndSet(result, newValue)) {
726 return result;
727 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000728 }
729 }
730 }
731
732 public int getViewIdForItem(ItemInfo info) {
733 // This cast is safe given the > 2B range for int.
734 int itemId = (int) info.id;
735 if (mItemIdToViewId.containsKey(itemId)) {
736 return mItemIdToViewId.get(itemId);
737 }
738 int viewId = generateViewId();
739 mItemIdToViewId.put(itemId, viewId);
740 return viewId;
741 }
742
743 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700744 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
745 * a configuration step, this allows the proper animations to run after other transitions.
746 */
Adam Cohendb364c32014-05-20 14:23:40 -0700747 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700748 long screenId = args.screenId;
749 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
750 // When the screen id represents an actual screen (as opposed to a rank) we make sure
751 // that the drop page actually exists.
752 screenId = ensurePendingDropLayoutExists(args.screenId);
753 }
Adam Cohendb364c32014-05-20 14:23:40 -0700754
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800755 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800756 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700757 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700758 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700759 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800760 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700761 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700762 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700763 case REQUEST_RECONFIGURE_APPWIDGET:
764 completeRestoreAppWidget(args.appWidgetId);
765 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800766 }
Michael Jurka27614d22012-04-02 06:40:22 -0700767 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
768 // if you turned the screen off and then back while in All Apps, Launcher would not
769 // return to the workspace. Clearing mAddInfo.container here fixes this issue
770 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700771 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800772 }
773
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800774 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700775 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700776 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700777 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700778 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800779 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700780
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 Runnable exitSpringLoaded = new Runnable() {
782 @Override
783 public void run() {
784 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
785 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
786 }
787 };
788
Michael Jurka8b805b12012-04-18 14:23:14 -0700789 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700790 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700791 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
792 if (resultCode == RESULT_CANCELED) {
793 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700794 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700795 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700796 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800797 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700798 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700799 }
800 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200801 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
802 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700803 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200804 }
805 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700806 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200807
Adam Cohened66b2b2012-01-23 17:28:51 -0800808 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Hyunyoung Songb7900832015-05-15 16:29:45 -0700809 requestCode == REQUEST_CREATE_APPWIDGET || requestCode == REQUEST_CREATE_SHORTCUT);
Romain Guyaad5ef42009-06-10 02:48:37 -0700810
Adam Cohendb364c32014-05-20 14:23:40 -0700811 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800812 // We have special handling for widgets
813 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800814 final int appWidgetId;
815 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
816 : -1;
817 if (widgetId < 0) {
818 appWidgetId = pendingAddWidgetId;
819 } else {
820 appWidgetId = widgetId;
821 }
822
Adam Cohenad4e15c2013-10-17 16:21:35 -0700823 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800824 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700825 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
826 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700827 result = RESULT_CANCELED;
828 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700829 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700830 @Override
831 public void run() {
832 exitSpringLoadedDragModeDelayed(false, 0, null);
833 }
834 };
Adam Cohendb364c32014-05-20 14:23:40 -0700835 if (workspaceLocked) {
836 // No need to remove the empty screen if we're mid-binding, as the
837 // the bind will not add the empty screen.
838 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
839 } else {
840 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
841 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
842 }
Winson Chung5aaab772012-04-27 15:24:02 -0700843 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700844 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700845 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
846 // When the screen id represents an actual screen (as opposed to a rank)
847 // we make sure that the drop page actually exists.
848 mPendingAddInfo.screenId =
849 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
850 }
Adam Cohendb364c32014-05-20 14:23:40 -0700851 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
852
853 dropLayout.setDropPending(true);
854 final Runnable onComplete = new Runnable() {
855 @Override
856 public void run() {
857 completeTwoStageWidgetDrop(resultCode, appWidgetId);
858 dropLayout.setDropPending(false);
859 }
860 };
861 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
862 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
863 } else {
864 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
865 mPendingAddInfo);
866 sPendingAddItem = args;
867 }
Winson Chung5aaab772012-04-27 15:24:02 -0700868 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 return;
870 }
871
Sunny Goyalff572272014-07-23 13:58:07 -0700872 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
873 if (resultCode == RESULT_OK) {
874 // Update the widget view.
875 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
876 pendingAddWidgetId, mPendingAddInfo);
877 if (workspaceLocked) {
878 sPendingAddItem = args;
879 } else {
880 completeAdd(args);
881 }
882 }
883 // Leave the widget in the pending state if the user canceled the configure.
884 return;
885 }
886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 // The pattern used here is that a user PICKs a specific application,
888 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700889
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
891 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700892 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700893 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
894 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800895 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700896 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800897 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700898 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700899 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
900 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 }
Adam Cohen00074722013-10-11 17:46:09 -0700902 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700903 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700904 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800906 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800907
908 }
909
910 @Override
911 protected void onActivityResult(
912 final int requestCode, final int resultCode, final Intent data) {
913 handleActivityResult(requestCode, resultCode, data);
914 if (mLauncherCallbacks != null) {
915 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
916 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 }
918
Adam Cohendb364c32014-05-20 14:23:40 -0700919 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
920 appWidgetId, ItemInfo info) {
921 PendingAddArguments args = new PendingAddArguments();
922 args.requestCode = requestCode;
923 args.intent = data;
924 args.container = info.container;
925 args.screenId = info.screenId;
926 args.cellX = info.cellX;
927 args.cellY = info.cellY;
928 args.appWidgetId = appWidgetId;
929 return args;
930 }
931
932 /**
933 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
934 *
935 * @param screenId the screen id to check
936 * @return the new screen, or screenId if it exists
937 */
938 private long ensurePendingDropLayoutExists(long screenId) {
939 CellLayout dropLayout =
940 (CellLayout) mWorkspace.getScreenWithId(screenId);
941 if (dropLayout == null) {
942 // it's possible that the add screen was removed because it was
943 // empty and a re-bind occurred
944 mWorkspace.addExtraEmptyScreen();
945 return mWorkspace.commitExtraEmptyScreen();
946 } else {
947 return screenId;
948 }
949 }
950
Adam Cohen091440a2015-03-18 14:16:05 -0700951 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700952 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700953 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800954 Runnable onCompleteRunnable = null;
955 int animationType = 0;
956
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700957 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800958 if (resultCode == RESULT_OK) {
959 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
960 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700961 mPendingAddWidgetInfo);
962 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800963 onCompleteRunnable = new Runnable() {
964 @Override
965 public void run() {
966 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700967 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700968 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
969 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800970 }
971 };
972 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800973 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800974 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800975 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700976 if (mDragLayer.getAnimatedView() != null) {
977 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
978 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
979 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700980 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700981 // The animated view may be null in the case of a rotation during widget configuration
982 onCompleteRunnable.run();
983 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 }
985
986 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700987 protected void onStop() {
988 super.onStop();
989 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700990
991 if (mLauncherCallbacks != null) {
992 mLauncherCallbacks.onStop();
993 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700994 }
995
996 @Override
997 protected void onStart() {
998 super.onStart();
999 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -07001000
1001 if (mLauncherCallbacks != null) {
1002 mLauncherCallbacks.onStart();
1003 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001004 }
1005
1006 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001008 long startTime = 0;
1009 if (DEBUG_RESUME_TIME) {
1010 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001011 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001012 }
Adam Cohen9211d422014-10-07 18:14:53 -07001013
1014 if (mLauncherCallbacks != null) {
1015 mLauncherCallbacks.preOnResume();
1016 }
1017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001019
Winson Chung4a2afa32012-07-19 14:53:05 -07001020 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001021 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001022 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001023 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001024 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001025 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001026 // view after launching an app, as they may be depending on the UI to be static to
1027 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001028 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -07001029 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001030 } else if (mOnResumeState == State.WIDGETS) {
1031 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001032 }
1033 mOnResumeState = State.NONE;
1034
Winson Chungcd99cd32015-04-29 11:03:24 -07001035 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001036 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1037 // Otherwise, notify the callbacks if we are in all apps mode
1038 if (mState == State.APPS) {
1039 if (mLauncherCallbacks != null) {
1040 mLauncherCallbacks.onAllAppsShown();
1041 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001042 }
1043 }
1044
Craig Mautner360310b2012-10-26 15:13:08 -07001045 // Background was set to gradient in onPause(), restore to black if in all apps.
1046 setWorkspaceBackground(mState == State.WORKSPACE);
1047
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001048 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001049 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001050 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001051 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001052 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001053 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001055 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001056 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1057 // execute them here
1058 long startTimeCallbacks = 0;
1059 if (DEBUG_RESUME_TIME) {
1060 startTimeCallbacks = System.currentTimeMillis();
1061 }
1062
Michael Jurka1e2f4652013-07-08 18:03:46 -07001063 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1064 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001065 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001066 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001067 if (DEBUG_RESUME_TIME) {
1068 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1069 (System.currentTimeMillis() - startTimeCallbacks));
1070 }
Michael Jurka447bf842013-05-15 14:52:15 +02001071 }
Michael Jurka54554252013-08-01 12:52:23 +02001072 if (mOnResumeCallbacks.size() > 0) {
1073 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1074 mOnResumeCallbacks.get(i).run();
1075 }
1076 mOnResumeCallbacks.clear();
1077 }
Winson Chunge4e50662012-01-23 14:45:13 -08001078
1079 // Reset the pressed state of icons that were locked in the press state while activities
1080 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001081 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001082 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001083 mWaitingForResume.setStayPressed(false);
1084 }
Winson Chung82963d52013-10-09 11:20:57 -07001085
Adam Cohen06dff352012-06-01 17:17:08 -07001086 // It is possible that widgets can receive updates while launcher is not in the foreground.
1087 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1088 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1089 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001090 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001091 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001092
Michael Jurka447bf842013-05-15 14:52:15 +02001093 if (DEBUG_RESUME_TIME) {
1094 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1095 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001096
1097 if (mWorkspace.getCustomContentCallbacks() != null) {
1098 // If we are resuming and the custom content is the current page, we call onShow().
1099 // It is also poassible that onShow will instead be called slightly after first layout
1100 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1101 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001102 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001103 }
1104 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001105 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001106 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001107
Sunny Goyal756adbc2015-04-16 15:20:51 -07001108 if (!isWorkspaceLoading()) {
1109 // Process any items that were added while Launcher was away.
1110 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1111 }
Adam Cohen9211d422014-10-07 18:14:53 -07001112
1113 if (mLauncherCallbacks != null) {
1114 mLauncherCallbacks.onResume();
1115 }
Adam Cohen06dff352012-06-01 17:17:08 -07001116 }
1117
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001119 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001120 // Ensure that items added to Launcher are queued until Launcher returns
1121 InstallShortcutReceiver.enableInstallQueue();
1122
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001123 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001124 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001125 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001126 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001127
1128 // We call onHide() aggressively. The custom content callbacks should be able to
1129 // debounce excess onHide calls.
1130 if (mWorkspace.getCustomContentCallbacks() != null) {
1131 mWorkspace.getCustomContentCallbacks().onHide();
1132 }
Romain Guycbb89e42009-06-08 15:52:54 -07001133
Adam Cohen9211d422014-10-07 18:14:53 -07001134 if (mLauncherCallbacks != null) {
1135 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001136 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001137 }
1138
1139 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001140 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1141 // by a onResume or by scrolling otherwise.
1142 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001143
1144 // Custom content is completely hidden
1145 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001146
1147 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1148 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001149
1150 // Indicates whether the user is allowed to scroll away from the custom content.
1151 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001152 }
1153
Adam Cohenc2d6e892014-10-16 09:49:24 -07001154 public interface LauncherOverlay {
1155
1156 /**
1157 * Touch interaction leading to overscroll has begun
1158 */
1159 public void onScrollInteractionBegin();
1160
1161 /**
1162 * Touch interaction related to overscroll has ended
1163 */
1164 public void onScrollInteractionEnd();
1165
1166 /**
1167 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1168 * screen (or in the case of RTL, the rightmost screen).
1169 */
1170 public void onScrollChange(int progress, boolean rtl);
1171
1172 /**
1173 * Screen has stopped scrolling
1174 */
1175 public void onScrollSettled();
1176
1177 /**
1178 * This method can be called by the Launcher in order to force the LauncherOverlay
1179 * to exit fully immersive mode.
1180 */
1181 public void forceExitFullImmersion();
1182 }
1183
Winson Chung0f785722015-04-08 10:27:49 -07001184 public interface LauncherAppsCallbacks {
1185 /**
1186 * Updates launcher to the available space that AllApps can take so as not to overlap with
1187 * any other views.
1188 */
1189 public void onAllAppsBoundsChanged(Rect bounds);
1190
1191 /**
1192 * Called to dismiss all apps if it is showing.
1193 */
1194 public void dismissAllApps();
1195 }
1196
Adam Cohenc2d6e892014-10-16 09:49:24 -07001197 public interface LauncherOverlayCallbacks {
1198 /**
1199 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1200 * however it doesn't modify any state within the launcher.
1201 */
1202 public boolean canEnterFullImmersion();
1203
1204 /**
1205 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1206 * eg. by occupying the full screen and handling all touch events.
1207 *
1208 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1209 * case, Launcher will modify any necessary state and assumes the overlay is
1210 * handling all interaction. If false, the LauncherOverlay should cancel any
1211 *
1212 */
1213 public boolean enterFullImmersion();
1214
1215 /**
1216 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1217 * full control over UI and state.
1218 */
1219 public void exitFullImmersion();
1220 }
1221
1222 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1223
1224 @Override
1225 public boolean canEnterFullImmersion() {
1226 return mState == State.WORKSPACE;
1227 }
1228
1229 @Override
1230 public boolean enterFullImmersion() {
1231 if (mState == State.WORKSPACE) {
1232 // When fully immersed, disregard any touches which fall through.
1233 mDragLayer.setBlockTouch(true);
1234 return true;
1235 }
1236 return false;
1237 }
1238
1239 @Override
1240 public void exitFullImmersion() {
1241 mDragLayer.setBlockTouch(false);
1242 }
1243 }
1244
Jorim Jaggid017f882014-01-14 17:08:48 -08001245 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001246 if (mLauncherCallbacks != null) {
1247 return mLauncherCallbacks.hasSettings();
1248 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001249 return false;
1250 }
1251
Adam Cohen9211d422014-10-07 18:14:53 -07001252 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001253 CustomContentCallbacks callbacks, String description) {
1254 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001255 }
1256
Adam Cohenedb40762013-07-18 16:45:45 -07001257 // The custom content needs to offset its content to account for the QSB
1258 public int getTopOffsetForCustomContent() {
1259 return mWorkspace.getPaddingTop();
1260 }
1261
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001262 @Override
1263 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001264 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001265 if (mModel.isCurrentCallbacks(this)) {
1266 mModel.stopLoader();
1267 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001268 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1269
Romain Guy13c2e7b2010-03-10 19:45:00 -08001270 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001271 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001272
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001273 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001274 @Override
1275 public void onWindowFocusChanged(boolean hasFocus) {
1276 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001277 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001278
1279 if (mLauncherCallbacks != null) {
1280 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1281 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001282 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 private boolean acceptFilter() {
1285 final InputMethodManager inputManager = (InputMethodManager)
1286 getSystemService(Context.INPUT_METHOD_SERVICE);
1287 return !inputManager.isFullscreenMode();
1288 }
1289
1290 @Override
1291 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001292 final int uniChar = event.getUnicodeChar();
1293 final boolean handled = super.onKeyDown(keyCode, event);
1294 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1295 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1297 keyCode, event);
1298 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001299 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001300 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001301 // showSearchDialog()
1302 // If there are multiple keystrokes before the search dialog takes focus,
1303 // onSearchRequested() will be called for every keystroke,
1304 // but it is idempotent, so it's fine.
1305 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
1307 }
1308
Joe Onorato8a9625e2010-01-28 15:55:35 -08001309 // Eat the long press event so the keyboard doesn't come up.
1310 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1311 return true;
1312 }
1313
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 return handled;
1315 }
1316
Karl Rosaen138a0412009-04-23 19:00:21 -07001317 private String getTypedText() {
1318 return mDefaultKeySsb.toString();
1319 }
1320
1321 private void clearTypedText() {
1322 mDefaultKeySsb.clear();
1323 mDefaultKeySsb.clearSpans();
1324 Selection.setSelection(mDefaultKeySsb, 0);
1325 }
1326
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001328 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1329 * State
1330 */
1331 private static State intToState(int stateOrdinal) {
1332 State state = State.WORKSPACE;
1333 final State[] stateValues = State.values();
1334 for (int i = 0; i < stateValues.length; i++) {
1335 if (stateValues[i].ordinal() == stateOrdinal) {
1336 state = stateValues[i];
1337 break;
1338 }
1339 }
1340 return state;
1341 }
1342
1343 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 * Restores the previous state, if it exists.
1345 *
1346 * @param savedState The previous state.
1347 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001348 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 private void restoreState(Bundle savedState) {
1350 if (savedState == null) {
1351 return;
1352 }
1353
Michael Jurka883f55b2010-10-21 15:47:14 -07001354 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001355 if (state == State.APPS || state == State.WIDGETS) {
1356 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001357 }
1358
Adam Cohen21cd0022013-10-09 18:57:02 -07001359 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1360 PagedView.INVALID_RESTORE_PAGE);
1361 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001362 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
1364
Winson Chung3d503fb2011-07-13 17:25:49 -07001365 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001366 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001367
Winson Chung3d503fb2011-07-13 17:25:49 -07001368 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1369 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001370 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001371 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1372 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001373 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1374 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001375 AppWidgetProviderInfo info = savedState.getParcelable(
1376 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001377 mPendingAddWidgetInfo = info == null ?
1378 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1379
Adam Cohen4637b5a2013-11-04 18:21:24 -08001380 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001381 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 mRestoring = true;
1383 }
1384
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001385 mItemIdToViewId = (HashMap<Integer, Integer>)
1386 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
1388
1389 /**
1390 * Finds all the views we need and configure them properly.
1391 */
1392 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001393 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001394
Craig Mautner360310b2012-10-26 15:13:08 -07001395 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001396 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001397 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1398 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001399 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001400 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001401
John Spurlock77e1f472013-09-11 10:09:51 -04001402 mLauncherView.setSystemUiVisibility(
1403 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001404 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405
Winson Chung4c98d922011-05-31 16:50:48 -07001406 // Setup the drag layer
1407 mDragLayer.setup(this, dragController);
1408
Winson Chung3d503fb2011-07-13 17:25:49 -07001409 // Setup the hotseat
1410 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1411 if (mHotseat != null) {
1412 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001413 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001414 }
1415
Jorim Jaggid017f882014-01-14 17:08:48 -08001416 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001417 View widgetButton = findViewById(R.id.widget_button);
1418 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001419 @Override
1420 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001421 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001422 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001423 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001424 }
1425 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001426 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427
1428 View wallpaperButton = findViewById(R.id.wallpaper_button);
1429 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001430 @Override
1431 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001432 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001433 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001434 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001435 }
1436 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001437 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1438
1439 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001440 if (hasSettings()) {
1441 settingsButton.setOnClickListener(new OnClickListener() {
1442 @Override
1443 public void onClick(View arg0) {
1444 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001445 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001446 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001447 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001448 });
1449 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1450 } else {
1451 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001452 }
1453
Adam Cohendbdff6b2013-09-18 19:09:15 -07001454 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001455
Winson Chung4c98d922011-05-31 16:50:48 -07001456 // Setup the workspace
1457 mWorkspace.setHapticFeedbackEnabled(false);
1458 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001459 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001460 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001461
Winson Chungf0ea4d32011-06-06 14:27:16 -07001462 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001463 mSearchDropTargetBar = (SearchDropTargetBar)
1464 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001465
Winson Chungb745afb2015-03-02 11:51:23 -08001466 // Setup Apps
1467 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001468 if (isAllAppsSearchOverridden()) {
1469 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001470 }
Winson Chungb745afb2015-03-02 11:51:23 -08001471
Winson Chungf0ea4d32011-06-06 14:27:16 -07001472 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001473 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001474
Winson Chung3d503fb2011-07-13 17:25:49 -07001475 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001476 dragController.setDragScoller(mWorkspace);
1477 dragController.setScrollView(mDragLayer);
1478 dragController.setMoveTarget(mWorkspace);
1479 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001480 if (mSearchDropTargetBar != null) {
1481 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001482 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001483 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001484
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001485 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001486 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001487 mWeightWatcher = new WeightWatcher(this);
1488 mWeightWatcher.setAlpha(0.5f);
1489 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001490 new FrameLayout.LayoutParams(
1491 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001492 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001493 Gravity.BOTTOM)
1494 );
Adam Cohen39a06042013-07-19 14:30:12 -07001495
1496 boolean show = shouldShowWeightWatcher();
1497 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 }
1500
1501 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001502 * Sets the all apps button. This method is called from {@link Hotseat}.
1503 */
1504 public void setAllAppsButton(View allAppsButton) {
1505 mAllAppsButton = allAppsButton;
1506 }
1507
1508 public View getAllAppsButton() {
1509 return mAllAppsButton;
1510 }
1511
1512 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 * Creates a view representing a shortcut.
1514 *
1515 * @param info The data structure describing the shortcut.
1516 *
1517 * @return A View inflated from R.layout.application.
1518 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001519 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001520 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 }
1522
1523 /**
1524 * Creates a view representing a shortcut inflated from the specified resource.
1525 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 * @param parent The group the shortcut belongs to.
1527 * @param info The data structure describing the shortcut.
1528 *
1529 * @return A View inflated from layoutResId.
1530 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001531 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
1532 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.application,
1533 parent, false);
1534 favorite.applyFromShortcutInfo(info, mIconCache);
1535 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001537 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 return favorite;
1539 }
1540
1541 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 * Add a shortcut to the workspace.
1543 *
1544 * @param data The intent describing the shortcut.
1545 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001548 int cellY) {
1549 int[] cellXY = mTmpAddItemCellCoordinates;
1550 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001551 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001552
Michael Jurkad3ef3062010-11-23 16:23:58 -08001553 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001554
Sunny Goyal2350bc92014-10-14 16:42:54 -07001555 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001556 if (info == null) {
1557 return;
1558 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001559 final View view = createShortcut(info);
1560
Adam Cohenfbba09b2011-07-18 21:43:05 -07001561 // First we check if we already know the exact location where we want to add this item.
1562 if (cellX >= 0 && cellY >= 0) {
1563 cellXY[0] = cellX;
1564 cellXY[1] = cellY;
1565 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001566
1567 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001568 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001569 true, null,null)) {
1570 return;
1571 }
1572 DragObject dragObject = new DragObject();
1573 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001574 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1575 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001576 return;
1577 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001578 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001579 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001580 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001581 foundCellSpan = (result != null);
1582 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001583 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001584 }
1585
1586 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001587 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001588 return;
1589 }
1590
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001591 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592
1593 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001594 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001595 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 }
1597 }
1598
Adam Cohen2f093b62012-04-30 18:59:53 -07001599 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1600 int minHeight) {
1601 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001602 // We want to account for the extra amount of padding that we are adding to the widget
1603 // to ensure that it gets the full amount of space that it has requested
1604 int requiredWidth = minWidth + padding.left + padding.right;
1605 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001606 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001607 }
1608
Adam Cohen2f093b62012-04-30 18:59:53 -07001609 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1610 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001611 }
1612
Adam Cohen2f093b62012-04-30 18:59:53 -07001613 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1614 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001615 }
1616
Adam Cohen2f093b62012-04-30 18:59:53 -07001617 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1618 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001619 }
1620
Adam Cohen2f093b62012-04-30 18:59:53 -07001621 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1622 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001623 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001624 }
1625
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001627 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001629 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 */
Adam Cohen091440a2015-03-18 14:16:05 -07001631 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001632 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1633
1634 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001635 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001636 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1637 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001638 }
Romain Guycbb89e42009-06-08 15:52:54 -07001639
Adam Cohen59400422014-03-05 18:07:04 -08001640 if (appWidgetInfo.isCustomWidget) {
1641 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001642 }
1643
Adam Cohen59400422014-03-05 18:07:04 -08001644 LauncherAppWidgetInfo launcherInfo;
1645 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1646 launcherInfo.spanX = info.spanX;
1647 launcherInfo.spanY = info.spanY;
1648 launcherInfo.minSpanX = info.minSpanX;
1649 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001650 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001653 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654
1655 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001656 if (hostView == null) {
1657 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001658 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1659 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001660 } else {
1661 // The AppWidgetHostView has already been inflated and instantiated
1662 launcherInfo.hostView = hostView;
1663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001665 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001666 launcherInfo.notifyWidgetSizeChanged(this);
1667
Adam Cohen59400422014-03-05 18:07:04 -08001668 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1669 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001670
1671 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001673 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 }
Romain Guycbb89e42009-06-08 15:52:54 -07001675
Adam Cohended9f8d2010-11-03 13:25:16 -07001676 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1677 @Override
1678 public void onReceive(Context context, Intent intent) {
1679 final String action = intent.getAction();
1680 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1681 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001682 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001683 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001684
Winson Chungc100e8e2011-08-09 16:02:43 -07001685 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001686 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001687 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001688 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001689 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001690 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001691 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1692 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001693 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001694 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1695 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001696 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001697 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1698 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1699 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001700 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001701 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1702 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001703 }
1704 }
1705 };
1706
1707 @Override
1708 public void onAttachedToWindow() {
1709 super.onAttachedToWindow();
1710
1711 // Listen for broadcasts related to user-presence
1712 final IntentFilter filter = new IntentFilter();
1713 filter.addAction(Intent.ACTION_SCREEN_OFF);
1714 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001715 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001716 if (ENABLE_DEBUG_INTENTS) {
1717 filter.addAction(DebugIntents.DELETE_DATABASE);
1718 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1719 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001720 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001721 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001722 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001723 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 mVisible = true;
1725 }
1726
Adam Cohen0b395352014-06-09 22:54:36 +00001727 /**
1728 * Sets up transparent navigation and status bars in LMP.
1729 * This method is a no-op for other platform versions.
1730 */
Sunny Goyald0091012015-01-20 15:55:34 -08001731 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001732 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001733 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001734 Window window = getWindow();
1735 window.getAttributes().systemUiVisibility |=
1736 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1737 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1738 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1739 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1740 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1741 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1742 window.setStatusBarColor(Color.TRANSPARENT);
1743 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001744 }
1745 }
1746
Adam Cohended9f8d2010-11-03 13:25:16 -07001747 @Override
1748 public void onDetachedFromWindow() {
1749 super.onDetachedFromWindow();
1750 mVisible = false;
1751
Adam Cohend113e0c2010-11-11 10:48:05 -08001752 if (mAttached) {
1753 unregisterReceiver(mReceiver);
1754 mAttached = false;
1755 }
Winson Chungb745afb2015-03-02 11:51:23 -08001756 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001757 }
1758
1759 public void onWindowVisibilityChanged(int visibility) {
1760 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001761 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001762 // The following code used to be in onResume, but it turns out onResume is called when
1763 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1764 // is a more appropriate event to handle
1765 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001766 if (!mWorkspaceLoading) {
1767 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001768 // We want to let Launcher draw itself at least once before we force it to build
1769 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001770 // apps is nice and speedy.
1771 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001772 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001773 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001774 if (mStarted) return;
1775 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001776 // We delay the layer building a bit in order to give
1777 // other message processing a time to run. In particular
1778 // this avoids a delay in hiding the IME if it was
1779 // currently shown, because doing that may involve
1780 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001781 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001782 final ViewTreeObserver.OnDrawListener listener = this;
1783 mWorkspace.post(new Runnable() {
1784 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001785 if (mWorkspace != null &&
1786 mWorkspace.getViewTreeObserver() != null) {
1787 mWorkspace.getViewTreeObserver().
1788 removeOnDrawListener(listener);
1789 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001790 }
1791 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001792 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001793 }
1794 });
1795 }
1796 clearTypedText();
1797 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001798 }
1799
Adam Cohen091440a2015-03-18 14:16:05 -07001800 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001801 mHandler.removeMessages(ADVANCE_MSG);
1802 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1803 mHandler.sendMessageDelayed(msg, delay);
1804 mAutoAdvanceSentTime = System.currentTimeMillis();
1805 }
1806
Adam Cohen091440a2015-03-18 14:16:05 -07001807 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001808 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1809 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1810 mAutoAdvanceRunning = autoAdvanceRunning;
1811 if (autoAdvanceRunning) {
1812 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1813 sendAdvanceMessage(delay);
1814 } else {
1815 if (!mWidgetsToAdvance.isEmpty()) {
1816 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1817 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1818 }
1819 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001820 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001821 }
1822 }
1823 }
1824
1825 private final Handler mHandler = new Handler() {
1826 @Override
1827 public void handleMessage(Message msg) {
1828 if (msg.what == ADVANCE_MSG) {
1829 int i = 0;
1830 for (View key: mWidgetsToAdvance.keySet()) {
1831 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1832 final int delay = mAdvanceStagger * i;
1833 if (v instanceof Advanceable) {
1834 postDelayed(new Runnable() {
1835 public void run() {
1836 ((Advanceable) v).advance();
1837 }
1838 }, delay);
1839 }
1840 i++;
1841 }
1842 sendAdvanceMessage(mAdvanceInterval);
1843 }
1844 }
1845 };
1846
1847 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001848 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001849 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1850 if (v instanceof Advanceable) {
1851 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001852 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001853 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001854 }
1855 }
1856
1857 void removeWidgetToAutoAdvance(View hostView) {
1858 if (mWidgetsToAdvance.containsKey(hostView)) {
1859 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001860 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001861 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001862 }
1863
Joe Onorato9c1289c2009-08-17 11:03:03 -04001864 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001865 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001866 launcherInfo.hostView = null;
1867 }
1868
Hyunyoung Song3f471442015-04-08 19:01:34 -07001869 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001870 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1871 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001872 }
1873
Winson Chunga6945242014-01-08 14:04:34 -08001874 public DragLayer getDragLayer() {
1875 return mDragLayer;
1876 }
1877
Winson Chungb745afb2015-03-02 11:51:23 -08001878 public AppsContainerView getAppsView() {
1879 return mAppsView;
1880 }
1881
Hyunyoung Song3f471442015-04-08 19:01:34 -07001882 public WidgetsContainerView getWidgetsView() {
1883 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001884 }
1885
Winson Chunga6945242014-01-08 14:04:34 -08001886 public Workspace getWorkspace() {
1887 return mWorkspace;
1888 }
1889
1890 public Hotseat getHotseat() {
1891 return mHotseat;
1892 }
1893
Jorim Jaggid017f882014-01-14 17:08:48 -08001894 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001895 return mOverviewPanel;
1896 }
1897
1898 public SearchDropTargetBar getSearchBar() {
1899 return mSearchDropTargetBar;
1900 }
1901
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001902 public LauncherAppWidgetHost getAppWidgetHost() {
1903 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 }
Romain Guycbb89e42009-06-08 15:52:54 -07001905
Winson Chunga9abd0e2010-10-27 17:18:37 -07001906 public LauncherModel getModel() {
1907 return mModel;
1908 }
1909
Winson Chunga6945242014-01-08 14:04:34 -08001910 protected SharedPreferences getSharedPrefs() {
1911 return mSharedPrefs;
1912 }
1913
Bjorn Bringertc459e522013-06-07 19:36:01 +01001914 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001915 getWindow().closeAllPanels();
1916
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001917 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001918 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001919 }
1920
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 @Override
1922 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001923 long startTime = 0;
1924 if (DEBUG_RESUME_TIME) {
1925 startTime = System.currentTimeMillis();
1926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 super.onNewIntent(intent);
1928
1929 // Close the menu
1930 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001931 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001932 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001933
Adam Cohened307df2013-10-02 09:37:31 -07001934 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1935 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1936 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001937
Adam Cohen6fecd412013-10-02 17:41:50 -07001938 if (mWorkspace == null) {
1939 // Can be cases where mWorkspace is null, this prevents a NPE
1940 return;
1941 }
1942 Folder openFolder = mWorkspace.getOpenFolder();
1943 // In all these cases, only animate if we're already on home
1944 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001945
1946 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1947 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001948 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001949 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001950 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001951 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001952
Adam Cohen6fecd412013-10-02 17:41:50 -07001953 closeFolder();
1954 exitSpringLoadedDragMode();
1955
1956 // If we are already on home, then just animate back to the workspace,
1957 // otherwise, just wait until onResume to set the state back to Workspace
1958 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001959 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001960 } else {
1961 mOnResumeState = State.WORKSPACE;
1962 }
1963
1964 final View v = getWindow().peekDecorView();
1965 if (v != null && v.getWindowToken() != null) {
1966 InputMethodManager imm = (InputMethodManager)getSystemService(
1967 INPUT_METHOD_SERVICE);
1968 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1969 }
1970
Winson Chungb745afb2015-03-02 11:51:23 -08001971 // Reset the apps view
1972 if (!alreadyOnHome && mAppsView != null) {
1973 mAppsView.scrollToTop();
1974 }
1975
Hyunyoung Song3f471442015-04-08 19:01:34 -07001976 // Reset the widgets view
1977 if (!alreadyOnHome && mWidgetsView != null) {
1978 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001979 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001980
Adam Cohen9211d422014-10-07 18:14:53 -07001981 if (mLauncherCallbacks != null) {
1982 mLauncherCallbacks.onHomeIntent();
1983 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 }
Adam Cohened307df2013-10-02 09:37:31 -07001985
Michael Jurka447bf842013-05-15 14:52:15 +02001986 if (DEBUG_RESUME_TIME) {
1987 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989
Adam Cohen9211d422014-10-07 18:14:53 -07001990 if (mLauncherCallbacks != null) {
1991 mLauncherCallbacks.onNewIntent(intent);
1992 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001993 }
1994
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001996 public void onRestoreInstanceState(Bundle state) {
1997 super.onRestoreInstanceState(state);
1998 for (int page: mSynchronouslyBoundPages) {
1999 mWorkspace.restoreInstanceStateForChild(page);
2000 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 }
2002
2003 @Override
2004 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002005 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002006 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2007 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002008 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002009 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010
Michael Jurka883f55b2010-10-21 15:47:14 -07002011 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002012 // We close any open folder since it will not be re-opened, and we need to make sure
2013 // this state is reflected.
2014 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015
Adam Cohendcd297f2013-06-18 13:13:40 -07002016 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002017 mWaitingForResult) {
2018 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002019 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002020 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2021 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002022 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2023 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2024 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002025 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 }
2027
Hyunyoung Song3f471442015-04-08 19:01:34 -07002028 // Save the current widgets tray?
2029 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002030 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002031
2032 if (mLauncherCallbacks != null) {
2033 mLauncherCallbacks.onSaveInstanceState(outState);
2034 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 }
2036
2037 @Override
2038 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002040
Winson Chunge7a03942011-08-05 15:05:12 -07002041 // Remove all pending runnables
2042 mHandler.removeMessages(ADVANCE_MSG);
2043 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002044 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002045
Winson Chungcd2b0142011-06-08 16:02:26 -07002046 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002047 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002048
2049 // It's possible to receive onDestroy after a new Launcher activity has
2050 // been created. In this case, don't interfere with the new Launcher.
2051 if (mModel.isCurrentCallbacks(this)) {
2052 mModel.stopLoader();
2053 app.setLauncher(null);
2054 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002055
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002057 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002059 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002061 mAppWidgetHost = null;
2062
2063 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064
2065 TextKeyListener.getInstance().release();
2066
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002067 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002068 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002069
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002070 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002071 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002072 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002073 mWorkspace = null;
2074 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002075
Michael Jurka2ecf9952012-06-18 12:52:28 -07002076 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002077
2078 if (mLauncherCallbacks != null) {
2079 mLauncherCallbacks.onDestroy();
2080 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 }
2082
Adam Cohena9cf38f2011-05-02 15:36:58 -07002083 public DragController getDragController() {
2084 return mDragController;
2085 }
2086
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 @Override
2088 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002089 onStartForResult(requestCode);
2090 super.startActivityForResult(intent, requestCode);
2091 }
2092
2093 @Override
2094 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2095 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2096 onStartForResult(requestCode);
2097 try {
2098 super.startIntentSenderForResult(intent, requestCode,
2099 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2100 } catch (IntentSender.SendIntentException e) {
2101 throw new ActivityNotFoundException();
2102 }
2103 }
2104
2105 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002106 if (requestCode >= 0) {
2107 setWaitingForResult(true);
2108 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 }
2110
Winson Chung70d72102011-08-12 11:24:35 -07002111 /**
2112 * Indicates that we want global search for this activity by setting the globalSearch
2113 * argument for {@link #startSearch} to true.
2114 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002116 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002118
Karl Rosaen138a0412009-04-23 19:00:21 -07002119 if (initialQuery == null) {
2120 // Use any text typed in the launcher as the initial query
2121 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 if (appSearchData == null) {
2124 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002125 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 }
Winson Chungadf0c182012-08-23 14:59:07 -07002127 Rect sourceBounds = new Rect();
2128 if (mSearchDropTargetBar != null) {
2129 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2130 }
Romain Guycbb89e42009-06-08 15:52:54 -07002131
Ian Parkinson047036e2014-09-01 15:40:53 +01002132 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002133 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002134 if (clearTextImmediately) {
2135 clearTypedText();
2136 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002137
2138 // We need to show the workspace after starting the search
2139 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002140 }
2141
Ian Parkinson047036e2014-09-01 15:40:53 +01002142 /**
2143 * Start a text search.
2144 *
2145 * @return {@code true} if the search will start immediately, so any further keypresses
2146 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2147 * to buffer keypresses.
2148 */
2149 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002150 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002151 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2152 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2153 sourceBounds);
2154 }
2155
Michael Jurkaa33411c2012-06-14 16:18:21 -07002156 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002157 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002158 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002159 }
2160
2161 /**
2162 * Starts the global search activity. This code is a copied from SearchManager
2163 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002164 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002165 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002166 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002167 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2168 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2169 if (globalSearchActivity == null) {
2170 Log.w(TAG, "No global search activity found.");
2171 return;
2172 }
2173 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2174 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2175 intent.setComponent(globalSearchActivity);
2176 // Make sure that we have a Bundle to put source in
2177 if (appSearchData == null) {
2178 appSearchData = new Bundle();
2179 } else {
2180 appSearchData = new Bundle(appSearchData);
2181 }
Adam Cohen9211d422014-10-07 18:14:53 -07002182 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002183 if (!appSearchData.containsKey("source")) {
2184 appSearchData.putString("source", getPackageName());
2185 }
2186 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2187 if (!TextUtils.isEmpty(initialQuery)) {
2188 intent.putExtra(SearchManager.QUERY, initialQuery);
2189 }
2190 if (selectInitialQuery) {
2191 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2192 }
2193 intent.setSourceBounds(sourceBounds);
2194 try {
2195 startActivity(intent);
2196 } catch (ActivityNotFoundException ex) {
2197 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2198 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 }
2200
Yura4f93ec62014-02-04 14:15:21 +00002201 public boolean isOnCustomContent() {
2202 return mWorkspace.isOnOrMovingToCustomContent();
2203 }
2204
Winson Chung70d72102011-08-12 11:24:35 -07002205 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002206 public boolean onPrepareOptionsMenu(Menu menu) {
2207 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002208 if (!isOnCustomContent()) {
2209 // Close any open folders
2210 closeFolder();
2211 // Stop resizing any widgets
2212 mWorkspace.exitWidgetResizeMode();
2213 if (!mWorkspace.isInOverviewMode()) {
2214 // Show the overview mode
2215 showOverviewMode(true);
2216 } else {
2217 showWorkspace(true);
2218 }
Winson Chunge0298742014-01-17 12:03:00 -08002219 }
Adam Cohen9211d422014-10-07 18:14:53 -07002220 if (mLauncherCallbacks != null) {
2221 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2222 }
2223
Michael Jurkab94f3f82013-09-11 18:08:54 +02002224 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002227 @Override
2228 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002229 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002230 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002231 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002232 }
2233
Joe Onorato9c1289c2009-08-17 11:03:03 -04002234 public boolean isWorkspaceLocked() {
2235 return mWorkspaceLoading || mWaitingForResult;
2236 }
2237
Adam Cohen517a7f52014-03-01 12:12:59 -08002238 public boolean isWorkspaceLoading() {
2239 return mWorkspaceLoading;
2240 }
2241
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002242 private void setWorkspaceLoading(boolean value) {
2243 boolean isLocked = isWorkspaceLocked();
2244 mWorkspaceLoading = value;
2245 if (isLocked != isWorkspaceLocked()) {
2246 onWorkspaceLockedChanged();
2247 }
2248 }
2249
2250 private void setWaitingForResult(boolean value) {
2251 boolean isLocked = isWorkspaceLocked();
2252 mWaitingForResult = value;
2253 if (isLocked != isWorkspaceLocked()) {
2254 onWorkspaceLockedChanged();
2255 }
2256 }
2257
Adam Cohen9211d422014-10-07 18:14:53 -07002258 protected void onWorkspaceLockedChanged() {
2259 if (mLauncherCallbacks != null) {
2260 mLauncherCallbacks.onWorkspaceLockedChanged();
2261 }
2262 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002263
Michael Jurka0280c3b2010-09-17 15:00:07 -07002264 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002265 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002266 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002267 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2268 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002269 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002270 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002271 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002272
Sunny Goyale6b63a32015-01-16 16:14:29 -08002273 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002274 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002275 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2276 }
2277
Sunny Goyale6b63a32015-01-16 16:14:29 -08002278 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002279 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2280 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002281 if (appWidgetInfo.configure != null) {
2282 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002283 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002284
Bjorn Bringert7984c942009-12-09 15:38:25 +00002285 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002286 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2287 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2288
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002289 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002290 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002291 Runnable onComplete = new Runnable() {
2292 @Override
2293 public void run() {
2294 // Exit spring loaded mode if necessary after adding the widget
2295 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2296 null);
2297 }
2298 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002299 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002300 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002301 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002302 }
2303 }
Romain Guycbb89e42009-06-08 15:52:54 -07002304
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002305 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002306 // Close any folders that may be open.
2307 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002308 mWorkspace.moveToCustomContentScreen(animate);
2309 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002310
2311 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2312 int[] cell, int spanX, int spanY) {
2313 switch (info.itemType) {
2314 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2315 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2316 int span[] = new int[2];
2317 span[0] = spanX;
2318 span[1] = spanY;
2319 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2320 container, screenId, cell, span);
2321 break;
2322 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2323 processShortcutFromDrop(info.componentName, container, screenId, cell);
2324 break;
2325 default:
2326 throw new IllegalStateException("Unknown item type: " + info.itemType);
2327 }
2328 }
2329
Adam Cohenfbba09b2011-07-18 21:43:05 -07002330 /**
2331 * Process a shortcut drop.
2332 *
2333 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002334 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002335 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002336 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002337 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2338 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002339 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002341 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002342 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002343
2344 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002345 mPendingAddInfo.cellX = cell[0];
2346 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002347 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002348
2349 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2350 createShortcutIntent.setComponent(componentName);
2351 processShortcut(createShortcutIntent);
2352 }
2353
Adam Cohenfbba09b2011-07-18 21:43:05 -07002354 /**
2355 * Process a widget drop.
2356 *
2357 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002358 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002359 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002360 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002361 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2362 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002363 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002364 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002365 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002366 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002367 mPendingAddInfo.minSpanX = info.minSpanX;
2368 mPendingAddInfo.minSpanY = info.minSpanY;
2369
Adam Cohenfbba09b2011-07-18 21:43:05 -07002370 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002371 mPendingAddInfo.cellX = cell[0];
2372 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002373 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002374 if (span != null) {
2375 mPendingAddInfo.spanX = span[0];
2376 mPendingAddInfo.spanY = span[1];
2377 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002378
Adam Cohened66b2b2012-01-23 17:28:51 -08002379 AppWidgetHostView hostView = info.boundWidget;
2380 int appWidgetId;
2381 if (hostView != null) {
2382 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002383 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002384 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002385 // In this case, we either need to start an activity to get permission to bind
2386 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002387 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002388 Bundle options = info.bindOptions;
2389
Sunny Goyalffe83f12014-08-14 17:39:34 -07002390 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2391 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002392 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002393 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002394 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002395 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002396 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2397 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2398 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002399 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2400 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002401 // TODO: we need to make sure that this accounts for the options bundle.
2402 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002403 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2404 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002405 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002406 }
2407
Joe Onoratodeb98af2010-02-19 14:59:39 -08002408 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002409 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 }
2411
Winson Chung24ab2f12010-09-16 14:10:47 -07002412 void processWallpaper(Intent intent) {
2413 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2414 }
2415
Adam Cohendcd297f2013-06-18 13:13:40 -07002416 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002417 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002418 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 folderInfo.title = getText(R.string.folder_name);
2420
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002422 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2423 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002424 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425
2426 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002427 FolderIcon newFolder =
2428 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002429 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002430 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002431 // Force measure the new folder icon
2432 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2433 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002434 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 }
Romain Guycbb89e42009-06-08 15:52:54 -07002436
Joe Onorato9c1289c2009-08-17 11:03:03 -04002437 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002438 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002439 }
2440
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002441 /**
2442 * Registers various content observers. The current implementation registers
2443 * only a favorites observer to keep track of the favorites applications.
2444 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002445 private void registerContentObservers() {
2446 ContentResolver resolver = getContentResolver();
2447 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2448 true, mWidgetObserver);
2449 }
2450
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 @Override
2452 public boolean dispatchKeyEvent(KeyEvent event) {
2453 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2454 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002456 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002457 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002458 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002459 dumpState();
2460 return true;
2461 }
2462 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002463 }
2464 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2465 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002466 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002467 return true;
2468 }
2469 }
2470
2471 return super.dispatchKeyEvent(event);
2472 }
2473
Joe Onorato88ec0992009-11-19 13:16:06 -08002474 @Override
2475 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002476 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2477 return;
2478 }
2479
Winson Chung3d9490a2015-03-24 17:38:42 -07002480 LauncherAccessibilityDelegate delegate =
2481 LauncherAppState.getInstance().getAccessibilityDelegate();
2482 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002483 return;
2484 }
2485
Winson Chungb745afb2015-03-02 11:51:23 -08002486 if (isAppsViewVisible()) {
2487 showWorkspace(true);
2488 } else if (isWidgetsViewVisible()) {
2489 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002490 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002491 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002492 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002493 Folder openFolder = mWorkspace.getOpenFolder();
2494 if (openFolder.isEditingName()) {
2495 openFolder.dismissEditingName();
2496 } else {
2497 closeFolder();
2498 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002499 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002500 mWorkspace.exitWidgetResizeMode();
2501
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002502 // Back button is a no-op here, but give at least some feedback for the button press
2503 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002504 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002505 }
2506
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002508 * Re-listen when widgets are reset.
2509 */
Adam Cohen091440a2015-03-18 14:16:05 -07002510 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002511 if (mAppWidgetHost != null) {
2512 mAppWidgetHost.startListening();
2513 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002514 }
2515
2516 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517 * Launches the intent referred by the clicked shortcut.
2518 *
2519 * @param v The view representing the clicked shortcut.
2520 */
2521 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002522 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2523 // view has detached (it's possible for this to happen if the view is removed mid touch).
2524 if (v.getWindowToken() == null) {
2525 return;
2526 }
2527
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002528 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002529 return;
2530 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002531
Adam Cohen1697b792013-09-17 19:08:21 -07002532 if (v instanceof Workspace) {
2533 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002534 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002535 }
2536 return;
2537 }
2538
2539 if (v instanceof CellLayout) {
2540 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002541 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002542 }
2543 }
2544
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002546 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002547 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002549 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002550 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002551 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002552 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002553 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002554 } else if (tag instanceof AppInfo) {
2555 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002556 } else if (tag instanceof LauncherAppWidgetInfo) {
2557 if (v instanceof PendingAppWidgetHostView) {
2558 onClickPendingWidget((PendingAppWidgetHostView) v);
2559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 }
2561 }
2562
Sunny Goyal508da152014-08-14 10:53:27 -07002563 public void onClickPagedViewIcon(View v) {
2564 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002565 if (mLauncherCallbacks != null) {
2566 mLauncherCallbacks.onClickPagedViewIcon(v);
2567 }
Sunny Goyal508da152014-08-14 10:53:27 -07002568 }
2569
Sunny Goyal70660032015-05-14 00:07:08 -07002570 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002571 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002572 return false;
2573 }
2574
Michael Jurkaaf442092010-06-10 17:01:57 -07002575 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002576 * Event handler for the app widget view which has not fully restored.
2577 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002578 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002579 if (mIsSafeModeEnabled) {
2580 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2581 return;
2582 }
2583
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002584 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002585 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002586 int widgetId = info.appWidgetId;
2587 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2588 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002589 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2590 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002591 mPendingAddInfo.copyFrom(info);
2592 mPendingAddWidgetId = widgetId;
2593
Sunny Goyalffe83f12014-08-14 17:39:34 -07002594 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2595 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002596 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002597 } else if (info.installProgress < 0) {
2598 // The install has not been queued
2599 final String packageName = info.providerName.getPackageName();
2600 showBrokenAppInstallDialog(packageName,
2601 new DialogInterface.OnClickListener() {
2602 public void onClick(DialogInterface dialog, int id) {
2603 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2604 }
2605 });
2606 } else {
2607 // Download has started.
2608 final String packageName = info.providerName.getPackageName();
2609 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002610 }
2611 }
2612
2613 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002614 * Event handler for the "grid" button that appears on the home screen, which
2615 * enters all apps mode.
2616 *
2617 * @param v The view that was clicked.
2618 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 protected void onClickAllAppsButton(View v) {
2620 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002621 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002622 showWorkspace(true);
2623 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002624 // Try and refresh the set of predicted apps before we enter launcher
2625 showAppsView(true /* animated */, false /* resetListToTop */,
2626 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002627 }
2628 }
2629
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002630 private void showBrokenAppInstallDialog(final String packageName,
2631 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002632 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002633 .setTitle(R.string.abandoned_promises_title)
2634 .setMessage(R.string.abandoned_promise_explanation)
2635 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2636 .setNeutralButton(R.string.abandoned_clean_this,
2637 new DialogInterface.OnClickListener() {
2638 public void onClick(DialogInterface dialog, int id) {
2639 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2640 mWorkspace.removeAbandonedPromise(packageName, user);
2641 }
2642 })
2643 .create().show();
2644 return;
2645 }
2646
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002647 /**
2648 * Event handler for an app shortcut click.
2649 *
2650 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2651 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002652 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002653 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2654 Object tag = v.getTag();
2655 if (!(tag instanceof ShortcutInfo)) {
2656 throw new IllegalArgumentException("Input must be a Shortcut");
2657 }
2658
2659 // Open shortcut
2660 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002661
2662 if (shortcut.isDisabled != 0) {
2663 int error = R.string.activity_not_available;
2664 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2665 error = R.string.safemode_shortcut_error;
2666 }
2667 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2668 return;
2669 }
2670
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002671 final Intent intent = shortcut.intent;
2672
2673 // Check for special shortcuts
2674 if (intent.getComponent() != null) {
2675 final String shortcutClass = intent.getComponent().getClassName();
2676
2677 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2678 MemoryDumpActivity.startDump(this);
2679 return;
2680 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2681 toggleShowWeightWatcher();
2682 return;
2683 }
2684 }
2685
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002687 if ((v instanceof BubbleTextView)
2688 && shortcut.isPromise()
2689 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002690 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002691 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002692 new DialogInterface.OnClickListener() {
2693 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002694 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002695 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002696 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002697 return;
2698 }
2699
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002700 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002701 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002702
2703 if (mLauncherCallbacks != null) {
2704 mLauncherCallbacks.onClickAppShortcut(v);
2705 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002706 }
2707
Adam Cohen091440a2015-03-18 14:16:05 -07002708 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002709 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002710 final ShortcutInfo shortcut;
2711 final Intent intent;
2712 if (tag instanceof ShortcutInfo) {
2713 shortcut = (ShortcutInfo) tag;
2714 intent = shortcut.intent;
2715 int[] pos = new int[2];
2716 v.getLocationOnScreen(pos);
2717 intent.setSourceBounds(new Rect(pos[0], pos[1],
2718 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002719
Sunny Goyal508da152014-08-14 10:53:27 -07002720 } else if (tag instanceof AppInfo) {
2721 shortcut = null;
2722 intent = ((AppInfo) tag).intent;
2723 } else {
2724 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2725 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002726
2727 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002728 mStats.recordLaunch(intent, shortcut);
2729
2730 if (success && v instanceof BubbleTextView) {
2731 mWaitingForResume = (BubbleTextView) v;
2732 mWaitingForResume.setStayPressed(true);
2733 }
2734 }
2735
2736 /**
2737 * Event handler for a folder icon click.
2738 *
2739 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2740 */
2741 protected void onClickFolderIcon(View v) {
2742 if (LOGD) Log.d(TAG, "onClickFolder");
2743 if (!(v instanceof FolderIcon)){
2744 throw new IllegalArgumentException("Input must be a FolderIcon");
2745 }
2746
2747 FolderIcon folderIcon = (FolderIcon) v;
2748 final FolderInfo info = folderIcon.getFolderInfo();
2749 Folder openFolder = mWorkspace.getFolderForTag(info);
2750
2751 // If the folder info reports that the associated folder is open, then verify that
2752 // it is actually opened. There have been a few instances where this gets out of sync.
2753 if (info.opened && openFolder == null) {
2754 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2755 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2756 info.opened = false;
2757 }
2758
2759 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2760 // Close any open folder
2761 closeFolder();
2762 // Open the requested folder
2763 openFolder(folderIcon);
2764 } else {
2765 // Find the open folder...
2766 int folderScreen;
2767 if (openFolder != null) {
2768 folderScreen = mWorkspace.getPageForView(openFolder);
2769 // .. and close it
2770 closeFolder(openFolder);
2771 if (folderScreen != mWorkspace.getCurrentPage()) {
2772 // Close any folder open on the current screen
2773 closeFolder();
2774 // Pull the folder onto this screen
2775 openFolder(folderIcon);
2776 }
2777 }
2778 }
Adam Cohen9211d422014-10-07 18:14:53 -07002779
2780 if (mLauncherCallbacks != null) {
2781 mLauncherCallbacks.onClickFolderIcon(v);
2782 }
Michael Jurka5130e402011-10-13 04:55:35 -07002783 }
2784
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002785 /**
2786 * Event handler for the (Add) Widgets button that appears after a long press
2787 * on the home screen.
2788 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002789 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002790 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002791 if (mIsSafeModeEnabled) {
2792 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2793 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002794 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002795 if (mLauncherCallbacks != null) {
2796 mLauncherCallbacks.onClickAddWidgetButton(view);
2797 }
Adam Cohen9211d422014-10-07 18:14:53 -07002798 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002799 }
2800
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002801 /**
2802 * Event handler for the wallpaper picker button that appears after a long press
2803 * on the home screen.
2804 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002805 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002806 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002807 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2808 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002809
2810 if (mLauncherCallbacks != null) {
2811 mLauncherCallbacks.onClickWallpaperPicker(v);
2812 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002813 }
2814
2815 /**
2816 * Event handler for a click on the settings button that appears after a long press
2817 * on the home screen.
2818 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002819 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002820 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002821 if (mLauncherCallbacks != null) {
2822 mLauncherCallbacks.onClickSettingsButton(v);
2823 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002824 }
2825
Michael Jurka5130e402011-10-13 04:55:35 -07002826 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002827 // Provide the same haptic feedback that the system offers for virtual keys.
2828 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002829 }
2830
Adam Cohen61f560d2013-09-30 15:58:20 -07002831 public void performHapticFeedbackOnTouchDown(View v) {
2832 // Provide the same haptic feedback that the system offers for virtual keys.
2833 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2834 }
2835
2836 public View.OnTouchListener getHapticFeedbackTouchListener() {
2837 if (mHapticFeedbackTouchListener == null) {
2838 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002839 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002840 @Override
2841 public boolean onTouch(View v, MotionEvent event) {
2842 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2843 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2844 }
2845 return false;
2846 }
2847 };
2848 }
2849 return mHapticFeedbackTouchListener;
2850 }
2851
Adam Cohen9211d422014-10-07 18:14:53 -07002852 public void onDragStarted(View view) {
2853 if (isOnCustomContent()) {
2854 // Custom content screen doesn't participate in drag and drop. If on custom
2855 // content screen, move to default.
2856 moveWorkspaceToDefaultScreen();
2857 }
2858
2859 if (mLauncherCallbacks != null) {
2860 mLauncherCallbacks.onDragStarted(view);
2861 }
2862 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002863
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002864 /**
2865 * Called when the user stops interacting with the launcher.
2866 * This implies that the user is now on the homescreen and is not doing housekeeping.
2867 */
Adam Cohen9211d422014-10-07 18:14:53 -07002868 protected void onInteractionEnd() {
2869 if (mLauncherCallbacks != null) {
2870 mLauncherCallbacks.onInteractionEnd();
2871 }
2872 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002873
2874 /**
2875 * Called when the user starts interacting with the launcher.
2876 * The possible interactions are:
2877 * - open all apps
2878 * - reorder an app shortcut, or a widget
2879 * - open the overview mode.
2880 * This is a good time to stop doing things that only make sense
2881 * when the user is on the homescreen and not doing housekeeping.
2882 */
Adam Cohen9211d422014-10-07 18:14:53 -07002883 protected void onInteractionBegin() {
2884 if (mLauncherCallbacks != null) {
2885 mLauncherCallbacks.onInteractionBegin();
2886 }
2887 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002888
Winson Chungcd99cd32015-04-29 11:03:24 -07002889 /** Updates the interaction state. */
2890 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002891 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002892 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002893 boolean fromStateWithOverlay;
2894 boolean toStateWithOverlay;
2895 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2896 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2897 toStateWithOverlay = toState != Workspace.State.NORMAL;
2898 } else {
2899 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2900 fromState != Workspace.State.NORMAL_HIDDEN;
2901 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2902 toState != Workspace.State.NORMAL_HIDDEN;
2903 }
2904 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002905 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002906 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002907 onInteractionEnd();
2908 }
2909 }
2910
Kenny Guyf07af7b2014-07-31 11:39:16 +01002911 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002912 try {
2913 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002914 launcherApps.showAppDetailsForProfile(componentName, user);
2915 } catch (SecurityException e) {
2916 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2917 Log.e(TAG, "Launcher does not have permission to launch settings");
2918 } catch (ActivityNotFoundException e) {
2919 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2920 Log.e(TAG, "Unable to launch settings");
2921 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002922 }
2923
Michael Jurka1e2f4652013-07-08 18:03:46 -07002924 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002925 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2926 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002927 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002928 // System applications cannot be installed. For now, show a toast explaining that.
2929 // We may give them the option of disabling apps this way.
2930 int messageId = R.string.uninstall_system_app_text;
2931 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002932 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002933 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002934 String packageName = componentName.getPackageName();
2935 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002936 Intent intent = new Intent(
2937 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002938 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2939 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002940 if (user != null) {
2941 user.addToIntent(intent, Intent.EXTRA_USER);
2942 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002943 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002944 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002945 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002946 }
2947
Michael Jurka86a720e2012-05-09 11:23:23 -07002948 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002949 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002951 // Only launch using the new animation if the shortcut has not opted out (this is a
2952 // private contract between launcher and may be ignored in the future).
2953 boolean useLaunchAnimation = (v != null) &&
2954 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002955 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2956 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002957
Kenny Guy1317e2d2014-05-08 18:52:50 +01002958 UserHandleCompat user = null;
2959 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2960 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2961 user = userManager.getUserForSerialNumber(serialNumber);
2962 }
2963
2964 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002965 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002966 ActivityOptions opts = null;
2967 if (sClipRevealMethod != null) {
2968 // TODO: call method directly when Launcher3 can depend on M APIs
2969 int left = 0, top = 0;
2970 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2971 if (v instanceof TextView) {
2972 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002973 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2974 if (icon != null) {
2975 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002976 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002977 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002978 width = bounds.width();
2979 height = bounds.height();
2980 }
2981 }
2982 try {
2983 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2984 left, top, width, height);
2985 } catch (IllegalAccessException e) {
2986 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2987 sClipRevealMethod = null;
2988 } catch (InvocationTargetException e) {
2989 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2990 sClipRevealMethod = null;
2991 }
2992 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002993 if (opts == null && !Utilities.isLmpOrAbove()) {
2994 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002995 v.getMeasuredWidth(), v.getMeasuredHeight());
2996 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002997 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002998 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002999
3000 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
3001 // Could be launching some bookkeeping activity
3002 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003003 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003004 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003005 launcherApps.startActivityForProfile(intent.getComponent(), user,
3006 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003007 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003008 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 } catch (SecurityException e) {
3010 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003011 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003012 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003013 "or use the exported attribute for this activity. "
3014 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003016 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003017 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003018
Michael Jurka86a720e2012-05-09 11:23:23 -07003019 boolean startActivitySafely(View v, Intent intent, Object tag) {
3020 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003021 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3022 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3023 return false;
3024 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003025 try {
3026 success = startActivity(v, intent, tag);
3027 } catch (ActivityNotFoundException e) {
3028 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3029 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3030 }
3031 return success;
3032 }
3033
Adam Cohen268c4752012-06-06 17:47:33 -07003034 /**
3035 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3036 * in the DragLayer in the exact absolute location of the original FolderIcon.
3037 */
3038 private void copyFolderIconToImage(FolderIcon fi) {
3039 final int width = fi.getMeasuredWidth();
3040 final int height = fi.getMeasuredHeight();
3041
3042 // Lazy load ImageView, Bitmap and Canvas
3043 if (mFolderIconImageView == null) {
3044 mFolderIconImageView = new ImageView(this);
3045 }
3046 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3047 mFolderIconBitmap.getHeight() != height) {
3048 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3049 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3050 }
3051
3052 DragLayer.LayoutParams lp;
3053 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3054 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3055 } else {
3056 lp = new DragLayer.LayoutParams(width, height);
3057 }
3058
Adam Cohen307fe232012-08-16 17:55:58 -07003059 // The layout from which the folder is being opened may be scaled, adjust the starting
3060 // view size by this scale factor.
3061 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003062 lp.customPosition = true;
3063 lp.x = mRectForFolderAnimation.left;
3064 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003065 lp.width = (int) (scale * width);
3066 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003067
3068 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3069 fi.draw(mFolderIconCanvas);
3070 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003071 if (fi.getFolder() != null) {
3072 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3073 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003074 }
Adam Cohen268c4752012-06-06 17:47:33 -07003075 // Just in case this image view is still in the drag layer from a previous animation,
3076 // we remove it and re-add it.
3077 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3078 mDragLayer.removeView(mFolderIconImageView);
3079 }
3080 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003081 if (fi.getFolder() != null) {
3082 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003083 }
Adam Cohen268c4752012-06-06 17:47:33 -07003084 }
3085
Adam Cohen2801caf2011-05-13 20:57:39 -07003086 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003087 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003088 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3089 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3090 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3091
Adam Cohenc51934b2011-07-26 21:07:43 -07003092 FolderInfo info = (FolderInfo) fi.getTag();
3093 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3094 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003095 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3096 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003097 }
3098
Adam Cohen268c4752012-06-06 17:47:33 -07003099 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3100 copyFolderIconToImage(fi);
3101 fi.setVisibility(View.INVISIBLE);
3102
Michael Jurka2ecf9952012-06-18 12:52:28 -07003103 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003104 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003105 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003106 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3107 }
3108 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003109 oa.start();
3110 }
3111
Adam Cohen268c4752012-06-06 17:47:33 -07003112 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003113 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003114 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3115 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3116 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3117
Adam Cohen268c4752012-06-06 17:47:33 -07003118 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003119
Adam Cohen268c4752012-06-06 17:47:33 -07003120 // We remove and re-draw the FolderIcon in-case it has changed
3121 mDragLayer.removeView(mFolderIconImageView);
3122 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003123 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003124 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003125 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003126 oa.addListener(new AnimatorListenerAdapter() {
3127 @Override
3128 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003129 if (cl != null) {
3130 cl.clearFolderLeaveBehind();
3131 // Remove the ImageView copy of the FolderIcon and make the original visible.
3132 mDragLayer.removeView(mFolderIconImageView);
3133 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003134 }
3135 }
3136 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003137 oa.start();
3138 }
3139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003140 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003141 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003142 * is animated relative to the specified View. If the View is null, no animation
3143 * is played.
3144 *
3145 * @param folderInfo The FolderInfo describing the folder to open.
3146 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003147 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003148 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003149 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3150 if (openFolder != null && openFolder != folder) {
3151 // Close any open folder before opening a folder.
3152 closeFolder();
3153 }
3154
Adam Cohena9cf38f2011-05-02 15:36:58 -07003155 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156
Adam Cohena9cf38f2011-05-02 15:36:58 -07003157 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003158
Sunny Goyalf4066152015-04-15 09:42:19 -07003159 // While the folder is open, the position of the icon cannot change.
3160 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3161
Adam Cohen4554ee12011-08-03 16:13:21 -07003162 // Just verify that the folder hasn't already been added to the DragLayer.
3163 // There was a one-off crash where the folder had a parent already.
3164 if (folder.getParent() == null) {
3165 mDragLayer.addView(folder);
3166 mDragController.addDropTarget((DropTarget) folder);
3167 } else {
3168 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3169 folder.getParent() + ").");
3170 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003171 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003172 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003173
3174 // Notify the accessibility manager that this folder "window" has appeared and occluded
3175 // the workspace items
3176 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3177 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003178 }
3179
3180 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003181 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003182 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003183 if (folder.isEditingName()) {
3184 folder.dismissEditingName();
3185 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003186 closeFolder(folder);
3187 }
3188 }
3189
Sunny Goyal83a8f042015-05-19 12:52:12 -07003190 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003191 folder.getInfo().opened = false;
3192
3193 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3194 if (parent != null) {
3195 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3196 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003197 if (fi != null) {
3198 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3199 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003200 }
3201 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003202
3203 // Notify the accessibility manager that this folder "window" has disappeard and no
3204 // longer occludeds the workspace items
3205 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206 }
3207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003208 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003209 if (!isDraggingEnabled()) return false;
3210 if (isWorkspaceLocked()) return false;
3211 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003212
Adam Cohen1697b792013-09-17 19:08:21 -07003213 if (v instanceof Workspace) {
3214 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003215 if (!mWorkspace.isTouchActive()) {
3216 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003217 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3218 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3219 return true;
3220 } else {
3221 return false;
3222 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003223 } else {
3224 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003225 }
Adam Cohen1697b792013-09-17 19:08:21 -07003226 }
3227
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003228 CellLayout.CellInfo longClickCellInfo = null;
3229 View itemUnderLongClick = null;
3230 if (v.getTag() instanceof ItemInfo) {
3231 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003232 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003233 itemUnderLongClick = longClickCellInfo.cell;
3234 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003235 }
3236
Winson Chung3d503fb2011-07-13 17:25:49 -07003237 // The hotseat touch handling does not go through Workspace, and we always allow long press
3238 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003239 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003240 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003241 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003242 // User long pressed on empty space
3243 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3244 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003245 if (mWorkspace.isInOverviewMode()) {
3246 mWorkspace.startReordering(v);
3247 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003248 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003249 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003250 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003251 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3252 mHotseat.getOrderInHotseat(
3253 longClickCellInfo.cellX,
3254 longClickCellInfo.cellY));
3255 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003256 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003257 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003258 }
3259 }
3260 }
3261 return true;
3262 }
3263
Winson Chung3d503fb2011-07-13 17:25:49 -07003264 boolean isHotseatLayout(View layout) {
3265 return mHotseat != null && layout != null &&
3266 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3267 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003268
Winson Chung3d503fb2011-07-13 17:25:49 -07003269 /**
3270 * Returns the CellLayout of the specified container at the specified screen.
3271 */
Sunny Goyal92820592015-03-02 11:31:35 -08003272 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003273 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3274 if (mHotseat != null) {
3275 return mHotseat.getLayout();
3276 } else {
3277 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003278 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003279 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003280 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003281 }
3282 }
3283
Winson Chungb745afb2015-03-02 11:51:23 -08003284 /**
3285 * For overridden classes.
3286 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003287 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003288 return isAppsViewVisible();
3289 }
3290
3291 public boolean isAppsViewVisible() {
3292 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3293 }
3294
3295 public boolean isWidgetsViewVisible() {
3296 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003297 }
3298
Craig Mautner360310b2012-10-26 15:13:08 -07003299 private void setWorkspaceBackground(boolean workspace) {
3300 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003301 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003302 }
3303
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003304 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003305 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3306 int curflags = getWindow().getAttributes().flags
3307 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3308 if (wpflags != curflags) {
3309 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3310 }
Craig Mautner360310b2012-10-26 15:13:08 -07003311 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003312 }
3313
Michael Jurkae326f182011-11-21 14:05:46 -08003314 @Override
3315 public void onTrimMemory(int level) {
3316 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003317 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3318 // The widget preview db can result in holding onto over
3319 // 3MB of memory for caching which isn't necessary.
3320 SQLiteDatabase.releaseMemory();
3321
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003322 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003323 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003324 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003325 if (mLauncherCallbacks != null) {
3326 mLauncherCallbacks.onTrimMemory(level);
3327 }
Michael Jurkae326f182011-11-21 14:05:46 -08003328 }
3329
Winson Chungb745afb2015-03-02 11:51:23 -08003330 @Override
3331 public void onStateTransitionHideSearchBar() {
3332 // Hide the search bar
3333 if (mSearchDropTargetBar != null) {
3334 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3335 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003336 }
3337
Winson Chungb745afb2015-03-02 11:51:23 -08003338 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003339 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3340 true);
Adam Cohened307df2013-10-02 09:37:31 -07003341 }
3342
Sunny Goyal83a8f042015-05-19 12:52:12 -07003343 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003344 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3345 onCompleteRunnable, true);
3346 }
3347
3348 protected void showWorkspace(int snapToPage, boolean animated) {
3349 showWorkspace(snapToPage, animated, null, true);
3350 }
3351
3352 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3353 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003354 boolean changed = mState != State.WORKSPACE ||
3355 mWorkspace.getState() != Workspace.State.NORMAL;
3356 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003357 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003358 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003359 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003360 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003361
Winson Chungc7d2b602012-05-16 17:02:20 -07003362 // Show the search bar (only animate if we were showing the drop target bar in spring
3363 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003364 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003365 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003366 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003367
Michael Jurkab3e22d92011-10-31 15:58:33 -07003368 // Set focus to the AppsCustomize button
3369 if (mAllAppsButton != null) {
3370 mAllAppsButton.requestFocus();
3371 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003372 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003373
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003374 // Change the state *after* we've called all the transition code
3375 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003376
Winson Chung5fb63472011-02-02 17:03:37 -08003377 // Resume the auto-advance of widgets
3378 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003379 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003380
Winson Chung0f785722015-04-08 10:27:49 -07003381 if (changed) {
3382 // Send an accessibility event to announce the context change
3383 getWindow().getDecorView()
3384 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003385 if (notifyLauncherCallbacks) {
3386 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003387 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003388 mLauncherCallbacks.onAllAppsHidden();
3389 }
3390 }
Winson Chung0f785722015-04-08 10:27:49 -07003391 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003392 }
3393
Adam Cohened307df2013-10-02 09:37:31 -07003394 void showOverviewMode(boolean animated) {
3395 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003396 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003397 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3398 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003399 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003400 }
3401
Winson Chungb745afb2015-03-02 11:51:23 -08003402 /**
3403 * Shows the apps view.
3404 */
Winson Chung4ac30062015-05-08 17:34:17 -07003405 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003406 if (resetListToTop) {
3407 mAppsView.scrollToTop();
3408 }
Winson Chung4ac30062015-05-08 17:34:17 -07003409 if (updatePredictedApps) {
3410 tryAndUpdatePredictedApps();
3411 }
Winson Chungb745afb2015-03-02 11:51:23 -08003412 showAppsOrWidgets(animated, State.APPS);
3413 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003414
Winson Chungb745afb2015-03-02 11:51:23 -08003415 /**
3416 * Shows the widgets view.
3417 */
3418 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003419 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003420 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003421 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003422 }
Winson Chungb745afb2015-03-02 11:51:23 -08003423 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003424
3425 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003426 @Override
3427 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003428 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003429 }
3430 });
Winson Chungb745afb2015-03-02 11:51:23 -08003431 }
3432
3433 /**
3434 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003435 *
3436 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003437 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003438 // TODO: calling method should use the return value so that when {@code false} is returned
3439 // the workspace transition doesn't fall into invalid state.
3440 private boolean showAppsOrWidgets(boolean animated, State toState) {
3441 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3442 mState != State.WIDGETS_SPRING_LOADED) {
3443 return false;
3444 }
3445 if (toState != State.APPS && toState != State.WIDGETS) {
3446 return false;
3447 }
Winson Chungb745afb2015-03-02 11:51:23 -08003448
3449 if (toState == State.APPS) {
3450 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003451 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003452 mLauncherCallbacks.onAllAppsShown();
3453 }
Winson Chungb745afb2015-03-02 11:51:23 -08003454 } else {
3455 mStateTransitionAnimation.startAnimationToWidgets(animated);
3456 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003457
Michael Jurkab3e22d92011-10-31 15:58:33 -07003458 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003459 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003460
3461 // Pause the auto-advance of widgets until we are out of AllApps
3462 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003463 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003464 closeFolder();
3465
3466 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003467 getWindow().getDecorView()
3468 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003469 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003470 }
3471
Winson Chungcd99cd32015-04-29 11:03:24 -07003472 /**
3473 * Updates the workspace and interaction state on state change, and return the animation to this
3474 * new state.
3475 */
3476 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3477 boolean animated, HashMap<View, Integer> layerViews) {
3478 Workspace.State fromState = mWorkspace.getState();
3479 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3480 updateInteraction(fromState, toState);
3481 return anim;
3482 }
3483
Hyunyoung Song3f471442015-04-08 19:01:34 -07003484 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003485 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003486 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3487 mState == State.WIDGETS_SPRING_LOADED) {
3488 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003489 }
Winson Chungb745afb2015-03-02 11:51:23 -08003490
3491 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003492 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3493 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003494 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003495 }
Adam Cohen7777d962011-08-18 18:58:38 -07003496
Hyunyoung Song3f471442015-04-08 19:01:34 -07003497 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003498 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003499 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003500
Winson Chungcd99cd32015-04-29 11:03:24 -07003501 if (successfulDrop) {
3502 // We need to trigger all apps hidden to notify search to update itself before the
3503 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003504 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003505 mLauncherCallbacks.onAllAppsHidden();
3506 }
3507 }
3508
Winson Chunge7a03942011-08-05 15:05:12 -07003509 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003510 @Override
3511 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003512 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003513 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3514 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003515 // Before we show workspace, hide all apps again because
3516 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3517 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003518 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003519 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003520 } else {
3521 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003522 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003523 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003524 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003525 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003526
Michael Jurkad3ef3062010-11-23 16:23:58 -08003527 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003528 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003529 showAppsView(true /* animated */, false /* resetListToTop */,
3530 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003531 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003532 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003533 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003534 }
3535
Winson Chung4ac30062015-05-08 17:34:17 -07003536 /**
3537 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3538 * resumed.
3539 */
3540 private void tryAndUpdatePredictedApps() {
3541 if (mLauncherCallbacks != null) {
3542 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3543 if (!apps.isEmpty()) {
3544 mAppsView.setPredictedApps(apps);
3545 }
3546 }
3547 }
3548
Michael Jurkab3e22d92011-10-31 15:58:33 -07003549 void lockAllApps() {
3550 // TODO
3551 }
3552
3553 void unlockAllApps() {
3554 // TODO
3555 }
3556
Sunny Goyal594d76d2014-11-06 10:12:54 -08003557 protected void disableVoiceButtonProxy(boolean disable) {
3558 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003559 }
3560
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003561 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003562 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3563 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003564 }
3565
Adam Cohen24ce0b32014-01-14 16:18:14 -08003566 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003567 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3568 if (searchProvider == null) {
3569 return null;
3570 }
3571
3572 Bundle opts = new Bundle();
3573 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003574 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003575
3576 SharedPreferences sp = getSharedPreferences(
3577 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3578 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003579 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003580 if (!searchProvider.provider.flattenToString().equals(
3581 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003582 || (widgetInfo == null)
3583 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003584 // A valid widget is not already bound.
3585 if (widgetId > -1) {
3586 mAppWidgetHost.deleteAppWidgetId(widgetId);
3587 widgetId = -1;
3588 }
3589
3590 // Try to bind a new widget
3591 widgetId = mAppWidgetHost.allocateAppWidgetId();
3592
3593 if (!AppWidgetManagerCompat.getInstance(this)
3594 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3595 mAppWidgetHost.deleteAppWidgetId(widgetId);
3596 widgetId = -1;
3597 }
3598
3599 sp.edit()
3600 .putInt(QSB_WIDGET_ID, widgetId)
3601 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3602 .commit();
3603 }
3604
3605 if (widgetId != -1) {
3606 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3607 mQsb.updateAppWidgetOptions(opts);
3608 mQsb.setPadding(0, 0, 0, 0);
3609 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003610 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003611 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003612 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003613 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003614 }
3615
Sunny Goyal22235bc2015-04-03 09:23:43 -07003616 private void reinflateQSBIfNecessary() {
3617 if (mQsb instanceof LauncherAppWidgetHostView &&
3618 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3619 mSearchDropTargetBar.removeView(mQsb);
3620 mQsb = null;
3621 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3622 }
3623 }
3624
Michael Jurkad7c28052012-04-27 15:43:36 -07003625 @Override
3626 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003627 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003628 final List<CharSequence> text = event.getText();
3629 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003630 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003631 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003632 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003633 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003634 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003635 } else {
3636 text.add(getString(R.string.all_apps_home_button_label));
3637 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003638 return result;
3639 }
3640
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003641 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003642 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003643 */
Adam Cohen091440a2015-03-18 14:16:05 -07003644 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003645 @Override
3646 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003647 closeSystemDialogs();
3648 }
3649 }
3650
3651 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003652 * Receives notifications whenever the appwidgets are reset.
3653 */
3654 private class AppWidgetResetObserver extends ContentObserver {
3655 public AppWidgetResetObserver() {
3656 super(new Handler());
3657 }
3658
3659 @Override
3660 public void onChange(boolean selfChange) {
3661 onAppWidgetReset();
3662 }
3663 }
3664
3665 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003666 * If the activity is currently paused, signal that we need to run the passed Runnable
3667 * in onResume.
3668 *
3669 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003670 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3671 * wrong when we're not running, and if the activity comes back to what the configuration was
3672 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003673 *
3674 * Implementation of the method from LauncherModel.Callbacks.
3675 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003676 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003677 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003678 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003679 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003680 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003681 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003682 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003683 }
3684 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003685 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003686 return true;
3687 } else {
3688 return false;
3689 }
3690 }
3691
Michael Jurkac402cd92013-05-20 15:49:32 +02003692 private boolean waitUntilResume(Runnable run) {
3693 return waitUntilResume(run, false);
3694 }
3695
Michael Jurka1e2f4652013-07-08 18:03:46 -07003696 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003697 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003698 }
3699
Michael Jurka7607c2f2013-04-03 14:33:19 -07003700 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003701 * If the activity is currently paused, signal that we need to re-run the loader
3702 * in onResume.
3703 *
3704 * This needs to be called from incoming places where resources might have been loaded
3705 * while we are paused. That is becaues the Configuration might be wrong
3706 * when we're not running, and if it comes back to what it was when we
3707 * were paused, we are not restarted.
3708 *
3709 * Implementation of the method from LauncherModel.Callbacks.
3710 *
3711 * @return true if we are currently paused. The caller might be able to
3712 * skip some work in that case since we will come back again.
3713 */
3714 public boolean setLoadOnResume() {
3715 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003716 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003717 mOnResumeNeedsLoad = true;
3718 return true;
3719 } else {
3720 return false;
3721 }
3722 }
3723
3724 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003725 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003726 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003728 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003729 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003730 } else {
3731 return SCREEN_COUNT / 2;
3732 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003733 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003734
Joe Onorato9c1289c2009-08-17 11:03:03 -04003735 /**
3736 * Refreshes the shortcuts shown on the workspace.
3737 *
3738 * Implementation of the method from LauncherModel.Callbacks.
3739 */
3740 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003741 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003742
Michael Jurka7607c2f2013-04-03 14:33:19 -07003743 // If we're starting binding all over again, clear any bind calls we'd postponed in
3744 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3745 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003746 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003747
Winson Chungd64d1762013-08-20 14:37:16 -07003748 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003749 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003750 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003751
Michael Jurka05bf644e2011-11-30 20:28:53 -08003752 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003753 if (mHotseat != null) {
3754 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003755 }
3756 }
3757
Adam Cohendcd297f2013-06-18 13:13:40 -07003758 @Override
3759 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003760 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003761
Adam Cohen5084cba2013-09-03 12:01:16 -07003762 // If there are no screens, we need to have an empty screen
3763 if (orderedScreenIds.size() == 0) {
3764 mWorkspace.addExtraEmptyScreen();
3765 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003766
3767 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003768 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003769 if (hasCustomContentToLeft()) {
3770 mWorkspace.createCustomContentContainer();
3771 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003772 }
Winson Chung64359a52013-07-08 17:17:08 -07003773 }
3774
3775 @Override
3776 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003777 // Log to disk
3778 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3779 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3780 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003781 int count = orderedScreenIds.size();
3782 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003783 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003784 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003785 }
3786
Adam Cohen39a06042013-07-19 14:30:12 -07003787 private boolean shouldShowWeightWatcher() {
3788 String spKey = LauncherAppState.getSharedPreferencesKey();
3789 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003790 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003791
3792 return show;
3793 }
3794
3795 private void toggleShowWeightWatcher() {
3796 String spKey = LauncherAppState.getSharedPreferencesKey();
3797 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3798 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3799
3800 show = !show;
3801
3802 SharedPreferences.Editor editor = sp.edit();
3803 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3804 editor.commit();
3805
3806 if (mWeightWatcher != null) {
3807 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3808 }
3809 }
3810
Winson Chungd64d1762013-08-20 14:37:16 -07003811 public void bindAppsAdded(final ArrayList<Long> newScreens,
3812 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003813 final ArrayList<ItemInfo> addAnimated,
3814 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003815 Runnable r = new Runnable() {
3816 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003817 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003818 }
3819 };
3820 if (waitUntilResume(r)) {
3821 return;
3822 }
3823
Winson Chungd64d1762013-08-20 14:37:16 -07003824 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003825 if (newScreens != null) {
3826 bindAddScreens(newScreens);
3827 }
Winson Chungd64d1762013-08-20 14:37:16 -07003828
3829 // We add the items without animation on non-visible pages, and with
3830 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003831 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003832 bindItems(addNotAnimated, 0,
3833 addNotAnimated.size(), false);
3834 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003835 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003836 bindItems(addAnimated, 0,
3837 addAnimated.size(), true);
3838 }
Winson Chungc58497e2013-09-03 17:48:37 -07003839
Winson Chung87412982013-10-03 18:34:14 -07003840 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003841 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003842
Winson Chungb745afb2015-03-02 11:51:23 -08003843 if (addedApps != null && mAppsView != null) {
3844 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003845 }
Winson Chungd64d1762013-08-20 14:37:16 -07003846 }
3847
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003848 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003849 * Bind the items start-end from the list.
3850 *
3851 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003852 */
Winson Chung64359a52013-07-08 17:17:08 -07003853 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3854 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003855 Runnable r = new Runnable() {
3856 public void run() {
3857 bindItems(shortcuts, start, end, forceAnimateIcons);
3858 }
3859 };
3860 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003861 return;
3862 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003863
Winson Chungf0c6ae02012-03-21 16:10:31 -07003864 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003865 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3866 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003867 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003868 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003869 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003870 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003871 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003872
3873 // Short circuit if we are loading dock items for a configuration which has no dock
3874 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3875 mHotseat == null) {
3876 continue;
3877 }
3878
Joe Onorato9c1289c2009-08-17 11:03:03 -04003879 switch (item.itemType) {
3880 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3881 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003882 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003883 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003884
Winson Chung64359a52013-07-08 17:17:08 -07003885 /*
3886 * TODO: FIX collision case
3887 */
Winson Chungce376632013-07-11 16:12:41 -07003888 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3889 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3890 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003891 View v = cl.getChildAt(item.cellX, item.cellY);
3892 Object tag = v.getTag();
3893 String desc = "Collision while binding workspace item: " + item
3894 + ". Collides with " + tag;
3895 if (LauncherAppState.isDogfoodBuild()) {
3896 throw (new RuntimeException(desc));
3897 } else {
3898 Log.d(TAG, desc);
3899 }
Winson Chungce376632013-07-11 16:12:41 -07003900 }
Winson Chung64359a52013-07-08 17:17:08 -07003901 }
3902
Adam Cohendcd297f2013-06-18 13:13:40 -07003903 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3904 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003905 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003906 // Animate all the applications up now
3907 shortcut.setAlpha(0f);
3908 shortcut.setScaleX(0f);
3909 shortcut.setScaleY(0f);
3910 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003911 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003912 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003913 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003914 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003915 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003916 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003917 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003918 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3919 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003920 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003921 default:
3922 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003924 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003925
Winson Chung997a9232013-07-24 15:33:46 -07003926 if (animateIcons) {
3927 // Animate to the correct page
3928 if (newShortcutsScreenId > -1) {
3929 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003930 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003931 final Runnable startBounceAnimRunnable = new Runnable() {
3932 public void run() {
3933 anim.playTogether(bounceAnims);
3934 anim.start();
3935 }
3936 };
Winson Chung997a9232013-07-24 15:33:46 -07003937 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003938 // We post the animation slightly delayed to prevent slowdowns
3939 // when we are loading right after we return to launcher.
3940 mWorkspace.postDelayed(new Runnable() {
3941 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003942 if (mWorkspace != null) {
3943 mWorkspace.snapToPage(newScreenIndex);
3944 mWorkspace.postDelayed(startBounceAnimRunnable,
3945 NEW_APPS_ANIMATION_DELAY);
3946 }
Winson Chung94d67682013-09-25 16:29:40 -07003947 }
3948 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003949 } else {
3950 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003951 }
3952 }
Winson Chung64359a52013-07-08 17:17:08 -07003953 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003954 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003955 }
3956
Joe Onorato9c1289c2009-08-17 11:03:03 -04003957 /**
3958 * Implementation of the method from LauncherModel.Callbacks.
3959 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003960 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003961 Runnable r = new Runnable() {
3962 public void run() {
3963 bindFolders(folders);
3964 }
3965 };
3966 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003967 return;
3968 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003969 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003970 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003971
3972 /**
3973 * Add the views for a widget to the workspace.
3974 *
3975 * Implementation of the method from LauncherModel.Callbacks.
3976 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003977 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003978 Runnable r = new Runnable() {
3979 public void run() {
3980 bindAppWidget(item);
3981 }
3982 };
3983 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003984 return;
3985 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003986
Daniel Sandler843e8602010-06-07 14:59:01 -04003987 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3988 if (DEBUG_WIDGETS) {
3989 Log.d(TAG, "bindAppWidget: " + item);
3990 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003991 final Workspace workspace = mWorkspace;
3992
Adam Cohen59400422014-03-05 18:07:04 -08003993 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003994 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003995
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003996 if (!mIsSafeModeEnabled
3997 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3998 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003999 if (appWidgetInfo == null) {
4000 if (DEBUG_WIDGETS) {
4001 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4002 + " belongs to component " + item.providerName
4003 + ", as the povider is null");
4004 }
4005 LauncherModel.deleteItemFromDatabase(this, item);
4006 return;
4007 }
4008 // Note: This assumes that the id remap broadcast is received before this step.
4009 // If that is not the case, the id remap will be ignored and user may see the
4010 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004011 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004012 pendingInfo.spanX = item.spanX;
4013 pendingInfo.spanY = item.spanY;
4014 pendingInfo.minSpanX = item.minSpanX;
4015 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004016 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004017 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004018
Sunny Goyalff572272014-07-23 13:58:07 -07004019 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004020 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4021 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004022
4023 // TODO consider showing a permission dialog when the widget is clicked.
4024 if (!success) {
4025 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4026 if (DEBUG_WIDGETS) {
4027 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4028 + " belongs to component " + item.providerName
4029 + ", as the launcher is unable to bing a new widget id");
4030 }
4031 LauncherModel.deleteItemFromDatabase(this, item);
4032 return;
4033 }
4034
4035 item.appWidgetId = newWidgetId;
4036
4037 // If the widget has a configure activity, it is still needs to set it up, otherwise
4038 // the widget is ready to go.
4039 item.restoreStatus = (appWidgetInfo.configure == null)
4040 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4041 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4042
4043 LauncherModel.updateItemInDatabase(this, item);
4044 }
4045
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004046 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004047 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004048 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004049 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4050 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004051 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004052
Sunny Goyal651077b2014-06-30 14:15:31 -07004053 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4054 } else {
4055 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004056 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4057 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004058 view.updateIcon(mIconCache);
4059 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004060 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004061 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004062 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004063
Joe Onorato9c1289c2009-08-17 11:03:03 -04004064 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004065 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066
Adam Cohendcd297f2013-06-18 13:13:40 -07004067 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004068 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004069 if (!item.isCustomWidget()) {
4070 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4071 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004072
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073 workspace.requestLayout();
4074
Daniel Sandler843e8602010-06-07 14:59:01 -04004075 if (DEBUG_WIDGETS) {
4076 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4077 + (SystemClock.uptimeMillis()-start) + "ms");
4078 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004079 }
4080
Sunny Goyalff572272014-07-23 13:58:07 -07004081 /**
4082 * Restores a pending widget.
4083 *
4084 * @param appWidgetId The app widget id
4085 * @param cellInfo The position on screen where to create the widget.
4086 */
4087 private void completeRestoreAppWidget(final int appWidgetId) {
4088 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4089 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4090 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4091 return;
4092 }
4093
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004094 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004095 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4096
4097 mWorkspace.reinflateWidgetsIfNecessary();
4098 LauncherModel.updateItemInDatabase(this, info);
4099 }
4100
Adam Cohen1462de32012-07-24 22:34:36 -07004101 public void onPageBoundSynchronously(int page) {
4102 mSynchronouslyBoundPages.add(page);
4103 }
4104
Joe Onorato9c1289c2009-08-17 11:03:03 -04004105 /**
4106 * Callback saying that there aren't any more items to bind.
4107 *
4108 * Implementation of the method from LauncherModel.Callbacks.
4109 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004110 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004111 Runnable r = new Runnable() {
4112 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004113 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004114 }
4115 };
4116 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004117 return;
4118 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004119 if (mSavedState != null) {
4120 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004121 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004122 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004123 mSavedState = null;
4124 }
4125
Adam Cohen1462de32012-07-24 22:34:36 -07004126 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004127
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004128 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004129 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004130
4131 // If we received the result of any pending adds while the loader was running (e.g. the
4132 // widget configuration forced an orientation change), process them now.
4133 if (sPendingAddItem != null) {
4134 final long screenId = completeAdd(sPendingAddItem);
4135
4136 // TODO: this moves the user to the page where the pending item was added. Ideally,
4137 // the screen would be guaranteed to exist after bind, and the page would be set through
4138 // the workspace restore process.
4139 mWorkspace.post(new Runnable() {
4140 @Override
4141 public void run() {
4142 mWorkspace.snapToScreenId(screenId);
4143 }
4144 });
4145 sPendingAddItem = null;
4146 }
4147
Sunny Goyal756adbc2015-04-16 15:20:51 -07004148 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004149
4150 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004151 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004152 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004153 }
4154
Adam Cohen3ed316a2014-07-23 18:21:20 -07004155 private void sendLoadingCompleteBroadcastIfNecessary() {
4156 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4157 String permission =
4158 getResources().getString(R.string.receive_first_load_broadcast_permission);
4159 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4160 sendBroadcast(intent, permission);
4161 SharedPreferences.Editor editor = mSharedPrefs.edit();
4162 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4163 editor.apply();
4164 }
4165 }
4166
Winson Chunga0b7e862013-09-05 16:03:15 -07004167 public boolean isAllAppsButtonRank(int rank) {
4168 if (mHotseat != null) {
4169 return mHotseat.isAllAppsButtonRank(rank);
4170 }
4171 return false;
4172 }
4173
Winson Chunga2413752012-04-03 14:22:34 -07004174 private boolean canRunNewAppsAnimation() {
4175 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4176 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4177 }
4178
Winson Chungc9168342013-06-26 14:54:55 -07004179 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4180 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4181 PropertyValuesHolder.ofFloat("alpha", 1f),
4182 PropertyValuesHolder.ofFloat("scaleX", 1f),
4183 PropertyValuesHolder.ofFloat("scaleY", 1f));
4184 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4185 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4186 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4187 return bounceAnim;
4188 }
4189
Adam Cohen27772732013-11-11 14:00:56 +00004190 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004191 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004192 }
4193
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004194 protected Rect getSearchBarBounds() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004195 return mDeviceProfile.getSearchBarBounds();
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004196 }
4197
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004198 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004199 if (mSearchDropTargetBar == null) {
4200 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004201 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004202 if (mQsb != null) {
4203 mSearchDropTargetBar.removeView(mQsb);
4204 mQsb = null;
4205 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004206 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004207 }
4208
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004209 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004210 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4211 * multiple calls to bind the same list.)
4212 */
4213 @Thunk ArrayList<AppInfo> mTmpAppsList;
4214 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4215 public void run() {
4216 bindAllApplications(mTmpAppsList);
4217 mTmpAppsList = null;
4218 }
4219 };
4220
4221 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004222 * Add the icons for all apps.
4223 *
4224 * Implementation of the method from LauncherModel.Callbacks.
4225 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004226 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004227 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4228 mTmpAppsList = apps;
4229 return;
4230 }
4231
Winson Chungb745afb2015-03-02 11:51:23 -08004232 if (mAppsView != null) {
4233 mAppsView.setApps(apps);
4234 }
Adam Cohen9211d422014-10-07 18:14:53 -07004235 if (mLauncherCallbacks != null) {
4236 mLauncherCallbacks.bindAllApplications(apps);
4237 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004238 }
4239
4240 /**
4241 * A package was updated.
4242 *
4243 * Implementation of the method from LauncherModel.Callbacks.
4244 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004245 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004246 Runnable r = new Runnable() {
4247 public void run() {
4248 bindAppsUpdated(apps);
4249 }
4250 };
4251 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004252 return;
4253 }
4254
Winson Chungb745afb2015-03-02 11:51:23 -08004255 if (mAppsView != null) {
4256 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004257 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004258 }
4259
Sunny Goyal4390ace2014-10-13 11:33:11 -07004260 @Override
4261 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4262 Runnable r = new Runnable() {
4263 public void run() {
4264 bindWidgetsRestored(widgets);
4265 }
4266 };
4267 if (waitUntilResume(r)) {
4268 return;
4269 }
4270 mWorkspace.widgetsRestored(widgets);
4271 }
4272
Joe Onorato9c1289c2009-08-17 11:03:03 -04004273 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004274 * Some shortcuts were updated in the background.
4275 *
4276 * Implementation of the method from LauncherModel.Callbacks.
4277 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004278 @Override
4279 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4280 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004281 Runnable r = new Runnable() {
4282 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004283 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004284 }
4285 };
4286 if (waitUntilResume(r)) {
4287 return;
4288 }
4289
Sunny Goyal4390ace2014-10-13 11:33:11 -07004290 if (!updated.isEmpty()) {
4291 mWorkspace.updateShortcuts(updated);
4292 }
4293
4294 if (!removed.isEmpty()) {
4295 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4296 for (ShortcutInfo si : removed) {
4297 removedComponents.add(si.getTargetComponent());
4298 }
4299 mWorkspace.removeItemsByComponentName(removedComponents, user);
4300 // Notify the drag controller
4301 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004302 }
4303 }
4304
4305 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004306 * Update the state of a package, typically related to install state.
4307 *
4308 * Implementation of the method from LauncherModel.Callbacks.
4309 */
Sunny Goyale755d462014-07-22 13:48:29 -07004310 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004311 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4312 Runnable r = new Runnable() {
4313 public void run() {
4314 bindRestoreItemsChange(updates);
4315 }
4316 };
4317 if (waitUntilResume(r)) {
4318 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004319 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004320
Sunny Goyal756adbc2015-04-16 15:20:51 -07004321 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004322 }
4323
4324 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004325 * A package was uninstalled. We take both the super set of packageNames
4326 * in addition to specific applications to remove, the reason being that
4327 * this can be called when a package is updated as well. In that scenario,
4328 * we only remove specific components from the workspace, where as
4329 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004330 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004331 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004332 * Implementation of the method from LauncherModel.Callbacks.
4333 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004334 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004335 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004336 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004337 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004338 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004339 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004340 }
Winson Chungd64d1762013-08-20 14:37:16 -07004341 };
4342 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004343 return;
4344 }
4345
Sunny Goyal1a745e82014-10-02 15:58:31 -07004346 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004347 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4348 for (AppInfo info : appInfos) {
4349 removedComponents.add(info.componentName);
4350 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004351 if (!packageNames.isEmpty()) {
4352 mWorkspace.removeItemsByPackageName(packageNames, user);
4353 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004354 if (!removedComponents.isEmpty()) {
4355 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004356 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004357 // Notify the drag controller
4358 mDragController.onAppsRemoved(packageNames, removedComponents);
4359
Sunny Goyal1a745e82014-10-02 15:58:31 -07004360 } else {
4361 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004362 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004363
Winson Chungdf95eb12013-10-16 14:57:07 -07004364 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004365 if (mAppsView != null) {
4366 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004367 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004368 }
Joe Onoratobe386092009-11-17 17:32:16 -08004369
Michael Jurkac402cd92013-05-20 15:49:32 +02004370 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004371 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004372 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004373 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004374 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004375
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004376 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004377 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004378 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004379 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004380 return;
4381 }
4382
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004383 if (mWidgetsView != null && model != null) {
4384 mWidgetsView.addWidgets(model);
4385 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004386 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004387 }
4388
Winson Chung400438b2011-01-16 17:53:48 -08004389 private int mapConfigurationOriActivityInfoOri(int configOri) {
4390 final Display d = getWindowManager().getDefaultDisplay();
4391 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4392 switch (d.getRotation()) {
4393 case Surface.ROTATION_0:
4394 case Surface.ROTATION_180:
4395 // We are currently in the same basic orientation as the natural orientation
4396 naturalOri = configOri;
4397 break;
4398 case Surface.ROTATION_90:
4399 case Surface.ROTATION_270:
4400 // We are currently in the other basic orientation to the natural orientation
4401 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4402 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4403 break;
4404 }
4405
4406 int[] oriMap = {
4407 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4408 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4409 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4410 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4411 };
4412 // Since the map starts at portrait, we need to offset if this device's natural orientation
4413 // is landscape.
4414 int indexOffset = 0;
4415 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4416 indexOffset = 1;
4417 }
4418 return oriMap[(d.getRotation() + indexOffset) % 4];
4419 }
Adam Cohen446e9402011-09-15 18:21:21 -07004420
Winson Chung4b919f82012-05-01 10:44:08 -07004421 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004422 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004423 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4424 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4425 .getConfiguration().orientation));
4426 } else {
4427 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4428 }
Winson Chung4b919f82012-05-01 10:44:08 -07004429 }
4430 }
4431 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004432 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004433 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004434 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004435 } else {
4436 mHandler.postDelayed(new Runnable() {
4437 public void run() {
4438 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4439 }
4440 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004441 }
Winson Chung4b919f82012-05-01 10:44:08 -07004442 }
Winson Chung400438b2011-01-16 17:53:48 -08004443 }
4444
Winson Chunge43a1e72014-01-15 10:33:02 -08004445 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004446 if (mLauncherCallbacks != null) {
4447 return mLauncherCallbacks.isLauncherPreinstalled();
4448 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004449 PackageManager pm = getPackageManager();
4450 try {
4451 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4452 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4453 return true;
4454 } else {
4455 return false;
4456 }
4457 } catch (NameNotFoundException e) {
4458 e.printStackTrace();
4459 return false;
4460 }
4461 }
4462
Adam Cohenea90f832014-05-21 15:03:34 -07004463 /**
4464 * This method indicates whether or not we should suggest default wallpaper dimensions
4465 * when our wallpaper cropper was not yet used to set a wallpaper.
4466 */
4467 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004468 if (mLauncherCallbacks != null) {
4469 return mLauncherCallbacks.overrideWallpaperDimensions();
4470 }
Adam Cohenea90f832014-05-21 15:03:34 -07004471 return true;
4472 }
4473
Adam Cohen432609a2014-03-13 17:03:22 -07004474 /**
4475 * To be overridden by subclasses to indicate that there is an activity to launch
4476 * before showing the standard launcher experience.
4477 */
4478 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004479 if (mLauncherCallbacks != null) {
4480 return mLauncherCallbacks.hasFirstRunActivity();
4481 }
Adam Cohen432609a2014-03-13 17:03:22 -07004482 return false;
4483 }
4484
4485 /**
4486 * To be overridden by subclasses to launch any first run activity
4487 */
4488 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004489 if (mLauncherCallbacks != null) {
4490 return mLauncherCallbacks.getFirstRunActivity();
4491 }
Adam Cohen432609a2014-03-13 17:03:22 -07004492 return null;
4493 }
4494
Winson Chung2826a402015-04-17 16:18:53 -07004495 /**
4496 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004497 */
4498 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004499 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4500 return false;
4501 }
4502
Winson Chung2826a402015-04-17 16:18:53 -07004503 if (mLauncherCallbacks != null) {
4504 return mLauncherCallbacks.overrideAllAppsSearch();
4505 }
4506 return false;
4507 }
4508
Winson Chunga6945242014-01-08 14:04:34 -08004509 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004510 return !ActivityManager.isRunningInTestHarness() &&
4511 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004512 }
4513
Adam Cohen4a9423d2014-03-28 14:22:31 -07004514 protected boolean hasRunFirstRunActivity() {
4515 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4516 }
4517
Adam Cohen432609a2014-03-13 17:03:22 -07004518 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004519 if (shouldRunFirstRunActivity() &&
4520 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004521 Intent firstRunIntent = getFirstRunActivity();
4522 if (firstRunIntent != null) {
4523 startActivity(firstRunIntent);
4524 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004525 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004526 }
4527 }
Adam Cohen432609a2014-03-13 17:03:22 -07004528 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004529 }
4530
4531 private void markFirstRunActivityShown() {
4532 SharedPreferences.Editor editor = mSharedPrefs.edit();
4533 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4534 editor.apply();
4535 }
4536
Adam Cohen432609a2014-03-13 17:03:22 -07004537 /**
4538 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4539 * screen that must be displayed and dismissed.
4540 */
4541 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004542 if (mLauncherCallbacks != null) {
4543 return mLauncherCallbacks.hasDismissableIntroScreen();
4544 }
Adam Cohen432609a2014-03-13 17:03:22 -07004545 return false;
4546 }
4547
4548 /**
4549 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4550 */
4551 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004552 if (mLauncherCallbacks != null) {
4553 return mLauncherCallbacks.getIntroScreen();
4554 }
Adam Cohen432609a2014-03-13 17:03:22 -07004555 return null;
4556 }
4557
4558 /**
4559 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4560 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4561 */
4562 private boolean shouldShowIntroScreen() {
4563 return hasDismissableIntroScreen() &&
4564 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4565 }
4566
4567 protected void showIntroScreen() {
4568 View introScreen = getIntroScreen();
4569 changeWallpaperVisiblity(false);
4570 if (introScreen != null) {
4571 mDragLayer.showOverlayView(introScreen);
4572 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004573 if (mLauncherOverlayContainer != null) {
4574 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4575 }
Adam Cohen432609a2014-03-13 17:03:22 -07004576 }
4577
4578 public void dismissIntroScreen() {
4579 markIntroScreenDismissed();
4580 if (showFirstRunActivity()) {
4581 // We delay hiding the intro view until the first run activity is showing. This
4582 // avoids a blip.
4583 mWorkspace.postDelayed(new Runnable() {
4584 @Override
4585 public void run() {
4586 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004587 if (mLauncherOverlayContainer != null) {
4588 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4589 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004590 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004591 }
4592 }, ACTIVITY_START_DELAY);
4593 } else {
4594 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004595 if (mLauncherOverlayContainer != null) {
4596 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4597 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004598 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004599 }
4600 changeWallpaperVisiblity(true);
4601 }
4602
4603 private void markIntroScreenDismissed() {
4604 SharedPreferences.Editor editor = mSharedPrefs.edit();
4605 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4606 editor.apply();
4607 }
4608
Adam Cohen091440a2015-03-18 14:16:05 -07004609 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004610 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4611 // on the device, then we always show the first run cling experience (or if there is no
4612 // launcher2). Otherwise, we prompt the user upon started for migration
4613 LauncherClings launcherClings = new LauncherClings(this);
4614 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4615 if (mModel.canMigrateFromOldLauncherDb(this)) {
4616 launcherClings.showMigrationCling();
4617 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004618 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004619 }
4620 }
4621 }
4622
Winson Chunga6945242014-01-08 14:04:34 -08004623 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004624 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4625 if (mHotseat != null) mHotseat.setAlpha(1f);
4626 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4627 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004628 }
4629
4630 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004631 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4632 if (mHotseat != null) mHotseat.setAlpha(0f);
4633 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4634 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004635 }
4636
Mathew Inwood72fbec12013-11-19 15:45:07 +00004637 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004638 // Called from search suggestion, not supported in other profiles.
4639 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4640 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4641 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4642 myUser);
4643 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004644 return null;
4645 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004646 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004647 }
4648
4649 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4650 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004651 // Called from search suggestion, not supported in other profiles.
4652 return createShortcutDragInfo(shortcutIntent, caption, icon,
4653 UserHandleCompat.myUserHandle());
4654 }
4655
4656 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4657 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004658 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004659 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004660 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004661 }
4662
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004663 protected void moveWorkspaceToDefaultScreen() {
4664 mWorkspace.moveToDefaultScreen(false);
4665 }
4666
Mathew Inwood72fbec12013-11-19 15:45:07 +00004667 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4668 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004669 mWorkspace.onExternalDragStartedWithItem(dragView);
4670 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004671 }
4672
Anjali Koppalf5d29132014-02-28 13:33:27 -08004673 @Override
4674 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004675 if (mLauncherCallbacks != null) {
4676 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4677 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004678 }
4679
Winson Chung80baf5a2010-08-09 16:03:15 -07004680 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004681 * Prints out out state for debugging.
4682 */
4683 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004684 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004685 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004686 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4687 Log.d(TAG, "mRestoring=" + mRestoring);
4688 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4689 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004690 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004691 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004692 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004693
Daniel Sandler325dc232013-06-05 22:57:57 -04004694 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004695 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004696
4697 @Override
4698 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4699 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004700 synchronized (sDumpLogs) {
4701 writer.println(" ");
4702 writer.println("Debug logs: ");
4703 for (int i = 0; i < sDumpLogs.size(); i++) {
4704 writer.println(" " + sDumpLogs.get(i));
4705 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004706 }
Adam Cohen9211d422014-10-07 18:14:53 -07004707 if (mLauncherCallbacks != null) {
4708 mLauncherCallbacks.dump(prefix, fd, writer, args);
4709 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004710 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004711
4712 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004713 if (DEBUG_DUMP_LOG) {
4714 synchronized (sDumpLogs) {
4715 Log.d(TAG, "");
4716 Log.d(TAG, "*********************");
4717 Log.d(TAG, "Launcher debug logs: ");
4718 for (int i = 0; i < sDumpLogs.size(); i++) {
4719 Log.d(TAG, " " + sDumpLogs.get(i));
4720 }
4721 Log.d(TAG, "*********************");
4722 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004723 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004724 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004725 }
4726
4727 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004728 addDumpLog(tag, log, null, debugLog);
4729 }
4730
4731 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004732 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004733 if (e != null) {
4734 Log.d(tag, log, e);
4735 } else {
4736 Log.d(tag, log);
4737 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004738 }
Winson Chungede41292013-09-19 16:27:36 -07004739 if (DEBUG_DUMP_LOG) {
4740 sDateStamp.setTime(System.currentTimeMillis());
4741 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004742 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4743 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004744 }
Winson Chungede41292013-09-19 16:27:36 -07004745 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004746 }
4747
Adam Cohen59400422014-03-05 18:07:04 -08004748 public static CustomAppWidget getCustomAppWidget(String name) {
4749 return sCustomAppWidgets.get(name);
4750 }
4751
4752 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4753 return sCustomAppWidgets;
4754 }
4755
Winson Chungede41292013-09-19 16:27:36 -07004756 public void dumpLogsToLocalData() {
4757 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004758 new AsyncTask<Void, Void, Void>() {
4759 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004760 boolean success = false;
4761 sDateStamp.setTime(sRunStart);
4762 String FILENAME = sDateStamp.getMonth() + "-"
4763 + sDateStamp.getDay() + "_"
4764 + sDateStamp.getHours() + "-"
4765 + sDateStamp.getMinutes() + "_"
4766 + sDateStamp.getSeconds() + ".txt";
4767
4768 FileOutputStream fos = null;
4769 File outFile = null;
4770 try {
4771 outFile = new File(getFilesDir(), FILENAME);
4772 outFile.createNewFile();
4773 fos = new FileOutputStream(outFile);
4774 } catch (Exception e) {
4775 e.printStackTrace();
4776 }
4777 if (fos != null) {
4778 PrintWriter writer = new PrintWriter(fos);
4779
4780 writer.println(" ");
4781 writer.println("Debug logs: ");
4782 synchronized (sDumpLogs) {
4783 for (int i = 0; i < sDumpLogs.size(); i++) {
4784 writer.println(" " + sDumpLogs.get(i));
4785 }
4786 }
4787 writer.close();
4788 }
4789 try {
4790 if (fos != null) {
4791 fos.close();
4792 success = true;
4793 }
4794 } catch (IOException e) {
4795 e.printStackTrace();
4796 }
Michael Jurka43467462013-11-14 12:03:58 +01004797 return null;
Winson Chungede41292013-09-19 16:27:36 -07004798 }
Michael Jurka43467462013-11-14 12:03:58 +01004799 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004800 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004802}
Michael Jurka2763be32011-02-24 11:19:57 -08004803
Michael Jurkaabded662011-03-04 12:06:57 -08004804interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004805 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004806 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004807 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004808 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004809 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004810}
Dan Sandlerd5024042014-01-09 15:01:33 -05004811
4812interface DebugIntents {
4813 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4814 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004815}