blob: 6c737390775f97900b938010823766023eecd23c [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;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070086import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070088import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080095import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070096import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070097
Sunny Goyal651077b2014-06-30 14:15:31 -070098import com.android.launcher3.DropTarget.DragObject;
99import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyal83a8f042015-05-19 12:52:12 -0700100import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700101import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100102import com.android.launcher3.compat.LauncherActivityInfoCompat;
103import com.android.launcher3.compat.LauncherAppsCompat;
104import com.android.launcher3.compat.UserHandleCompat;
105import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700106import com.android.launcher3.model.WidgetsModel;
Sunny Goyale2df0622015-04-24 11:27:00 -0700107import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700108import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700109import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700110import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700111import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700112
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.io.DataInputStream;
114import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700116import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700118import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700120import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800121import java.lang.reflect.InvocationTargetException;
122import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700125import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700128import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700129import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000130import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132/**
133 * Default launcher application.
134 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100135public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700136 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800137 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
138 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700139 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700140 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Winson Chung83f59ab2015-05-05 17:21:58 -0700142 // Temporary flag
143 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
144
Daniel Sandlerf061f822013-06-27 13:59:36 -0400145 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700146 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700147 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400148 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700149 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700150
Dan Sandlerd5024042014-01-09 15:01:33 -0500151 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700156 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700159 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700160
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700161 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
162 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
163 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
164
Mathew Inwood876a8462013-06-14 14:12:41 +0100165 /**
166 * IntentStarter uses request codes starting with this. This must be greater than all activity
167 * request codes used internally.
168 */
169 protected static final int REQUEST_LAST = 100;
170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
172
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800173 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 // To turn on these properties, type
176 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Winson Chung2672ff92012-05-04 16:22:30 -0700179 // The Intent extra that defines whether to ignore the launch animation
180 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400181 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
184 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700185 // Type: int
186 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700188 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
189 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
191 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700192 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700194 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700195 // Type: int
196 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
197 // Type: int
198 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
199 // Type: parcelable
200 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000201 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800202 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000203 // Type: int[]
204 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Adam Cohen432609a2014-03-13 17:03:22 -0700206 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800207 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
208
Adam Cohen3ed316a2014-07-23 18:21:20 -0700209 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
210 static final String ACTION_FIRST_LOAD_COMPLETE =
211 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
212
Adam Cohen39a06042013-07-19 14:30:12 -0700213 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700214 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700215
Sunny Goyal594d76d2014-11-06 10:12:54 -0800216 private static final String QSB_WIDGET_ID = "qsb_widget_id";
217 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
218
Winson Chunga6945242014-01-08 14:04:34 -0800219 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
220
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700221 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800222 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700223 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700225
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700226 private boolean mIsSafeModeEnabled;
227
Adam Cohenc2d6e892014-10-16 09:49:24 -0700228 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
229 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700230 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700231
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700233 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
234 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700235 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000237 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
238 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
239
Winson Chunga2413752012-04-03 14:22:34 -0700240 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700241 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
242 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700243 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700244
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800245 private final BroadcastReceiver mCloseSystemDialogsReceiver
246 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800247 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private LayoutInflater mInflater;
250
Adam Cohen091440a2015-03-18 14:16:05 -0700251 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700252 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800253 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700254 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800255 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700256 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700257
Sunny Goyalffe83f12014-08-14 17:39:34 -0700258 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700259 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700260
Adam Cohen091440a2015-03-18 14:16:05 -0700261 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800262 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800263 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700264
Michael Jurka0280c3b2010-09-17 15:00:07 -0700265 private int[] mTmpAddItemCellCoordinates = new int[2];
266
Winson Chung3d503fb2011-07-13 17:25:49 -0700267 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800268 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700269
Michael Jurka838a4ca2011-02-07 13:33:06 -0800270 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700271
Winson Chungc51db6a2011-10-05 11:44:49 -0700272 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700273
274 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700275 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700276
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700277 // Main container view and the model for the widget tray screen.
278 @Thunk WidgetsContainerView mWidgetsView;
279 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700280
Winson Chungf0ea4d32011-06-06 14:27:16 -0700281 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800282 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700285 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
286 // scroll issues (because the workspace may not have been measured yet) and extra work.
287 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
288 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289
290 private SpannableStringBuilder mDefaultKeySsb = null;
291
Adam Cohen091440a2015-03-18 14:16:05 -0700292 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800294 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 private boolean mRestoring;
296 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700297 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298
Michael Jurka1e2f4652013-07-08 18:03:46 -0700299 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700300 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
301
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 private Bundle mSavedInstanceState;
303
Joe Onorato9c1289c2009-08-17 11:03:03 -0400304 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800305 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700306 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800307 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700308 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800309 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400310
Adam Cohen091440a2015-03-18 14:16:05 -0700311 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700312
Sunny Goyale2df0622015-04-24 11:27:00 -0700313 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700314
Adam Cohen61f560d2013-09-30 15:58:20 -0700315 private View.OnTouchListener mHapticFeedbackTouchListener;
316
Adam Cohended9f8d2010-11-03 13:25:16 -0700317 // Related to the auto-advancing of widgets
318 private final int ADVANCE_MSG = 1;
319 private final int mAdvanceInterval = 20000;
320 private final int mAdvanceStagger = 250;
321 private long mAutoAdvanceSentTime;
322 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700323 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700324 new HashMap<View, AppWidgetProviderInfo>();
325
Winson Chung400438b2011-01-16 17:53:48 -0800326 // Determines how long to wait after a rotation before restoring the screen orientation to
327 // match the sensor state.
328 private final int mRestoreScreenOrientationDelay = 500;
329
Adam Cohen091440a2015-03-18 14:16:05 -0700330 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700331
Adam Cohen1462de32012-07-24 22:34:36 -0700332 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700333 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700334
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700335 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700336 static Date sDateStamp = new Date();
337 static DateFormat sDateFormat =
338 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
339 static long sRunStart = System.currentTimeMillis();
340 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700341
Winson Chung46353de2012-02-16 14:05:10 -0800342 // We only want to get the SharedPreferences once since it does an FS stat each time we get
343 // it from the context.
344 private SharedPreferences mSharedPrefs;
345
Winson Chungf0c6ae02012-03-21 16:10:31 -0700346 // Holds the page that we need to animate to, and the icon views that we need to animate up
347 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700348 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700349 private Bitmap mFolderIconBitmap;
350 private Canvas mFolderIconCanvas;
351 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700352
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700353 private DeviceProfile mDeviceProfile;
354
Winson Chung13eb5272015-05-11 16:30:13 -0700355 // This is set to the view that launched the activity that navigated the user away from
356 // launcher. Since there is no callback for when the activity has finished launching, enable
357 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800358 private BubbleTextView mWaitingForResume;
359
Adam Cohen59400422014-03-05 18:07:04 -0800360 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
361 new HashMap<String, CustomAppWidget>();
362
363 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
364 static {
365 if (ENABLE_CUSTOM_WIDGET_TEST) {
366 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
367 }
368 }
369
Chet Haasea8f996d2015-02-17 12:56:04 -0800370 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
371 private static Method sClipRevealMethod = null;
372 static {
373 Class<?> activityOptionsClass = ActivityOptions.class;
374 try {
375 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
376 View.class, int.class, int.class, int.class, int.class);
377 } catch (Exception e) {
378 // Earlier version
379 }
380 }
381
Adam Cohen091440a2015-03-18 14:16:05 -0700382 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700383 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700384 if (mWorkspace != null) {
385 mWorkspace.buildPageHardwareLayers();
386 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700387 }
388 };
389
Adam Cohendb364c32014-05-20 14:23:40 -0700390 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800391
Adam Cohen091440a2015-03-18 14:16:05 -0700392 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800393 int requestCode;
394 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700395 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700396 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800397 int cellX;
398 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700399 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800400 }
401
Daniel Sandlerff02d492013-08-05 02:12:05 -0400402 private Stats mStats;
403
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700404 FocusIndicatorView mFocusHandler;
405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 @Override
407 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700408 if (DEBUG_STRICT_MODE) {
409 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
410 .detectDiskReads()
411 .detectDiskWrites()
412 .detectNetwork() // or .detectAll() for all detectable problems
413 .penaltyLog()
414 .build());
415 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
416 .detectLeakedSqlLiteObjects()
417 .detectLeakedClosableObjects()
418 .penaltyLog()
419 .penaltyDeath()
420 .build());
421 }
422
Adam Cohen9211d422014-10-07 18:14:53 -0700423 if (mLauncherCallbacks != null) {
424 mLauncherCallbacks.preOnCreate();
425 }
426
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400428
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400429 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400430 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700431 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700432
Adam Cohen2e6da152015-05-06 11:42:25 -0700433 // Load configuration-specific DeviceProfile
434 mDeviceProfile = new DeviceProfile(this, app.getInvariantDeviceProfile());
435 mDeviceProfile.addCallback(LauncherAppState.getInstance());
Winson Chung5f8afe62013-08-12 16:19:28 -0700436
437 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400438 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700439 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700440 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800441 mModel = app.setLauncher(this);
442 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700443
Joe Onorato41a12d22009-10-31 18:30:00 -0400444 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800446 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700447
Daniel Sandlerff02d492013-08-05 02:12:05 -0400448 mStats = new Stats(this);
449
Sunny Goyalffe83f12014-08-14 17:39:34 -0700450 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700451
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700452 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
453 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700454
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700455 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
456 // this also ensures that any synchronous binding below doesn't re-trigger another
457 // LauncherModel load.
458 mPaused = false;
459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200461 android.os.Debug.startMethodTracing(
462 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 }
464
465 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700467
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700469 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800471 registerContentObservers();
472
Joe Onorato7c312c12009-08-13 21:36:53 -0700473 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700474
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 mSavedState = savedInstanceState;
476 restoreState(mSavedState);
477
478 if (PROFILE_STARTUP) {
479 android.os.Debug.stopMethodTracing();
480 }
481
482 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700483 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700484 // If the user leaves launcher, then we should just load items asynchronously when
485 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700486 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700487 } else {
488 // We only load the page synchronously if the user rotates (or triggers a
489 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700490 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 }
493
494 // For handling default keys
495 mDefaultKeySsb = new SpannableStringBuilder();
496 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800497
498 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
499 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800500
Adam Cohenf9426d52012-06-04 17:26:21 -0700501 // On large interfaces, we want the screen to auto-rotate based on the current orientation
502 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700503
Adam Cohen9211d422014-10-07 18:14:53 -0700504 if (mLauncherCallbacks != null) {
505 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700506 if (mLauncherCallbacks.hasLauncherOverlay()) {
507 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700508 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
509 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
510 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700511 mWorkspace.setLauncherOverlay(mLauncherOverlay);
512 }
Adam Cohen9211d422014-10-07 18:14:53 -0700513 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700514
515 if (shouldShowIntroScreen()) {
516 showIntroScreen();
517 } else {
518 showFirstRunActivity();
519 showFirstRunClings();
520 }
Adam Cohen9211d422014-10-07 18:14:53 -0700521 }
522
523 private LauncherCallbacks mLauncherCallbacks;
524
525 public void onPostCreate(Bundle savedInstanceState) {
526 super.onPostCreate(savedInstanceState);
527 if (mLauncherCallbacks != null) {
528 mLauncherCallbacks.onPostCreate(savedInstanceState);
529 }
530 }
531
532 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
533 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700534 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
535 @Override
536 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700537 if (LOGD) {
538 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
539 }
Winson Chung94804152015-05-08 13:06:44 -0700540 mAppsView.setFixedBounds(bounds);
541 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700542 }
543
544 @Override
545 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700546 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
547 // Dismiss All Apps if we aren't already paused/invisible
548 if (!mPaused) {
549 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
550 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
551 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700552 }
Winson Chung0f785722015-04-08 10:27:49 -0700553 }
554 });
Adam Cohen9211d422014-10-07 18:14:53 -0700555 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700556 }
557
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700558 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700559 public void onLauncherProviderChange() {
560 if (mLauncherCallbacks != null) {
561 mLauncherCallbacks.onLauncherProviderChange();
562 }
563 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700564
Adam Cohen9211d422014-10-07 18:14:53 -0700565 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700566 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700567 if (mLauncherCallbacks != null) {
568 return mLauncherCallbacks.hasCustomContentToLeft();
569 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700570 return false;
571 }
572
Dave Hawkeya8881582013-09-17 15:55:33 -0600573 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500574 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600575 * {@link #addToCustomContentPage}. This will only be invoked if
576 * {@link #hasCustomContentToLeft()} is {@code true}.
577 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500578 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700579 if (mLauncherCallbacks != null) {
580 mLauncherCallbacks.populateCustomContentContainer();
581 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600582 }
583
584 /**
585 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
586 * ensure the custom content page is added or removed if necessary.
587 */
588 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600589 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600590 // Not bound yet, wait for bindScreens to be called.
591 return;
592 }
593
594 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
595 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500596 mWorkspace.createCustomContentContainer();
597 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600598 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
599 mWorkspace.removeCustomContentPage();
600 }
601 }
602
Adam Cohen091440a2015-03-18 14:16:05 -0700603 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 if (sLocaleConfiguration == null) {
605 new AsyncTask<Void, Void, LocaleConfiguration>() {
606 @Override
607 protected LocaleConfiguration doInBackground(Void... unused) {
608 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
609 readConfiguration(Launcher.this, localeConfiguration);
610 return localeConfiguration;
611 }
612
613 @Override
614 protected void onPostExecute(LocaleConfiguration result) {
615 sLocaleConfiguration = result;
616 checkForLocaleChange(); // recursive, but now with a locale configuration
617 }
618 }.execute();
619 return;
620 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 final Configuration configuration = getResources().getConfiguration();
623
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700624 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 final String locale = configuration.locale.toString();
626
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700627 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 final int mcc = configuration.mcc;
629
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700630 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 final int mnc = configuration.mnc;
632
Romain Guy84f296c2009-11-04 15:00:44 -0800633 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634
Romain Guy84f296c2009-11-04 15:00:44 -0800635 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700636 sLocaleConfiguration.locale = locale;
637 sLocaleConfiguration.mcc = mcc;
638 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700639
Joe Onorato0589f0f2010-02-08 13:44:00 -0800640 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700641
642 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100643 new AsyncTask<Void, Void, Void>() {
644 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700645 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100646 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700647 }
Michael Jurka43467462013-11-14 12:03:58 +0100648 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700649 }
650 }
651
Adam Cohen091440a2015-03-18 14:16:05 -0700652 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700653 public String locale;
654 public int mcc = -1;
655 public int mnc = -1;
656 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700657
Adam Cohen091440a2015-03-18 14:16:05 -0700658 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700659 DataInputStream in = null;
660 try {
Helena Josol28db2802014-10-09 17:04:09 +0100661 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700662 configuration.locale = in.readUTF();
663 configuration.mcc = in.readInt();
664 configuration.mnc = in.readInt();
665 } catch (FileNotFoundException e) {
666 // Ignore
667 } catch (IOException e) {
668 // Ignore
669 } finally {
670 if (in != null) {
671 try {
672 in.close();
673 } catch (IOException e) {
674 // Ignore
675 }
676 }
677 }
678 }
679
Adam Cohen091440a2015-03-18 14:16:05 -0700680 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700681 DataOutputStream out = null;
682 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100683 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100684 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700685 out.writeUTF(configuration.locale);
686 out.writeInt(configuration.mcc);
687 out.writeInt(configuration.mnc);
688 out.flush();
689 } catch (FileNotFoundException e) {
690 // Ignore
691 } catch (IOException e) {
692 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100693 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700694 } finally {
695 if (out != null) {
696 try {
697 out.close();
698 } catch (IOException e) {
699 // Ignore
700 }
701 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 }
703 }
704
Anton Hanssona2f665f2013-09-26 12:18:32 +0100705 public Stats getStats() {
706 return mStats;
707 }
708
Bjorn Bringertc459e522013-06-07 19:36:01 +0100709 public LayoutInflater getInflater() {
710 return mInflater;
711 }
712
Hyunyoung Song3f471442015-04-08 19:01:34 -0700713 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700714 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
715 // that is subsequently removed from the workspace in startBinding().
716 return !mModel.isLoadingWorkspace();
717 }
718
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800719 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000720 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100721 if (Build.VERSION.SDK_INT >= 17) {
722 return View.generateViewId();
723 } else {
724 // View.generateViewId() is not available. The following fallback logic is a copy
725 // of its implementation.
726 for (;;) {
727 final int result = sNextGeneratedId.get();
728 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
729 int newValue = result + 1;
730 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
731 if (sNextGeneratedId.compareAndSet(result, newValue)) {
732 return result;
733 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000734 }
735 }
736 }
737
738 public int getViewIdForItem(ItemInfo info) {
739 // This cast is safe given the > 2B range for int.
740 int itemId = (int) info.id;
741 if (mItemIdToViewId.containsKey(itemId)) {
742 return mItemIdToViewId.get(itemId);
743 }
744 int viewId = generateViewId();
745 mItemIdToViewId.put(itemId, viewId);
746 return viewId;
747 }
748
749 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700750 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
751 * a configuration step, this allows the proper animations to run after other transitions.
752 */
Adam Cohendb364c32014-05-20 14:23:40 -0700753 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700754 long screenId = args.screenId;
755 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
756 // When the screen id represents an actual screen (as opposed to a rank) we make sure
757 // that the drop page actually exists.
758 screenId = ensurePendingDropLayoutExists(args.screenId);
759 }
Adam Cohendb364c32014-05-20 14:23:40 -0700760
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800761 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800762 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700763 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700764 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700765 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800766 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700767 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700768 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700769 case REQUEST_RECONFIGURE_APPWIDGET:
770 completeRestoreAppWidget(args.appWidgetId);
771 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800772 }
Michael Jurka27614d22012-04-02 06:40:22 -0700773 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
774 // if you turned the screen off and then back while in All Apps, Launcher would not
775 // return to the workspace. Clearing mAddInfo.container here fixes this issue
776 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700777 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800778 }
779
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800780 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700781 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700782 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700783 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700784 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800785 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700786
Adam Cohenad4e15c2013-10-17 16:21:35 -0700787 Runnable exitSpringLoaded = new Runnable() {
788 @Override
789 public void run() {
790 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
791 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
792 }
793 };
794
Michael Jurka8b805b12012-04-18 14:23:14 -0700795 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700796 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700797 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
798 if (resultCode == RESULT_CANCELED) {
799 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700800 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700801 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700802 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800803 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700804 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700805 }
806 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200807 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
808 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700809 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200810 }
811 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700812 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200813
Adam Cohened66b2b2012-01-23 17:28:51 -0800814 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700815 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700816
Adam Cohendb364c32014-05-20 14:23:40 -0700817 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800818 // We have special handling for widgets
819 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800820 final int appWidgetId;
821 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
822 : -1;
823 if (widgetId < 0) {
824 appWidgetId = pendingAddWidgetId;
825 } else {
826 appWidgetId = widgetId;
827 }
828
Adam Cohenad4e15c2013-10-17 16:21:35 -0700829 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800830 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700831 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
832 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700833 result = RESULT_CANCELED;
834 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700835 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700836 @Override
837 public void run() {
838 exitSpringLoadedDragModeDelayed(false, 0, null);
839 }
840 };
Adam Cohendb364c32014-05-20 14:23:40 -0700841 if (workspaceLocked) {
842 // No need to remove the empty screen if we're mid-binding, as the
843 // the bind will not add the empty screen.
844 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
845 } else {
846 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
847 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
848 }
Winson Chung5aaab772012-04-27 15:24:02 -0700849 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700850 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700851 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
852 // When the screen id represents an actual screen (as opposed to a rank)
853 // we make sure that the drop page actually exists.
854 mPendingAddInfo.screenId =
855 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
856 }
Adam Cohendb364c32014-05-20 14:23:40 -0700857 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
858
859 dropLayout.setDropPending(true);
860 final Runnable onComplete = new Runnable() {
861 @Override
862 public void run() {
863 completeTwoStageWidgetDrop(resultCode, appWidgetId);
864 dropLayout.setDropPending(false);
865 }
866 };
867 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
868 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
869 } else {
870 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
871 mPendingAddInfo);
872 sPendingAddItem = args;
873 }
Winson Chung5aaab772012-04-27 15:24:02 -0700874 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 return;
876 }
877
Sunny Goyalff572272014-07-23 13:58:07 -0700878 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
879 if (resultCode == RESULT_OK) {
880 // Update the widget view.
881 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
882 pendingAddWidgetId, mPendingAddInfo);
883 if (workspaceLocked) {
884 sPendingAddItem = args;
885 } else {
886 completeAdd(args);
887 }
888 }
889 // Leave the widget in the pending state if the user canceled the configure.
890 return;
891 }
892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 // The pattern used here is that a user PICKs a specific application,
894 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700895
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
897 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700898 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700899 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
900 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800901 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700902 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800903 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700904 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700905 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
906 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 }
Adam Cohen00074722013-10-11 17:46:09 -0700908 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700909 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700910 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800912 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800913
914 }
915
916 @Override
917 protected void onActivityResult(
918 final int requestCode, final int resultCode, final Intent data) {
919 handleActivityResult(requestCode, resultCode, data);
920 if (mLauncherCallbacks != null) {
921 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
922 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 }
924
Adam Cohendb364c32014-05-20 14:23:40 -0700925 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
926 appWidgetId, ItemInfo info) {
927 PendingAddArguments args = new PendingAddArguments();
928 args.requestCode = requestCode;
929 args.intent = data;
930 args.container = info.container;
931 args.screenId = info.screenId;
932 args.cellX = info.cellX;
933 args.cellY = info.cellY;
934 args.appWidgetId = appWidgetId;
935 return args;
936 }
937
938 /**
939 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
940 *
941 * @param screenId the screen id to check
942 * @return the new screen, or screenId if it exists
943 */
944 private long ensurePendingDropLayoutExists(long screenId) {
945 CellLayout dropLayout =
946 (CellLayout) mWorkspace.getScreenWithId(screenId);
947 if (dropLayout == null) {
948 // it's possible that the add screen was removed because it was
949 // empty and a re-bind occurred
950 mWorkspace.addExtraEmptyScreen();
951 return mWorkspace.commitExtraEmptyScreen();
952 } else {
953 return screenId;
954 }
955 }
956
Adam Cohen091440a2015-03-18 14:16:05 -0700957 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700958 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700959 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800960 Runnable onCompleteRunnable = null;
961 int animationType = 0;
962
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700963 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800964 if (resultCode == RESULT_OK) {
965 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
966 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700967 mPendingAddWidgetInfo);
968 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800969 onCompleteRunnable = new Runnable() {
970 @Override
971 public void run() {
972 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700973 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700974 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
975 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800976 }
977 };
978 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800979 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800980 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800981 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700982 if (mDragLayer.getAnimatedView() != null) {
983 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
984 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
985 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700986 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700987 // The animated view may be null in the case of a rotation during widget configuration
988 onCompleteRunnable.run();
989 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 }
991
992 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700993 protected void onStop() {
994 super.onStop();
995 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700996
997 if (mLauncherCallbacks != null) {
998 mLauncherCallbacks.onStop();
999 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001000 }
1001
1002 @Override
1003 protected void onStart() {
1004 super.onStart();
1005 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -07001006
1007 if (mLauncherCallbacks != null) {
1008 mLauncherCallbacks.onStart();
1009 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001010 }
1011
1012 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001014 long startTime = 0;
1015 if (DEBUG_RESUME_TIME) {
1016 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001017 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001018 }
Adam Cohen9211d422014-10-07 18:14:53 -07001019
1020 if (mLauncherCallbacks != null) {
1021 mLauncherCallbacks.preOnResume();
1022 }
1023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001025
Winson Chung4a2afa32012-07-19 14:53:05 -07001026 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001027 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001028 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001029 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001030 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001031 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001032 // view after launching an app, as they may be depending on the UI to be static to
1033 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001034 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -07001035 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001036 } else if (mOnResumeState == State.WIDGETS) {
1037 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001038 }
1039 mOnResumeState = State.NONE;
1040
Winson Chungcd99cd32015-04-29 11:03:24 -07001041 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001042 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1043 // Otherwise, notify the callbacks if we are in all apps mode
1044 if (mState == State.APPS) {
1045 if (mLauncherCallbacks != null) {
1046 mLauncherCallbacks.onAllAppsShown();
1047 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001048 }
1049 }
1050
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001051 // Background was set to gradient in onPause(), restore to transparent if in all apps.
1052 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_TRANSPARENT
1053 : WORKSPACE_BACKGROUND_GRADIENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001054
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001055 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001056 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001057 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001058 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001059 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001060 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001062 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001063 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1064 // execute them here
1065 long startTimeCallbacks = 0;
1066 if (DEBUG_RESUME_TIME) {
1067 startTimeCallbacks = System.currentTimeMillis();
1068 }
1069
Michael Jurka1e2f4652013-07-08 18:03:46 -07001070 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1071 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001072 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001073 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001074 if (DEBUG_RESUME_TIME) {
1075 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1076 (System.currentTimeMillis() - startTimeCallbacks));
1077 }
Michael Jurka447bf842013-05-15 14:52:15 +02001078 }
Michael Jurka54554252013-08-01 12:52:23 +02001079 if (mOnResumeCallbacks.size() > 0) {
1080 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1081 mOnResumeCallbacks.get(i).run();
1082 }
1083 mOnResumeCallbacks.clear();
1084 }
Winson Chunge4e50662012-01-23 14:45:13 -08001085
1086 // Reset the pressed state of icons that were locked in the press state while activities
1087 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001088 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001089 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001090 mWaitingForResume.setStayPressed(false);
1091 }
Winson Chung82963d52013-10-09 11:20:57 -07001092
Adam Cohen06dff352012-06-01 17:17:08 -07001093 // It is possible that widgets can receive updates while launcher is not in the foreground.
1094 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1095 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1096 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001097 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001098 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001099
Michael Jurka447bf842013-05-15 14:52:15 +02001100 if (DEBUG_RESUME_TIME) {
1101 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1102 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001103
1104 if (mWorkspace.getCustomContentCallbacks() != null) {
1105 // If we are resuming and the custom content is the current page, we call onShow().
1106 // It is also poassible that onShow will instead be called slightly after first layout
1107 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1108 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001109 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001110 }
1111 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001112 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001113 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001114
Sunny Goyal756adbc2015-04-16 15:20:51 -07001115 if (!isWorkspaceLoading()) {
1116 // Process any items that were added while Launcher was away.
1117 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1118 }
Adam Cohen9211d422014-10-07 18:14:53 -07001119
1120 if (mLauncherCallbacks != null) {
1121 mLauncherCallbacks.onResume();
1122 }
Adam Cohen06dff352012-06-01 17:17:08 -07001123 }
1124
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001126 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001127 // Ensure that items added to Launcher are queued until Launcher returns
1128 InstallShortcutReceiver.enableInstallQueue();
1129
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001130 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001131 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001132 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001133 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001134
1135 // We call onHide() aggressively. The custom content callbacks should be able to
1136 // debounce excess onHide calls.
1137 if (mWorkspace.getCustomContentCallbacks() != null) {
1138 mWorkspace.getCustomContentCallbacks().onHide();
1139 }
Romain Guycbb89e42009-06-08 15:52:54 -07001140
Adam Cohen9211d422014-10-07 18:14:53 -07001141 if (mLauncherCallbacks != null) {
1142 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001143 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001144 }
1145
1146 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001147 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1148 // by a onResume or by scrolling otherwise.
1149 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001150
1151 // Custom content is completely hidden
1152 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001153
1154 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1155 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001156
1157 // Indicates whether the user is allowed to scroll away from the custom content.
1158 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001159 }
1160
Adam Cohenc2d6e892014-10-16 09:49:24 -07001161 public interface LauncherOverlay {
1162
1163 /**
1164 * Touch interaction leading to overscroll has begun
1165 */
1166 public void onScrollInteractionBegin();
1167
1168 /**
1169 * Touch interaction related to overscroll has ended
1170 */
1171 public void onScrollInteractionEnd();
1172
1173 /**
1174 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1175 * screen (or in the case of RTL, the rightmost screen).
1176 */
1177 public void onScrollChange(int progress, boolean rtl);
1178
1179 /**
1180 * Screen has stopped scrolling
1181 */
1182 public void onScrollSettled();
1183
1184 /**
1185 * This method can be called by the Launcher in order to force the LauncherOverlay
1186 * to exit fully immersive mode.
1187 */
1188 public void forceExitFullImmersion();
1189 }
1190
Winson Chung0f785722015-04-08 10:27:49 -07001191 public interface LauncherAppsCallbacks {
1192 /**
1193 * Updates launcher to the available space that AllApps can take so as not to overlap with
1194 * any other views.
1195 */
1196 public void onAllAppsBoundsChanged(Rect bounds);
1197
1198 /**
1199 * Called to dismiss all apps if it is showing.
1200 */
1201 public void dismissAllApps();
1202 }
1203
Adam Cohenc2d6e892014-10-16 09:49:24 -07001204 public interface LauncherOverlayCallbacks {
1205 /**
1206 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1207 * however it doesn't modify any state within the launcher.
1208 */
1209 public boolean canEnterFullImmersion();
1210
1211 /**
1212 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1213 * eg. by occupying the full screen and handling all touch events.
1214 *
1215 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1216 * case, Launcher will modify any necessary state and assumes the overlay is
1217 * handling all interaction. If false, the LauncherOverlay should cancel any
1218 *
1219 */
1220 public boolean enterFullImmersion();
1221
1222 /**
1223 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1224 * full control over UI and state.
1225 */
1226 public void exitFullImmersion();
1227 }
1228
1229 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1230
1231 @Override
1232 public boolean canEnterFullImmersion() {
1233 return mState == State.WORKSPACE;
1234 }
1235
1236 @Override
1237 public boolean enterFullImmersion() {
1238 if (mState == State.WORKSPACE) {
1239 // When fully immersed, disregard any touches which fall through.
1240 mDragLayer.setBlockTouch(true);
1241 return true;
1242 }
1243 return false;
1244 }
1245
1246 @Override
1247 public void exitFullImmersion() {
1248 mDragLayer.setBlockTouch(false);
1249 }
1250 }
1251
Jorim Jaggid017f882014-01-14 17:08:48 -08001252 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001253 if (mLauncherCallbacks != null) {
1254 return mLauncherCallbacks.hasSettings();
1255 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001256 return false;
1257 }
1258
Adam Cohen9211d422014-10-07 18:14:53 -07001259 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001260 CustomContentCallbacks callbacks, String description) {
1261 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001262 }
1263
Adam Cohenedb40762013-07-18 16:45:45 -07001264 // The custom content needs to offset its content to account for the QSB
1265 public int getTopOffsetForCustomContent() {
1266 return mWorkspace.getPaddingTop();
1267 }
1268
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001269 @Override
1270 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001271 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001272 if (mModel.isCurrentCallbacks(this)) {
1273 mModel.stopLoader();
1274 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001275 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1276
Romain Guy13c2e7b2010-03-10 19:45:00 -08001277 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001278 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001279
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001280 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001281 @Override
1282 public void onWindowFocusChanged(boolean hasFocus) {
1283 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001284 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001285
1286 if (mLauncherCallbacks != null) {
1287 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1288 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001289 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001290
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 private boolean acceptFilter() {
1292 final InputMethodManager inputManager = (InputMethodManager)
1293 getSystemService(Context.INPUT_METHOD_SERVICE);
1294 return !inputManager.isFullscreenMode();
1295 }
1296
1297 @Override
1298 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001299 final int uniChar = event.getUnicodeChar();
1300 final boolean handled = super.onKeyDown(keyCode, event);
1301 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1302 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1304 keyCode, event);
1305 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001306 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001307 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001308 // showSearchDialog()
1309 // If there are multiple keystrokes before the search dialog takes focus,
1310 // onSearchRequested() will be called for every keystroke,
1311 // but it is idempotent, so it's fine.
1312 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
1314 }
1315
Joe Onorato8a9625e2010-01-28 15:55:35 -08001316 // Eat the long press event so the keyboard doesn't come up.
1317 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1318 return true;
1319 }
1320
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 return handled;
1322 }
1323
Karl Rosaen138a0412009-04-23 19:00:21 -07001324 private String getTypedText() {
1325 return mDefaultKeySsb.toString();
1326 }
1327
1328 private void clearTypedText() {
1329 mDefaultKeySsb.clear();
1330 mDefaultKeySsb.clearSpans();
1331 Selection.setSelection(mDefaultKeySsb, 0);
1332 }
1333
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001335 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1336 * State
1337 */
1338 private static State intToState(int stateOrdinal) {
1339 State state = State.WORKSPACE;
1340 final State[] stateValues = State.values();
1341 for (int i = 0; i < stateValues.length; i++) {
1342 if (stateValues[i].ordinal() == stateOrdinal) {
1343 state = stateValues[i];
1344 break;
1345 }
1346 }
1347 return state;
1348 }
1349
1350 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 * Restores the previous state, if it exists.
1352 *
1353 * @param savedState The previous state.
1354 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001355 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 private void restoreState(Bundle savedState) {
1357 if (savedState == null) {
1358 return;
1359 }
1360
Michael Jurka883f55b2010-10-21 15:47:14 -07001361 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001362 if (state == State.APPS || state == State.WIDGETS) {
1363 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001364 }
1365
Adam Cohen21cd0022013-10-09 18:57:02 -07001366 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1367 PagedView.INVALID_RESTORE_PAGE);
1368 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001369 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 }
1371
Winson Chung3d503fb2011-07-13 17:25:49 -07001372 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001373 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001374
Winson Chung3d503fb2011-07-13 17:25:49 -07001375 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1376 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001377 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001378 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1379 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001380 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1381 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001382 AppWidgetProviderInfo info = savedState.getParcelable(
1383 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001384 mPendingAddWidgetInfo = info == null ?
1385 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1386
Adam Cohen4637b5a2013-11-04 18:21:24 -08001387 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001388 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 mRestoring = true;
1390 }
1391
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001392 mItemIdToViewId = (HashMap<Integer, Integer>)
1393 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 }
1395
1396 /**
1397 * Finds all the views we need and configure them properly.
1398 */
1399 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001400 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001401
Craig Mautner360310b2012-10-26 15:13:08 -07001402 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001403 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001404 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1405 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001406 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001407 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001408
John Spurlock77e1f472013-09-11 10:09:51 -04001409 mLauncherView.setSystemUiVisibility(
1410 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001411 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412
Winson Chung4c98d922011-05-31 16:50:48 -07001413 // Setup the drag layer
1414 mDragLayer.setup(this, dragController);
1415
Winson Chung3d503fb2011-07-13 17:25:49 -07001416 // Setup the hotseat
1417 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1418 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001419 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001420 }
1421
Jorim Jaggid017f882014-01-14 17:08:48 -08001422 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001423 View widgetButton = findViewById(R.id.widget_button);
1424 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001425 @Override
1426 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001427 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001428 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001429 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001430 }
1431 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001432 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1433
1434 View wallpaperButton = findViewById(R.id.wallpaper_button);
1435 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001436 @Override
1437 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001438 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001439 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001440 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001441 }
1442 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001443 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1444
1445 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001446 if (hasSettings()) {
1447 settingsButton.setOnClickListener(new OnClickListener() {
1448 @Override
1449 public void onClick(View arg0) {
1450 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001451 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001452 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001453 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001454 });
1455 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1456 } else {
1457 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001458 }
1459
Adam Cohendbdff6b2013-09-18 19:09:15 -07001460 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001461
Winson Chung4c98d922011-05-31 16:50:48 -07001462 // Setup the workspace
1463 mWorkspace.setHapticFeedbackEnabled(false);
1464 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001465 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001466 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001467
Winson Chungf0ea4d32011-06-06 14:27:16 -07001468 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001469 mSearchDropTargetBar = (SearchDropTargetBar)
1470 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001471
Winson Chungb745afb2015-03-02 11:51:23 -08001472 // Setup Apps
1473 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001474 if (isAllAppsSearchOverridden()) {
1475 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001476 }
Winson Chungb745afb2015-03-02 11:51:23 -08001477
Winson Chungf0ea4d32011-06-06 14:27:16 -07001478 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001479 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001480
Winson Chung3d503fb2011-07-13 17:25:49 -07001481 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001482 dragController.setDragScoller(mWorkspace);
1483 dragController.setScrollView(mDragLayer);
1484 dragController.setMoveTarget(mWorkspace);
1485 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001486 if (mSearchDropTargetBar != null) {
1487 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001488 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001489 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001490
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001491 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001492 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001493 mWeightWatcher = new WeightWatcher(this);
1494 mWeightWatcher.setAlpha(0.5f);
1495 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001496 new FrameLayout.LayoutParams(
1497 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001498 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001499 Gravity.BOTTOM)
1500 );
Adam Cohen39a06042013-07-19 14:30:12 -07001501
1502 boolean show = shouldShowWeightWatcher();
1503 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001504 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 }
1506
1507 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001508 * Sets the all apps button. This method is called from {@link Hotseat}.
1509 */
1510 public void setAllAppsButton(View allAppsButton) {
1511 mAllAppsButton = allAppsButton;
1512 }
1513
1514 public View getAllAppsButton() {
1515 return mAllAppsButton;
1516 }
1517
1518 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 * Creates a view representing a shortcut.
1520 *
1521 * @param info The data structure describing the shortcut.
1522 *
1523 * @return A View inflated from R.layout.application.
1524 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001525 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001526 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 }
1528
1529 /**
1530 * Creates a view representing a shortcut inflated from the specified resource.
1531 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 * @param parent The group the shortcut belongs to.
1533 * @param info The data structure describing the shortcut.
1534 *
1535 * @return A View inflated from layoutResId.
1536 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001537 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
1538 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.application,
1539 parent, false);
1540 favorite.applyFromShortcutInfo(info, mIconCache);
1541 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001543 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 return favorite;
1545 }
1546
1547 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 * Add a shortcut to the workspace.
1549 *
1550 * @param data The intent describing the shortcut.
1551 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001553 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001554 int cellY) {
1555 int[] cellXY = mTmpAddItemCellCoordinates;
1556 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001558
Sunny Goyal5c97f512015-05-19 16:03:28 -07001559 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001560 if (info == null) {
1561 return;
1562 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001563 final View view = createShortcut(info);
1564
Sunny Goyal5c97f512015-05-19 16:03:28 -07001565 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001566 // First we check if we already know the exact location where we want to add this item.
1567 if (cellX >= 0 && cellY >= 0) {
1568 cellXY[0] = cellX;
1569 cellXY[1] = cellY;
1570 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001571
1572 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001573 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001574 true, null,null)) {
1575 return;
1576 }
1577 DragObject dragObject = new DragObject();
1578 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001579 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1580 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001581 return;
1582 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001583 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001584 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001585 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001586 foundCellSpan = (result != null);
1587 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001588 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001589 }
1590
1591 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001592 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001593 return;
1594 }
1595
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001596 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597
1598 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001599 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001600 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 }
1602 }
1603
Adam Cohen2e6da152015-05-06 11:42:25 -07001604 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1605 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001606 // We want to account for the extra amount of padding that we are adding to the widget
1607 // to ensure that it gets the full amount of space that it has requested
1608 int requiredWidth = minWidth + padding.left + padding.right;
1609 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001610 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001611 }
1612
Adam Cohen2e6da152015-05-06 11:42:25 -07001613 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1614 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001615 }
1616
Adam Cohen2e6da152015-05-06 11:42:25 -07001617 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1618 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001619 }
1620
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001622 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001624 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 */
Adam Cohen091440a2015-03-18 14:16:05 -07001626 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001627 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1628
1629 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001630 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001631 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1632 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001633 }
Romain Guycbb89e42009-06-08 15:52:54 -07001634
Adam Cohen59400422014-03-05 18:07:04 -08001635 if (appWidgetInfo.isCustomWidget) {
1636 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001637 }
1638
Adam Cohen59400422014-03-05 18:07:04 -08001639 LauncherAppWidgetInfo launcherInfo;
1640 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1641 launcherInfo.spanX = info.spanX;
1642 launcherInfo.spanY = info.spanY;
1643 launcherInfo.minSpanX = info.minSpanX;
1644 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001645 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001646
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001648 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649
1650 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001651 if (hostView == null) {
1652 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001653 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1654 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001655 } else {
1656 // The AppWidgetHostView has already been inflated and instantiated
1657 launcherInfo.hostView = hostView;
1658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001660 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001661 launcherInfo.notifyWidgetSizeChanged(this);
1662
Adam Cohen59400422014-03-05 18:07:04 -08001663 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1664 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001665
1666 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001668 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 }
Romain Guycbb89e42009-06-08 15:52:54 -07001670
Adam Cohended9f8d2010-11-03 13:25:16 -07001671 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1672 @Override
1673 public void onReceive(Context context, Intent intent) {
1674 final String action = intent.getAction();
1675 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1676 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001677 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001678 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001679
Winson Chungc100e8e2011-08-09 16:02:43 -07001680 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001681 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001682 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001683 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001684 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001685 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001686 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1687 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001688 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001689 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1690 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001691 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001692 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1693 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1694 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001695 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001696 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1697 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 }
1699 }
1700 };
1701
1702 @Override
1703 public void onAttachedToWindow() {
1704 super.onAttachedToWindow();
1705
1706 // Listen for broadcasts related to user-presence
1707 final IntentFilter filter = new IntentFilter();
1708 filter.addAction(Intent.ACTION_SCREEN_OFF);
1709 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001710 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001711 if (ENABLE_DEBUG_INTENTS) {
1712 filter.addAction(DebugIntents.DELETE_DATABASE);
1713 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1714 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001716 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001717 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001718 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 mVisible = true;
1720 }
1721
Adam Cohen0b395352014-06-09 22:54:36 +00001722 /**
1723 * Sets up transparent navigation and status bars in LMP.
1724 * This method is a no-op for other platform versions.
1725 */
Sunny Goyald0091012015-01-20 15:55:34 -08001726 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001727 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001728 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001729 Window window = getWindow();
1730 window.getAttributes().systemUiVisibility |=
1731 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1732 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1733 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1734 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1735 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1736 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1737 window.setStatusBarColor(Color.TRANSPARENT);
1738 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001739 }
1740 }
1741
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 @Override
1743 public void onDetachedFromWindow() {
1744 super.onDetachedFromWindow();
1745 mVisible = false;
1746
Adam Cohend113e0c2010-11-11 10:48:05 -08001747 if (mAttached) {
1748 unregisterReceiver(mReceiver);
1749 mAttached = false;
1750 }
Winson Chungb745afb2015-03-02 11:51:23 -08001751 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 }
1753
1754 public void onWindowVisibilityChanged(int visibility) {
1755 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001756 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001757 // The following code used to be in onResume, but it turns out onResume is called when
1758 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1759 // is a more appropriate event to handle
1760 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001761 if (!mWorkspaceLoading) {
1762 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001763 // We want to let Launcher draw itself at least once before we force it to build
1764 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001765 // apps is nice and speedy.
1766 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001767 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001768 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001769 if (mStarted) return;
1770 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001771 // We delay the layer building a bit in order to give
1772 // other message processing a time to run. In particular
1773 // this avoids a delay in hiding the IME if it was
1774 // currently shown, because doing that may involve
1775 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001776 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001777 final ViewTreeObserver.OnDrawListener listener = this;
1778 mWorkspace.post(new Runnable() {
1779 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001780 if (mWorkspace != null &&
1781 mWorkspace.getViewTreeObserver() != null) {
1782 mWorkspace.getViewTreeObserver().
1783 removeOnDrawListener(listener);
1784 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001785 }
1786 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001787 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001788 }
1789 });
1790 }
1791 clearTypedText();
1792 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001793 }
1794
Adam Cohen091440a2015-03-18 14:16:05 -07001795 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001796 mHandler.removeMessages(ADVANCE_MSG);
1797 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1798 mHandler.sendMessageDelayed(msg, delay);
1799 mAutoAdvanceSentTime = System.currentTimeMillis();
1800 }
1801
Adam Cohen091440a2015-03-18 14:16:05 -07001802 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001803 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1804 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1805 mAutoAdvanceRunning = autoAdvanceRunning;
1806 if (autoAdvanceRunning) {
1807 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1808 sendAdvanceMessage(delay);
1809 } else {
1810 if (!mWidgetsToAdvance.isEmpty()) {
1811 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1812 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1813 }
1814 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001815 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001816 }
1817 }
1818 }
1819
1820 private final Handler mHandler = new Handler() {
1821 @Override
1822 public void handleMessage(Message msg) {
1823 if (msg.what == ADVANCE_MSG) {
1824 int i = 0;
1825 for (View key: mWidgetsToAdvance.keySet()) {
1826 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1827 final int delay = mAdvanceStagger * i;
1828 if (v instanceof Advanceable) {
1829 postDelayed(new Runnable() {
1830 public void run() {
1831 ((Advanceable) v).advance();
1832 }
1833 }, delay);
1834 }
1835 i++;
1836 }
1837 sendAdvanceMessage(mAdvanceInterval);
1838 }
1839 }
1840 };
1841
1842 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001843 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001844 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1845 if (v instanceof Advanceable) {
1846 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001847 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001848 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001849 }
1850 }
1851
1852 void removeWidgetToAutoAdvance(View hostView) {
1853 if (mWidgetsToAdvance.containsKey(hostView)) {
1854 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001855 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001856 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001857 }
1858
Joe Onorato9c1289c2009-08-17 11:03:03 -04001859 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001860 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001861 launcherInfo.hostView = null;
1862 }
1863
Hyunyoung Song3f471442015-04-08 19:01:34 -07001864 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001865 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1866 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001867 }
1868
Winson Chunga6945242014-01-08 14:04:34 -08001869 public DragLayer getDragLayer() {
1870 return mDragLayer;
1871 }
1872
Winson Chungb745afb2015-03-02 11:51:23 -08001873 public AppsContainerView getAppsView() {
1874 return mAppsView;
1875 }
1876
Hyunyoung Song3f471442015-04-08 19:01:34 -07001877 public WidgetsContainerView getWidgetsView() {
1878 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001879 }
1880
Winson Chunga6945242014-01-08 14:04:34 -08001881 public Workspace getWorkspace() {
1882 return mWorkspace;
1883 }
1884
1885 public Hotseat getHotseat() {
1886 return mHotseat;
1887 }
1888
Jorim Jaggid017f882014-01-14 17:08:48 -08001889 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001890 return mOverviewPanel;
1891 }
1892
1893 public SearchDropTargetBar getSearchBar() {
1894 return mSearchDropTargetBar;
1895 }
1896
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001897 public LauncherAppWidgetHost getAppWidgetHost() {
1898 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 }
Romain Guycbb89e42009-06-08 15:52:54 -07001900
Winson Chunga9abd0e2010-10-27 17:18:37 -07001901 public LauncherModel getModel() {
1902 return mModel;
1903 }
1904
Winson Chunga6945242014-01-08 14:04:34 -08001905 protected SharedPreferences getSharedPrefs() {
1906 return mSharedPrefs;
1907 }
1908
Adam Cohen2e6da152015-05-06 11:42:25 -07001909 public DeviceProfile getDeviceProfile() {
1910 return mDeviceProfile;
1911 }
1912
Bjorn Bringertc459e522013-06-07 19:36:01 +01001913 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001914 getWindow().closeAllPanels();
1915
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001916 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001917 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001918 }
1919
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 @Override
1921 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001922 long startTime = 0;
1923 if (DEBUG_RESUME_TIME) {
1924 startTime = System.currentTimeMillis();
1925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 super.onNewIntent(intent);
1927
1928 // Close the menu
1929 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001930 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001931 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001932
Adam Cohened307df2013-10-02 09:37:31 -07001933 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1934 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1935 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001936
Adam Cohen6fecd412013-10-02 17:41:50 -07001937 if (mWorkspace == null) {
1938 // Can be cases where mWorkspace is null, this prevents a NPE
1939 return;
1940 }
1941 Folder openFolder = mWorkspace.getOpenFolder();
1942 // In all these cases, only animate if we're already on home
1943 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001944
1945 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1946 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001947 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001948 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001949 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001950 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001951
Adam Cohen6fecd412013-10-02 17:41:50 -07001952 closeFolder();
1953 exitSpringLoadedDragMode();
1954
1955 // If we are already on home, then just animate back to the workspace,
1956 // otherwise, just wait until onResume to set the state back to Workspace
1957 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001958 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001959 } else {
1960 mOnResumeState = State.WORKSPACE;
1961 }
1962
1963 final View v = getWindow().peekDecorView();
1964 if (v != null && v.getWindowToken() != null) {
1965 InputMethodManager imm = (InputMethodManager)getSystemService(
1966 INPUT_METHOD_SERVICE);
1967 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1968 }
1969
Winson Chungb745afb2015-03-02 11:51:23 -08001970 // Reset the apps view
1971 if (!alreadyOnHome && mAppsView != null) {
1972 mAppsView.scrollToTop();
1973 }
1974
Hyunyoung Song3f471442015-04-08 19:01:34 -07001975 // Reset the widgets view
1976 if (!alreadyOnHome && mWidgetsView != null) {
1977 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001978 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001979
Adam Cohen9211d422014-10-07 18:14:53 -07001980 if (mLauncherCallbacks != null) {
1981 mLauncherCallbacks.onHomeIntent();
1982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
Adam Cohened307df2013-10-02 09:37:31 -07001984
Michael Jurka447bf842013-05-15 14:52:15 +02001985 if (DEBUG_RESUME_TIME) {
1986 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988
Adam Cohen9211d422014-10-07 18:14:53 -07001989 if (mLauncherCallbacks != null) {
1990 mLauncherCallbacks.onNewIntent(intent);
1991 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001992 }
1993
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001995 public void onRestoreInstanceState(Bundle state) {
1996 super.onRestoreInstanceState(state);
1997 for (int page: mSynchronouslyBoundPages) {
1998 mWorkspace.restoreInstanceStateForChild(page);
1999 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 }
2001
2002 @Override
2003 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002004 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002005 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2006 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002007 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002008 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009
Michael Jurka883f55b2010-10-21 15:47:14 -07002010 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002011 // We close any open folder since it will not be re-opened, and we need to make sure
2012 // this state is reflected.
2013 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014
Adam Cohendcd297f2013-06-18 13:13:40 -07002015 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002016 mWaitingForResult) {
2017 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002018 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002019 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2020 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002021 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2022 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2023 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002024 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 }
2026
Hyunyoung Song3f471442015-04-08 19:01:34 -07002027 // Save the current widgets tray?
2028 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002029 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002030
2031 if (mLauncherCallbacks != null) {
2032 mLauncherCallbacks.onSaveInstanceState(outState);
2033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
2035
2036 @Override
2037 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002039
Winson Chunge7a03942011-08-05 15:05:12 -07002040 // Remove all pending runnables
2041 mHandler.removeMessages(ADVANCE_MSG);
2042 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002043 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002044
Winson Chungcd2b0142011-06-08 16:02:26 -07002045 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002046 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002047
2048 // It's possible to receive onDestroy after a new Launcher activity has
2049 // been created. In this case, don't interfere with the new Launcher.
2050 if (mModel.isCurrentCallbacks(this)) {
2051 mModel.stopLoader();
2052 app.setLauncher(null);
2053 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002054
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002056 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002058 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002060 mAppWidgetHost = null;
2061
2062 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063
2064 TextKeyListener.getInstance().release();
2065
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002066 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002067 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002068
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002069 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002070 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002071 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002072 mWorkspace = null;
2073 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002074
Michael Jurka2ecf9952012-06-18 12:52:28 -07002075 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002076
2077 if (mLauncherCallbacks != null) {
2078 mLauncherCallbacks.onDestroy();
2079 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 }
2081
Adam Cohena9cf38f2011-05-02 15:36:58 -07002082 public DragController getDragController() {
2083 return mDragController;
2084 }
2085
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 @Override
2087 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002088 onStartForResult(requestCode);
2089 super.startActivityForResult(intent, requestCode);
2090 }
2091
2092 @Override
2093 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2094 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2095 onStartForResult(requestCode);
2096 try {
2097 super.startIntentSenderForResult(intent, requestCode,
2098 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2099 } catch (IntentSender.SendIntentException e) {
2100 throw new ActivityNotFoundException();
2101 }
2102 }
2103
2104 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002105 if (requestCode >= 0) {
2106 setWaitingForResult(true);
2107 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 }
2109
Winson Chung70d72102011-08-12 11:24:35 -07002110 /**
2111 * Indicates that we want global search for this activity by setting the globalSearch
2112 * argument for {@link #startSearch} to true.
2113 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002115 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002117
Karl Rosaen138a0412009-04-23 19:00:21 -07002118 if (initialQuery == null) {
2119 // Use any text typed in the launcher as the initial query
2120 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002121 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 if (appSearchData == null) {
2123 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002124 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 }
Winson Chungadf0c182012-08-23 14:59:07 -07002126 Rect sourceBounds = new Rect();
2127 if (mSearchDropTargetBar != null) {
2128 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2129 }
Romain Guycbb89e42009-06-08 15:52:54 -07002130
Ian Parkinson047036e2014-09-01 15:40:53 +01002131 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002132 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002133 if (clearTextImmediately) {
2134 clearTypedText();
2135 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002136
2137 // We need to show the workspace after starting the search
2138 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002139 }
2140
Ian Parkinson047036e2014-09-01 15:40:53 +01002141 /**
2142 * Start a text search.
2143 *
2144 * @return {@code true} if the search will start immediately, so any further keypresses
2145 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2146 * to buffer keypresses.
2147 */
2148 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002149 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002150 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2151 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2152 sourceBounds);
2153 }
2154
Michael Jurkaa33411c2012-06-14 16:18:21 -07002155 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002156 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002157 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002158 }
2159
2160 /**
2161 * Starts the global search activity. This code is a copied from SearchManager
2162 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002163 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002164 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002165 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002166 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2167 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2168 if (globalSearchActivity == null) {
2169 Log.w(TAG, "No global search activity found.");
2170 return;
2171 }
2172 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2173 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2174 intent.setComponent(globalSearchActivity);
2175 // Make sure that we have a Bundle to put source in
2176 if (appSearchData == null) {
2177 appSearchData = new Bundle();
2178 } else {
2179 appSearchData = new Bundle(appSearchData);
2180 }
Adam Cohen9211d422014-10-07 18:14:53 -07002181 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002182 if (!appSearchData.containsKey("source")) {
2183 appSearchData.putString("source", getPackageName());
2184 }
2185 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2186 if (!TextUtils.isEmpty(initialQuery)) {
2187 intent.putExtra(SearchManager.QUERY, initialQuery);
2188 }
2189 if (selectInitialQuery) {
2190 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2191 }
2192 intent.setSourceBounds(sourceBounds);
2193 try {
2194 startActivity(intent);
2195 } catch (ActivityNotFoundException ex) {
2196 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2197 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 }
2199
Yura4f93ec62014-02-04 14:15:21 +00002200 public boolean isOnCustomContent() {
2201 return mWorkspace.isOnOrMovingToCustomContent();
2202 }
2203
Winson Chung70d72102011-08-12 11:24:35 -07002204 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002205 public boolean onPrepareOptionsMenu(Menu menu) {
2206 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002207 if (!isOnCustomContent()) {
2208 // Close any open folders
2209 closeFolder();
2210 // Stop resizing any widgets
2211 mWorkspace.exitWidgetResizeMode();
2212 if (!mWorkspace.isInOverviewMode()) {
2213 // Show the overview mode
2214 showOverviewMode(true);
2215 } else {
2216 showWorkspace(true);
2217 }
Winson Chunge0298742014-01-17 12:03:00 -08002218 }
Adam Cohen9211d422014-10-07 18:14:53 -07002219 if (mLauncherCallbacks != null) {
2220 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2221 }
2222
Michael Jurkab94f3f82013-09-11 18:08:54 +02002223 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002226 @Override
2227 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002228 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002229 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002230 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002231 }
2232
Joe Onorato9c1289c2009-08-17 11:03:03 -04002233 public boolean isWorkspaceLocked() {
2234 return mWorkspaceLoading || mWaitingForResult;
2235 }
2236
Adam Cohen517a7f52014-03-01 12:12:59 -08002237 public boolean isWorkspaceLoading() {
2238 return mWorkspaceLoading;
2239 }
2240
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002241 private void setWorkspaceLoading(boolean value) {
2242 boolean isLocked = isWorkspaceLocked();
2243 mWorkspaceLoading = value;
2244 if (isLocked != isWorkspaceLocked()) {
2245 onWorkspaceLockedChanged();
2246 }
2247 }
2248
2249 private void setWaitingForResult(boolean value) {
2250 boolean isLocked = isWorkspaceLocked();
2251 mWaitingForResult = value;
2252 if (isLocked != isWorkspaceLocked()) {
2253 onWorkspaceLockedChanged();
2254 }
2255 }
2256
Adam Cohen9211d422014-10-07 18:14:53 -07002257 protected void onWorkspaceLockedChanged() {
2258 if (mLauncherCallbacks != null) {
2259 mLauncherCallbacks.onWorkspaceLockedChanged();
2260 }
2261 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002262
Michael Jurka0280c3b2010-09-17 15:00:07 -07002263 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002264 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002265 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002266 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2267 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002268 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002269 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002270 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002271
Sunny Goyale6b63a32015-01-16 16:14:29 -08002272 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002273 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002274 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2275 }
2276
Sunny Goyale6b63a32015-01-16 16:14:29 -08002277 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002278 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2279 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002280 if (appWidgetInfo.configure != null) {
2281 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002282 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002283
Bjorn Bringert7984c942009-12-09 15:38:25 +00002284 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002285 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2286 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002289 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002290 Runnable onComplete = new Runnable() {
2291 @Override
2292 public void run() {
2293 // Exit spring loaded mode if necessary after adding the widget
2294 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2295 null);
2296 }
2297 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002298 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002299 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002300 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 }
2302 }
Romain Guycbb89e42009-06-08 15:52:54 -07002303
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002304 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002305 // Close any folders that may be open.
2306 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002307 mWorkspace.moveToCustomContentScreen(animate);
2308 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002309
2310 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2311 int[] cell, int spanX, int spanY) {
2312 switch (info.itemType) {
2313 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2314 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2315 int span[] = new int[2];
2316 span[0] = spanX;
2317 span[1] = spanY;
2318 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2319 container, screenId, cell, span);
2320 break;
2321 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2322 processShortcutFromDrop(info.componentName, container, screenId, cell);
2323 break;
2324 default:
2325 throw new IllegalStateException("Unknown item type: " + info.itemType);
2326 }
2327 }
2328
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 /**
2330 * Process a shortcut drop.
2331 *
2332 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002333 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002334 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002335 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002336 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2337 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002338 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002339 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002340 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002341 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342
2343 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002344 mPendingAddInfo.cellX = cell[0];
2345 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002346 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002347
2348 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2349 createShortcutIntent.setComponent(componentName);
2350 processShortcut(createShortcutIntent);
2351 }
2352
Adam Cohenfbba09b2011-07-18 21:43:05 -07002353 /**
2354 * Process a widget drop.
2355 *
2356 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002357 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002358 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002359 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002360 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2361 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002362 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002363 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002364 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002365 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002366 mPendingAddInfo.minSpanX = info.minSpanX;
2367 mPendingAddInfo.minSpanY = info.minSpanY;
2368
Adam Cohenfbba09b2011-07-18 21:43:05 -07002369 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002370 mPendingAddInfo.cellX = cell[0];
2371 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002372 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002373 if (span != null) {
2374 mPendingAddInfo.spanX = span[0];
2375 mPendingAddInfo.spanY = span[1];
2376 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002377
Adam Cohened66b2b2012-01-23 17:28:51 -08002378 AppWidgetHostView hostView = info.boundWidget;
2379 int appWidgetId;
2380 if (hostView != null) {
2381 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002382 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002383 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002384 // In this case, we either need to start an activity to get permission to bind
2385 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002386 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002387 Bundle options = info.bindOptions;
2388
Sunny Goyalffe83f12014-08-14 17:39:34 -07002389 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2390 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002391 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002392 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002393 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002394 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002395 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2396 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2397 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002398 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2399 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002400 // TODO: we need to make sure that this accounts for the options bundle.
2401 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002402 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2403 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002404 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002405 }
2406
Joe Onoratodeb98af2010-02-19 14:59:39 -08002407 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002408 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 }
2410
Winson Chung24ab2f12010-09-16 14:10:47 -07002411 void processWallpaper(Intent intent) {
2412 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2413 }
2414
Adam Cohendcd297f2013-06-18 13:13:40 -07002415 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002416 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002417 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 folderInfo.title = getText(R.string.folder_name);
2419
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002421 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2422 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002423 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424
2425 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002426 FolderIcon newFolder =
2427 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002428 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002429 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002430 // Force measure the new folder icon
2431 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2432 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002433 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 }
Romain Guycbb89e42009-06-08 15:52:54 -07002435
Joe Onorato9c1289c2009-08-17 11:03:03 -04002436 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002437 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002438 }
2439
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002440 /**
2441 * Registers various content observers. The current implementation registers
2442 * only a favorites observer to keep track of the favorites applications.
2443 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002444 private void registerContentObservers() {
2445 ContentResolver resolver = getContentResolver();
2446 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2447 true, mWidgetObserver);
2448 }
2449
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 @Override
2451 public boolean dispatchKeyEvent(KeyEvent event) {
2452 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2453 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002454 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002455 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002456 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002457 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002458 dumpState();
2459 return true;
2460 }
2461 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002462 }
2463 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2464 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002465 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 return true;
2467 }
2468 }
2469
2470 return super.dispatchKeyEvent(event);
2471 }
2472
Joe Onorato88ec0992009-11-19 13:16:06 -08002473 @Override
2474 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002475 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2476 return;
2477 }
2478
Winson Chung3d9490a2015-03-24 17:38:42 -07002479 LauncherAccessibilityDelegate delegate =
2480 LauncherAppState.getInstance().getAccessibilityDelegate();
2481 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002482 return;
2483 }
2484
Winson Chungb745afb2015-03-02 11:51:23 -08002485 if (isAppsViewVisible()) {
2486 showWorkspace(true);
2487 } else if (isWidgetsViewVisible()) {
2488 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002489 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002490 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002491 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002492 Folder openFolder = mWorkspace.getOpenFolder();
2493 if (openFolder.isEditingName()) {
2494 openFolder.dismissEditingName();
2495 } else {
2496 closeFolder();
2497 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002498 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002499 mWorkspace.exitWidgetResizeMode();
2500
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002501 // Back button is a no-op here, but give at least some feedback for the button press
2502 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002503 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002504 }
2505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002507 * Re-listen when widgets are reset.
2508 */
Adam Cohen091440a2015-03-18 14:16:05 -07002509 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002510 if (mAppWidgetHost != null) {
2511 mAppWidgetHost.startListening();
2512 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002513 }
2514
2515 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 * Launches the intent referred by the clicked shortcut.
2517 *
2518 * @param v The view representing the clicked shortcut.
2519 */
2520 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002521 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2522 // view has detached (it's possible for this to happen if the view is removed mid touch).
2523 if (v.getWindowToken() == null) {
2524 return;
2525 }
2526
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002527 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002528 return;
2529 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002530
Adam Cohen1697b792013-09-17 19:08:21 -07002531 if (v instanceof Workspace) {
2532 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002533 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002534 }
2535 return;
2536 }
2537
2538 if (v instanceof CellLayout) {
2539 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002540 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002541 }
2542 }
2543
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002544 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002545 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002546 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002548 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002549 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002550 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002551 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002552 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002553 } else if (tag instanceof AppInfo) {
2554 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002555 } else if (tag instanceof LauncherAppWidgetInfo) {
2556 if (v instanceof PendingAppWidgetHostView) {
2557 onClickPendingWidget((PendingAppWidgetHostView) v);
2558 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002559 }
2560 }
2561
Sunny Goyal508da152014-08-14 10:53:27 -07002562 public void onClickPagedViewIcon(View v) {
2563 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002564 if (mLauncherCallbacks != null) {
2565 mLauncherCallbacks.onClickPagedViewIcon(v);
2566 }
Sunny Goyal508da152014-08-14 10:53:27 -07002567 }
2568
Sunny Goyal70660032015-05-14 00:07:08 -07002569 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002570 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002571 return false;
2572 }
2573
Michael Jurkaaf442092010-06-10 17:01:57 -07002574 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002575 * Event handler for the app widget view which has not fully restored.
2576 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002577 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002578 if (mIsSafeModeEnabled) {
2579 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2580 return;
2581 }
2582
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002583 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002584 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002585 int widgetId = info.appWidgetId;
2586 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2587 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002588 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2589 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002590 mPendingAddInfo.copyFrom(info);
2591 mPendingAddWidgetId = widgetId;
2592
Sunny Goyalffe83f12014-08-14 17:39:34 -07002593 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2594 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002595 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002596 } else if (info.installProgress < 0) {
2597 // The install has not been queued
2598 final String packageName = info.providerName.getPackageName();
2599 showBrokenAppInstallDialog(packageName,
2600 new DialogInterface.OnClickListener() {
2601 public void onClick(DialogInterface dialog, int id) {
2602 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2603 }
2604 });
2605 } else {
2606 // Download has started.
2607 final String packageName = info.providerName.getPackageName();
2608 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002609 }
2610 }
2611
2612 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002613 * Event handler for the "grid" button that appears on the home screen, which
2614 * enters all apps mode.
2615 *
2616 * @param v The view that was clicked.
2617 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002618 protected void onClickAllAppsButton(View v) {
2619 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002620 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002621 showWorkspace(true);
2622 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002623 // Try and refresh the set of predicted apps before we enter launcher
2624 showAppsView(true /* animated */, false /* resetListToTop */,
2625 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002626 }
2627 }
2628
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002629 private void showBrokenAppInstallDialog(final String packageName,
2630 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002631 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002632 .setTitle(R.string.abandoned_promises_title)
2633 .setMessage(R.string.abandoned_promise_explanation)
2634 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2635 .setNeutralButton(R.string.abandoned_clean_this,
2636 new DialogInterface.OnClickListener() {
2637 public void onClick(DialogInterface dialog, int id) {
2638 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2639 mWorkspace.removeAbandonedPromise(packageName, user);
2640 }
2641 })
2642 .create().show();
2643 return;
2644 }
2645
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002646 /**
2647 * Event handler for an app shortcut click.
2648 *
2649 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2650 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002651 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002652 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2653 Object tag = v.getTag();
2654 if (!(tag instanceof ShortcutInfo)) {
2655 throw new IllegalArgumentException("Input must be a Shortcut");
2656 }
2657
2658 // Open shortcut
2659 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002660
2661 if (shortcut.isDisabled != 0) {
2662 int error = R.string.activity_not_available;
2663 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2664 error = R.string.safemode_shortcut_error;
2665 }
2666 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2667 return;
2668 }
2669
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002670 final Intent intent = shortcut.intent;
2671
2672 // Check for special shortcuts
2673 if (intent.getComponent() != null) {
2674 final String shortcutClass = intent.getComponent().getClassName();
2675
2676 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2677 MemoryDumpActivity.startDump(this);
2678 return;
2679 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2680 toggleShowWeightWatcher();
2681 return;
2682 }
2683 }
2684
Chris Wren40c5ed32014-06-24 18:24:23 -04002685 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002686 if ((v instanceof BubbleTextView)
2687 && shortcut.isPromise()
2688 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002689 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002690 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002691 new DialogInterface.OnClickListener() {
2692 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002693 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002694 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002695 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002696 return;
2697 }
2698
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002699 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002700 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002701
2702 if (mLauncherCallbacks != null) {
2703 mLauncherCallbacks.onClickAppShortcut(v);
2704 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002705 }
2706
Adam Cohen091440a2015-03-18 14:16:05 -07002707 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002708 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002709 final ShortcutInfo shortcut;
2710 final Intent intent;
2711 if (tag instanceof ShortcutInfo) {
2712 shortcut = (ShortcutInfo) tag;
2713 intent = shortcut.intent;
2714 int[] pos = new int[2];
2715 v.getLocationOnScreen(pos);
2716 intent.setSourceBounds(new Rect(pos[0], pos[1],
2717 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002718
Sunny Goyal508da152014-08-14 10:53:27 -07002719 } else if (tag instanceof AppInfo) {
2720 shortcut = null;
2721 intent = ((AppInfo) tag).intent;
2722 } else {
2723 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2724 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002725
2726 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002727 mStats.recordLaunch(intent, shortcut);
2728
2729 if (success && v instanceof BubbleTextView) {
2730 mWaitingForResume = (BubbleTextView) v;
2731 mWaitingForResume.setStayPressed(true);
2732 }
2733 }
2734
2735 /**
2736 * Event handler for a folder icon click.
2737 *
2738 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2739 */
2740 protected void onClickFolderIcon(View v) {
2741 if (LOGD) Log.d(TAG, "onClickFolder");
2742 if (!(v instanceof FolderIcon)){
2743 throw new IllegalArgumentException("Input must be a FolderIcon");
2744 }
2745
2746 FolderIcon folderIcon = (FolderIcon) v;
2747 final FolderInfo info = folderIcon.getFolderInfo();
2748 Folder openFolder = mWorkspace.getFolderForTag(info);
2749
2750 // If the folder info reports that the associated folder is open, then verify that
2751 // it is actually opened. There have been a few instances where this gets out of sync.
2752 if (info.opened && openFolder == null) {
2753 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2754 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2755 info.opened = false;
2756 }
2757
2758 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2759 // Close any open folder
2760 closeFolder();
2761 // Open the requested folder
2762 openFolder(folderIcon);
2763 } else {
2764 // Find the open folder...
2765 int folderScreen;
2766 if (openFolder != null) {
2767 folderScreen = mWorkspace.getPageForView(openFolder);
2768 // .. and close it
2769 closeFolder(openFolder);
2770 if (folderScreen != mWorkspace.getCurrentPage()) {
2771 // Close any folder open on the current screen
2772 closeFolder();
2773 // Pull the folder onto this screen
2774 openFolder(folderIcon);
2775 }
2776 }
2777 }
Adam Cohen9211d422014-10-07 18:14:53 -07002778
2779 if (mLauncherCallbacks != null) {
2780 mLauncherCallbacks.onClickFolderIcon(v);
2781 }
Michael Jurka5130e402011-10-13 04:55:35 -07002782 }
2783
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002784 /**
2785 * Event handler for the (Add) Widgets button that appears after a long press
2786 * on the home screen.
2787 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002788 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002789 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002790 if (mIsSafeModeEnabled) {
2791 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2792 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002793 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002794 if (mLauncherCallbacks != null) {
2795 mLauncherCallbacks.onClickAddWidgetButton(view);
2796 }
Adam Cohen9211d422014-10-07 18:14:53 -07002797 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002798 }
2799
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002800 /**
2801 * Event handler for the wallpaper picker button that appears after a long press
2802 * on the home screen.
2803 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002804 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002805 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002806 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2807 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002808
2809 if (mLauncherCallbacks != null) {
2810 mLauncherCallbacks.onClickWallpaperPicker(v);
2811 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002812 }
2813
2814 /**
2815 * Event handler for a click on the settings button that appears after a long press
2816 * on the home screen.
2817 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002818 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002819 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002820 if (mLauncherCallbacks != null) {
2821 mLauncherCallbacks.onClickSettingsButton(v);
2822 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002823 }
2824
Michael Jurka5130e402011-10-13 04:55:35 -07002825 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002826 // Provide the same haptic feedback that the system offers for virtual keys.
2827 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002828 }
2829
Adam Cohen61f560d2013-09-30 15:58:20 -07002830 public void performHapticFeedbackOnTouchDown(View v) {
2831 // Provide the same haptic feedback that the system offers for virtual keys.
2832 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2833 }
2834
2835 public View.OnTouchListener getHapticFeedbackTouchListener() {
2836 if (mHapticFeedbackTouchListener == null) {
2837 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002838 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002839 @Override
2840 public boolean onTouch(View v, MotionEvent event) {
2841 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2842 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2843 }
2844 return false;
2845 }
2846 };
2847 }
2848 return mHapticFeedbackTouchListener;
2849 }
2850
Adam Cohen9211d422014-10-07 18:14:53 -07002851 public void onDragStarted(View view) {
2852 if (isOnCustomContent()) {
2853 // Custom content screen doesn't participate in drag and drop. If on custom
2854 // content screen, move to default.
2855 moveWorkspaceToDefaultScreen();
2856 }
2857
2858 if (mLauncherCallbacks != null) {
2859 mLauncherCallbacks.onDragStarted(view);
2860 }
2861 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002862
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002863 /**
2864 * Called when the user stops interacting with the launcher.
2865 * This implies that the user is now on the homescreen and is not doing housekeeping.
2866 */
Adam Cohen9211d422014-10-07 18:14:53 -07002867 protected void onInteractionEnd() {
2868 if (mLauncherCallbacks != null) {
2869 mLauncherCallbacks.onInteractionEnd();
2870 }
2871 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002872
2873 /**
2874 * Called when the user starts interacting with the launcher.
2875 * The possible interactions are:
2876 * - open all apps
2877 * - reorder an app shortcut, or a widget
2878 * - open the overview mode.
2879 * This is a good time to stop doing things that only make sense
2880 * when the user is on the homescreen and not doing housekeeping.
2881 */
Adam Cohen9211d422014-10-07 18:14:53 -07002882 protected void onInteractionBegin() {
2883 if (mLauncherCallbacks != null) {
2884 mLauncherCallbacks.onInteractionBegin();
2885 }
2886 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002887
Winson Chungcd99cd32015-04-29 11:03:24 -07002888 /** Updates the interaction state. */
2889 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002890 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002891 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002892 boolean fromStateWithOverlay;
2893 boolean toStateWithOverlay;
2894 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2895 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2896 toStateWithOverlay = toState != Workspace.State.NORMAL;
2897 } else {
2898 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2899 fromState != Workspace.State.NORMAL_HIDDEN;
2900 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2901 toState != Workspace.State.NORMAL_HIDDEN;
2902 }
2903 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002904 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002905 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002906 onInteractionEnd();
2907 }
2908 }
2909
Kenny Guyf07af7b2014-07-31 11:39:16 +01002910 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002911 try {
2912 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002913 launcherApps.showAppDetailsForProfile(componentName, user);
2914 } catch (SecurityException e) {
2915 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2916 Log.e(TAG, "Launcher does not have permission to launch settings");
2917 } catch (ActivityNotFoundException e) {
2918 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2919 Log.e(TAG, "Unable to launch settings");
2920 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002921 }
2922
Michael Jurka1e2f4652013-07-08 18:03:46 -07002923 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002924 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2925 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002926 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002927 // System applications cannot be installed. For now, show a toast explaining that.
2928 // We may give them the option of disabling apps this way.
2929 int messageId = R.string.uninstall_system_app_text;
2930 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002931 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002932 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002933 String packageName = componentName.getPackageName();
2934 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002935 Intent intent = new Intent(
2936 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002937 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2938 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002939 if (user != null) {
2940 user.addToIntent(intent, Intent.EXTRA_USER);
2941 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002942 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002943 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002944 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002945 }
2946
Michael Jurka86a720e2012-05-09 11:23:23 -07002947 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002948 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002950 // Only launch using the new animation if the shortcut has not opted out (this is a
2951 // private contract between launcher and may be ignored in the future).
2952 boolean useLaunchAnimation = (v != null) &&
2953 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002954 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2955 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002956
Kenny Guy1317e2d2014-05-08 18:52:50 +01002957 UserHandleCompat user = null;
2958 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2959 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2960 user = userManager.getUserForSerialNumber(serialNumber);
2961 }
2962
2963 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002964 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002965 ActivityOptions opts = null;
2966 if (sClipRevealMethod != null) {
2967 // TODO: call method directly when Launcher3 can depend on M APIs
2968 int left = 0, top = 0;
2969 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2970 if (v instanceof TextView) {
2971 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002972 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2973 if (icon != null) {
2974 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002975 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002976 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002977 width = bounds.width();
2978 height = bounds.height();
2979 }
2980 }
2981 try {
2982 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2983 left, top, width, height);
2984 } catch (IllegalAccessException e) {
2985 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2986 sClipRevealMethod = null;
2987 } catch (InvocationTargetException e) {
2988 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2989 sClipRevealMethod = null;
2990 }
2991 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002992 if (opts == null && !Utilities.isLmpOrAbove()) {
2993 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002994 v.getMeasuredWidth(), v.getMeasuredHeight());
2995 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002996 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002997 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002998
2999 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
3000 // Could be launching some bookkeeping activity
3001 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003002 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003003 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003004 launcherApps.startActivityForProfile(intent.getComponent(), user,
3005 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003006 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003007 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003008 } catch (SecurityException e) {
3009 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003010 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003011 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003012 "or use the exported attribute for this activity. "
3013 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003014 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003015 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003016 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003017
Michael Jurka86a720e2012-05-09 11:23:23 -07003018 boolean startActivitySafely(View v, Intent intent, Object tag) {
3019 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003020 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3021 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3022 return false;
3023 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003024 try {
3025 success = startActivity(v, intent, tag);
3026 } catch (ActivityNotFoundException e) {
3027 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3028 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3029 }
3030 return success;
3031 }
3032
Adam Cohen268c4752012-06-06 17:47:33 -07003033 /**
3034 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3035 * in the DragLayer in the exact absolute location of the original FolderIcon.
3036 */
3037 private void copyFolderIconToImage(FolderIcon fi) {
3038 final int width = fi.getMeasuredWidth();
3039 final int height = fi.getMeasuredHeight();
3040
3041 // Lazy load ImageView, Bitmap and Canvas
3042 if (mFolderIconImageView == null) {
3043 mFolderIconImageView = new ImageView(this);
3044 }
3045 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3046 mFolderIconBitmap.getHeight() != height) {
3047 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3048 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3049 }
3050
3051 DragLayer.LayoutParams lp;
3052 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3053 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3054 } else {
3055 lp = new DragLayer.LayoutParams(width, height);
3056 }
3057
Adam Cohen307fe232012-08-16 17:55:58 -07003058 // The layout from which the folder is being opened may be scaled, adjust the starting
3059 // view size by this scale factor.
3060 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003061 lp.customPosition = true;
3062 lp.x = mRectForFolderAnimation.left;
3063 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003064 lp.width = (int) (scale * width);
3065 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003066
3067 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3068 fi.draw(mFolderIconCanvas);
3069 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003070 if (fi.getFolder() != null) {
3071 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3072 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003073 }
Adam Cohen268c4752012-06-06 17:47:33 -07003074 // Just in case this image view is still in the drag layer from a previous animation,
3075 // we remove it and re-add it.
3076 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3077 mDragLayer.removeView(mFolderIconImageView);
3078 }
3079 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003080 if (fi.getFolder() != null) {
3081 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003082 }
Adam Cohen268c4752012-06-06 17:47:33 -07003083 }
3084
Adam Cohen2801caf2011-05-13 20:57:39 -07003085 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003086 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003087 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3088 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3089 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3090
Adam Cohenc51934b2011-07-26 21:07:43 -07003091 FolderInfo info = (FolderInfo) fi.getTag();
3092 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3093 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003094 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3095 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003096 }
3097
Adam Cohen268c4752012-06-06 17:47:33 -07003098 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3099 copyFolderIconToImage(fi);
3100 fi.setVisibility(View.INVISIBLE);
3101
Michael Jurka2ecf9952012-06-18 12:52:28 -07003102 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003103 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003104 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003105 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3106 }
3107 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003108 oa.start();
3109 }
3110
Adam Cohen268c4752012-06-06 17:47:33 -07003111 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003112 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003113 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3114 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3115 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3116
Adam Cohen268c4752012-06-06 17:47:33 -07003117 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003118
Adam Cohen268c4752012-06-06 17:47:33 -07003119 // We remove and re-draw the FolderIcon in-case it has changed
3120 mDragLayer.removeView(mFolderIconImageView);
3121 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003122 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003123 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003124 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003125 oa.addListener(new AnimatorListenerAdapter() {
3126 @Override
3127 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003128 if (cl != null) {
3129 cl.clearFolderLeaveBehind();
3130 // Remove the ImageView copy of the FolderIcon and make the original visible.
3131 mDragLayer.removeView(mFolderIconImageView);
3132 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003133 }
3134 }
3135 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003136 oa.start();
3137 }
3138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003139 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003140 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003141 * is animated relative to the specified View. If the View is null, no animation
3142 * is played.
3143 *
3144 * @param folderInfo The FolderInfo describing the folder to open.
3145 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003146 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003147 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003148 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3149 if (openFolder != null && openFolder != folder) {
3150 // Close any open folder before opening a folder.
3151 closeFolder();
3152 }
3153
Adam Cohena9cf38f2011-05-02 15:36:58 -07003154 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155
Adam Cohena9cf38f2011-05-02 15:36:58 -07003156 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003157
Sunny Goyalf4066152015-04-15 09:42:19 -07003158 // While the folder is open, the position of the icon cannot change.
3159 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3160
Adam Cohen4554ee12011-08-03 16:13:21 -07003161 // Just verify that the folder hasn't already been added to the DragLayer.
3162 // There was a one-off crash where the folder had a parent already.
3163 if (folder.getParent() == null) {
3164 mDragLayer.addView(folder);
3165 mDragController.addDropTarget((DropTarget) folder);
3166 } else {
3167 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3168 folder.getParent() + ").");
3169 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003170 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003171 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003172
3173 // Notify the accessibility manager that this folder "window" has appeared and occluded
3174 // the workspace items
3175 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3176 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003177 }
3178
3179 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003180 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003181 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003182 if (folder.isEditingName()) {
3183 folder.dismissEditingName();
3184 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003185 closeFolder(folder);
3186 }
3187 }
3188
Sunny Goyal83a8f042015-05-19 12:52:12 -07003189 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003190 folder.getInfo().opened = false;
3191
3192 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3193 if (parent != null) {
3194 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3195 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003196 if (fi != null) {
3197 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3198 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003199 }
3200 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003201
3202 // Notify the accessibility manager that this folder "window" has disappeard and no
3203 // longer occludeds the workspace items
3204 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003205 }
3206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003207 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003208 if (!isDraggingEnabled()) return false;
3209 if (isWorkspaceLocked()) return false;
3210 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003211
Adam Cohen1697b792013-09-17 19:08:21 -07003212 if (v instanceof Workspace) {
3213 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003214 if (!mWorkspace.isTouchActive()) {
3215 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003216 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3217 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3218 return true;
3219 } else {
3220 return false;
3221 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003222 } else {
3223 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003224 }
Adam Cohen1697b792013-09-17 19:08:21 -07003225 }
3226
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003227 CellLayout.CellInfo longClickCellInfo = null;
3228 View itemUnderLongClick = null;
3229 if (v.getTag() instanceof ItemInfo) {
3230 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003231 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003232 itemUnderLongClick = longClickCellInfo.cell;
3233 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003234 }
3235
Winson Chung3d503fb2011-07-13 17:25:49 -07003236 // The hotseat touch handling does not go through Workspace, and we always allow long press
3237 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003238 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003239 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003240 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003241 // User long pressed on empty space
3242 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3243 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003244 if (mWorkspace.isInOverviewMode()) {
3245 mWorkspace.startReordering(v);
3246 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003247 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003248 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003249 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003250 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3251 mHotseat.getOrderInHotseat(
3252 longClickCellInfo.cellX,
3253 longClickCellInfo.cellY));
3254 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003255 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003256 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003257 }
3258 }
3259 }
3260 return true;
3261 }
3262
Winson Chung3d503fb2011-07-13 17:25:49 -07003263 boolean isHotseatLayout(View layout) {
3264 return mHotseat != null && layout != null &&
3265 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3266 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003267
Winson Chung3d503fb2011-07-13 17:25:49 -07003268 /**
3269 * Returns the CellLayout of the specified container at the specified screen.
3270 */
Sunny Goyal92820592015-03-02 11:31:35 -08003271 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003272 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3273 if (mHotseat != null) {
3274 return mHotseat.getLayout();
3275 } else {
3276 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003277 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003278 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003279 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003280 }
3281 }
3282
Winson Chungb745afb2015-03-02 11:51:23 -08003283 /**
3284 * For overridden classes.
3285 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003286 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003287 return isAppsViewVisible();
3288 }
3289
3290 public boolean isAppsViewVisible() {
3291 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3292 }
3293
3294 public boolean isWidgetsViewVisible() {
3295 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003296 }
3297
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003298 private void setWorkspaceBackground(int background) {
3299 switch (background) {
3300 case WORKSPACE_BACKGROUND_TRANSPARENT:
3301 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3302 break;
3303 case WORKSPACE_BACKGROUND_BLACK:
3304 getWindow().setBackgroundDrawable(null);
3305 break;
3306 default:
3307 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3308 }
Craig Mautner360310b2012-10-26 15:13:08 -07003309 }
3310
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003311 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003312 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3313 int curflags = getWindow().getAttributes().flags
3314 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3315 if (wpflags != curflags) {
3316 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3317 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003318 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003319 }
3320
Michael Jurkae326f182011-11-21 14:05:46 -08003321 @Override
3322 public void onTrimMemory(int level) {
3323 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003324 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3325 // The widget preview db can result in holding onto over
3326 // 3MB of memory for caching which isn't necessary.
3327 SQLiteDatabase.releaseMemory();
3328
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003329 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003330 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003331 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003332 if (mLauncherCallbacks != null) {
3333 mLauncherCallbacks.onTrimMemory(level);
3334 }
Michael Jurkae326f182011-11-21 14:05:46 -08003335 }
3336
Winson Chungb745afb2015-03-02 11:51:23 -08003337 @Override
3338 public void onStateTransitionHideSearchBar() {
3339 // Hide the search bar
3340 if (mSearchDropTargetBar != null) {
3341 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3342 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003343 }
3344
Winson Chungb745afb2015-03-02 11:51:23 -08003345 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003346 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3347 true);
Adam Cohened307df2013-10-02 09:37:31 -07003348 }
3349
Sunny Goyal83a8f042015-05-19 12:52:12 -07003350 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003351 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3352 onCompleteRunnable, true);
3353 }
3354
3355 protected void showWorkspace(int snapToPage, boolean animated) {
3356 showWorkspace(snapToPage, animated, null, true);
3357 }
3358
3359 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3360 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003361 boolean changed = mState != State.WORKSPACE ||
3362 mWorkspace.getState() != Workspace.State.NORMAL;
3363 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003364 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003365 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003366 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003367 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003368
Winson Chungc7d2b602012-05-16 17:02:20 -07003369 // Show the search bar (only animate if we were showing the drop target bar in spring
3370 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003371 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003372 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003373 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003374
Michael Jurkab3e22d92011-10-31 15:58:33 -07003375 // Set focus to the AppsCustomize button
3376 if (mAllAppsButton != null) {
3377 mAllAppsButton.requestFocus();
3378 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003379 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003380
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003381 // Change the state *after* we've called all the transition code
3382 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003383
Winson Chung5fb63472011-02-02 17:03:37 -08003384 // Resume the auto-advance of widgets
3385 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003386 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003387
Winson Chung0f785722015-04-08 10:27:49 -07003388 if (changed) {
3389 // Send an accessibility event to announce the context change
3390 getWindow().getDecorView()
3391 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003392 if (notifyLauncherCallbacks) {
3393 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003394 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003395 mLauncherCallbacks.onAllAppsHidden();
3396 }
3397 }
Winson Chung0f785722015-04-08 10:27:49 -07003398 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003399 }
3400
Adam Cohened307df2013-10-02 09:37:31 -07003401 void showOverviewMode(boolean animated) {
3402 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003403 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003404 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3405 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003406 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003407 }
3408
Winson Chungb745afb2015-03-02 11:51:23 -08003409 /**
3410 * Shows the apps view.
3411 */
Winson Chung4ac30062015-05-08 17:34:17 -07003412 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003413 if (resetListToTop) {
3414 mAppsView.scrollToTop();
3415 }
Winson Chung4ac30062015-05-08 17:34:17 -07003416 if (updatePredictedApps) {
3417 tryAndUpdatePredictedApps();
3418 }
Winson Chungb745afb2015-03-02 11:51:23 -08003419 showAppsOrWidgets(animated, State.APPS);
3420 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003421
Winson Chungb745afb2015-03-02 11:51:23 -08003422 /**
3423 * Shows the widgets view.
3424 */
3425 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003426 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003427 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003428 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003429 }
Winson Chungb745afb2015-03-02 11:51:23 -08003430 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003431
3432 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003433 @Override
3434 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003435 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003436 }
3437 });
Winson Chungb745afb2015-03-02 11:51:23 -08003438 }
3439
3440 /**
3441 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003442 *
3443 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003444 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003445 // TODO: calling method should use the return value so that when {@code false} is returned
3446 // the workspace transition doesn't fall into invalid state.
3447 private boolean showAppsOrWidgets(boolean animated, State toState) {
3448 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3449 mState != State.WIDGETS_SPRING_LOADED) {
3450 return false;
3451 }
3452 if (toState != State.APPS && toState != State.WIDGETS) {
3453 return false;
3454 }
Winson Chungb745afb2015-03-02 11:51:23 -08003455
3456 if (toState == State.APPS) {
3457 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003458 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003459 mLauncherCallbacks.onAllAppsShown();
3460 }
Winson Chungb745afb2015-03-02 11:51:23 -08003461 } else {
3462 mStateTransitionAnimation.startAnimationToWidgets(animated);
3463 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003464
Michael Jurkab3e22d92011-10-31 15:58:33 -07003465 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003466 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003467
3468 // Pause the auto-advance of widgets until we are out of AllApps
3469 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003470 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003471 closeFolder();
3472
3473 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003474 getWindow().getDecorView()
3475 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003476 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003477 }
3478
Winson Chungcd99cd32015-04-29 11:03:24 -07003479 /**
3480 * Updates the workspace and interaction state on state change, and return the animation to this
3481 * new state.
3482 */
3483 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3484 boolean animated, HashMap<View, Integer> layerViews) {
3485 Workspace.State fromState = mWorkspace.getState();
3486 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3487 updateInteraction(fromState, toState);
3488 return anim;
3489 }
3490
Hyunyoung Song3f471442015-04-08 19:01:34 -07003491 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003492 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003493 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3494 mState == State.WIDGETS_SPRING_LOADED) {
3495 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003496 }
Winson Chungb745afb2015-03-02 11:51:23 -08003497
3498 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003499 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3500 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003501 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003502 }
Adam Cohen7777d962011-08-18 18:58:38 -07003503
Hyunyoung Song3f471442015-04-08 19:01:34 -07003504 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003505 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003506 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003507
Winson Chungcd99cd32015-04-29 11:03:24 -07003508 if (successfulDrop) {
3509 // We need to trigger all apps hidden to notify search to update itself before the
3510 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003511 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003512 mLauncherCallbacks.onAllAppsHidden();
3513 }
3514 }
3515
Winson Chunge7a03942011-08-05 15:05:12 -07003516 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003517 @Override
3518 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003519 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003520 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3521 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003522 // Before we show workspace, hide all apps again because
3523 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3524 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003525 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003526 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003527 } else {
3528 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003529 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003530 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003531 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003532 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003533
Michael Jurkad3ef3062010-11-23 16:23:58 -08003534 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003535 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003536 showAppsView(true /* animated */, false /* resetListToTop */,
3537 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003538 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003539 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003540 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003541 }
3542
Winson Chung4ac30062015-05-08 17:34:17 -07003543 /**
3544 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3545 * resumed.
3546 */
3547 private void tryAndUpdatePredictedApps() {
3548 if (mLauncherCallbacks != null) {
3549 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3550 if (!apps.isEmpty()) {
3551 mAppsView.setPredictedApps(apps);
3552 }
3553 }
3554 }
3555
Michael Jurkab3e22d92011-10-31 15:58:33 -07003556 void lockAllApps() {
3557 // TODO
3558 }
3559
3560 void unlockAllApps() {
3561 // TODO
3562 }
3563
Sunny Goyal594d76d2014-11-06 10:12:54 -08003564 protected void disableVoiceButtonProxy(boolean disable) {
3565 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003566 }
3567
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003568 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003569 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3570 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003571 }
3572
Adam Cohen24ce0b32014-01-14 16:18:14 -08003573 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003574 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3575 if (searchProvider == null) {
3576 return null;
3577 }
3578
3579 Bundle opts = new Bundle();
3580 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003581 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003582
3583 SharedPreferences sp = getSharedPreferences(
3584 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3585 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003586 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003587 if (!searchProvider.provider.flattenToString().equals(
3588 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003589 || (widgetInfo == null)
3590 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003591 // A valid widget is not already bound.
3592 if (widgetId > -1) {
3593 mAppWidgetHost.deleteAppWidgetId(widgetId);
3594 widgetId = -1;
3595 }
3596
3597 // Try to bind a new widget
3598 widgetId = mAppWidgetHost.allocateAppWidgetId();
3599
3600 if (!AppWidgetManagerCompat.getInstance(this)
3601 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3602 mAppWidgetHost.deleteAppWidgetId(widgetId);
3603 widgetId = -1;
3604 }
3605
3606 sp.edit()
3607 .putInt(QSB_WIDGET_ID, widgetId)
3608 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3609 .commit();
3610 }
3611
3612 if (widgetId != -1) {
3613 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3614 mQsb.updateAppWidgetOptions(opts);
3615 mQsb.setPadding(0, 0, 0, 0);
3616 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003617 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003618 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003619 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003620 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003621 }
3622
Sunny Goyal22235bc2015-04-03 09:23:43 -07003623 private void reinflateQSBIfNecessary() {
3624 if (mQsb instanceof LauncherAppWidgetHostView &&
3625 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3626 mSearchDropTargetBar.removeView(mQsb);
3627 mQsb = null;
3628 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3629 }
3630 }
3631
Michael Jurkad7c28052012-04-27 15:43:36 -07003632 @Override
3633 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003634 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003635 final List<CharSequence> text = event.getText();
3636 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003637 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003638 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003639 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003640 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003641 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003642 } else {
3643 text.add(getString(R.string.all_apps_home_button_label));
3644 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003645 return result;
3646 }
3647
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003648 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003649 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003650 */
Adam Cohen091440a2015-03-18 14:16:05 -07003651 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003652 @Override
3653 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003654 closeSystemDialogs();
3655 }
3656 }
3657
3658 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003659 * Receives notifications whenever the appwidgets are reset.
3660 */
3661 private class AppWidgetResetObserver extends ContentObserver {
3662 public AppWidgetResetObserver() {
3663 super(new Handler());
3664 }
3665
3666 @Override
3667 public void onChange(boolean selfChange) {
3668 onAppWidgetReset();
3669 }
3670 }
3671
3672 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003673 * If the activity is currently paused, signal that we need to run the passed Runnable
3674 * in onResume.
3675 *
3676 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003677 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3678 * wrong when we're not running, and if the activity comes back to what the configuration was
3679 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003680 *
3681 * Implementation of the method from LauncherModel.Callbacks.
3682 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003683 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003684 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003685 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003686 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003687 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003688 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003689 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003690 }
3691 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003692 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003693 return true;
3694 } else {
3695 return false;
3696 }
3697 }
3698
Michael Jurkac402cd92013-05-20 15:49:32 +02003699 private boolean waitUntilResume(Runnable run) {
3700 return waitUntilResume(run, false);
3701 }
3702
Michael Jurka1e2f4652013-07-08 18:03:46 -07003703 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003704 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003705 }
3706
Michael Jurka7607c2f2013-04-03 14:33:19 -07003707 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003708 * If the activity is currently paused, signal that we need to re-run the loader
3709 * in onResume.
3710 *
3711 * This needs to be called from incoming places where resources might have been loaded
3712 * while we are paused. That is becaues the Configuration might be wrong
3713 * when we're not running, and if it comes back to what it was when we
3714 * were paused, we are not restarted.
3715 *
3716 * Implementation of the method from LauncherModel.Callbacks.
3717 *
3718 * @return true if we are currently paused. The caller might be able to
3719 * skip some work in that case since we will come back again.
3720 */
3721 public boolean setLoadOnResume() {
3722 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003723 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003724 mOnResumeNeedsLoad = true;
3725 return true;
3726 } else {
3727 return false;
3728 }
3729 }
3730
3731 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003733 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003735 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003736 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003737 } else {
3738 return SCREEN_COUNT / 2;
3739 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003740 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003741
Joe Onorato9c1289c2009-08-17 11:03:03 -04003742 /**
3743 * Refreshes the shortcuts shown on the workspace.
3744 *
3745 * Implementation of the method from LauncherModel.Callbacks.
3746 */
3747 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003748 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003749
Michael Jurka7607c2f2013-04-03 14:33:19 -07003750 // If we're starting binding all over again, clear any bind calls we'd postponed in
3751 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3752 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003753 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003754
Winson Chungd64d1762013-08-20 14:37:16 -07003755 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003756 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003757 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003758
Michael Jurka05bf6442011-11-30 20:28:53 -08003759 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003760 if (mHotseat != null) {
3761 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003762 }
3763 }
3764
Adam Cohendcd297f2013-06-18 13:13:40 -07003765 @Override
3766 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003767 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003768
Adam Cohen5084cba2013-09-03 12:01:16 -07003769 // If there are no screens, we need to have an empty screen
3770 if (orderedScreenIds.size() == 0) {
3771 mWorkspace.addExtraEmptyScreen();
3772 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003773
3774 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003775 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003776 if (hasCustomContentToLeft()) {
3777 mWorkspace.createCustomContentContainer();
3778 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003779 }
Winson Chung64359a52013-07-08 17:17:08 -07003780 }
3781
3782 @Override
3783 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003784 // Log to disk
3785 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3786 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3787 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003788 int count = orderedScreenIds.size();
3789 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003790 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003791 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003792 }
3793
Adam Cohen39a06042013-07-19 14:30:12 -07003794 private boolean shouldShowWeightWatcher() {
3795 String spKey = LauncherAppState.getSharedPreferencesKey();
3796 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003797 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003798
3799 return show;
3800 }
3801
3802 private void toggleShowWeightWatcher() {
3803 String spKey = LauncherAppState.getSharedPreferencesKey();
3804 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3805 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3806
3807 show = !show;
3808
3809 SharedPreferences.Editor editor = sp.edit();
3810 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3811 editor.commit();
3812
3813 if (mWeightWatcher != null) {
3814 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3815 }
3816 }
3817
Winson Chungd64d1762013-08-20 14:37:16 -07003818 public void bindAppsAdded(final ArrayList<Long> newScreens,
3819 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003820 final ArrayList<ItemInfo> addAnimated,
3821 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003822 Runnable r = new Runnable() {
3823 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003824 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003825 }
3826 };
3827 if (waitUntilResume(r)) {
3828 return;
3829 }
3830
Winson Chungd64d1762013-08-20 14:37:16 -07003831 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003832 if (newScreens != null) {
3833 bindAddScreens(newScreens);
3834 }
Winson Chungd64d1762013-08-20 14:37:16 -07003835
3836 // We add the items without animation on non-visible pages, and with
3837 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003838 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003839 bindItems(addNotAnimated, 0,
3840 addNotAnimated.size(), false);
3841 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003842 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003843 bindItems(addAnimated, 0,
3844 addAnimated.size(), true);
3845 }
Winson Chungc58497e2013-09-03 17:48:37 -07003846
Winson Chung87412982013-10-03 18:34:14 -07003847 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003848 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003849
Winson Chungb745afb2015-03-02 11:51:23 -08003850 if (addedApps != null && mAppsView != null) {
3851 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003852 }
Winson Chungd64d1762013-08-20 14:37:16 -07003853 }
3854
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003855 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003856 * Bind the items start-end from the list.
3857 *
3858 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003859 */
Winson Chung64359a52013-07-08 17:17:08 -07003860 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3861 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003862 Runnable r = new Runnable() {
3863 public void run() {
3864 bindItems(shortcuts, start, end, forceAnimateIcons);
3865 }
3866 };
3867 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003868 return;
3869 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003870
Winson Chungf0c6ae02012-03-21 16:10:31 -07003871 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003872 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3873 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003874 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003875 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003876 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003877 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003878 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003879
3880 // Short circuit if we are loading dock items for a configuration which has no dock
3881 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3882 mHotseat == null) {
3883 continue;
3884 }
3885
Joe Onorato9c1289c2009-08-17 11:03:03 -04003886 switch (item.itemType) {
3887 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3888 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003889 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003890 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003891
Winson Chung64359a52013-07-08 17:17:08 -07003892 /*
3893 * TODO: FIX collision case
3894 */
Winson Chungce376632013-07-11 16:12:41 -07003895 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3896 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3897 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003898 View v = cl.getChildAt(item.cellX, item.cellY);
3899 Object tag = v.getTag();
3900 String desc = "Collision while binding workspace item: " + item
3901 + ". Collides with " + tag;
3902 if (LauncherAppState.isDogfoodBuild()) {
3903 throw (new RuntimeException(desc));
3904 } else {
3905 Log.d(TAG, desc);
3906 }
Winson Chungce376632013-07-11 16:12:41 -07003907 }
Winson Chung64359a52013-07-08 17:17:08 -07003908 }
3909
Adam Cohendcd297f2013-06-18 13:13:40 -07003910 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3911 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003912 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003913 // Animate all the applications up now
3914 shortcut.setAlpha(0f);
3915 shortcut.setScaleX(0f);
3916 shortcut.setScaleY(0f);
3917 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003918 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003919 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003920 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003921 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003922 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003923 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003924 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003925 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3926 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003927 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003928 default:
3929 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003931 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003932
Winson Chung997a9232013-07-24 15:33:46 -07003933 if (animateIcons) {
3934 // Animate to the correct page
3935 if (newShortcutsScreenId > -1) {
3936 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003937 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003938 final Runnable startBounceAnimRunnable = new Runnable() {
3939 public void run() {
3940 anim.playTogether(bounceAnims);
3941 anim.start();
3942 }
3943 };
Winson Chung997a9232013-07-24 15:33:46 -07003944 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003945 // We post the animation slightly delayed to prevent slowdowns
3946 // when we are loading right after we return to launcher.
3947 mWorkspace.postDelayed(new Runnable() {
3948 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003949 if (mWorkspace != null) {
3950 mWorkspace.snapToPage(newScreenIndex);
3951 mWorkspace.postDelayed(startBounceAnimRunnable,
3952 NEW_APPS_ANIMATION_DELAY);
3953 }
Winson Chung94d67682013-09-25 16:29:40 -07003954 }
3955 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003956 } else {
3957 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003958 }
3959 }
Winson Chung64359a52013-07-08 17:17:08 -07003960 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003961 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003962 }
3963
Joe Onorato9c1289c2009-08-17 11:03:03 -04003964 /**
3965 * Implementation of the method from LauncherModel.Callbacks.
3966 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003967 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003968 Runnable r = new Runnable() {
3969 public void run() {
3970 bindFolders(folders);
3971 }
3972 };
3973 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003974 return;
3975 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003976 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003977 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003978
3979 /**
3980 * Add the views for a widget to the workspace.
3981 *
3982 * Implementation of the method from LauncherModel.Callbacks.
3983 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003984 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003985 Runnable r = new Runnable() {
3986 public void run() {
3987 bindAppWidget(item);
3988 }
3989 };
3990 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003991 return;
3992 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003993
Daniel Sandler843e8602010-06-07 14:59:01 -04003994 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3995 if (DEBUG_WIDGETS) {
3996 Log.d(TAG, "bindAppWidget: " + item);
3997 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003998 final Workspace workspace = mWorkspace;
3999
Adam Cohen59400422014-03-05 18:07:04 -08004000 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00004001 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08004002
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004003 if (!mIsSafeModeEnabled
4004 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4005 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004006 if (appWidgetInfo == null) {
4007 if (DEBUG_WIDGETS) {
4008 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4009 + " belongs to component " + item.providerName
4010 + ", as the povider is null");
4011 }
4012 LauncherModel.deleteItemFromDatabase(this, item);
4013 return;
4014 }
4015 // Note: This assumes that the id remap broadcast is received before this step.
4016 // If that is not the case, the id remap will be ignored and user may see the
4017 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07004018 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004019 pendingInfo.spanX = item.spanX;
4020 pendingInfo.spanY = item.spanY;
4021 pendingInfo.minSpanX = item.minSpanX;
4022 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004023 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004024 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004025
Sunny Goyalff572272014-07-23 13:58:07 -07004026 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004027 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4028 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004029
4030 // TODO consider showing a permission dialog when the widget is clicked.
4031 if (!success) {
4032 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4033 if (DEBUG_WIDGETS) {
4034 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4035 + " belongs to component " + item.providerName
4036 + ", as the launcher is unable to bing a new widget id");
4037 }
4038 LauncherModel.deleteItemFromDatabase(this, item);
4039 return;
4040 }
4041
4042 item.appWidgetId = newWidgetId;
4043
4044 // If the widget has a configure activity, it is still needs to set it up, otherwise
4045 // the widget is ready to go.
4046 item.restoreStatus = (appWidgetInfo.configure == null)
4047 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4048 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4049
4050 LauncherModel.updateItemInDatabase(this, item);
4051 }
4052
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004053 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004054 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004055 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004056 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4057 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004058 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004059
Sunny Goyal651077b2014-06-30 14:15:31 -07004060 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4061 } else {
4062 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004063 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4064 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004065 view.updateIcon(mIconCache);
4066 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004067 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004068 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004069 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004070
Joe Onorato9c1289c2009-08-17 11:03:03 -04004071 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004072 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073
Adam Cohendcd297f2013-06-18 13:13:40 -07004074 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004075 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004076 if (!item.isCustomWidget()) {
4077 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4078 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004079
Joe Onorato9c1289c2009-08-17 11:03:03 -04004080 workspace.requestLayout();
4081
Daniel Sandler843e8602010-06-07 14:59:01 -04004082 if (DEBUG_WIDGETS) {
4083 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4084 + (SystemClock.uptimeMillis()-start) + "ms");
4085 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004086 }
4087
Sunny Goyalff572272014-07-23 13:58:07 -07004088 /**
4089 * Restores a pending widget.
4090 *
4091 * @param appWidgetId The app widget id
4092 * @param cellInfo The position on screen where to create the widget.
4093 */
4094 private void completeRestoreAppWidget(final int appWidgetId) {
4095 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4096 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4097 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4098 return;
4099 }
4100
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004101 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004102 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4103
4104 mWorkspace.reinflateWidgetsIfNecessary();
4105 LauncherModel.updateItemInDatabase(this, info);
4106 }
4107
Adam Cohen1462de32012-07-24 22:34:36 -07004108 public void onPageBoundSynchronously(int page) {
4109 mSynchronouslyBoundPages.add(page);
4110 }
4111
Joe Onorato9c1289c2009-08-17 11:03:03 -04004112 /**
4113 * Callback saying that there aren't any more items to bind.
4114 *
4115 * Implementation of the method from LauncherModel.Callbacks.
4116 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004117 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004118 Runnable r = new Runnable() {
4119 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004120 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004121 }
4122 };
4123 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004124 return;
4125 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004126 if (mSavedState != null) {
4127 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004128 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004129 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004130 mSavedState = null;
4131 }
4132
Adam Cohen1462de32012-07-24 22:34:36 -07004133 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004134
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004135 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004136 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004137
4138 // If we received the result of any pending adds while the loader was running (e.g. the
4139 // widget configuration forced an orientation change), process them now.
4140 if (sPendingAddItem != null) {
4141 final long screenId = completeAdd(sPendingAddItem);
4142
4143 // TODO: this moves the user to the page where the pending item was added. Ideally,
4144 // the screen would be guaranteed to exist after bind, and the page would be set through
4145 // the workspace restore process.
4146 mWorkspace.post(new Runnable() {
4147 @Override
4148 public void run() {
4149 mWorkspace.snapToScreenId(screenId);
4150 }
4151 });
4152 sPendingAddItem = null;
4153 }
4154
Sunny Goyal756adbc2015-04-16 15:20:51 -07004155 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004156
4157 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004158 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004159 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004160 }
4161
Adam Cohen3ed316a2014-07-23 18:21:20 -07004162 private void sendLoadingCompleteBroadcastIfNecessary() {
4163 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4164 String permission =
4165 getResources().getString(R.string.receive_first_load_broadcast_permission);
4166 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4167 sendBroadcast(intent, permission);
4168 SharedPreferences.Editor editor = mSharedPrefs.edit();
4169 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4170 editor.apply();
4171 }
4172 }
4173
Winson Chunga0b7e862013-09-05 16:03:15 -07004174 public boolean isAllAppsButtonRank(int rank) {
4175 if (mHotseat != null) {
4176 return mHotseat.isAllAppsButtonRank(rank);
4177 }
4178 return false;
4179 }
4180
Winson Chunga2413752012-04-03 14:22:34 -07004181 private boolean canRunNewAppsAnimation() {
4182 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4183 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4184 }
4185
Winson Chungc9168342013-06-26 14:54:55 -07004186 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4187 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4188 PropertyValuesHolder.ofFloat("alpha", 1f),
4189 PropertyValuesHolder.ofFloat("scaleX", 1f),
4190 PropertyValuesHolder.ofFloat("scaleY", 1f));
4191 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4192 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4193 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4194 return bounceAnim;
4195 }
4196
Adam Cohen27772732013-11-11 14:00:56 +00004197 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004198 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004199 }
4200
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004201 protected Rect getSearchBarBounds() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004202 return mDeviceProfile.getSearchBarBounds();
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004203 }
4204
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004205 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004206 if (mSearchDropTargetBar == null) {
4207 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004208 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004209 if (mQsb != null) {
4210 mSearchDropTargetBar.removeView(mQsb);
4211 mQsb = null;
4212 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004213 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004214 }
4215
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004216 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004217 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4218 * multiple calls to bind the same list.)
4219 */
4220 @Thunk ArrayList<AppInfo> mTmpAppsList;
4221 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4222 public void run() {
4223 bindAllApplications(mTmpAppsList);
4224 mTmpAppsList = null;
4225 }
4226 };
4227
4228 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004229 * Add the icons for all apps.
4230 *
4231 * Implementation of the method from LauncherModel.Callbacks.
4232 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004233 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004234 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4235 mTmpAppsList = apps;
4236 return;
4237 }
4238
Winson Chungb745afb2015-03-02 11:51:23 -08004239 if (mAppsView != null) {
4240 mAppsView.setApps(apps);
4241 }
Adam Cohen9211d422014-10-07 18:14:53 -07004242 if (mLauncherCallbacks != null) {
4243 mLauncherCallbacks.bindAllApplications(apps);
4244 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004245 }
4246
4247 /**
4248 * A package was updated.
4249 *
4250 * Implementation of the method from LauncherModel.Callbacks.
4251 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004252 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004253 Runnable r = new Runnable() {
4254 public void run() {
4255 bindAppsUpdated(apps);
4256 }
4257 };
4258 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004259 return;
4260 }
4261
Winson Chungb745afb2015-03-02 11:51:23 -08004262 if (mAppsView != null) {
4263 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004264 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004265 }
4266
Sunny Goyal4390ace2014-10-13 11:33:11 -07004267 @Override
4268 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4269 Runnable r = new Runnable() {
4270 public void run() {
4271 bindWidgetsRestored(widgets);
4272 }
4273 };
4274 if (waitUntilResume(r)) {
4275 return;
4276 }
4277 mWorkspace.widgetsRestored(widgets);
4278 }
4279
Joe Onorato9c1289c2009-08-17 11:03:03 -04004280 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004281 * Some shortcuts were updated in the background.
4282 *
4283 * Implementation of the method from LauncherModel.Callbacks.
4284 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004285 @Override
4286 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4287 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004288 Runnable r = new Runnable() {
4289 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004290 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004291 }
4292 };
4293 if (waitUntilResume(r)) {
4294 return;
4295 }
4296
Sunny Goyal4390ace2014-10-13 11:33:11 -07004297 if (!updated.isEmpty()) {
4298 mWorkspace.updateShortcuts(updated);
4299 }
4300
4301 if (!removed.isEmpty()) {
4302 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4303 for (ShortcutInfo si : removed) {
4304 removedComponents.add(si.getTargetComponent());
4305 }
4306 mWorkspace.removeItemsByComponentName(removedComponents, user);
4307 // Notify the drag controller
4308 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004309 }
4310 }
4311
4312 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004313 * Update the state of a package, typically related to install state.
4314 *
4315 * Implementation of the method from LauncherModel.Callbacks.
4316 */
Sunny Goyale755d462014-07-22 13:48:29 -07004317 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004318 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4319 Runnable r = new Runnable() {
4320 public void run() {
4321 bindRestoreItemsChange(updates);
4322 }
4323 };
4324 if (waitUntilResume(r)) {
4325 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004326 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004327
Sunny Goyal756adbc2015-04-16 15:20:51 -07004328 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004329 }
4330
4331 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004332 * A package was uninstalled. We take both the super set of packageNames
4333 * in addition to specific applications to remove, the reason being that
4334 * this can be called when a package is updated as well. In that scenario,
4335 * we only remove specific components from the workspace, where as
4336 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004337 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004338 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004339 * Implementation of the method from LauncherModel.Callbacks.
4340 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004341 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004342 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004343 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004344 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004345 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004346 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004347 }
Winson Chungd64d1762013-08-20 14:37:16 -07004348 };
4349 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004350 return;
4351 }
4352
Sunny Goyal1a745e82014-10-02 15:58:31 -07004353 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004354 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4355 for (AppInfo info : appInfos) {
4356 removedComponents.add(info.componentName);
4357 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004358 if (!packageNames.isEmpty()) {
4359 mWorkspace.removeItemsByPackageName(packageNames, user);
4360 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004361 if (!removedComponents.isEmpty()) {
4362 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004363 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004364 // Notify the drag controller
4365 mDragController.onAppsRemoved(packageNames, removedComponents);
4366
Sunny Goyal1a745e82014-10-02 15:58:31 -07004367 } else {
4368 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004369 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004370
Winson Chungdf95eb12013-10-16 14:57:07 -07004371 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004372 if (mAppsView != null) {
4373 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004374 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004375 }
Joe Onoratobe386092009-11-17 17:32:16 -08004376
Michael Jurkac402cd92013-05-20 15:49:32 +02004377 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004378 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004379 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004380 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004381 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004382
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004383 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004384 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004385 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004386 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004387 return;
4388 }
4389
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004390 if (mWidgetsView != null && model != null) {
4391 mWidgetsView.addWidgets(model);
4392 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004393 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004394 }
4395
Winson Chung400438b2011-01-16 17:53:48 -08004396 private int mapConfigurationOriActivityInfoOri(int configOri) {
4397 final Display d = getWindowManager().getDefaultDisplay();
4398 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4399 switch (d.getRotation()) {
4400 case Surface.ROTATION_0:
4401 case Surface.ROTATION_180:
4402 // We are currently in the same basic orientation as the natural orientation
4403 naturalOri = configOri;
4404 break;
4405 case Surface.ROTATION_90:
4406 case Surface.ROTATION_270:
4407 // We are currently in the other basic orientation to the natural orientation
4408 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4409 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4410 break;
4411 }
4412
4413 int[] oriMap = {
4414 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4415 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4416 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4417 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4418 };
4419 // Since the map starts at portrait, we need to offset if this device's natural orientation
4420 // is landscape.
4421 int indexOffset = 0;
4422 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4423 indexOffset = 1;
4424 }
4425 return oriMap[(d.getRotation() + indexOffset) % 4];
4426 }
Adam Cohen446e9402011-09-15 18:21:21 -07004427
Winson Chung4b919f82012-05-01 10:44:08 -07004428 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004429 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004430 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4431 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4432 .getConfiguration().orientation));
4433 } else {
4434 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4435 }
Winson Chung4b919f82012-05-01 10:44:08 -07004436 }
4437 }
4438 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004439 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004440 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004441 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004442 } else {
4443 mHandler.postDelayed(new Runnable() {
4444 public void run() {
4445 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4446 }
4447 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004448 }
Winson Chung4b919f82012-05-01 10:44:08 -07004449 }
Winson Chung400438b2011-01-16 17:53:48 -08004450 }
4451
Winson Chunge43a1e72014-01-15 10:33:02 -08004452 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004453 if (mLauncherCallbacks != null) {
4454 return mLauncherCallbacks.isLauncherPreinstalled();
4455 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004456 PackageManager pm = getPackageManager();
4457 try {
4458 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4459 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4460 return true;
4461 } else {
4462 return false;
4463 }
4464 } catch (NameNotFoundException e) {
4465 e.printStackTrace();
4466 return false;
4467 }
4468 }
4469
Adam Cohenea90f832014-05-21 15:03:34 -07004470 /**
4471 * This method indicates whether or not we should suggest default wallpaper dimensions
4472 * when our wallpaper cropper was not yet used to set a wallpaper.
4473 */
4474 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004475 if (mLauncherCallbacks != null) {
4476 return mLauncherCallbacks.overrideWallpaperDimensions();
4477 }
Adam Cohenea90f832014-05-21 15:03:34 -07004478 return true;
4479 }
4480
Adam Cohen432609a2014-03-13 17:03:22 -07004481 /**
4482 * To be overridden by subclasses to indicate that there is an activity to launch
4483 * before showing the standard launcher experience.
4484 */
4485 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004486 if (mLauncherCallbacks != null) {
4487 return mLauncherCallbacks.hasFirstRunActivity();
4488 }
Adam Cohen432609a2014-03-13 17:03:22 -07004489 return false;
4490 }
4491
4492 /**
4493 * To be overridden by subclasses to launch any first run activity
4494 */
4495 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004496 if (mLauncherCallbacks != null) {
4497 return mLauncherCallbacks.getFirstRunActivity();
4498 }
Adam Cohen432609a2014-03-13 17:03:22 -07004499 return null;
4500 }
4501
Winson Chung2826a402015-04-17 16:18:53 -07004502 /**
4503 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004504 */
4505 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004506 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4507 return false;
4508 }
4509
Winson Chung2826a402015-04-17 16:18:53 -07004510 if (mLauncherCallbacks != null) {
4511 return mLauncherCallbacks.overrideAllAppsSearch();
4512 }
4513 return false;
4514 }
4515
Winson Chunga6945242014-01-08 14:04:34 -08004516 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004517 return !ActivityManager.isRunningInTestHarness() &&
4518 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004519 }
4520
Adam Cohen4a9423d2014-03-28 14:22:31 -07004521 protected boolean hasRunFirstRunActivity() {
4522 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4523 }
4524
Adam Cohen432609a2014-03-13 17:03:22 -07004525 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004526 if (shouldRunFirstRunActivity() &&
4527 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004528 Intent firstRunIntent = getFirstRunActivity();
4529 if (firstRunIntent != null) {
4530 startActivity(firstRunIntent);
4531 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004532 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004533 }
4534 }
Adam Cohen432609a2014-03-13 17:03:22 -07004535 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004536 }
4537
4538 private void markFirstRunActivityShown() {
4539 SharedPreferences.Editor editor = mSharedPrefs.edit();
4540 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4541 editor.apply();
4542 }
4543
Adam Cohen432609a2014-03-13 17:03:22 -07004544 /**
4545 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4546 * screen that must be displayed and dismissed.
4547 */
4548 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004549 if (mLauncherCallbacks != null) {
4550 return mLauncherCallbacks.hasDismissableIntroScreen();
4551 }
Adam Cohen432609a2014-03-13 17:03:22 -07004552 return false;
4553 }
4554
4555 /**
4556 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4557 */
4558 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004559 if (mLauncherCallbacks != null) {
4560 return mLauncherCallbacks.getIntroScreen();
4561 }
Adam Cohen432609a2014-03-13 17:03:22 -07004562 return null;
4563 }
4564
4565 /**
4566 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4567 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4568 */
4569 private boolean shouldShowIntroScreen() {
4570 return hasDismissableIntroScreen() &&
4571 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4572 }
4573
4574 protected void showIntroScreen() {
4575 View introScreen = getIntroScreen();
4576 changeWallpaperVisiblity(false);
4577 if (introScreen != null) {
4578 mDragLayer.showOverlayView(introScreen);
4579 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004580 if (mLauncherOverlayContainer != null) {
4581 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4582 }
Adam Cohen432609a2014-03-13 17:03:22 -07004583 }
4584
4585 public void dismissIntroScreen() {
4586 markIntroScreenDismissed();
4587 if (showFirstRunActivity()) {
4588 // We delay hiding the intro view until the first run activity is showing. This
4589 // avoids a blip.
4590 mWorkspace.postDelayed(new Runnable() {
4591 @Override
4592 public void run() {
4593 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004594 if (mLauncherOverlayContainer != null) {
4595 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4596 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004597 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004598 }
4599 }, ACTIVITY_START_DELAY);
4600 } else {
4601 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004602 if (mLauncherOverlayContainer != null) {
4603 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4604 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004605 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004606 }
4607 changeWallpaperVisiblity(true);
4608 }
4609
4610 private void markIntroScreenDismissed() {
4611 SharedPreferences.Editor editor = mSharedPrefs.edit();
4612 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4613 editor.apply();
4614 }
4615
Adam Cohen091440a2015-03-18 14:16:05 -07004616 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004617 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4618 // on the device, then we always show the first run cling experience (or if there is no
4619 // launcher2). Otherwise, we prompt the user upon started for migration
4620 LauncherClings launcherClings = new LauncherClings(this);
4621 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4622 if (mModel.canMigrateFromOldLauncherDb(this)) {
4623 launcherClings.showMigrationCling();
4624 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004625 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004626 }
4627 }
4628 }
4629
Winson Chunga6945242014-01-08 14:04:34 -08004630 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004631 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4632 if (mHotseat != null) mHotseat.setAlpha(1f);
4633 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4634 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004635 }
4636
4637 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004638 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4639 if (mHotseat != null) mHotseat.setAlpha(0f);
4640 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4641 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004642 }
4643
Mathew Inwood72fbec12013-11-19 15:45:07 +00004644 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004645 // Called from search suggestion, not supported in other profiles.
4646 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4647 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4648 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4649 myUser);
4650 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004651 return null;
4652 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004653 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004654 }
4655
4656 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4657 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004658 // Called from search suggestion, not supported in other profiles.
4659 return createShortcutDragInfo(shortcutIntent, caption, icon,
4660 UserHandleCompat.myUserHandle());
4661 }
4662
4663 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4664 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004665 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004666 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004667 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004668 }
4669
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004670 protected void moveWorkspaceToDefaultScreen() {
4671 mWorkspace.moveToDefaultScreen(false);
4672 }
4673
Mathew Inwood72fbec12013-11-19 15:45:07 +00004674 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4675 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004676 mWorkspace.onExternalDragStartedWithItem(dragView);
4677 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004678 }
4679
Anjali Koppalf5d29132014-02-28 13:33:27 -08004680 @Override
4681 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004682 if (mLauncherCallbacks != null) {
4683 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4684 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004685 }
4686
Winson Chung80baf5a2010-08-09 16:03:15 -07004687 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004688 * Prints out out state for debugging.
4689 */
4690 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004691 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004692 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004693 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4694 Log.d(TAG, "mRestoring=" + mRestoring);
4695 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4696 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004697 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004698 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004699 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004700
Daniel Sandler325dc232013-06-05 22:57:57 -04004701 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004702 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004703
4704 @Override
4705 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4706 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004707 synchronized (sDumpLogs) {
4708 writer.println(" ");
4709 writer.println("Debug logs: ");
4710 for (int i = 0; i < sDumpLogs.size(); i++) {
4711 writer.println(" " + sDumpLogs.get(i));
4712 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004713 }
Adam Cohen9211d422014-10-07 18:14:53 -07004714 if (mLauncherCallbacks != null) {
4715 mLauncherCallbacks.dump(prefix, fd, writer, args);
4716 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004717 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004718
4719 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004720 if (DEBUG_DUMP_LOG) {
4721 synchronized (sDumpLogs) {
4722 Log.d(TAG, "");
4723 Log.d(TAG, "*********************");
4724 Log.d(TAG, "Launcher debug logs: ");
4725 for (int i = 0; i < sDumpLogs.size(); i++) {
4726 Log.d(TAG, " " + sDumpLogs.get(i));
4727 }
4728 Log.d(TAG, "*********************");
4729 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004730 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004731 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004732 }
4733
4734 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004735 addDumpLog(tag, log, null, debugLog);
4736 }
4737
4738 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004739 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004740 if (e != null) {
4741 Log.d(tag, log, e);
4742 } else {
4743 Log.d(tag, log);
4744 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004745 }
Winson Chungede41292013-09-19 16:27:36 -07004746 if (DEBUG_DUMP_LOG) {
4747 sDateStamp.setTime(System.currentTimeMillis());
4748 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004749 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4750 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004751 }
Winson Chungede41292013-09-19 16:27:36 -07004752 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004753 }
4754
Adam Cohen59400422014-03-05 18:07:04 -08004755 public static CustomAppWidget getCustomAppWidget(String name) {
4756 return sCustomAppWidgets.get(name);
4757 }
4758
4759 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4760 return sCustomAppWidgets;
4761 }
4762
Winson Chungede41292013-09-19 16:27:36 -07004763 public void dumpLogsToLocalData() {
4764 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004765 new AsyncTask<Void, Void, Void>() {
4766 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004767 boolean success = false;
4768 sDateStamp.setTime(sRunStart);
4769 String FILENAME = sDateStamp.getMonth() + "-"
4770 + sDateStamp.getDay() + "_"
4771 + sDateStamp.getHours() + "-"
4772 + sDateStamp.getMinutes() + "_"
4773 + sDateStamp.getSeconds() + ".txt";
4774
4775 FileOutputStream fos = null;
4776 File outFile = null;
4777 try {
4778 outFile = new File(getFilesDir(), FILENAME);
4779 outFile.createNewFile();
4780 fos = new FileOutputStream(outFile);
4781 } catch (Exception e) {
4782 e.printStackTrace();
4783 }
4784 if (fos != null) {
4785 PrintWriter writer = new PrintWriter(fos);
4786
4787 writer.println(" ");
4788 writer.println("Debug logs: ");
4789 synchronized (sDumpLogs) {
4790 for (int i = 0; i < sDumpLogs.size(); i++) {
4791 writer.println(" " + sDumpLogs.get(i));
4792 }
4793 }
4794 writer.close();
4795 }
4796 try {
4797 if (fos != null) {
4798 fos.close();
4799 success = true;
4800 }
4801 } catch (IOException e) {
4802 e.printStackTrace();
4803 }
Michael Jurka43467462013-11-14 12:03:58 +01004804 return null;
Winson Chungede41292013-09-19 16:27:36 -07004805 }
Michael Jurka43467462013-11-14 12:03:58 +01004806 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004807 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004809}
Michael Jurka2763be32011-02-24 11:19:57 -08004810
Michael Jurkaabded662011-03-04 12:06:57 -08004811interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004812 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004813 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004814 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004815 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004816 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004817}
Dan Sandlerd5024042014-01-09 15:01:33 -05004818
4819interface DebugIntents {
4820 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4821 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004822}