blob: 6697dc105588db3ddaf3450cf922cb28c676d31d [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040059import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020061import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070085import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070087import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
92import android.widget.FrameLayout;
93import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080094import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070096
Sunny Goyal651077b2014-06-30 14:15:31 -070097import com.android.launcher3.DropTarget.DragObject;
98import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyal83a8f042015-05-19 12:52:12 -070099import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700100import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700105import com.android.launcher3.model.WidgetsModel;
Sunny Goyale2df0622015-04-24 11:27:00 -0700106import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700107import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700108import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700109import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700110import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700111
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.io.DataInputStream;
113import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700115import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700119import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800120import java.lang.reflect.InvocationTargetException;
121import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700124import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700127import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000129import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131/**
132 * Default launcher application.
133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100134public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
137 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700138 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700139 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Winson Chung83f59ab2015-05-05 17:21:58 -0700141 // Temporary flag
142 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
143
Daniel Sandlerf061f822013-06-27 13:59:36 -0400144 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700145 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700146 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400147 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700148 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700149
Dan Sandlerd5024042014-01-09 15:01:33 -0500150 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700153 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700155 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Michael Jurka8b805b12012-04-18 14:23:14 -0700157 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700158 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700159
Mathew Inwood876a8462013-06-14 14:12:41 +0100160 /**
161 * IntentStarter uses request codes starting with this. This must be greater than all activity
162 * request codes used internally.
163 */
164 protected static final int REQUEST_LAST = 100;
165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
167
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800168 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Michael Jurka0a457bf2012-11-19 14:05:05 -0800170 // To turn on these properties, type
171 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800172 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Winson Chung2672ff92012-05-04 16:22:30 -0700174 // The Intent extra that defines whether to ignore the launch animation
175 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400176 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: int
179 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700180 // Type: int
181 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
184 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700189 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
192 // Type: int
193 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
194 // Type: parcelable
195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000198 // Type: int[]
199 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Adam Cohen432609a2014-03-13 17:03:22 -0700201 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800202 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
203
Adam Cohen3ed316a2014-07-23 18:21:20 -0700204 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
205 static final String ACTION_FIRST_LOAD_COMPLETE =
206 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
207
Adam Cohen39a06042013-07-19 14:30:12 -0700208 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700209 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700210
Sunny Goyal594d76d2014-11-06 10:12:54 -0800211 private static final String QSB_WIDGET_ID = "qsb_widget_id";
212 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
213
Winson Chunga6945242014-01-08 14:04:34 -0800214 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
215
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700216 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800217 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700220
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700221 private boolean mIsSafeModeEnabled;
222
Adam Cohenc2d6e892014-10-16 09:49:24 -0700223 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
224 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700225 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700226
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700228 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
229 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700230 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000232 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
233 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
234
Winson Chunga2413752012-04-03 14:22:34 -0700235 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700236 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
237 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700238 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700239
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800240 private final BroadcastReceiver mCloseSystemDialogsReceiver
241 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800242 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 private LayoutInflater mInflater;
245
Adam Cohen091440a2015-03-18 14:16:05 -0700246 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700247 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800248 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700249 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800250 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700251 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700252
Sunny Goyalffe83f12014-08-14 17:39:34 -0700253 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700254 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700255
Adam Cohen091440a2015-03-18 14:16:05 -0700256 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800257 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800258 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700259
Michael Jurka0280c3b2010-09-17 15:00:07 -0700260 private int[] mTmpAddItemCellCoordinates = new int[2];
261
Winson Chung3d503fb2011-07-13 17:25:49 -0700262 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800263 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700264
Michael Jurka838a4ca2011-02-07 13:33:06 -0800265 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700266
Winson Chungc51db6a2011-10-05 11:44:49 -0700267 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700268
269 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700270 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700271
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700272 // Main container view and the model for the widget tray screen.
273 @Thunk WidgetsContainerView mWidgetsView;
274 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700275
Winson Chungf0ea4d32011-06-06 14:27:16 -0700276 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800277 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700280 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
281 // scroll issues (because the workspace may not have been measured yet) and extra work.
282 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
283 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284
285 private SpannableStringBuilder mDefaultKeySsb = null;
286
Adam Cohen091440a2015-03-18 14:16:05 -0700287 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800289 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 private boolean mRestoring;
291 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700292 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293
Michael Jurka1e2f4652013-07-08 18:03:46 -0700294 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700295 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
296
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 private Bundle mSavedInstanceState;
298
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800300 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700301 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800302 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700303 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305
Adam Cohen091440a2015-03-18 14:16:05 -0700306 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700307
Sunny Goyale2df0622015-04-24 11:27:00 -0700308 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700309
Adam Cohen61f560d2013-09-30 15:58:20 -0700310 private View.OnTouchListener mHapticFeedbackTouchListener;
311
Adam Cohended9f8d2010-11-03 13:25:16 -0700312 // Related to the auto-advancing of widgets
313 private final int ADVANCE_MSG = 1;
314 private final int mAdvanceInterval = 20000;
315 private final int mAdvanceStagger = 250;
316 private long mAutoAdvanceSentTime;
317 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700318 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700319 new HashMap<View, AppWidgetProviderInfo>();
320
Winson Chung400438b2011-01-16 17:53:48 -0800321 // Determines how long to wait after a rotation before restoring the screen orientation to
322 // match the sensor state.
323 private final int mRestoreScreenOrientationDelay = 500;
324
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700326
Adam Cohen1462de32012-07-24 22:34:36 -0700327 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700328 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700329
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700330 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700331 static Date sDateStamp = new Date();
332 static DateFormat sDateFormat =
333 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
334 static long sRunStart = System.currentTimeMillis();
335 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700336
Winson Chung46353de2012-02-16 14:05:10 -0800337 // We only want to get the SharedPreferences once since it does an FS stat each time we get
338 // it from the context.
339 private SharedPreferences mSharedPrefs;
340
Winson Chungf0c6ae02012-03-21 16:10:31 -0700341 // Holds the page that we need to animate to, and the icon views that we need to animate up
342 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700343 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700344 private Bitmap mFolderIconBitmap;
345 private Canvas mFolderIconCanvas;
346 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700347
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700348 private DeviceProfile mDeviceProfile;
349
Winson Chung13eb5272015-05-11 16:30:13 -0700350 // This is set to the view that launched the activity that navigated the user away from
351 // launcher. Since there is no callback for when the activity has finished launching, enable
352 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800353 private BubbleTextView mWaitingForResume;
354
Adam Cohen59400422014-03-05 18:07:04 -0800355 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
356 new HashMap<String, CustomAppWidget>();
357
358 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
359 static {
360 if (ENABLE_CUSTOM_WIDGET_TEST) {
361 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
362 }
363 }
364
Chet Haasea8f996d2015-02-17 12:56:04 -0800365 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
366 private static Method sClipRevealMethod = null;
367 static {
368 Class<?> activityOptionsClass = ActivityOptions.class;
369 try {
370 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
371 View.class, int.class, int.class, int.class, int.class);
372 } catch (Exception e) {
373 // Earlier version
374 }
375 }
376
Adam Cohen091440a2015-03-18 14:16:05 -0700377 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700378 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700379 if (mWorkspace != null) {
380 mWorkspace.buildPageHardwareLayers();
381 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700382 }
383 };
384
Adam Cohendb364c32014-05-20 14:23:40 -0700385 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800386
Adam Cohen091440a2015-03-18 14:16:05 -0700387 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800388 int requestCode;
389 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700390 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700391 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800392 int cellX;
393 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700394 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800395 }
396
Daniel Sandlerff02d492013-08-05 02:12:05 -0400397 private Stats mStats;
398
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700399 FocusIndicatorView mFocusHandler;
400
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 @Override
402 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700403 if (DEBUG_STRICT_MODE) {
404 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
405 .detectDiskReads()
406 .detectDiskWrites()
407 .detectNetwork() // or .detectAll() for all detectable problems
408 .penaltyLog()
409 .build());
410 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
411 .detectLeakedSqlLiteObjects()
412 .detectLeakedClosableObjects()
413 .penaltyLog()
414 .penaltyDeath()
415 .build());
416 }
417
Adam Cohen9211d422014-10-07 18:14:53 -0700418 if (mLauncherCallbacks != null) {
419 mLauncherCallbacks.preOnCreate();
420 }
421
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400423
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400424 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400425 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700426 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700427
Winson Chung5f8afe62013-08-12 16:19:28 -0700428 // Lazy-initialize the dynamic grid
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700429 mDeviceProfile = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700430
431 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400432 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700433 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700434 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800435 mModel = app.setLauncher(this);
436 mIconCache = app.getIconCache();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700437 mIconCache.flushInvalidIcons(mDeviceProfile);
Joe Onorato41a12d22009-10-31 18:30:00 -0400438 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800440 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700441
Daniel Sandlerff02d492013-08-05 02:12:05 -0400442 mStats = new Stats(this);
443
Sunny Goyalffe83f12014-08-14 17:39:34 -0700444 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700445
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700446 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
447 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700448
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700449 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
450 // this also ensures that any synchronous binding below doesn't re-trigger another
451 // LauncherModel load.
452 mPaused = false;
453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200455 android.os.Debug.startMethodTracing(
456 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
458
459 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700463 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800465 registerContentObservers();
466
Joe Onorato7c312c12009-08-13 21:36:53 -0700467 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700468
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 mSavedState = savedInstanceState;
470 restoreState(mSavedState);
471
472 if (PROFILE_STARTUP) {
473 android.os.Debug.stopMethodTracing();
474 }
475
476 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700477 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 // If the user leaves launcher, then we should just load items asynchronously when
479 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700480 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700481 } else {
482 // We only load the page synchronously if the user rotates (or triggers a
483 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700484 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700485 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 }
487
488 // For handling default keys
489 mDefaultKeySsb = new SpannableStringBuilder();
490 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800491
492 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
493 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800494
Adam Cohenf9426d52012-06-04 17:26:21 -0700495 // On large interfaces, we want the screen to auto-rotate based on the current orientation
496 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700497
Adam Cohen9211d422014-10-07 18:14:53 -0700498 if (mLauncherCallbacks != null) {
499 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700500 if (mLauncherCallbacks.hasLauncherOverlay()) {
501 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700502 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
503 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
504 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700505 mWorkspace.setLauncherOverlay(mLauncherOverlay);
506 }
Adam Cohen9211d422014-10-07 18:14:53 -0700507 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700508
509 if (shouldShowIntroScreen()) {
510 showIntroScreen();
511 } else {
512 showFirstRunActivity();
513 showFirstRunClings();
514 }
Adam Cohen9211d422014-10-07 18:14:53 -0700515 }
516
517 private LauncherCallbacks mLauncherCallbacks;
518
519 public void onPostCreate(Bundle savedInstanceState) {
520 super.onPostCreate(savedInstanceState);
521 if (mLauncherCallbacks != null) {
522 mLauncherCallbacks.onPostCreate(savedInstanceState);
523 }
524 }
525
526 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
527 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700528 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
529 @Override
530 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700531 if (LOGD) {
532 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
533 }
Winson Chung94804152015-05-08 13:06:44 -0700534 mAppsView.setFixedBounds(bounds);
535 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700536 }
537
538 @Override
539 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700540 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
541 // Dismiss All Apps if we aren't already paused/invisible
542 if (!mPaused) {
543 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
544 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
545 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700546 }
Winson Chung0f785722015-04-08 10:27:49 -0700547 }
548 });
Adam Cohen9211d422014-10-07 18:14:53 -0700549 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700550 }
551
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700552 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700553 public void onLauncherProviderChange() {
554 if (mLauncherCallbacks != null) {
555 mLauncherCallbacks.onLauncherProviderChange();
556 }
557 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700558
Adam Cohen9211d422014-10-07 18:14:53 -0700559 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700560 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700561 if (mLauncherCallbacks != null) {
562 return mLauncherCallbacks.hasCustomContentToLeft();
563 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700564 return false;
565 }
566
Dave Hawkeya8881582013-09-17 15:55:33 -0600567 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500568 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600569 * {@link #addToCustomContentPage}. This will only be invoked if
570 * {@link #hasCustomContentToLeft()} is {@code true}.
571 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500572 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700573 if (mLauncherCallbacks != null) {
574 mLauncherCallbacks.populateCustomContentContainer();
575 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600576 }
577
578 /**
579 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
580 * ensure the custom content page is added or removed if necessary.
581 */
582 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600583 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600584 // Not bound yet, wait for bindScreens to be called.
585 return;
586 }
587
588 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
589 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500590 mWorkspace.createCustomContentContainer();
591 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600592 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
593 mWorkspace.removeCustomContentPage();
594 }
595 }
596
Adam Cohen091440a2015-03-18 14:16:05 -0700597 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700598 if (sLocaleConfiguration == null) {
599 new AsyncTask<Void, Void, LocaleConfiguration>() {
600 @Override
601 protected LocaleConfiguration doInBackground(Void... unused) {
602 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
603 readConfiguration(Launcher.this, localeConfiguration);
604 return localeConfiguration;
605 }
606
607 @Override
608 protected void onPostExecute(LocaleConfiguration result) {
609 sLocaleConfiguration = result;
610 checkForLocaleChange(); // recursive, but now with a locale configuration
611 }
612 }.execute();
613 return;
614 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700615
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 final Configuration configuration = getResources().getConfiguration();
617
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700618 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 final String locale = configuration.locale.toString();
620
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700621 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 final int mcc = configuration.mcc;
623
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700624 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 final int mnc = configuration.mnc;
626
Romain Guy84f296c2009-11-04 15:00:44 -0800627 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628
Romain Guy84f296c2009-11-04 15:00:44 -0800629 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700630 sLocaleConfiguration.locale = locale;
631 sLocaleConfiguration.mcc = mcc;
632 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700633
Joe Onorato0589f0f2010-02-08 13:44:00 -0800634 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700635
636 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100637 new AsyncTask<Void, Void, Void>() {
638 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700639 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100640 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700641 }
Michael Jurka43467462013-11-14 12:03:58 +0100642 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700643 }
644 }
645
Adam Cohen091440a2015-03-18 14:16:05 -0700646 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700647 public String locale;
648 public int mcc = -1;
649 public int mnc = -1;
650 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700651
Adam Cohen091440a2015-03-18 14:16:05 -0700652 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700653 DataInputStream in = null;
654 try {
Helena Josol28db2802014-10-09 17:04:09 +0100655 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700656 configuration.locale = in.readUTF();
657 configuration.mcc = in.readInt();
658 configuration.mnc = in.readInt();
659 } catch (FileNotFoundException e) {
660 // Ignore
661 } catch (IOException e) {
662 // Ignore
663 } finally {
664 if (in != null) {
665 try {
666 in.close();
667 } catch (IOException e) {
668 // Ignore
669 }
670 }
671 }
672 }
673
Adam Cohen091440a2015-03-18 14:16:05 -0700674 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700675 DataOutputStream out = null;
676 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100677 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100678 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700679 out.writeUTF(configuration.locale);
680 out.writeInt(configuration.mcc);
681 out.writeInt(configuration.mnc);
682 out.flush();
683 } catch (FileNotFoundException e) {
684 // Ignore
685 } catch (IOException e) {
686 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100687 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700688 } finally {
689 if (out != null) {
690 try {
691 out.close();
692 } catch (IOException e) {
693 // Ignore
694 }
695 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 }
697 }
698
Anton Hanssona2f665f2013-09-26 12:18:32 +0100699 public Stats getStats() {
700 return mStats;
701 }
702
Bjorn Bringertc459e522013-06-07 19:36:01 +0100703 public LayoutInflater getInflater() {
704 return mInflater;
705 }
706
Hyunyoung Song3f471442015-04-08 19:01:34 -0700707 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700708 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
709 // that is subsequently removed from the workspace in startBinding().
710 return !mModel.isLoadingWorkspace();
711 }
712
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800713 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000714 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100715 if (Build.VERSION.SDK_INT >= 17) {
716 return View.generateViewId();
717 } else {
718 // View.generateViewId() is not available. The following fallback logic is a copy
719 // of its implementation.
720 for (;;) {
721 final int result = sNextGeneratedId.get();
722 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
723 int newValue = result + 1;
724 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
725 if (sNextGeneratedId.compareAndSet(result, newValue)) {
726 return result;
727 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000728 }
729 }
730 }
731
732 public int getViewIdForItem(ItemInfo info) {
733 // This cast is safe given the > 2B range for int.
734 int itemId = (int) info.id;
735 if (mItemIdToViewId.containsKey(itemId)) {
736 return mItemIdToViewId.get(itemId);
737 }
738 int viewId = generateViewId();
739 mItemIdToViewId.put(itemId, viewId);
740 return viewId;
741 }
742
743 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700744 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
745 * a configuration step, this allows the proper animations to run after other transitions.
746 */
Adam Cohendb364c32014-05-20 14:23:40 -0700747 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700748 long screenId = args.screenId;
749 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
750 // When the screen id represents an actual screen (as opposed to a rank) we make sure
751 // that the drop page actually exists.
752 screenId = ensurePendingDropLayoutExists(args.screenId);
753 }
Adam Cohendb364c32014-05-20 14:23:40 -0700754
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800755 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800756 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700757 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700758 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700759 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800760 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700761 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700762 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700763 case REQUEST_RECONFIGURE_APPWIDGET:
764 completeRestoreAppWidget(args.appWidgetId);
765 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800766 }
Michael Jurka27614d22012-04-02 06:40:22 -0700767 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
768 // if you turned the screen off and then back while in All Apps, Launcher would not
769 // return to the workspace. Clearing mAddInfo.container here fixes this issue
770 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700771 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800772 }
773
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800774 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700775 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700776 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700777 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700778 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800779 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700780
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 Runnable exitSpringLoaded = new Runnable() {
782 @Override
783 public void run() {
784 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
785 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
786 }
787 };
788
Michael Jurka8b805b12012-04-18 14:23:14 -0700789 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700790 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700791 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
792 if (resultCode == RESULT_CANCELED) {
793 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700794 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700795 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700796 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800797 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700798 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700799 }
800 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200801 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
802 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700803 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200804 }
805 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700806 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200807
Adam Cohened66b2b2012-01-23 17:28:51 -0800808 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700809 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700810
Adam Cohendb364c32014-05-20 14:23:40 -0700811 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800812 // We have special handling for widgets
813 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800814 final int appWidgetId;
815 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
816 : -1;
817 if (widgetId < 0) {
818 appWidgetId = pendingAddWidgetId;
819 } else {
820 appWidgetId = widgetId;
821 }
822
Adam Cohenad4e15c2013-10-17 16:21:35 -0700823 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800824 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700825 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
826 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700827 result = RESULT_CANCELED;
828 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700829 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700830 @Override
831 public void run() {
832 exitSpringLoadedDragModeDelayed(false, 0, null);
833 }
834 };
Adam Cohendb364c32014-05-20 14:23:40 -0700835 if (workspaceLocked) {
836 // No need to remove the empty screen if we're mid-binding, as the
837 // the bind will not add the empty screen.
838 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
839 } else {
840 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
841 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
842 }
Winson Chung5aaab772012-04-27 15:24:02 -0700843 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700844 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700845 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
846 // When the screen id represents an actual screen (as opposed to a rank)
847 // we make sure that the drop page actually exists.
848 mPendingAddInfo.screenId =
849 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
850 }
Adam Cohendb364c32014-05-20 14:23:40 -0700851 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
852
853 dropLayout.setDropPending(true);
854 final Runnable onComplete = new Runnable() {
855 @Override
856 public void run() {
857 completeTwoStageWidgetDrop(resultCode, appWidgetId);
858 dropLayout.setDropPending(false);
859 }
860 };
861 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
862 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
863 } else {
864 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
865 mPendingAddInfo);
866 sPendingAddItem = args;
867 }
Winson Chung5aaab772012-04-27 15:24:02 -0700868 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 return;
870 }
871
Sunny Goyalff572272014-07-23 13:58:07 -0700872 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
873 if (resultCode == RESULT_OK) {
874 // Update the widget view.
875 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
876 pendingAddWidgetId, mPendingAddInfo);
877 if (workspaceLocked) {
878 sPendingAddItem = args;
879 } else {
880 completeAdd(args);
881 }
882 }
883 // Leave the widget in the pending state if the user canceled the configure.
884 return;
885 }
886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 // The pattern used here is that a user PICKs a specific application,
888 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700889
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
891 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700892 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700893 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
894 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800895 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700896 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800897 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700898 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700899 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
900 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 }
Adam Cohen00074722013-10-11 17:46:09 -0700902 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700903 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700904 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800906 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800907
908 }
909
910 @Override
911 protected void onActivityResult(
912 final int requestCode, final int resultCode, final Intent data) {
913 handleActivityResult(requestCode, resultCode, data);
914 if (mLauncherCallbacks != null) {
915 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
916 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 }
918
Adam Cohendb364c32014-05-20 14:23:40 -0700919 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
920 appWidgetId, ItemInfo info) {
921 PendingAddArguments args = new PendingAddArguments();
922 args.requestCode = requestCode;
923 args.intent = data;
924 args.container = info.container;
925 args.screenId = info.screenId;
926 args.cellX = info.cellX;
927 args.cellY = info.cellY;
928 args.appWidgetId = appWidgetId;
929 return args;
930 }
931
932 /**
933 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
934 *
935 * @param screenId the screen id to check
936 * @return the new screen, or screenId if it exists
937 */
938 private long ensurePendingDropLayoutExists(long screenId) {
939 CellLayout dropLayout =
940 (CellLayout) mWorkspace.getScreenWithId(screenId);
941 if (dropLayout == null) {
942 // it's possible that the add screen was removed because it was
943 // empty and a re-bind occurred
944 mWorkspace.addExtraEmptyScreen();
945 return mWorkspace.commitExtraEmptyScreen();
946 } else {
947 return screenId;
948 }
949 }
950
Adam Cohen091440a2015-03-18 14:16:05 -0700951 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700952 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700953 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800954 Runnable onCompleteRunnable = null;
955 int animationType = 0;
956
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700957 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800958 if (resultCode == RESULT_OK) {
959 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
960 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700961 mPendingAddWidgetInfo);
962 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800963 onCompleteRunnable = new Runnable() {
964 @Override
965 public void run() {
966 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700967 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700968 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
969 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800970 }
971 };
972 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800973 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800974 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800975 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700976 if (mDragLayer.getAnimatedView() != null) {
977 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
978 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
979 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700980 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700981 // The animated view may be null in the case of a rotation during widget configuration
982 onCompleteRunnable.run();
983 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 }
985
986 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700987 protected void onStop() {
988 super.onStop();
989 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700990
991 if (mLauncherCallbacks != null) {
992 mLauncherCallbacks.onStop();
993 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700994 }
995
996 @Override
997 protected void onStart() {
998 super.onStart();
999 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -07001000
1001 if (mLauncherCallbacks != null) {
1002 mLauncherCallbacks.onStart();
1003 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001004 }
1005
1006 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001008 long startTime = 0;
1009 if (DEBUG_RESUME_TIME) {
1010 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001011 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001012 }
Adam Cohen9211d422014-10-07 18:14:53 -07001013
1014 if (mLauncherCallbacks != null) {
1015 mLauncherCallbacks.preOnResume();
1016 }
1017
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001019
Winson Chung4a2afa32012-07-19 14:53:05 -07001020 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001021 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001022 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001023 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001024 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001025 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001026 // view after launching an app, as they may be depending on the UI to be static to
1027 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001028 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -07001029 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001030 } else if (mOnResumeState == State.WIDGETS) {
1031 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001032 }
1033 mOnResumeState = State.NONE;
1034
Winson Chungcd99cd32015-04-29 11:03:24 -07001035 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001036 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1037 // Otherwise, notify the callbacks if we are in all apps mode
1038 if (mState == State.APPS) {
1039 if (mLauncherCallbacks != null) {
1040 mLauncherCallbacks.onAllAppsShown();
1041 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001042 }
1043 }
1044
Craig Mautner360310b2012-10-26 15:13:08 -07001045 // Background was set to gradient in onPause(), restore to black if in all apps.
1046 setWorkspaceBackground(mState == State.WORKSPACE);
1047
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001048 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001049 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001050 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001051 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001052 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001053 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001055 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001056 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1057 // execute them here
1058 long startTimeCallbacks = 0;
1059 if (DEBUG_RESUME_TIME) {
1060 startTimeCallbacks = System.currentTimeMillis();
1061 }
1062
Michael Jurka1e2f4652013-07-08 18:03:46 -07001063 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1064 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001065 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001066 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001067 if (DEBUG_RESUME_TIME) {
1068 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1069 (System.currentTimeMillis() - startTimeCallbacks));
1070 }
Michael Jurka447bf842013-05-15 14:52:15 +02001071 }
Michael Jurka54554252013-08-01 12:52:23 +02001072 if (mOnResumeCallbacks.size() > 0) {
1073 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1074 mOnResumeCallbacks.get(i).run();
1075 }
1076 mOnResumeCallbacks.clear();
1077 }
Winson Chunge4e50662012-01-23 14:45:13 -08001078
1079 // Reset the pressed state of icons that were locked in the press state while activities
1080 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001081 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001082 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001083 mWaitingForResume.setStayPressed(false);
1084 }
Winson Chung82963d52013-10-09 11:20:57 -07001085
Adam Cohen06dff352012-06-01 17:17:08 -07001086 // It is possible that widgets can receive updates while launcher is not in the foreground.
1087 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1088 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1089 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001090 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001091 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001092
Michael Jurka447bf842013-05-15 14:52:15 +02001093 if (DEBUG_RESUME_TIME) {
1094 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1095 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001096
1097 if (mWorkspace.getCustomContentCallbacks() != null) {
1098 // If we are resuming and the custom content is the current page, we call onShow().
1099 // It is also poassible that onShow will instead be called slightly after first layout
1100 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1101 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001102 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001103 }
1104 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001105 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001106 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001107
Sunny Goyal756adbc2015-04-16 15:20:51 -07001108 if (!isWorkspaceLoading()) {
1109 // Process any items that were added while Launcher was away.
1110 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1111 }
Adam Cohen9211d422014-10-07 18:14:53 -07001112
1113 if (mLauncherCallbacks != null) {
1114 mLauncherCallbacks.onResume();
1115 }
Adam Cohen06dff352012-06-01 17:17:08 -07001116 }
1117
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001119 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001120 // Ensure that items added to Launcher are queued until Launcher returns
1121 InstallShortcutReceiver.enableInstallQueue();
1122
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001123 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001124 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001125 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001126 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001127
1128 // We call onHide() aggressively. The custom content callbacks should be able to
1129 // debounce excess onHide calls.
1130 if (mWorkspace.getCustomContentCallbacks() != null) {
1131 mWorkspace.getCustomContentCallbacks().onHide();
1132 }
Romain Guycbb89e42009-06-08 15:52:54 -07001133
Adam Cohen9211d422014-10-07 18:14:53 -07001134 if (mLauncherCallbacks != null) {
1135 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001136 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001137 }
1138
1139 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001140 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1141 // by a onResume or by scrolling otherwise.
1142 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001143
1144 // Custom content is completely hidden
1145 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001146
1147 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1148 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001149
1150 // Indicates whether the user is allowed to scroll away from the custom content.
1151 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001152 }
1153
Adam Cohenc2d6e892014-10-16 09:49:24 -07001154 public interface LauncherOverlay {
1155
1156 /**
1157 * Touch interaction leading to overscroll has begun
1158 */
1159 public void onScrollInteractionBegin();
1160
1161 /**
1162 * Touch interaction related to overscroll has ended
1163 */
1164 public void onScrollInteractionEnd();
1165
1166 /**
1167 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1168 * screen (or in the case of RTL, the rightmost screen).
1169 */
1170 public void onScrollChange(int progress, boolean rtl);
1171
1172 /**
1173 * Screen has stopped scrolling
1174 */
1175 public void onScrollSettled();
1176
1177 /**
1178 * This method can be called by the Launcher in order to force the LauncherOverlay
1179 * to exit fully immersive mode.
1180 */
1181 public void forceExitFullImmersion();
1182 }
1183
Winson Chung0f785722015-04-08 10:27:49 -07001184 public interface LauncherAppsCallbacks {
1185 /**
1186 * Updates launcher to the available space that AllApps can take so as not to overlap with
1187 * any other views.
1188 */
1189 public void onAllAppsBoundsChanged(Rect bounds);
1190
1191 /**
1192 * Called to dismiss all apps if it is showing.
1193 */
1194 public void dismissAllApps();
1195 }
1196
Adam Cohenc2d6e892014-10-16 09:49:24 -07001197 public interface LauncherOverlayCallbacks {
1198 /**
1199 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1200 * however it doesn't modify any state within the launcher.
1201 */
1202 public boolean canEnterFullImmersion();
1203
1204 /**
1205 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1206 * eg. by occupying the full screen and handling all touch events.
1207 *
1208 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1209 * case, Launcher will modify any necessary state and assumes the overlay is
1210 * handling all interaction. If false, the LauncherOverlay should cancel any
1211 *
1212 */
1213 public boolean enterFullImmersion();
1214
1215 /**
1216 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1217 * full control over UI and state.
1218 */
1219 public void exitFullImmersion();
1220 }
1221
1222 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1223
1224 @Override
1225 public boolean canEnterFullImmersion() {
1226 return mState == State.WORKSPACE;
1227 }
1228
1229 @Override
1230 public boolean enterFullImmersion() {
1231 if (mState == State.WORKSPACE) {
1232 // When fully immersed, disregard any touches which fall through.
1233 mDragLayer.setBlockTouch(true);
1234 return true;
1235 }
1236 return false;
1237 }
1238
1239 @Override
1240 public void exitFullImmersion() {
1241 mDragLayer.setBlockTouch(false);
1242 }
1243 }
1244
Jorim Jaggid017f882014-01-14 17:08:48 -08001245 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001246 if (mLauncherCallbacks != null) {
1247 return mLauncherCallbacks.hasSettings();
1248 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001249 return false;
1250 }
1251
Adam Cohen9211d422014-10-07 18:14:53 -07001252 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001253 CustomContentCallbacks callbacks, String description) {
1254 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001255 }
1256
Adam Cohenedb40762013-07-18 16:45:45 -07001257 // The custom content needs to offset its content to account for the QSB
1258 public int getTopOffsetForCustomContent() {
1259 return mWorkspace.getPaddingTop();
1260 }
1261
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001262 @Override
1263 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001264 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001265 if (mModel.isCurrentCallbacks(this)) {
1266 mModel.stopLoader();
1267 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001268 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1269
Romain Guy13c2e7b2010-03-10 19:45:00 -08001270 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001271 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001272
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001273 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001274 @Override
1275 public void onWindowFocusChanged(boolean hasFocus) {
1276 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001277 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001278
1279 if (mLauncherCallbacks != null) {
1280 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1281 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001282 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001283
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 private boolean acceptFilter() {
1285 final InputMethodManager inputManager = (InputMethodManager)
1286 getSystemService(Context.INPUT_METHOD_SERVICE);
1287 return !inputManager.isFullscreenMode();
1288 }
1289
1290 @Override
1291 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001292 final int uniChar = event.getUnicodeChar();
1293 final boolean handled = super.onKeyDown(keyCode, event);
1294 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1295 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1297 keyCode, event);
1298 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001299 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001300 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001301 // showSearchDialog()
1302 // If there are multiple keystrokes before the search dialog takes focus,
1303 // onSearchRequested() will be called for every keystroke,
1304 // but it is idempotent, so it's fine.
1305 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
1307 }
1308
Joe Onorato8a9625e2010-01-28 15:55:35 -08001309 // Eat the long press event so the keyboard doesn't come up.
1310 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1311 return true;
1312 }
1313
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 return handled;
1315 }
1316
Karl Rosaen138a0412009-04-23 19:00:21 -07001317 private String getTypedText() {
1318 return mDefaultKeySsb.toString();
1319 }
1320
1321 private void clearTypedText() {
1322 mDefaultKeySsb.clear();
1323 mDefaultKeySsb.clearSpans();
1324 Selection.setSelection(mDefaultKeySsb, 0);
1325 }
1326
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001328 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1329 * State
1330 */
1331 private static State intToState(int stateOrdinal) {
1332 State state = State.WORKSPACE;
1333 final State[] stateValues = State.values();
1334 for (int i = 0; i < stateValues.length; i++) {
1335 if (stateValues[i].ordinal() == stateOrdinal) {
1336 state = stateValues[i];
1337 break;
1338 }
1339 }
1340 return state;
1341 }
1342
1343 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 * Restores the previous state, if it exists.
1345 *
1346 * @param savedState The previous state.
1347 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001348 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 private void restoreState(Bundle savedState) {
1350 if (savedState == null) {
1351 return;
1352 }
1353
Michael Jurka883f55b2010-10-21 15:47:14 -07001354 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001355 if (state == State.APPS || state == State.WIDGETS) {
1356 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001357 }
1358
Adam Cohen21cd0022013-10-09 18:57:02 -07001359 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1360 PagedView.INVALID_RESTORE_PAGE);
1361 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001362 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 }
1364
Winson Chung3d503fb2011-07-13 17:25:49 -07001365 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001366 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001367
Winson Chung3d503fb2011-07-13 17:25:49 -07001368 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1369 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001370 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001371 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1372 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001373 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1374 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001375 AppWidgetProviderInfo info = savedState.getParcelable(
1376 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001377 mPendingAddWidgetInfo = info == null ?
1378 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1379
Adam Cohen4637b5a2013-11-04 18:21:24 -08001380 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001381 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 mRestoring = true;
1383 }
1384
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001385 mItemIdToViewId = (HashMap<Integer, Integer>)
1386 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
1388
1389 /**
1390 * Finds all the views we need and configure them properly.
1391 */
1392 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001393 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001394
Craig Mautner360310b2012-10-26 15:13:08 -07001395 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001396 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001397 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1398 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001399 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001400 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001401
John Spurlock77e1f472013-09-11 10:09:51 -04001402 mLauncherView.setSystemUiVisibility(
1403 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001404 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405
Winson Chung4c98d922011-05-31 16:50:48 -07001406 // Setup the drag layer
1407 mDragLayer.setup(this, dragController);
1408
Winson Chung3d503fb2011-07-13 17:25:49 -07001409 // Setup the hotseat
1410 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1411 if (mHotseat != null) {
1412 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001413 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001414 }
1415
Jorim Jaggid017f882014-01-14 17:08:48 -08001416 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001417 View widgetButton = findViewById(R.id.widget_button);
1418 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001419 @Override
1420 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001421 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001422 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001423 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001424 }
1425 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001426 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427
1428 View wallpaperButton = findViewById(R.id.wallpaper_button);
1429 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001430 @Override
1431 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001432 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001433 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001434 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001435 }
1436 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001437 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1438
1439 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001440 if (hasSettings()) {
1441 settingsButton.setOnClickListener(new OnClickListener() {
1442 @Override
1443 public void onClick(View arg0) {
1444 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001445 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001446 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001447 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001448 });
1449 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1450 } else {
1451 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001452 }
1453
Adam Cohendbdff6b2013-09-18 19:09:15 -07001454 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001455
Winson Chung4c98d922011-05-31 16:50:48 -07001456 // Setup the workspace
1457 mWorkspace.setHapticFeedbackEnabled(false);
1458 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001459 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001460 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001461
Winson Chungf0ea4d32011-06-06 14:27:16 -07001462 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001463 mSearchDropTargetBar = (SearchDropTargetBar)
1464 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001465
Winson Chungb745afb2015-03-02 11:51:23 -08001466 // Setup Apps
1467 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001468 if (isAllAppsSearchOverridden()) {
1469 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001470 }
Winson Chungb745afb2015-03-02 11:51:23 -08001471
Winson Chungf0ea4d32011-06-06 14:27:16 -07001472 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001473 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001474
Winson Chung3d503fb2011-07-13 17:25:49 -07001475 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001476 dragController.setDragScoller(mWorkspace);
1477 dragController.setScrollView(mDragLayer);
1478 dragController.setMoveTarget(mWorkspace);
1479 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001480 if (mSearchDropTargetBar != null) {
1481 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001482 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001483 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001484
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001485 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001486 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001487 mWeightWatcher = new WeightWatcher(this);
1488 mWeightWatcher.setAlpha(0.5f);
1489 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001490 new FrameLayout.LayoutParams(
1491 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001492 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001493 Gravity.BOTTOM)
1494 );
Adam Cohen39a06042013-07-19 14:30:12 -07001495
1496 boolean show = shouldShowWeightWatcher();
1497 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 }
1500
1501 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001502 * Sets the all apps button. This method is called from {@link Hotseat}.
1503 */
1504 public void setAllAppsButton(View allAppsButton) {
1505 mAllAppsButton = allAppsButton;
1506 }
1507
1508 public View getAllAppsButton() {
1509 return mAllAppsButton;
1510 }
1511
1512 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 * Creates a view representing a shortcut.
1514 *
1515 * @param info The data structure describing the shortcut.
1516 *
1517 * @return A View inflated from R.layout.application.
1518 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001519 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001520 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 }
1522
1523 /**
1524 * Creates a view representing a shortcut inflated from the specified resource.
1525 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 * @param parent The group the shortcut belongs to.
1527 * @param info The data structure describing the shortcut.
1528 *
1529 * @return A View inflated from layoutResId.
1530 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001531 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
1532 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.application,
1533 parent, false);
1534 favorite.applyFromShortcutInfo(info, mIconCache);
1535 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001537 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 return favorite;
1539 }
1540
1541 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 * Add a shortcut to the workspace.
1543 *
1544 * @param data The intent describing the shortcut.
1545 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001547 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001548 int cellY) {
1549 int[] cellXY = mTmpAddItemCellCoordinates;
1550 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001551 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001552
Sunny Goyal5c97f512015-05-19 16:03:28 -07001553 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001554 if (info == null) {
1555 return;
1556 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001557 final View view = createShortcut(info);
1558
Sunny Goyal5c97f512015-05-19 16:03:28 -07001559 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001560 // First we check if we already know the exact location where we want to add this item.
1561 if (cellX >= 0 && cellY >= 0) {
1562 cellXY[0] = cellX;
1563 cellXY[1] = cellY;
1564 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001565
1566 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001567 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001568 true, null,null)) {
1569 return;
1570 }
1571 DragObject dragObject = new DragObject();
1572 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001573 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1574 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001575 return;
1576 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001577 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001578 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001579 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001580 foundCellSpan = (result != null);
1581 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001582 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001583 }
1584
1585 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001586 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001587 return;
1588 }
1589
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001590 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591
1592 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001593 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001594 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
1596 }
1597
Adam Cohen2f093b62012-04-30 18:59:53 -07001598 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1599 int minHeight) {
1600 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001601 // We want to account for the extra amount of padding that we are adding to the widget
1602 // to ensure that it gets the full amount of space that it has requested
1603 int requiredWidth = minWidth + padding.left + padding.right;
1604 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001605 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001606 }
1607
Adam Cohen2f093b62012-04-30 18:59:53 -07001608 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1609 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001610 }
1611
Adam Cohen2f093b62012-04-30 18:59:53 -07001612 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1613 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001614 }
1615
Adam Cohen2f093b62012-04-30 18:59:53 -07001616 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1617 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001618 }
1619
Adam Cohen2f093b62012-04-30 18:59:53 -07001620 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1621 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001622 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001623 }
1624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001626 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001628 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 */
Adam Cohen091440a2015-03-18 14:16:05 -07001630 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001631 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1632
1633 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001634 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001635 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1636 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001637 }
Romain Guycbb89e42009-06-08 15:52:54 -07001638
Adam Cohen59400422014-03-05 18:07:04 -08001639 if (appWidgetInfo.isCustomWidget) {
1640 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001641 }
1642
Adam Cohen59400422014-03-05 18:07:04 -08001643 LauncherAppWidgetInfo launcherInfo;
1644 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1645 launcherInfo.spanX = info.spanX;
1646 launcherInfo.spanY = info.spanY;
1647 launcherInfo.minSpanX = info.minSpanX;
1648 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001649 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001650
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001652 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653
1654 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001655 if (hostView == null) {
1656 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001657 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1658 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001659 } else {
1660 // The AppWidgetHostView has already been inflated and instantiated
1661 launcherInfo.hostView = hostView;
1662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001664 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001665 launcherInfo.notifyWidgetSizeChanged(this);
1666
Adam Cohen59400422014-03-05 18:07:04 -08001667 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1668 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001669
1670 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001672 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
Romain Guycbb89e42009-06-08 15:52:54 -07001674
Adam Cohended9f8d2010-11-03 13:25:16 -07001675 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1676 @Override
1677 public void onReceive(Context context, Intent intent) {
1678 final String action = intent.getAction();
1679 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1680 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001681 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001682 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001683
Winson Chungc100e8e2011-08-09 16:02:43 -07001684 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001685 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001686 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001687 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001688 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001689 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001690 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1691 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001692 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001693 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_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 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1698 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001699 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001700 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1701 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 }
1703 }
1704 };
1705
1706 @Override
1707 public void onAttachedToWindow() {
1708 super.onAttachedToWindow();
1709
1710 // Listen for broadcasts related to user-presence
1711 final IntentFilter filter = new IntentFilter();
1712 filter.addAction(Intent.ACTION_SCREEN_OFF);
1713 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001714 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001715 if (ENABLE_DEBUG_INTENTS) {
1716 filter.addAction(DebugIntents.DELETE_DATABASE);
1717 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1718 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001719 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001720 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001721 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001722 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 mVisible = true;
1724 }
1725
Adam Cohen0b395352014-06-09 22:54:36 +00001726 /**
1727 * Sets up transparent navigation and status bars in LMP.
1728 * This method is a no-op for other platform versions.
1729 */
Sunny Goyald0091012015-01-20 15:55:34 -08001730 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001731 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001732 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001733 Window window = getWindow();
1734 window.getAttributes().systemUiVisibility |=
1735 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1736 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1737 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1738 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1739 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1740 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1741 window.setStatusBarColor(Color.TRANSPARENT);
1742 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001743 }
1744 }
1745
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 @Override
1747 public void onDetachedFromWindow() {
1748 super.onDetachedFromWindow();
1749 mVisible = false;
1750
Adam Cohend113e0c2010-11-11 10:48:05 -08001751 if (mAttached) {
1752 unregisterReceiver(mReceiver);
1753 mAttached = false;
1754 }
Winson Chungb745afb2015-03-02 11:51:23 -08001755 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 }
1757
1758 public void onWindowVisibilityChanged(int visibility) {
1759 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001760 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001761 // The following code used to be in onResume, but it turns out onResume is called when
1762 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1763 // is a more appropriate event to handle
1764 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001765 if (!mWorkspaceLoading) {
1766 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001767 // We want to let Launcher draw itself at least once before we force it to build
1768 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001769 // apps is nice and speedy.
1770 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001771 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001772 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001773 if (mStarted) return;
1774 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001775 // We delay the layer building a bit in order to give
1776 // other message processing a time to run. In particular
1777 // this avoids a delay in hiding the IME if it was
1778 // currently shown, because doing that may involve
1779 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001780 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001781 final ViewTreeObserver.OnDrawListener listener = this;
1782 mWorkspace.post(new Runnable() {
1783 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001784 if (mWorkspace != null &&
1785 mWorkspace.getViewTreeObserver() != null) {
1786 mWorkspace.getViewTreeObserver().
1787 removeOnDrawListener(listener);
1788 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001789 }
1790 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001791 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001792 }
1793 });
1794 }
1795 clearTypedText();
1796 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001797 }
1798
Adam Cohen091440a2015-03-18 14:16:05 -07001799 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001800 mHandler.removeMessages(ADVANCE_MSG);
1801 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1802 mHandler.sendMessageDelayed(msg, delay);
1803 mAutoAdvanceSentTime = System.currentTimeMillis();
1804 }
1805
Adam Cohen091440a2015-03-18 14:16:05 -07001806 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001807 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1808 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1809 mAutoAdvanceRunning = autoAdvanceRunning;
1810 if (autoAdvanceRunning) {
1811 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1812 sendAdvanceMessage(delay);
1813 } else {
1814 if (!mWidgetsToAdvance.isEmpty()) {
1815 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1816 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1817 }
1818 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001819 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001820 }
1821 }
1822 }
1823
1824 private final Handler mHandler = new Handler() {
1825 @Override
1826 public void handleMessage(Message msg) {
1827 if (msg.what == ADVANCE_MSG) {
1828 int i = 0;
1829 for (View key: mWidgetsToAdvance.keySet()) {
1830 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1831 final int delay = mAdvanceStagger * i;
1832 if (v instanceof Advanceable) {
1833 postDelayed(new Runnable() {
1834 public void run() {
1835 ((Advanceable) v).advance();
1836 }
1837 }, delay);
1838 }
1839 i++;
1840 }
1841 sendAdvanceMessage(mAdvanceInterval);
1842 }
1843 }
1844 };
1845
1846 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001847 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001848 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1849 if (v instanceof Advanceable) {
1850 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001851 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001852 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001853 }
1854 }
1855
1856 void removeWidgetToAutoAdvance(View hostView) {
1857 if (mWidgetsToAdvance.containsKey(hostView)) {
1858 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001859 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001860 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001861 }
1862
Joe Onorato9c1289c2009-08-17 11:03:03 -04001863 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001864 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001865 launcherInfo.hostView = null;
1866 }
1867
Hyunyoung Song3f471442015-04-08 19:01:34 -07001868 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001869 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1870 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001871 }
1872
Winson Chunga6945242014-01-08 14:04:34 -08001873 public DragLayer getDragLayer() {
1874 return mDragLayer;
1875 }
1876
Winson Chungb745afb2015-03-02 11:51:23 -08001877 public AppsContainerView getAppsView() {
1878 return mAppsView;
1879 }
1880
Hyunyoung Song3f471442015-04-08 19:01:34 -07001881 public WidgetsContainerView getWidgetsView() {
1882 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001883 }
1884
Winson Chunga6945242014-01-08 14:04:34 -08001885 public Workspace getWorkspace() {
1886 return mWorkspace;
1887 }
1888
1889 public Hotseat getHotseat() {
1890 return mHotseat;
1891 }
1892
Jorim Jaggid017f882014-01-14 17:08:48 -08001893 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001894 return mOverviewPanel;
1895 }
1896
1897 public SearchDropTargetBar getSearchBar() {
1898 return mSearchDropTargetBar;
1899 }
1900
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001901 public LauncherAppWidgetHost getAppWidgetHost() {
1902 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 }
Romain Guycbb89e42009-06-08 15:52:54 -07001904
Winson Chunga9abd0e2010-10-27 17:18:37 -07001905 public LauncherModel getModel() {
1906 return mModel;
1907 }
1908
Winson Chunga6945242014-01-08 14:04:34 -08001909 protected SharedPreferences getSharedPrefs() {
1910 return mSharedPrefs;
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
Craig Mautner360310b2012-10-26 15:13:08 -07003298 private void setWorkspaceBackground(boolean workspace) {
3299 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003300 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003301 }
3302
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003303 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003304 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3305 int curflags = getWindow().getAttributes().flags
3306 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3307 if (wpflags != curflags) {
3308 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3309 }
Craig Mautner360310b2012-10-26 15:13:08 -07003310 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003311 }
3312
Michael Jurkae326f182011-11-21 14:05:46 -08003313 @Override
3314 public void onTrimMemory(int level) {
3315 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003316 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3317 // The widget preview db can result in holding onto over
3318 // 3MB of memory for caching which isn't necessary.
3319 SQLiteDatabase.releaseMemory();
3320
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003321 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003322 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003323 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003324 if (mLauncherCallbacks != null) {
3325 mLauncherCallbacks.onTrimMemory(level);
3326 }
Michael Jurkae326f182011-11-21 14:05:46 -08003327 }
3328
Winson Chungb745afb2015-03-02 11:51:23 -08003329 @Override
3330 public void onStateTransitionHideSearchBar() {
3331 // Hide the search bar
3332 if (mSearchDropTargetBar != null) {
3333 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3334 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003335 }
3336
Winson Chungb745afb2015-03-02 11:51:23 -08003337 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003338 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3339 true);
Adam Cohened307df2013-10-02 09:37:31 -07003340 }
3341
Sunny Goyal83a8f042015-05-19 12:52:12 -07003342 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003343 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3344 onCompleteRunnable, true);
3345 }
3346
3347 protected void showWorkspace(int snapToPage, boolean animated) {
3348 showWorkspace(snapToPage, animated, null, true);
3349 }
3350
3351 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3352 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003353 boolean changed = mState != State.WORKSPACE ||
3354 mWorkspace.getState() != Workspace.State.NORMAL;
3355 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003356 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003357 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003358 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003359 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003360
Winson Chungc7d2b602012-05-16 17:02:20 -07003361 // Show the search bar (only animate if we were showing the drop target bar in spring
3362 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003363 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003364 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003365 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003366
Michael Jurkab3e22d92011-10-31 15:58:33 -07003367 // Set focus to the AppsCustomize button
3368 if (mAllAppsButton != null) {
3369 mAllAppsButton.requestFocus();
3370 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003371 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003372
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003373 // Change the state *after* we've called all the transition code
3374 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003375
Winson Chung5fb63472011-02-02 17:03:37 -08003376 // Resume the auto-advance of widgets
3377 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003378 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003379
Winson Chung0f785722015-04-08 10:27:49 -07003380 if (changed) {
3381 // Send an accessibility event to announce the context change
3382 getWindow().getDecorView()
3383 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003384 if (notifyLauncherCallbacks) {
3385 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003386 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003387 mLauncherCallbacks.onAllAppsHidden();
3388 }
3389 }
Winson Chung0f785722015-04-08 10:27:49 -07003390 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003391 }
3392
Adam Cohened307df2013-10-02 09:37:31 -07003393 void showOverviewMode(boolean animated) {
3394 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003395 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003396 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3397 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003398 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003399 }
3400
Winson Chungb745afb2015-03-02 11:51:23 -08003401 /**
3402 * Shows the apps view.
3403 */
Winson Chung4ac30062015-05-08 17:34:17 -07003404 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003405 if (resetListToTop) {
3406 mAppsView.scrollToTop();
3407 }
Winson Chung4ac30062015-05-08 17:34:17 -07003408 if (updatePredictedApps) {
3409 tryAndUpdatePredictedApps();
3410 }
Winson Chungb745afb2015-03-02 11:51:23 -08003411 showAppsOrWidgets(animated, State.APPS);
3412 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003413
Winson Chungb745afb2015-03-02 11:51:23 -08003414 /**
3415 * Shows the widgets view.
3416 */
3417 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003418 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003419 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003420 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003421 }
Winson Chungb745afb2015-03-02 11:51:23 -08003422 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003423
3424 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003425 @Override
3426 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003427 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003428 }
3429 });
Winson Chungb745afb2015-03-02 11:51:23 -08003430 }
3431
3432 /**
3433 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003434 *
3435 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003436 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003437 // TODO: calling method should use the return value so that when {@code false} is returned
3438 // the workspace transition doesn't fall into invalid state.
3439 private boolean showAppsOrWidgets(boolean animated, State toState) {
3440 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3441 mState != State.WIDGETS_SPRING_LOADED) {
3442 return false;
3443 }
3444 if (toState != State.APPS && toState != State.WIDGETS) {
3445 return false;
3446 }
Winson Chungb745afb2015-03-02 11:51:23 -08003447
3448 if (toState == State.APPS) {
3449 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003450 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003451 mLauncherCallbacks.onAllAppsShown();
3452 }
Winson Chungb745afb2015-03-02 11:51:23 -08003453 } else {
3454 mStateTransitionAnimation.startAnimationToWidgets(animated);
3455 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003456
Michael Jurkab3e22d92011-10-31 15:58:33 -07003457 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003458 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003459
3460 // Pause the auto-advance of widgets until we are out of AllApps
3461 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003462 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003463 closeFolder();
3464
3465 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003466 getWindow().getDecorView()
3467 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003468 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003469 }
3470
Winson Chungcd99cd32015-04-29 11:03:24 -07003471 /**
3472 * Updates the workspace and interaction state on state change, and return the animation to this
3473 * new state.
3474 */
3475 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3476 boolean animated, HashMap<View, Integer> layerViews) {
3477 Workspace.State fromState = mWorkspace.getState();
3478 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3479 updateInteraction(fromState, toState);
3480 return anim;
3481 }
3482
Hyunyoung Song3f471442015-04-08 19:01:34 -07003483 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003484 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003485 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3486 mState == State.WIDGETS_SPRING_LOADED) {
3487 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003488 }
Winson Chungb745afb2015-03-02 11:51:23 -08003489
3490 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003491 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3492 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003493 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003494 }
Adam Cohen7777d962011-08-18 18:58:38 -07003495
Hyunyoung Song3f471442015-04-08 19:01:34 -07003496 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003497 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003498 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003499
Winson Chungcd99cd32015-04-29 11:03:24 -07003500 if (successfulDrop) {
3501 // We need to trigger all apps hidden to notify search to update itself before the
3502 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003503 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003504 mLauncherCallbacks.onAllAppsHidden();
3505 }
3506 }
3507
Winson Chunge7a03942011-08-05 15:05:12 -07003508 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003509 @Override
3510 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003511 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003512 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3513 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003514 // Before we show workspace, hide all apps again because
3515 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3516 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003517 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003518 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003519 } else {
3520 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003521 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003522 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003523 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003524 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003525
Michael Jurkad3ef3062010-11-23 16:23:58 -08003526 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003527 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003528 showAppsView(true /* animated */, false /* resetListToTop */,
3529 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003530 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003531 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003532 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003533 }
3534
Winson Chung4ac30062015-05-08 17:34:17 -07003535 /**
3536 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3537 * resumed.
3538 */
3539 private void tryAndUpdatePredictedApps() {
3540 if (mLauncherCallbacks != null) {
3541 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3542 if (!apps.isEmpty()) {
3543 mAppsView.setPredictedApps(apps);
3544 }
3545 }
3546 }
3547
Michael Jurkab3e22d92011-10-31 15:58:33 -07003548 void lockAllApps() {
3549 // TODO
3550 }
3551
3552 void unlockAllApps() {
3553 // TODO
3554 }
3555
Sunny Goyal594d76d2014-11-06 10:12:54 -08003556 protected void disableVoiceButtonProxy(boolean disable) {
3557 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003558 }
3559
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003560 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003561 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3562 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003563 }
3564
Adam Cohen24ce0b32014-01-14 16:18:14 -08003565 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003566 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3567 if (searchProvider == null) {
3568 return null;
3569 }
3570
3571 Bundle opts = new Bundle();
3572 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003573 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003574
3575 SharedPreferences sp = getSharedPreferences(
3576 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3577 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003578 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003579 if (!searchProvider.provider.flattenToString().equals(
3580 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003581 || (widgetInfo == null)
3582 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003583 // A valid widget is not already bound.
3584 if (widgetId > -1) {
3585 mAppWidgetHost.deleteAppWidgetId(widgetId);
3586 widgetId = -1;
3587 }
3588
3589 // Try to bind a new widget
3590 widgetId = mAppWidgetHost.allocateAppWidgetId();
3591
3592 if (!AppWidgetManagerCompat.getInstance(this)
3593 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3594 mAppWidgetHost.deleteAppWidgetId(widgetId);
3595 widgetId = -1;
3596 }
3597
3598 sp.edit()
3599 .putInt(QSB_WIDGET_ID, widgetId)
3600 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3601 .commit();
3602 }
3603
3604 if (widgetId != -1) {
3605 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3606 mQsb.updateAppWidgetOptions(opts);
3607 mQsb.setPadding(0, 0, 0, 0);
3608 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003609 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003610 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003611 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003612 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003613 }
3614
Sunny Goyal22235bc2015-04-03 09:23:43 -07003615 private void reinflateQSBIfNecessary() {
3616 if (mQsb instanceof LauncherAppWidgetHostView &&
3617 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3618 mSearchDropTargetBar.removeView(mQsb);
3619 mQsb = null;
3620 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3621 }
3622 }
3623
Michael Jurkad7c28052012-04-27 15:43:36 -07003624 @Override
3625 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003626 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003627 final List<CharSequence> text = event.getText();
3628 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003629 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003630 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003631 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003632 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003633 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003634 } else {
3635 text.add(getString(R.string.all_apps_home_button_label));
3636 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003637 return result;
3638 }
3639
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003640 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003641 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003642 */
Adam Cohen091440a2015-03-18 14:16:05 -07003643 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003644 @Override
3645 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003646 closeSystemDialogs();
3647 }
3648 }
3649
3650 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003651 * Receives notifications whenever the appwidgets are reset.
3652 */
3653 private class AppWidgetResetObserver extends ContentObserver {
3654 public AppWidgetResetObserver() {
3655 super(new Handler());
3656 }
3657
3658 @Override
3659 public void onChange(boolean selfChange) {
3660 onAppWidgetReset();
3661 }
3662 }
3663
3664 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003665 * If the activity is currently paused, signal that we need to run the passed Runnable
3666 * in onResume.
3667 *
3668 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003669 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3670 * wrong when we're not running, and if the activity comes back to what the configuration was
3671 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003672 *
3673 * Implementation of the method from LauncherModel.Callbacks.
3674 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003675 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003676 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003677 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003678 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003679 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003680 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003681 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003682 }
3683 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003684 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003685 return true;
3686 } else {
3687 return false;
3688 }
3689 }
3690
Michael Jurkac402cd92013-05-20 15:49:32 +02003691 private boolean waitUntilResume(Runnable run) {
3692 return waitUntilResume(run, false);
3693 }
3694
Michael Jurka1e2f4652013-07-08 18:03:46 -07003695 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003696 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003697 }
3698
Michael Jurka7607c2f2013-04-03 14:33:19 -07003699 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003700 * If the activity is currently paused, signal that we need to re-run the loader
3701 * in onResume.
3702 *
3703 * This needs to be called from incoming places where resources might have been loaded
3704 * while we are paused. That is becaues the Configuration might be wrong
3705 * when we're not running, and if it comes back to what it was when we
3706 * were paused, we are not restarted.
3707 *
3708 * Implementation of the method from LauncherModel.Callbacks.
3709 *
3710 * @return true if we are currently paused. The caller might be able to
3711 * skip some work in that case since we will come back again.
3712 */
3713 public boolean setLoadOnResume() {
3714 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003715 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003716 mOnResumeNeedsLoad = true;
3717 return true;
3718 } else {
3719 return false;
3720 }
3721 }
3722
3723 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003724 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003725 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003726 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003727 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003728 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003729 } else {
3730 return SCREEN_COUNT / 2;
3731 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003733
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 /**
3735 * Refreshes the shortcuts shown on the workspace.
3736 *
3737 * Implementation of the method from LauncherModel.Callbacks.
3738 */
3739 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003740 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003741
Michael Jurka7607c2f2013-04-03 14:33:19 -07003742 // If we're starting binding all over again, clear any bind calls we'd postponed in
3743 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3744 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003745 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003746
Winson Chungd64d1762013-08-20 14:37:16 -07003747 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003748 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003749 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003750
Michael Jurka05bf6442011-11-30 20:28:53 -08003751 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003752 if (mHotseat != null) {
3753 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003754 }
3755 }
3756
Adam Cohendcd297f2013-06-18 13:13:40 -07003757 @Override
3758 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003759 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003760
Adam Cohen5084cba2013-09-03 12:01:16 -07003761 // If there are no screens, we need to have an empty screen
3762 if (orderedScreenIds.size() == 0) {
3763 mWorkspace.addExtraEmptyScreen();
3764 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003765
3766 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003767 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003768 if (hasCustomContentToLeft()) {
3769 mWorkspace.createCustomContentContainer();
3770 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003771 }
Winson Chung64359a52013-07-08 17:17:08 -07003772 }
3773
3774 @Override
3775 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003776 // Log to disk
3777 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3778 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3779 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003780 int count = orderedScreenIds.size();
3781 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003782 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003783 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003784 }
3785
Adam Cohen39a06042013-07-19 14:30:12 -07003786 private boolean shouldShowWeightWatcher() {
3787 String spKey = LauncherAppState.getSharedPreferencesKey();
3788 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003789 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003790
3791 return show;
3792 }
3793
3794 private void toggleShowWeightWatcher() {
3795 String spKey = LauncherAppState.getSharedPreferencesKey();
3796 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3797 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3798
3799 show = !show;
3800
3801 SharedPreferences.Editor editor = sp.edit();
3802 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3803 editor.commit();
3804
3805 if (mWeightWatcher != null) {
3806 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3807 }
3808 }
3809
Winson Chungd64d1762013-08-20 14:37:16 -07003810 public void bindAppsAdded(final ArrayList<Long> newScreens,
3811 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003812 final ArrayList<ItemInfo> addAnimated,
3813 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003814 Runnable r = new Runnable() {
3815 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003816 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003817 }
3818 };
3819 if (waitUntilResume(r)) {
3820 return;
3821 }
3822
Winson Chungd64d1762013-08-20 14:37:16 -07003823 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003824 if (newScreens != null) {
3825 bindAddScreens(newScreens);
3826 }
Winson Chungd64d1762013-08-20 14:37:16 -07003827
3828 // We add the items without animation on non-visible pages, and with
3829 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003830 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003831 bindItems(addNotAnimated, 0,
3832 addNotAnimated.size(), false);
3833 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003834 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003835 bindItems(addAnimated, 0,
3836 addAnimated.size(), true);
3837 }
Winson Chungc58497e2013-09-03 17:48:37 -07003838
Winson Chung87412982013-10-03 18:34:14 -07003839 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003840 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003841
Winson Chungb745afb2015-03-02 11:51:23 -08003842 if (addedApps != null && mAppsView != null) {
3843 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003844 }
Winson Chungd64d1762013-08-20 14:37:16 -07003845 }
3846
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003847 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003848 * Bind the items start-end from the list.
3849 *
3850 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003851 */
Winson Chung64359a52013-07-08 17:17:08 -07003852 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3853 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003854 Runnable r = new Runnable() {
3855 public void run() {
3856 bindItems(shortcuts, start, end, forceAnimateIcons);
3857 }
3858 };
3859 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003860 return;
3861 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003862
Winson Chungf0c6ae02012-03-21 16:10:31 -07003863 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003864 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3865 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003866 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003867 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003868 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003869 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003870 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003871
3872 // Short circuit if we are loading dock items for a configuration which has no dock
3873 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3874 mHotseat == null) {
3875 continue;
3876 }
3877
Joe Onorato9c1289c2009-08-17 11:03:03 -04003878 switch (item.itemType) {
3879 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3880 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003881 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003882 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003883
Winson Chung64359a52013-07-08 17:17:08 -07003884 /*
3885 * TODO: FIX collision case
3886 */
Winson Chungce376632013-07-11 16:12:41 -07003887 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3888 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3889 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003890 View v = cl.getChildAt(item.cellX, item.cellY);
3891 Object tag = v.getTag();
3892 String desc = "Collision while binding workspace item: " + item
3893 + ". Collides with " + tag;
3894 if (LauncherAppState.isDogfoodBuild()) {
3895 throw (new RuntimeException(desc));
3896 } else {
3897 Log.d(TAG, desc);
3898 }
Winson Chungce376632013-07-11 16:12:41 -07003899 }
Winson Chung64359a52013-07-08 17:17:08 -07003900 }
3901
Adam Cohendcd297f2013-06-18 13:13:40 -07003902 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3903 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003904 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003905 // Animate all the applications up now
3906 shortcut.setAlpha(0f);
3907 shortcut.setScaleX(0f);
3908 shortcut.setScaleY(0f);
3909 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003910 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003911 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003912 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003913 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003914 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003915 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003916 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003917 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3918 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003919 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003920 default:
3921 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003923 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003924
Winson Chung997a9232013-07-24 15:33:46 -07003925 if (animateIcons) {
3926 // Animate to the correct page
3927 if (newShortcutsScreenId > -1) {
3928 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003929 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003930 final Runnable startBounceAnimRunnable = new Runnable() {
3931 public void run() {
3932 anim.playTogether(bounceAnims);
3933 anim.start();
3934 }
3935 };
Winson Chung997a9232013-07-24 15:33:46 -07003936 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003937 // We post the animation slightly delayed to prevent slowdowns
3938 // when we are loading right after we return to launcher.
3939 mWorkspace.postDelayed(new Runnable() {
3940 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003941 if (mWorkspace != null) {
3942 mWorkspace.snapToPage(newScreenIndex);
3943 mWorkspace.postDelayed(startBounceAnimRunnable,
3944 NEW_APPS_ANIMATION_DELAY);
3945 }
Winson Chung94d67682013-09-25 16:29:40 -07003946 }
3947 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003948 } else {
3949 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003950 }
3951 }
Winson Chung64359a52013-07-08 17:17:08 -07003952 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003953 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003954 }
3955
Joe Onorato9c1289c2009-08-17 11:03:03 -04003956 /**
3957 * Implementation of the method from LauncherModel.Callbacks.
3958 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003959 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003960 Runnable r = new Runnable() {
3961 public void run() {
3962 bindFolders(folders);
3963 }
3964 };
3965 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003966 return;
3967 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003968 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003969 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003970
3971 /**
3972 * Add the views for a widget to the workspace.
3973 *
3974 * Implementation of the method from LauncherModel.Callbacks.
3975 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003976 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003977 Runnable r = new Runnable() {
3978 public void run() {
3979 bindAppWidget(item);
3980 }
3981 };
3982 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003983 return;
3984 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003985
Daniel Sandler843e8602010-06-07 14:59:01 -04003986 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3987 if (DEBUG_WIDGETS) {
3988 Log.d(TAG, "bindAppWidget: " + item);
3989 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003990 final Workspace workspace = mWorkspace;
3991
Adam Cohen59400422014-03-05 18:07:04 -08003992 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003993 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003994
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003995 if (!mIsSafeModeEnabled
3996 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3997 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003998 if (appWidgetInfo == null) {
3999 if (DEBUG_WIDGETS) {
4000 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4001 + " belongs to component " + item.providerName
4002 + ", as the povider is null");
4003 }
4004 LauncherModel.deleteItemFromDatabase(this, item);
4005 return;
4006 }
4007 // Note: This assumes that the id remap broadcast is received before this step.
4008 // If that is not the case, the id remap will be ignored and user may see the
4009 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004010 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004011 pendingInfo.spanX = item.spanX;
4012 pendingInfo.spanY = item.spanY;
4013 pendingInfo.minSpanX = item.minSpanX;
4014 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004015 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004016 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004017
Sunny Goyalff572272014-07-23 13:58:07 -07004018 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004019 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4020 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004021
4022 // TODO consider showing a permission dialog when the widget is clicked.
4023 if (!success) {
4024 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4025 if (DEBUG_WIDGETS) {
4026 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4027 + " belongs to component " + item.providerName
4028 + ", as the launcher is unable to bing a new widget id");
4029 }
4030 LauncherModel.deleteItemFromDatabase(this, item);
4031 return;
4032 }
4033
4034 item.appWidgetId = newWidgetId;
4035
4036 // If the widget has a configure activity, it is still needs to set it up, otherwise
4037 // the widget is ready to go.
4038 item.restoreStatus = (appWidgetInfo.configure == null)
4039 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4040 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4041
4042 LauncherModel.updateItemInDatabase(this, item);
4043 }
4044
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004045 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004046 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004047 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004048 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4049 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004050 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004051
Sunny Goyal651077b2014-06-30 14:15:31 -07004052 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4053 } else {
4054 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004055 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4056 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004057 view.updateIcon(mIconCache);
4058 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004059 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004060 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004061 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004062
Joe Onorato9c1289c2009-08-17 11:03:03 -04004063 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004064 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065
Adam Cohendcd297f2013-06-18 13:13:40 -07004066 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004067 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004068 if (!item.isCustomWidget()) {
4069 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4070 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004071
Joe Onorato9c1289c2009-08-17 11:03:03 -04004072 workspace.requestLayout();
4073
Daniel Sandler843e8602010-06-07 14:59:01 -04004074 if (DEBUG_WIDGETS) {
4075 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4076 + (SystemClock.uptimeMillis()-start) + "ms");
4077 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004078 }
4079
Sunny Goyalff572272014-07-23 13:58:07 -07004080 /**
4081 * Restores a pending widget.
4082 *
4083 * @param appWidgetId The app widget id
4084 * @param cellInfo The position on screen where to create the widget.
4085 */
4086 private void completeRestoreAppWidget(final int appWidgetId) {
4087 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4088 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4089 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4090 return;
4091 }
4092
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004093 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004094 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4095
4096 mWorkspace.reinflateWidgetsIfNecessary();
4097 LauncherModel.updateItemInDatabase(this, info);
4098 }
4099
Adam Cohen1462de32012-07-24 22:34:36 -07004100 public void onPageBoundSynchronously(int page) {
4101 mSynchronouslyBoundPages.add(page);
4102 }
4103
Joe Onorato9c1289c2009-08-17 11:03:03 -04004104 /**
4105 * Callback saying that there aren't any more items to bind.
4106 *
4107 * Implementation of the method from LauncherModel.Callbacks.
4108 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004109 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004110 Runnable r = new Runnable() {
4111 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004112 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004113 }
4114 };
4115 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004116 return;
4117 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004118 if (mSavedState != null) {
4119 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004120 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004121 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004122 mSavedState = null;
4123 }
4124
Adam Cohen1462de32012-07-24 22:34:36 -07004125 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004126
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004127 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004128 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004129
4130 // If we received the result of any pending adds while the loader was running (e.g. the
4131 // widget configuration forced an orientation change), process them now.
4132 if (sPendingAddItem != null) {
4133 final long screenId = completeAdd(sPendingAddItem);
4134
4135 // TODO: this moves the user to the page where the pending item was added. Ideally,
4136 // the screen would be guaranteed to exist after bind, and the page would be set through
4137 // the workspace restore process.
4138 mWorkspace.post(new Runnable() {
4139 @Override
4140 public void run() {
4141 mWorkspace.snapToScreenId(screenId);
4142 }
4143 });
4144 sPendingAddItem = null;
4145 }
4146
Sunny Goyal756adbc2015-04-16 15:20:51 -07004147 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004148
4149 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004150 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004151 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004152 }
4153
Adam Cohen3ed316a2014-07-23 18:21:20 -07004154 private void sendLoadingCompleteBroadcastIfNecessary() {
4155 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4156 String permission =
4157 getResources().getString(R.string.receive_first_load_broadcast_permission);
4158 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4159 sendBroadcast(intent, permission);
4160 SharedPreferences.Editor editor = mSharedPrefs.edit();
4161 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4162 editor.apply();
4163 }
4164 }
4165
Winson Chunga0b7e862013-09-05 16:03:15 -07004166 public boolean isAllAppsButtonRank(int rank) {
4167 if (mHotseat != null) {
4168 return mHotseat.isAllAppsButtonRank(rank);
4169 }
4170 return false;
4171 }
4172
Winson Chunga2413752012-04-03 14:22:34 -07004173 private boolean canRunNewAppsAnimation() {
4174 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4175 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4176 }
4177
Winson Chungc9168342013-06-26 14:54:55 -07004178 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4179 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4180 PropertyValuesHolder.ofFloat("alpha", 1f),
4181 PropertyValuesHolder.ofFloat("scaleX", 1f),
4182 PropertyValuesHolder.ofFloat("scaleY", 1f));
4183 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4184 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4185 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4186 return bounceAnim;
4187 }
4188
Adam Cohen27772732013-11-11 14:00:56 +00004189 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004190 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004191 }
4192
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004193 protected Rect getSearchBarBounds() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004194 return mDeviceProfile.getSearchBarBounds();
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004195 }
4196
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004197 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004198 if (mSearchDropTargetBar == null) {
4199 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004200 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004201 if (mQsb != null) {
4202 mSearchDropTargetBar.removeView(mQsb);
4203 mQsb = null;
4204 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004205 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004206 }
4207
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004208 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004209 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4210 * multiple calls to bind the same list.)
4211 */
4212 @Thunk ArrayList<AppInfo> mTmpAppsList;
4213 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4214 public void run() {
4215 bindAllApplications(mTmpAppsList);
4216 mTmpAppsList = null;
4217 }
4218 };
4219
4220 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004221 * Add the icons for all apps.
4222 *
4223 * Implementation of the method from LauncherModel.Callbacks.
4224 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004225 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004226 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4227 mTmpAppsList = apps;
4228 return;
4229 }
4230
Winson Chungb745afb2015-03-02 11:51:23 -08004231 if (mAppsView != null) {
4232 mAppsView.setApps(apps);
4233 }
Adam Cohen9211d422014-10-07 18:14:53 -07004234 if (mLauncherCallbacks != null) {
4235 mLauncherCallbacks.bindAllApplications(apps);
4236 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004237 }
4238
4239 /**
4240 * A package was updated.
4241 *
4242 * Implementation of the method from LauncherModel.Callbacks.
4243 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004244 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004245 Runnable r = new Runnable() {
4246 public void run() {
4247 bindAppsUpdated(apps);
4248 }
4249 };
4250 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004251 return;
4252 }
4253
Winson Chungb745afb2015-03-02 11:51:23 -08004254 if (mAppsView != null) {
4255 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004256 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004257 }
4258
Sunny Goyal4390ace2014-10-13 11:33:11 -07004259 @Override
4260 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4261 Runnable r = new Runnable() {
4262 public void run() {
4263 bindWidgetsRestored(widgets);
4264 }
4265 };
4266 if (waitUntilResume(r)) {
4267 return;
4268 }
4269 mWorkspace.widgetsRestored(widgets);
4270 }
4271
Joe Onorato9c1289c2009-08-17 11:03:03 -04004272 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004273 * Some shortcuts were updated in the background.
4274 *
4275 * Implementation of the method from LauncherModel.Callbacks.
4276 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004277 @Override
4278 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4279 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004280 Runnable r = new Runnable() {
4281 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004282 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004283 }
4284 };
4285 if (waitUntilResume(r)) {
4286 return;
4287 }
4288
Sunny Goyal4390ace2014-10-13 11:33:11 -07004289 if (!updated.isEmpty()) {
4290 mWorkspace.updateShortcuts(updated);
4291 }
4292
4293 if (!removed.isEmpty()) {
4294 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4295 for (ShortcutInfo si : removed) {
4296 removedComponents.add(si.getTargetComponent());
4297 }
4298 mWorkspace.removeItemsByComponentName(removedComponents, user);
4299 // Notify the drag controller
4300 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004301 }
4302 }
4303
4304 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004305 * Update the state of a package, typically related to install state.
4306 *
4307 * Implementation of the method from LauncherModel.Callbacks.
4308 */
Sunny Goyale755d462014-07-22 13:48:29 -07004309 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004310 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4311 Runnable r = new Runnable() {
4312 public void run() {
4313 bindRestoreItemsChange(updates);
4314 }
4315 };
4316 if (waitUntilResume(r)) {
4317 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004318 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004319
Sunny Goyal756adbc2015-04-16 15:20:51 -07004320 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004321 }
4322
4323 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004324 * A package was uninstalled. We take both the super set of packageNames
4325 * in addition to specific applications to remove, the reason being that
4326 * this can be called when a package is updated as well. In that scenario,
4327 * we only remove specific components from the workspace, where as
4328 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004329 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004330 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004331 * Implementation of the method from LauncherModel.Callbacks.
4332 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004333 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004334 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004335 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004336 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004337 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004338 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004339 }
Winson Chungd64d1762013-08-20 14:37:16 -07004340 };
4341 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004342 return;
4343 }
4344
Sunny Goyal1a745e82014-10-02 15:58:31 -07004345 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004346 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4347 for (AppInfo info : appInfos) {
4348 removedComponents.add(info.componentName);
4349 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004350 if (!packageNames.isEmpty()) {
4351 mWorkspace.removeItemsByPackageName(packageNames, user);
4352 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004353 if (!removedComponents.isEmpty()) {
4354 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004355 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004356 // Notify the drag controller
4357 mDragController.onAppsRemoved(packageNames, removedComponents);
4358
Sunny Goyal1a745e82014-10-02 15:58:31 -07004359 } else {
4360 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004361 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004362
Winson Chungdf95eb12013-10-16 14:57:07 -07004363 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004364 if (mAppsView != null) {
4365 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004366 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004367 }
Joe Onoratobe386092009-11-17 17:32:16 -08004368
Michael Jurkac402cd92013-05-20 15:49:32 +02004369 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004370 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004371 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004372 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004373 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004374
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004375 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004376 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004377 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004378 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004379 return;
4380 }
4381
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004382 if (mWidgetsView != null && model != null) {
4383 mWidgetsView.addWidgets(model);
4384 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004385 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004386 }
4387
Winson Chung400438b2011-01-16 17:53:48 -08004388 private int mapConfigurationOriActivityInfoOri(int configOri) {
4389 final Display d = getWindowManager().getDefaultDisplay();
4390 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4391 switch (d.getRotation()) {
4392 case Surface.ROTATION_0:
4393 case Surface.ROTATION_180:
4394 // We are currently in the same basic orientation as the natural orientation
4395 naturalOri = configOri;
4396 break;
4397 case Surface.ROTATION_90:
4398 case Surface.ROTATION_270:
4399 // We are currently in the other basic orientation to the natural orientation
4400 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4401 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4402 break;
4403 }
4404
4405 int[] oriMap = {
4406 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4407 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4408 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4409 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4410 };
4411 // Since the map starts at portrait, we need to offset if this device's natural orientation
4412 // is landscape.
4413 int indexOffset = 0;
4414 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4415 indexOffset = 1;
4416 }
4417 return oriMap[(d.getRotation() + indexOffset) % 4];
4418 }
Adam Cohen446e9402011-09-15 18:21:21 -07004419
Winson Chung4b919f82012-05-01 10:44:08 -07004420 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004421 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004422 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4423 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4424 .getConfiguration().orientation));
4425 } else {
4426 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4427 }
Winson Chung4b919f82012-05-01 10:44:08 -07004428 }
4429 }
4430 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004431 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004432 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004433 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004434 } else {
4435 mHandler.postDelayed(new Runnable() {
4436 public void run() {
4437 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4438 }
4439 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004440 }
Winson Chung4b919f82012-05-01 10:44:08 -07004441 }
Winson Chung400438b2011-01-16 17:53:48 -08004442 }
4443
Winson Chunge43a1e72014-01-15 10:33:02 -08004444 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004445 if (mLauncherCallbacks != null) {
4446 return mLauncherCallbacks.isLauncherPreinstalled();
4447 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004448 PackageManager pm = getPackageManager();
4449 try {
4450 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4451 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4452 return true;
4453 } else {
4454 return false;
4455 }
4456 } catch (NameNotFoundException e) {
4457 e.printStackTrace();
4458 return false;
4459 }
4460 }
4461
Adam Cohenea90f832014-05-21 15:03:34 -07004462 /**
4463 * This method indicates whether or not we should suggest default wallpaper dimensions
4464 * when our wallpaper cropper was not yet used to set a wallpaper.
4465 */
4466 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004467 if (mLauncherCallbacks != null) {
4468 return mLauncherCallbacks.overrideWallpaperDimensions();
4469 }
Adam Cohenea90f832014-05-21 15:03:34 -07004470 return true;
4471 }
4472
Adam Cohen432609a2014-03-13 17:03:22 -07004473 /**
4474 * To be overridden by subclasses to indicate that there is an activity to launch
4475 * before showing the standard launcher experience.
4476 */
4477 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004478 if (mLauncherCallbacks != null) {
4479 return mLauncherCallbacks.hasFirstRunActivity();
4480 }
Adam Cohen432609a2014-03-13 17:03:22 -07004481 return false;
4482 }
4483
4484 /**
4485 * To be overridden by subclasses to launch any first run activity
4486 */
4487 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004488 if (mLauncherCallbacks != null) {
4489 return mLauncherCallbacks.getFirstRunActivity();
4490 }
Adam Cohen432609a2014-03-13 17:03:22 -07004491 return null;
4492 }
4493
Winson Chung2826a402015-04-17 16:18:53 -07004494 /**
4495 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004496 */
4497 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004498 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4499 return false;
4500 }
4501
Winson Chung2826a402015-04-17 16:18:53 -07004502 if (mLauncherCallbacks != null) {
4503 return mLauncherCallbacks.overrideAllAppsSearch();
4504 }
4505 return false;
4506 }
4507
Winson Chunga6945242014-01-08 14:04:34 -08004508 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004509 return !ActivityManager.isRunningInTestHarness() &&
4510 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004511 }
4512
Adam Cohen4a9423d2014-03-28 14:22:31 -07004513 protected boolean hasRunFirstRunActivity() {
4514 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4515 }
4516
Adam Cohen432609a2014-03-13 17:03:22 -07004517 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004518 if (shouldRunFirstRunActivity() &&
4519 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004520 Intent firstRunIntent = getFirstRunActivity();
4521 if (firstRunIntent != null) {
4522 startActivity(firstRunIntent);
4523 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004524 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004525 }
4526 }
Adam Cohen432609a2014-03-13 17:03:22 -07004527 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004528 }
4529
4530 private void markFirstRunActivityShown() {
4531 SharedPreferences.Editor editor = mSharedPrefs.edit();
4532 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4533 editor.apply();
4534 }
4535
Adam Cohen432609a2014-03-13 17:03:22 -07004536 /**
4537 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4538 * screen that must be displayed and dismissed.
4539 */
4540 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004541 if (mLauncherCallbacks != null) {
4542 return mLauncherCallbacks.hasDismissableIntroScreen();
4543 }
Adam Cohen432609a2014-03-13 17:03:22 -07004544 return false;
4545 }
4546
4547 /**
4548 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4549 */
4550 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004551 if (mLauncherCallbacks != null) {
4552 return mLauncherCallbacks.getIntroScreen();
4553 }
Adam Cohen432609a2014-03-13 17:03:22 -07004554 return null;
4555 }
4556
4557 /**
4558 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4559 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4560 */
4561 private boolean shouldShowIntroScreen() {
4562 return hasDismissableIntroScreen() &&
4563 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4564 }
4565
4566 protected void showIntroScreen() {
4567 View introScreen = getIntroScreen();
4568 changeWallpaperVisiblity(false);
4569 if (introScreen != null) {
4570 mDragLayer.showOverlayView(introScreen);
4571 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004572 if (mLauncherOverlayContainer != null) {
4573 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4574 }
Adam Cohen432609a2014-03-13 17:03:22 -07004575 }
4576
4577 public void dismissIntroScreen() {
4578 markIntroScreenDismissed();
4579 if (showFirstRunActivity()) {
4580 // We delay hiding the intro view until the first run activity is showing. This
4581 // avoids a blip.
4582 mWorkspace.postDelayed(new Runnable() {
4583 @Override
4584 public void run() {
4585 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004586 if (mLauncherOverlayContainer != null) {
4587 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4588 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004589 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004590 }
4591 }, ACTIVITY_START_DELAY);
4592 } else {
4593 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004594 if (mLauncherOverlayContainer != null) {
4595 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4596 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004597 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004598 }
4599 changeWallpaperVisiblity(true);
4600 }
4601
4602 private void markIntroScreenDismissed() {
4603 SharedPreferences.Editor editor = mSharedPrefs.edit();
4604 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4605 editor.apply();
4606 }
4607
Adam Cohen091440a2015-03-18 14:16:05 -07004608 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004609 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4610 // on the device, then we always show the first run cling experience (or if there is no
4611 // launcher2). Otherwise, we prompt the user upon started for migration
4612 LauncherClings launcherClings = new LauncherClings(this);
4613 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4614 if (mModel.canMigrateFromOldLauncherDb(this)) {
4615 launcherClings.showMigrationCling();
4616 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004617 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004618 }
4619 }
4620 }
4621
Winson Chunga6945242014-01-08 14:04:34 -08004622 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004623 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4624 if (mHotseat != null) mHotseat.setAlpha(1f);
4625 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4626 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004627 }
4628
4629 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004630 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4631 if (mHotseat != null) mHotseat.setAlpha(0f);
4632 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4633 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004634 }
4635
Mathew Inwood72fbec12013-11-19 15:45:07 +00004636 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004637 // Called from search suggestion, not supported in other profiles.
4638 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4639 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4640 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4641 myUser);
4642 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004643 return null;
4644 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004645 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004646 }
4647
4648 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4649 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004650 // Called from search suggestion, not supported in other profiles.
4651 return createShortcutDragInfo(shortcutIntent, caption, icon,
4652 UserHandleCompat.myUserHandle());
4653 }
4654
4655 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4656 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004657 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004658 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004659 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004660 }
4661
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004662 protected void moveWorkspaceToDefaultScreen() {
4663 mWorkspace.moveToDefaultScreen(false);
4664 }
4665
Mathew Inwood72fbec12013-11-19 15:45:07 +00004666 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4667 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004668 mWorkspace.onExternalDragStartedWithItem(dragView);
4669 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004670 }
4671
Anjali Koppalf5d29132014-02-28 13:33:27 -08004672 @Override
4673 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004674 if (mLauncherCallbacks != null) {
4675 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4676 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004677 }
4678
Winson Chung80baf5a2010-08-09 16:03:15 -07004679 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004680 * Prints out out state for debugging.
4681 */
4682 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004683 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004684 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004685 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4686 Log.d(TAG, "mRestoring=" + mRestoring);
4687 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4688 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004689 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004690 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004691 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004692
Daniel Sandler325dc232013-06-05 22:57:57 -04004693 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004694 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004695
4696 @Override
4697 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4698 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004699 synchronized (sDumpLogs) {
4700 writer.println(" ");
4701 writer.println("Debug logs: ");
4702 for (int i = 0; i < sDumpLogs.size(); i++) {
4703 writer.println(" " + sDumpLogs.get(i));
4704 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004705 }
Adam Cohen9211d422014-10-07 18:14:53 -07004706 if (mLauncherCallbacks != null) {
4707 mLauncherCallbacks.dump(prefix, fd, writer, args);
4708 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004709 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004710
4711 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004712 if (DEBUG_DUMP_LOG) {
4713 synchronized (sDumpLogs) {
4714 Log.d(TAG, "");
4715 Log.d(TAG, "*********************");
4716 Log.d(TAG, "Launcher debug logs: ");
4717 for (int i = 0; i < sDumpLogs.size(); i++) {
4718 Log.d(TAG, " " + sDumpLogs.get(i));
4719 }
4720 Log.d(TAG, "*********************");
4721 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004722 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004723 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004724 }
4725
4726 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004727 addDumpLog(tag, log, null, debugLog);
4728 }
4729
4730 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004731 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004732 if (e != null) {
4733 Log.d(tag, log, e);
4734 } else {
4735 Log.d(tag, log);
4736 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004737 }
Winson Chungede41292013-09-19 16:27:36 -07004738 if (DEBUG_DUMP_LOG) {
4739 sDateStamp.setTime(System.currentTimeMillis());
4740 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004741 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4742 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004743 }
Winson Chungede41292013-09-19 16:27:36 -07004744 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004745 }
4746
Adam Cohen59400422014-03-05 18:07:04 -08004747 public static CustomAppWidget getCustomAppWidget(String name) {
4748 return sCustomAppWidgets.get(name);
4749 }
4750
4751 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4752 return sCustomAppWidgets;
4753 }
4754
Winson Chungede41292013-09-19 16:27:36 -07004755 public void dumpLogsToLocalData() {
4756 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004757 new AsyncTask<Void, Void, Void>() {
4758 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004759 boolean success = false;
4760 sDateStamp.setTime(sRunStart);
4761 String FILENAME = sDateStamp.getMonth() + "-"
4762 + sDateStamp.getDay() + "_"
4763 + sDateStamp.getHours() + "-"
4764 + sDateStamp.getMinutes() + "_"
4765 + sDateStamp.getSeconds() + ".txt";
4766
4767 FileOutputStream fos = null;
4768 File outFile = null;
4769 try {
4770 outFile = new File(getFilesDir(), FILENAME);
4771 outFile.createNewFile();
4772 fos = new FileOutputStream(outFile);
4773 } catch (Exception e) {
4774 e.printStackTrace();
4775 }
4776 if (fos != null) {
4777 PrintWriter writer = new PrintWriter(fos);
4778
4779 writer.println(" ");
4780 writer.println("Debug logs: ");
4781 synchronized (sDumpLogs) {
4782 for (int i = 0; i < sDumpLogs.size(); i++) {
4783 writer.println(" " + sDumpLogs.get(i));
4784 }
4785 }
4786 writer.close();
4787 }
4788 try {
4789 if (fos != null) {
4790 fos.close();
4791 success = true;
4792 }
4793 } catch (IOException e) {
4794 e.printStackTrace();
4795 }
Michael Jurka43467462013-11-14 12:03:58 +01004796 return null;
Winson Chungede41292013-09-19 16:27:36 -07004797 }
Michael Jurka43467462013-11-14 12:03:58 +01004798 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004799 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004800 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004801}
Michael Jurka2763be32011-02-24 11:19:57 -08004802
Michael Jurkaabded662011-03-04 12:06:57 -08004803interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004804 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004805 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004806 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004807 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004808 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004809}
Dan Sandlerd5024042014-01-09 15:01:33 -05004810
4811interface DebugIntents {
4812 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4813 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004814}