blob: a828b1a427bcca43b12fedff64bd4778f473dbda [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040059import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020061import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070085import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070087import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.widget.Advanceable;
92import android.widget.FrameLayout;
93import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080094import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070096
Sunny Goyal651077b2014-06-30 14:15:31 -070097import com.android.launcher3.DropTarget.DragObject;
98import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070099import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100100import com.android.launcher3.compat.LauncherActivityInfoCompat;
101import com.android.launcher3.compat.LauncherAppsCompat;
102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyale2df0622015-04-24 11:27:00 -0700104import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700105import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700106import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700107import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700108import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700109
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.io.DataInputStream;
111import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700113import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800118import java.lang.reflect.InvocationTargetException;
119import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700120import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700122import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700125import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700126import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000127import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129/**
130 * Default launcher application.
131 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100132public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700133 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800134 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
135 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700136 static final String TAG = "Launcher";
Hyunyoung Song3f471442015-04-08 19:01:34 -0700137 static final boolean LOGD = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Winson Chung83f59ab2015-05-05 17:21:58 -0700139 // Temporary flag
140 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
141
Daniel Sandlerf061f822013-06-27 13:59:36 -0400142 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700143 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700144 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400145 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700146 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700147
Dan Sandlerd5024042014-01-09 15:01:33 -0500148 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700153 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Michael Jurka8b805b12012-04-18 14:23:14 -0700155 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700156 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700157
Mathew Inwood876a8462013-06-14 14:12:41 +0100158 /**
159 * IntentStarter uses request codes starting with this. This must be greater than all activity
160 * request codes used internally.
161 */
162 protected static final int REQUEST_LAST = 100;
163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
165
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800166 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 // To turn on these properties, type
169 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800170 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Winson Chung2672ff92012-05-04 16:22:30 -0700172 // The Intent extra that defines whether to ignore the launch animation
173 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400174 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
177 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700178 // Type: int
179 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
182 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
184 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700185 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: boolean
189 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
190 // Type: long
191 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700192 // Type: int
193 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
194 // Type: int
195 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
196 // Type: parcelable
197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000198 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800199 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000200 // Type: int[]
201 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Adam Cohen432609a2014-03-13 17:03:22 -0700203 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800204 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
205
Adam Cohen3ed316a2014-07-23 18:21:20 -0700206 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
207 static final String ACTION_FIRST_LOAD_COMPLETE =
208 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
209
Adam Cohen39a06042013-07-19 14:30:12 -0700210 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700211 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700212
Sunny Goyal594d76d2014-11-06 10:12:54 -0800213 private static final String QSB_WIDGET_ID = "qsb_widget_id";
214 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
215
Winson Chunga6945242014-01-08 14:04:34 -0800216 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
217
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700218 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800219 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700221 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700222
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700223 private boolean mIsSafeModeEnabled;
224
Adam Cohenc2d6e892014-10-16 09:49:24 -0700225 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
226 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700227 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700230 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
231 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700232 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000234 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
235 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
236
Winson Chunga2413752012-04-03 14:22:34 -0700237 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700238 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
239 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700240 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700241
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800242 private final BroadcastReceiver mCloseSystemDialogsReceiver
243 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800244 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private LayoutInflater mInflater;
247
Adam Cohen091440a2015-03-18 14:16:05 -0700248 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700249 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800250 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700251 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800252 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700253 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700254
Sunny Goyalffe83f12014-08-14 17:39:34 -0700255 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700256 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700257
Adam Cohen091440a2015-03-18 14:16:05 -0700258 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800259 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800260 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700261
Michael Jurka0280c3b2010-09-17 15:00:07 -0700262 private int[] mTmpAddItemCellCoordinates = new int[2];
263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 private FolderInfo mFolderInfo;
265
Winson Chung3d503fb2011-07-13 17:25:49 -0700266 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800267 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700268
Michael Jurka838a4ca2011-02-07 13:33:06 -0800269 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700270
Winson Chungc51db6a2011-10-05 11:44:49 -0700271 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700272
273 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700274 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700275
276 // Main container view for the widget tray screen.
277 private WidgetsContainerView mWidgetsView;
278
Winson Chungf0ea4d32011-06-06 14:27:16 -0700279 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800280 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700283 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
284 // scroll issues (because the workspace may not have been measured yet) and extra work.
285 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
286 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287
288 private SpannableStringBuilder mDefaultKeySsb = null;
289
Adam Cohen091440a2015-03-18 14:16:05 -0700290 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800292 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 private boolean mRestoring;
294 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700295 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296
Michael Jurka1e2f4652013-07-08 18:03:46 -0700297 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700298 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 private Bundle mSavedInstanceState;
301
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800303 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700304 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800305 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700306 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800307 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400308
Adam Cohen091440a2015-03-18 14:16:05 -0700309 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700310
Sunny Goyale2df0622015-04-24 11:27:00 -0700311 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700312
Adam Cohen61f560d2013-09-30 15:58:20 -0700313 private View.OnTouchListener mHapticFeedbackTouchListener;
314
Adam Cohended9f8d2010-11-03 13:25:16 -0700315 // Related to the auto-advancing of widgets
316 private final int ADVANCE_MSG = 1;
317 private final int mAdvanceInterval = 20000;
318 private final int mAdvanceStagger = 250;
319 private long mAutoAdvanceSentTime;
320 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700321 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700322 new HashMap<View, AppWidgetProviderInfo>();
323
Winson Chung400438b2011-01-16 17:53:48 -0800324 // Determines how long to wait after a rotation before restoring the screen orientation to
325 // match the sensor state.
326 private final int mRestoreScreenOrientationDelay = 500;
327
Adam Cohen091440a2015-03-18 14:16:05 -0700328 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700329
Adam Cohen1462de32012-07-24 22:34:36 -0700330 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700331 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700332
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700333 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700334 static Date sDateStamp = new Date();
335 static DateFormat sDateFormat =
336 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
337 static long sRunStart = System.currentTimeMillis();
338 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700339
Winson Chung46353de2012-02-16 14:05:10 -0800340 // We only want to get the SharedPreferences once since it does an FS stat each time we get
341 // it from the context.
342 private SharedPreferences mSharedPrefs;
343
Winson Chungf0c6ae02012-03-21 16:10:31 -0700344 // Holds the page that we need to animate to, and the icon views that we need to animate up
345 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700346 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700347 private Bitmap mFolderIconBitmap;
348 private Canvas mFolderIconCanvas;
349 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700350
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700351 private DeviceProfile mDeviceProfile;
352
Winson Chung13eb5272015-05-11 16:30:13 -0700353 // This is set to the view that launched the activity that navigated the user away from
354 // launcher. Since there is no callback for when the activity has finished launching, enable
355 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800356 private BubbleTextView mWaitingForResume;
357
Adam Cohen59400422014-03-05 18:07:04 -0800358 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
359 new HashMap<String, CustomAppWidget>();
360
361 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
362 static {
363 if (ENABLE_CUSTOM_WIDGET_TEST) {
364 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
365 }
366 }
367
Chet Haasea8f996d2015-02-17 12:56:04 -0800368 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
369 private static Method sClipRevealMethod = null;
370 static {
371 Class<?> activityOptionsClass = ActivityOptions.class;
372 try {
373 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
374 View.class, int.class, int.class, int.class, int.class);
375 } catch (Exception e) {
376 // Earlier version
377 }
378 }
379
Adam Cohen091440a2015-03-18 14:16:05 -0700380 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700381 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700382 if (mWorkspace != null) {
383 mWorkspace.buildPageHardwareLayers();
384 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700385 }
386 };
387
Adam Cohendb364c32014-05-20 14:23:40 -0700388 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800389
Adam Cohen091440a2015-03-18 14:16:05 -0700390 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800391 int requestCode;
392 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700393 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700394 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800395 int cellX;
396 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700397 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800398 }
399
Daniel Sandlerff02d492013-08-05 02:12:05 -0400400 private Stats mStats;
401
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700402 FocusIndicatorView mFocusHandler;
403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 @Override
405 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700406 if (DEBUG_STRICT_MODE) {
407 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
408 .detectDiskReads()
409 .detectDiskWrites()
410 .detectNetwork() // or .detectAll() for all detectable problems
411 .penaltyLog()
412 .build());
413 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
414 .detectLeakedSqlLiteObjects()
415 .detectLeakedClosableObjects()
416 .penaltyLog()
417 .penaltyDeath()
418 .build());
419 }
420
Adam Cohen9211d422014-10-07 18:14:53 -0700421 if (mLauncherCallbacks != null) {
422 mLauncherCallbacks.preOnCreate();
423 }
424
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400426
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400427 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400428 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700429 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700430
Winson Chung5f8afe62013-08-12 16:19:28 -0700431 // Lazy-initialize the dynamic grid
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700432 mDeviceProfile = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700433
434 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400435 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700436 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700437 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800438 mModel = app.setLauncher(this);
439 mIconCache = app.getIconCache();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700440 mIconCache.flushInvalidIcons(mDeviceProfile);
Joe Onorato41a12d22009-10-31 18:30:00 -0400441 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800443 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700444
Daniel Sandlerff02d492013-08-05 02:12:05 -0400445 mStats = new Stats(this);
446
Sunny Goyalffe83f12014-08-14 17:39:34 -0700447 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700448
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700449 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
450 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700451
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700452 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
453 // this also ensures that any synchronous binding below doesn't re-trigger another
454 // LauncherModel load.
455 mPaused = false;
456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200458 android.os.Debug.startMethodTracing(
459 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 }
461
462 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700464
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700466 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800468 registerContentObservers();
469
Joe Onorato7c312c12009-08-13 21:36:53 -0700470 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700471
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 mSavedState = savedInstanceState;
473 restoreState(mSavedState);
474
475 if (PROFILE_STARTUP) {
476 android.os.Debug.stopMethodTracing();
477 }
478
479 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700480 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700481 // If the user leaves launcher, then we should just load items asynchronously when
482 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500483 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700484 } else {
485 // We only load the page synchronously if the user rotates (or triggers a
486 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800487 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700488 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 }
490
491 // For handling default keys
492 mDefaultKeySsb = new SpannableStringBuilder();
493 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800494
495 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
496 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800497
Adam Cohenf9426d52012-06-04 17:26:21 -0700498 // On large interfaces, we want the screen to auto-rotate based on the current orientation
499 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700500
Adam Cohen9211d422014-10-07 18:14:53 -0700501 if (mLauncherCallbacks != null) {
502 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700503 if (mLauncherCallbacks.hasLauncherOverlay()) {
504 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700505 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
506 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
507 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700508 mWorkspace.setLauncherOverlay(mLauncherOverlay);
509 }
Adam Cohen9211d422014-10-07 18:14:53 -0700510 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700511
512 if (shouldShowIntroScreen()) {
513 showIntroScreen();
514 } else {
515 showFirstRunActivity();
516 showFirstRunClings();
517 }
Adam Cohen9211d422014-10-07 18:14:53 -0700518 }
519
520 private LauncherCallbacks mLauncherCallbacks;
521
522 public void onPostCreate(Bundle savedInstanceState) {
523 super.onPostCreate(savedInstanceState);
524 if (mLauncherCallbacks != null) {
525 mLauncherCallbacks.onPostCreate(savedInstanceState);
526 }
527 }
528
529 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
530 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700531 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
532 @Override
533 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700534 if (LOGD) {
535 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
536 }
Winson Chung94804152015-05-08 13:06:44 -0700537 mAppsView.setFixedBounds(bounds);
538 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700539 }
540
541 @Override
542 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700543 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
544 // Dismiss All Apps if we aren't already paused/invisible
545 if (!mPaused) {
546 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
547 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
548 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700549 }
Winson Chung0f785722015-04-08 10:27:49 -0700550 }
551 });
Adam Cohen9211d422014-10-07 18:14:53 -0700552 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700553 }
554
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700555 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700556 public void onLauncherProviderChange() {
557 if (mLauncherCallbacks != null) {
558 mLauncherCallbacks.onLauncherProviderChange();
559 }
560 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700561
Adam Cohen9211d422014-10-07 18:14:53 -0700562 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700563 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700564 if (mLauncherCallbacks != null) {
565 return mLauncherCallbacks.hasCustomContentToLeft();
566 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700567 return false;
568 }
569
Dave Hawkeya8881582013-09-17 15:55:33 -0600570 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500571 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600572 * {@link #addToCustomContentPage}. This will only be invoked if
573 * {@link #hasCustomContentToLeft()} is {@code true}.
574 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500575 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700576 if (mLauncherCallbacks != null) {
577 mLauncherCallbacks.populateCustomContentContainer();
578 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600579 }
580
581 /**
582 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
583 * ensure the custom content page is added or removed if necessary.
584 */
585 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600586 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600587 // Not bound yet, wait for bindScreens to be called.
588 return;
589 }
590
591 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
592 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500593 mWorkspace.createCustomContentContainer();
594 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600595 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
596 mWorkspace.removeCustomContentPage();
597 }
598 }
599
Adam Cohen091440a2015-03-18 14:16:05 -0700600 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700601 if (sLocaleConfiguration == null) {
602 new AsyncTask<Void, Void, LocaleConfiguration>() {
603 @Override
604 protected LocaleConfiguration doInBackground(Void... unused) {
605 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
606 readConfiguration(Launcher.this, localeConfiguration);
607 return localeConfiguration;
608 }
609
610 @Override
611 protected void onPostExecute(LocaleConfiguration result) {
612 sLocaleConfiguration = result;
613 checkForLocaleChange(); // recursive, but now with a locale configuration
614 }
615 }.execute();
616 return;
617 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700618
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 final Configuration configuration = getResources().getConfiguration();
620
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700621 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 final String locale = configuration.locale.toString();
623
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700624 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 final int mcc = configuration.mcc;
626
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700627 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 final int mnc = configuration.mnc;
629
Romain Guy84f296c2009-11-04 15:00:44 -0800630 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631
Romain Guy84f296c2009-11-04 15:00:44 -0800632 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700633 sLocaleConfiguration.locale = locale;
634 sLocaleConfiguration.mcc = mcc;
635 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700636
Joe Onorato0589f0f2010-02-08 13:44:00 -0800637 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700638
639 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100640 new AsyncTask<Void, Void, Void>() {
641 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700642 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100643 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700644 }
Michael Jurka43467462013-11-14 12:03:58 +0100645 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700646 }
647 }
648
Adam Cohen091440a2015-03-18 14:16:05 -0700649 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700650 public String locale;
651 public int mcc = -1;
652 public int mnc = -1;
653 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700654
Adam Cohen091440a2015-03-18 14:16:05 -0700655 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700656 DataInputStream in = null;
657 try {
Helena Josol28db2802014-10-09 17:04:09 +0100658 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700659 configuration.locale = in.readUTF();
660 configuration.mcc = in.readInt();
661 configuration.mnc = in.readInt();
662 } catch (FileNotFoundException e) {
663 // Ignore
664 } catch (IOException e) {
665 // Ignore
666 } finally {
667 if (in != null) {
668 try {
669 in.close();
670 } catch (IOException e) {
671 // Ignore
672 }
673 }
674 }
675 }
676
Adam Cohen091440a2015-03-18 14:16:05 -0700677 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700678 DataOutputStream out = null;
679 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100680 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100681 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700682 out.writeUTF(configuration.locale);
683 out.writeInt(configuration.mcc);
684 out.writeInt(configuration.mnc);
685 out.flush();
686 } catch (FileNotFoundException e) {
687 // Ignore
688 } catch (IOException e) {
689 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100690 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700691 } finally {
692 if (out != null) {
693 try {
694 out.close();
695 } catch (IOException e) {
696 // Ignore
697 }
698 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 }
700 }
701
Anton Hanssona2f665f2013-09-26 12:18:32 +0100702 public Stats getStats() {
703 return mStats;
704 }
705
Bjorn Bringertc459e522013-06-07 19:36:01 +0100706 public LayoutInflater getInflater() {
707 return mInflater;
708 }
709
Hyunyoung Song3f471442015-04-08 19:01:34 -0700710 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700711 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
712 // that is subsequently removed from the workspace in startBinding().
713 return !mModel.isLoadingWorkspace();
714 }
715
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800716 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000717 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100718 if (Build.VERSION.SDK_INT >= 17) {
719 return View.generateViewId();
720 } else {
721 // View.generateViewId() is not available. The following fallback logic is a copy
722 // of its implementation.
723 for (;;) {
724 final int result = sNextGeneratedId.get();
725 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
726 int newValue = result + 1;
727 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
728 if (sNextGeneratedId.compareAndSet(result, newValue)) {
729 return result;
730 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000731 }
732 }
733 }
734
735 public int getViewIdForItem(ItemInfo info) {
736 // This cast is safe given the > 2B range for int.
737 int itemId = (int) info.id;
738 if (mItemIdToViewId.containsKey(itemId)) {
739 return mItemIdToViewId.get(itemId);
740 }
741 int viewId = generateViewId();
742 mItemIdToViewId.put(itemId, viewId);
743 return viewId;
744 }
745
746 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700747 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
748 * a configuration step, this allows the proper animations to run after other transitions.
749 */
Adam Cohendb364c32014-05-20 14:23:40 -0700750 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700751 long screenId = args.screenId;
752 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
753 // When the screen id represents an actual screen (as opposed to a rank) we make sure
754 // that the drop page actually exists.
755 screenId = ensurePendingDropLayoutExists(args.screenId);
756 }
Adam Cohendb364c32014-05-20 14:23:40 -0700757
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800758 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800759 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700760 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700761 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700762 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800763 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700764 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700765 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700766 case REQUEST_RECONFIGURE_APPWIDGET:
767 completeRestoreAppWidget(args.appWidgetId);
768 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800769 }
Michael Jurka27614d22012-04-02 06:40:22 -0700770 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
771 // if you turned the screen off and then back while in All Apps, Launcher would not
772 // return to the workspace. Clearing mAddInfo.container here fixes this issue
773 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700774 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800775 }
776
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800777 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700778 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700779 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700780 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700781 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800782 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700783
Adam Cohenad4e15c2013-10-17 16:21:35 -0700784 Runnable exitSpringLoaded = new Runnable() {
785 @Override
786 public void run() {
787 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
788 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
789 }
790 };
791
Michael Jurka8b805b12012-04-18 14:23:14 -0700792 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700793 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700794 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
795 if (resultCode == RESULT_CANCELED) {
796 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700797 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700798 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700799 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800800 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700801 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700802 }
803 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200804 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
805 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700806 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200807 }
808 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700809 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200810
Adam Cohened66b2b2012-01-23 17:28:51 -0800811 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Hyunyoung Songb7900832015-05-15 16:29:45 -0700812 requestCode == REQUEST_CREATE_APPWIDGET || requestCode == REQUEST_CREATE_SHORTCUT);
Romain Guyaad5ef42009-06-10 02:48:37 -0700813
Adam Cohendb364c32014-05-20 14:23:40 -0700814 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800815 // We have special handling for widgets
816 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800817 final int appWidgetId;
818 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
819 : -1;
820 if (widgetId < 0) {
821 appWidgetId = pendingAddWidgetId;
822 } else {
823 appWidgetId = widgetId;
824 }
825
Adam Cohenad4e15c2013-10-17 16:21:35 -0700826 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800827 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700828 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
829 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700830 result = RESULT_CANCELED;
831 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700832 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700833 @Override
834 public void run() {
835 exitSpringLoadedDragModeDelayed(false, 0, null);
836 }
837 };
Adam Cohendb364c32014-05-20 14:23:40 -0700838 if (workspaceLocked) {
839 // No need to remove the empty screen if we're mid-binding, as the
840 // the bind will not add the empty screen.
841 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
842 } else {
843 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
844 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
845 }
Winson Chung5aaab772012-04-27 15:24:02 -0700846 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700847 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700848 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
849 // When the screen id represents an actual screen (as opposed to a rank)
850 // we make sure that the drop page actually exists.
851 mPendingAddInfo.screenId =
852 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
853 }
Adam Cohendb364c32014-05-20 14:23:40 -0700854 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
855
856 dropLayout.setDropPending(true);
857 final Runnable onComplete = new Runnable() {
858 @Override
859 public void run() {
860 completeTwoStageWidgetDrop(resultCode, appWidgetId);
861 dropLayout.setDropPending(false);
862 }
863 };
864 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
865 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
866 } else {
867 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
868 mPendingAddInfo);
869 sPendingAddItem = args;
870 }
Winson Chung5aaab772012-04-27 15:24:02 -0700871 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800872 return;
873 }
874
Sunny Goyalff572272014-07-23 13:58:07 -0700875 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
876 if (resultCode == RESULT_OK) {
877 // Update the widget view.
878 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
879 pendingAddWidgetId, mPendingAddInfo);
880 if (workspaceLocked) {
881 sPendingAddItem = args;
882 } else {
883 completeAdd(args);
884 }
885 }
886 // Leave the widget in the pending state if the user canceled the configure.
887 return;
888 }
889
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 // The pattern used here is that a user PICKs a specific application,
891 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
894 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700895 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700896 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
897 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800898 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700899 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800900 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700901 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700902 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
903 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
Adam Cohen00074722013-10-11 17:46:09 -0700905 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700906 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700907 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800909 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800910
911 }
912
913 @Override
914 protected void onActivityResult(
915 final int requestCode, final int resultCode, final Intent data) {
916 handleActivityResult(requestCode, resultCode, data);
917 if (mLauncherCallbacks != null) {
918 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
919 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800920 }
921
Adam Cohendb364c32014-05-20 14:23:40 -0700922 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
923 appWidgetId, ItemInfo info) {
924 PendingAddArguments args = new PendingAddArguments();
925 args.requestCode = requestCode;
926 args.intent = data;
927 args.container = info.container;
928 args.screenId = info.screenId;
929 args.cellX = info.cellX;
930 args.cellY = info.cellY;
931 args.appWidgetId = appWidgetId;
932 return args;
933 }
934
935 /**
936 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
937 *
938 * @param screenId the screen id to check
939 * @return the new screen, or screenId if it exists
940 */
941 private long ensurePendingDropLayoutExists(long screenId) {
942 CellLayout dropLayout =
943 (CellLayout) mWorkspace.getScreenWithId(screenId);
944 if (dropLayout == null) {
945 // it's possible that the add screen was removed because it was
946 // empty and a re-bind occurred
947 mWorkspace.addExtraEmptyScreen();
948 return mWorkspace.commitExtraEmptyScreen();
949 } else {
950 return screenId;
951 }
952 }
953
Adam Cohen091440a2015-03-18 14:16:05 -0700954 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700955 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700956 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800957 Runnable onCompleteRunnable = null;
958 int animationType = 0;
959
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700960 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800961 if (resultCode == RESULT_OK) {
962 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
963 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700964 mPendingAddWidgetInfo);
965 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800966 onCompleteRunnable = new Runnable() {
967 @Override
968 public void run() {
969 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700970 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700971 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
972 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800973 }
974 };
975 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800976 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800977 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800978 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700979 if (mDragLayer.getAnimatedView() != null) {
980 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
981 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
982 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700983 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700984 // The animated view may be null in the case of a rotation during widget configuration
985 onCompleteRunnable.run();
986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 }
988
989 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700990 protected void onStop() {
991 super.onStop();
992 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700993
994 if (mLauncherCallbacks != null) {
995 mLauncherCallbacks.onStop();
996 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700997 }
998
999 @Override
1000 protected void onStart() {
1001 super.onStart();
1002 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -07001003
1004 if (mLauncherCallbacks != null) {
1005 mLauncherCallbacks.onStart();
1006 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001007 }
1008
1009 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001011 long startTime = 0;
1012 if (DEBUG_RESUME_TIME) {
1013 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001014 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001015 }
Adam Cohen9211d422014-10-07 18:14:53 -07001016
1017 if (mLauncherCallbacks != null) {
1018 mLauncherCallbacks.preOnResume();
1019 }
1020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001022
Winson Chung4a2afa32012-07-19 14:53:05 -07001023 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001024 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001025 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001026 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001027 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001028 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001029 // view after launching an app, as they may be depending on the UI to be static to
1030 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001031 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -07001032 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001033 } else if (mOnResumeState == State.WIDGETS) {
1034 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001035 }
1036 mOnResumeState = State.NONE;
1037
Winson Chungcd99cd32015-04-29 11:03:24 -07001038 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001039 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1040 // Otherwise, notify the callbacks if we are in all apps mode
1041 if (mState == State.APPS) {
1042 if (mLauncherCallbacks != null) {
1043 mLauncherCallbacks.onAllAppsShown();
1044 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001045 }
1046 }
1047
Craig Mautner360310b2012-10-26 15:13:08 -07001048 // Background was set to gradient in onPause(), restore to black if in all apps.
1049 setWorkspaceBackground(mState == State.WORKSPACE);
1050
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001051 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001052 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001053 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001054 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001055 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001056 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001058 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001059 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1060 // execute them here
1061 long startTimeCallbacks = 0;
1062 if (DEBUG_RESUME_TIME) {
1063 startTimeCallbacks = System.currentTimeMillis();
1064 }
1065
Michael Jurka1e2f4652013-07-08 18:03:46 -07001066 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1067 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001068 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001069 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001070 if (DEBUG_RESUME_TIME) {
1071 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1072 (System.currentTimeMillis() - startTimeCallbacks));
1073 }
Michael Jurka447bf842013-05-15 14:52:15 +02001074 }
Michael Jurka54554252013-08-01 12:52:23 +02001075 if (mOnResumeCallbacks.size() > 0) {
1076 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1077 mOnResumeCallbacks.get(i).run();
1078 }
1079 mOnResumeCallbacks.clear();
1080 }
Winson Chunge4e50662012-01-23 14:45:13 -08001081
1082 // Reset the pressed state of icons that were locked in the press state while activities
1083 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001084 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001085 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001086 mWaitingForResume.setStayPressed(false);
1087 }
Winson Chung82963d52013-10-09 11:20:57 -07001088
Adam Cohen06dff352012-06-01 17:17:08 -07001089 // It is possible that widgets can receive updates while launcher is not in the foreground.
1090 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1091 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1092 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001093 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001094 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001095
Michael Jurka447bf842013-05-15 14:52:15 +02001096 if (DEBUG_RESUME_TIME) {
1097 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1098 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001099
1100 if (mWorkspace.getCustomContentCallbacks() != null) {
1101 // If we are resuming and the custom content is the current page, we call onShow().
1102 // It is also poassible that onShow will instead be called slightly after first layout
1103 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1104 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001105 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001106 }
1107 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001108 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001109 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001110
Sunny Goyal756adbc2015-04-16 15:20:51 -07001111 if (!isWorkspaceLoading()) {
1112 // Process any items that were added while Launcher was away.
1113 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1114 }
Adam Cohen9211d422014-10-07 18:14:53 -07001115
1116 if (mLauncherCallbacks != null) {
1117 mLauncherCallbacks.onResume();
1118 }
Adam Cohen06dff352012-06-01 17:17:08 -07001119 }
1120
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001122 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001123 // Ensure that items added to Launcher are queued until Launcher returns
1124 InstallShortcutReceiver.enableInstallQueue();
1125
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001126 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001127 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001128 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001129 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001130
1131 // We call onHide() aggressively. The custom content callbacks should be able to
1132 // debounce excess onHide calls.
1133 if (mWorkspace.getCustomContentCallbacks() != null) {
1134 mWorkspace.getCustomContentCallbacks().onHide();
1135 }
Romain Guycbb89e42009-06-08 15:52:54 -07001136
Adam Cohen9211d422014-10-07 18:14:53 -07001137 if (mLauncherCallbacks != null) {
1138 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001139 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001140 }
1141
1142 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001143 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1144 // by a onResume or by scrolling otherwise.
1145 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001146
1147 // Custom content is completely hidden
1148 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001149
1150 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1151 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001152
1153 // Indicates whether the user is allowed to scroll away from the custom content.
1154 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001155 }
1156
Adam Cohenc2d6e892014-10-16 09:49:24 -07001157 public interface LauncherOverlay {
1158
1159 /**
1160 * Touch interaction leading to overscroll has begun
1161 */
1162 public void onScrollInteractionBegin();
1163
1164 /**
1165 * Touch interaction related to overscroll has ended
1166 */
1167 public void onScrollInteractionEnd();
1168
1169 /**
1170 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1171 * screen (or in the case of RTL, the rightmost screen).
1172 */
1173 public void onScrollChange(int progress, boolean rtl);
1174
1175 /**
1176 * Screen has stopped scrolling
1177 */
1178 public void onScrollSettled();
1179
1180 /**
1181 * This method can be called by the Launcher in order to force the LauncherOverlay
1182 * to exit fully immersive mode.
1183 */
1184 public void forceExitFullImmersion();
1185 }
1186
Winson Chung0f785722015-04-08 10:27:49 -07001187 public interface LauncherAppsCallbacks {
1188 /**
1189 * Updates launcher to the available space that AllApps can take so as not to overlap with
1190 * any other views.
1191 */
1192 public void onAllAppsBoundsChanged(Rect bounds);
1193
1194 /**
1195 * Called to dismiss all apps if it is showing.
1196 */
1197 public void dismissAllApps();
1198 }
1199
Adam Cohenc2d6e892014-10-16 09:49:24 -07001200 public interface LauncherOverlayCallbacks {
1201 /**
1202 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1203 * however it doesn't modify any state within the launcher.
1204 */
1205 public boolean canEnterFullImmersion();
1206
1207 /**
1208 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1209 * eg. by occupying the full screen and handling all touch events.
1210 *
1211 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1212 * case, Launcher will modify any necessary state and assumes the overlay is
1213 * handling all interaction. If false, the LauncherOverlay should cancel any
1214 *
1215 */
1216 public boolean enterFullImmersion();
1217
1218 /**
1219 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1220 * full control over UI and state.
1221 */
1222 public void exitFullImmersion();
1223 }
1224
1225 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1226
1227 @Override
1228 public boolean canEnterFullImmersion() {
1229 return mState == State.WORKSPACE;
1230 }
1231
1232 @Override
1233 public boolean enterFullImmersion() {
1234 if (mState == State.WORKSPACE) {
1235 // When fully immersed, disregard any touches which fall through.
1236 mDragLayer.setBlockTouch(true);
1237 return true;
1238 }
1239 return false;
1240 }
1241
1242 @Override
1243 public void exitFullImmersion() {
1244 mDragLayer.setBlockTouch(false);
1245 }
1246 }
1247
Jorim Jaggid017f882014-01-14 17:08:48 -08001248 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001249 if (mLauncherCallbacks != null) {
1250 return mLauncherCallbacks.hasSettings();
1251 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001252 return false;
1253 }
1254
Adam Cohen9211d422014-10-07 18:14:53 -07001255 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001256 CustomContentCallbacks callbacks, String description) {
1257 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001258 }
1259
Adam Cohenedb40762013-07-18 16:45:45 -07001260 // The custom content needs to offset its content to account for the QSB
1261 public int getTopOffsetForCustomContent() {
1262 return mWorkspace.getPaddingTop();
1263 }
1264
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001265 @Override
1266 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001267 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001268 if (mModel.isCurrentCallbacks(this)) {
1269 mModel.stopLoader();
1270 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001271 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1272
Romain Guy13c2e7b2010-03-10 19:45:00 -08001273 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001274 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001275
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001276 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001277 @Override
1278 public void onWindowFocusChanged(boolean hasFocus) {
1279 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001280 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001281
1282 if (mLauncherCallbacks != null) {
1283 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1284 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001285 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 private boolean acceptFilter() {
1288 final InputMethodManager inputManager = (InputMethodManager)
1289 getSystemService(Context.INPUT_METHOD_SERVICE);
1290 return !inputManager.isFullscreenMode();
1291 }
1292
1293 @Override
1294 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001295 final int uniChar = event.getUnicodeChar();
1296 final boolean handled = super.onKeyDown(keyCode, event);
1297 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1298 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1300 keyCode, event);
1301 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001302 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001303 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001304 // showSearchDialog()
1305 // If there are multiple keystrokes before the search dialog takes focus,
1306 // onSearchRequested() will be called for every keystroke,
1307 // but it is idempotent, so it's fine.
1308 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 }
1310 }
1311
Joe Onorato8a9625e2010-01-28 15:55:35 -08001312 // Eat the long press event so the keyboard doesn't come up.
1313 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1314 return true;
1315 }
1316
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 return handled;
1318 }
1319
Karl Rosaen138a0412009-04-23 19:00:21 -07001320 private String getTypedText() {
1321 return mDefaultKeySsb.toString();
1322 }
1323
1324 private void clearTypedText() {
1325 mDefaultKeySsb.clear();
1326 mDefaultKeySsb.clearSpans();
1327 Selection.setSelection(mDefaultKeySsb, 0);
1328 }
1329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001331 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1332 * State
1333 */
1334 private static State intToState(int stateOrdinal) {
1335 State state = State.WORKSPACE;
1336 final State[] stateValues = State.values();
1337 for (int i = 0; i < stateValues.length; i++) {
1338 if (stateValues[i].ordinal() == stateOrdinal) {
1339 state = stateValues[i];
1340 break;
1341 }
1342 }
1343 return state;
1344 }
1345
1346 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 * Restores the previous state, if it exists.
1348 *
1349 * @param savedState The previous state.
1350 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001351 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 private void restoreState(Bundle savedState) {
1353 if (savedState == null) {
1354 return;
1355 }
1356
Michael Jurka883f55b2010-10-21 15:47:14 -07001357 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001358 if (state == State.APPS || state == State.WIDGETS) {
1359 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001360 }
1361
Adam Cohen21cd0022013-10-09 18:57:02 -07001362 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1363 PagedView.INVALID_RESTORE_PAGE);
1364 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001365 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 }
1367
Winson Chung3d503fb2011-07-13 17:25:49 -07001368 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001369 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001370
Winson Chung3d503fb2011-07-13 17:25:49 -07001371 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1372 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001373 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001374 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1375 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001376 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1377 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001378 AppWidgetProviderInfo info = savedState.getParcelable(
1379 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001380 mPendingAddWidgetInfo = info == null ?
1381 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1382
Adam Cohen4637b5a2013-11-04 18:21:24 -08001383 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001384 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 mRestoring = true;
1386 }
1387
1388 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1389 if (renameFolder) {
1390 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001391 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 mRestoring = true;
1393 }
Winson Chunga12a2502010-12-20 14:41:35 -08001394
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001395 mItemIdToViewId = (HashMap<Integer, Integer>)
1396 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 }
1398
1399 /**
1400 * Finds all the views we need and configure them properly.
1401 */
1402 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001403 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001404
Craig Mautner360310b2012-10-26 15:13:08 -07001405 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001406 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001407 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1408 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001409 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001410 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001411
John Spurlock77e1f472013-09-11 10:09:51 -04001412 mLauncherView.setSystemUiVisibility(
1413 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001414 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415
Winson Chung4c98d922011-05-31 16:50:48 -07001416 // Setup the drag layer
1417 mDragLayer.setup(this, dragController);
1418
Winson Chung3d503fb2011-07-13 17:25:49 -07001419 // Setup the hotseat
1420 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1421 if (mHotseat != null) {
1422 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001423 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001424 }
1425
Jorim Jaggid017f882014-01-14 17:08:48 -08001426 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001427 View widgetButton = findViewById(R.id.widget_button);
1428 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001429 @Override
1430 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001431 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001432 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001433 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001434 }
1435 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001436 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1437
1438 View wallpaperButton = findViewById(R.id.wallpaper_button);
1439 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001440 @Override
1441 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001442 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001443 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001444 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001445 }
1446 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001447 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1448
1449 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001450 if (hasSettings()) {
1451 settingsButton.setOnClickListener(new OnClickListener() {
1452 @Override
1453 public void onClick(View arg0) {
1454 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001455 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001456 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001457 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001458 });
1459 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1460 } else {
1461 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001462 }
1463
Adam Cohendbdff6b2013-09-18 19:09:15 -07001464 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001465
Winson Chung4c98d922011-05-31 16:50:48 -07001466 // Setup the workspace
1467 mWorkspace.setHapticFeedbackEnabled(false);
1468 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001469 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001470 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001471
Winson Chungf0ea4d32011-06-06 14:27:16 -07001472 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001473 mSearchDropTargetBar = (SearchDropTargetBar)
1474 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001475
Winson Chungb745afb2015-03-02 11:51:23 -08001476 // Setup Apps
1477 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001478 if (isAllAppsSearchOverridden()) {
1479 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001480 }
Winson Chungb745afb2015-03-02 11:51:23 -08001481
Winson Chungf0ea4d32011-06-06 14:27:16 -07001482 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001483 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001484
Winson Chung3d503fb2011-07-13 17:25:49 -07001485 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001486 dragController.setDragScoller(mWorkspace);
1487 dragController.setScrollView(mDragLayer);
1488 dragController.setMoveTarget(mWorkspace);
1489 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001490 if (mSearchDropTargetBar != null) {
1491 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001492 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001493 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001494
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001495 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001496 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001497 mWeightWatcher = new WeightWatcher(this);
1498 mWeightWatcher.setAlpha(0.5f);
1499 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001500 new FrameLayout.LayoutParams(
1501 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001502 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001503 Gravity.BOTTOM)
1504 );
Adam Cohen39a06042013-07-19 14:30:12 -07001505
1506 boolean show = shouldShowWeightWatcher();
1507 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001508 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 }
1510
1511 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001512 * Sets the all apps button. This method is called from {@link Hotseat}.
1513 */
1514 public void setAllAppsButton(View allAppsButton) {
1515 mAllAppsButton = allAppsButton;
1516 }
1517
1518 public View getAllAppsButton() {
1519 return mAllAppsButton;
1520 }
1521
1522 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 * Creates a view representing a shortcut.
1524 *
1525 * @param info The data structure describing the shortcut.
1526 *
1527 * @return A View inflated from R.layout.application.
1528 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001529 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001530 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 }
1532
1533 /**
1534 * Creates a view representing a shortcut inflated from the specified resource.
1535 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 * @param parent The group the shortcut belongs to.
1537 * @param info The data structure describing the shortcut.
1538 *
1539 * @return A View inflated from layoutResId.
1540 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001541 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
1542 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.application,
1543 parent, false);
1544 favorite.applyFromShortcutInfo(info, mIconCache);
1545 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001547 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 return favorite;
1549 }
1550
1551 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 * Add a shortcut to the workspace.
1553 *
1554 * @param data The intent describing the shortcut.
1555 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001558 int cellY) {
1559 int[] cellXY = mTmpAddItemCellCoordinates;
1560 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001562
Michael Jurkad3ef3062010-11-23 16:23:58 -08001563 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001564
Sunny Goyal2350bc92014-10-14 16:42:54 -07001565 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001566 if (info == null) {
1567 return;
1568 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001569 final View view = createShortcut(info);
1570
Adam Cohenfbba09b2011-07-18 21:43:05 -07001571 // First we check if we already know the exact location where we want to add this item.
1572 if (cellX >= 0 && cellY >= 0) {
1573 cellXY[0] = cellX;
1574 cellXY[1] = cellY;
1575 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001576
1577 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001578 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001579 true, null,null)) {
1580 return;
1581 }
1582 DragObject dragObject = new DragObject();
1583 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001584 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1585 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001586 return;
1587 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001588 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001589 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001590 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001591 foundCellSpan = (result != null);
1592 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001593 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001594 }
1595
1596 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001597 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001598 return;
1599 }
1600
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001601 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602
1603 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001604 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001605 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 }
1607 }
1608
Adam Cohen2f093b62012-04-30 18:59:53 -07001609 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1610 int minHeight) {
1611 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001612 // We want to account for the extra amount of padding that we are adding to the widget
1613 // to ensure that it gets the full amount of space that it has requested
1614 int requiredWidth = minWidth + padding.left + padding.right;
1615 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001616 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001617 }
1618
Adam Cohen2f093b62012-04-30 18:59:53 -07001619 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1620 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001621 }
1622
Adam Cohen2f093b62012-04-30 18:59:53 -07001623 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1624 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001625 }
1626
Adam Cohen2f093b62012-04-30 18:59:53 -07001627 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1628 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001629 }
1630
Adam Cohen2f093b62012-04-30 18:59:53 -07001631 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1632 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001633 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001634 }
1635
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001637 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001639 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 */
Adam Cohen091440a2015-03-18 14:16:05 -07001641 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001642 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1643
1644 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001645 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001646 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1647 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001648 }
Romain Guycbb89e42009-06-08 15:52:54 -07001649
Adam Cohen59400422014-03-05 18:07:04 -08001650 if (appWidgetInfo.isCustomWidget) {
1651 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001652 }
1653
Adam Cohen59400422014-03-05 18:07:04 -08001654 LauncherAppWidgetInfo launcherInfo;
1655 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1656 launcherInfo.spanX = info.spanX;
1657 launcherInfo.spanY = info.spanY;
1658 launcherInfo.minSpanX = info.minSpanX;
1659 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001660 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001663 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664
1665 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001666 if (hostView == null) {
1667 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001668 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1669 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001670 } else {
1671 // The AppWidgetHostView has already been inflated and instantiated
1672 launcherInfo.hostView = hostView;
1673 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001675 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001676 launcherInfo.notifyWidgetSizeChanged(this);
1677
Adam Cohen59400422014-03-05 18:07:04 -08001678 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1679 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001680
1681 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001683 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 }
Romain Guycbb89e42009-06-08 15:52:54 -07001685
Adam Cohended9f8d2010-11-03 13:25:16 -07001686 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1687 @Override
1688 public void onReceive(Context context, Intent intent) {
1689 final String action = intent.getAction();
1690 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1691 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001692 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001693 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001694
Winson Chungc100e8e2011-08-09 16:02:43 -07001695 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001696 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001697 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001698 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001699 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001700 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001701 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1702 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001703 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001704 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1705 mModel.resetLoadedState(false, true);
1706 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1707 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1708 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1709 mModel.resetLoadedState(false, true);
1710 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1711 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1712 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001713 }
1714 }
1715 };
1716
1717 @Override
1718 public void onAttachedToWindow() {
1719 super.onAttachedToWindow();
1720
1721 // Listen for broadcasts related to user-presence
1722 final IntentFilter filter = new IntentFilter();
1723 filter.addAction(Intent.ACTION_SCREEN_OFF);
1724 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001725 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001726 if (ENABLE_DEBUG_INTENTS) {
1727 filter.addAction(DebugIntents.DELETE_DATABASE);
1728 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1729 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001730 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001731 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001732 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001733 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001734 mVisible = true;
1735 }
1736
Adam Cohen0b395352014-06-09 22:54:36 +00001737 /**
1738 * Sets up transparent navigation and status bars in LMP.
1739 * This method is a no-op for other platform versions.
1740 */
Sunny Goyald0091012015-01-20 15:55:34 -08001741 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001742 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001743 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001744 Window window = getWindow();
1745 window.getAttributes().systemUiVisibility |=
1746 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1747 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1748 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1749 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1750 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1751 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1752 window.setStatusBarColor(Color.TRANSPARENT);
1753 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001754 }
1755 }
1756
Adam Cohended9f8d2010-11-03 13:25:16 -07001757 @Override
1758 public void onDetachedFromWindow() {
1759 super.onDetachedFromWindow();
1760 mVisible = false;
1761
Adam Cohend113e0c2010-11-11 10:48:05 -08001762 if (mAttached) {
1763 unregisterReceiver(mReceiver);
1764 mAttached = false;
1765 }
Winson Chungb745afb2015-03-02 11:51:23 -08001766 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 }
1768
1769 public void onWindowVisibilityChanged(int visibility) {
1770 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001771 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001772 // The following code used to be in onResume, but it turns out onResume is called when
1773 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1774 // is a more appropriate event to handle
1775 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001776 if (!mWorkspaceLoading) {
1777 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001778 // We want to let Launcher draw itself at least once before we force it to build
1779 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001780 // apps is nice and speedy.
1781 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001782 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001783 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001784 if (mStarted) return;
1785 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001786 // We delay the layer building a bit in order to give
1787 // other message processing a time to run. In particular
1788 // this avoids a delay in hiding the IME if it was
1789 // currently shown, because doing that may involve
1790 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001791 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001792 final ViewTreeObserver.OnDrawListener listener = this;
1793 mWorkspace.post(new Runnable() {
1794 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001795 if (mWorkspace != null &&
1796 mWorkspace.getViewTreeObserver() != null) {
1797 mWorkspace.getViewTreeObserver().
1798 removeOnDrawListener(listener);
1799 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001800 }
1801 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001802 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001803 }
1804 });
1805 }
1806 clearTypedText();
1807 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001808 }
1809
Adam Cohen091440a2015-03-18 14:16:05 -07001810 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001811 mHandler.removeMessages(ADVANCE_MSG);
1812 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1813 mHandler.sendMessageDelayed(msg, delay);
1814 mAutoAdvanceSentTime = System.currentTimeMillis();
1815 }
1816
Adam Cohen091440a2015-03-18 14:16:05 -07001817 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001818 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1819 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1820 mAutoAdvanceRunning = autoAdvanceRunning;
1821 if (autoAdvanceRunning) {
1822 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1823 sendAdvanceMessage(delay);
1824 } else {
1825 if (!mWidgetsToAdvance.isEmpty()) {
1826 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1827 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1828 }
1829 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001830 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001831 }
1832 }
1833 }
1834
1835 private final Handler mHandler = new Handler() {
1836 @Override
1837 public void handleMessage(Message msg) {
1838 if (msg.what == ADVANCE_MSG) {
1839 int i = 0;
1840 for (View key: mWidgetsToAdvance.keySet()) {
1841 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1842 final int delay = mAdvanceStagger * i;
1843 if (v instanceof Advanceable) {
1844 postDelayed(new Runnable() {
1845 public void run() {
1846 ((Advanceable) v).advance();
1847 }
1848 }, delay);
1849 }
1850 i++;
1851 }
1852 sendAdvanceMessage(mAdvanceInterval);
1853 }
1854 }
1855 };
1856
1857 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001858 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001859 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1860 if (v instanceof Advanceable) {
1861 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001862 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001863 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001864 }
1865 }
1866
1867 void removeWidgetToAutoAdvance(View hostView) {
1868 if (mWidgetsToAdvance.containsKey(hostView)) {
1869 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001870 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001871 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001872 }
1873
Joe Onorato9c1289c2009-08-17 11:03:03 -04001874 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001875 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001876 launcherInfo.hostView = null;
1877 }
1878
Hyunyoung Song3f471442015-04-08 19:01:34 -07001879 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001880 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1881 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001882 }
1883
Winson Chunga6945242014-01-08 14:04:34 -08001884 public DragLayer getDragLayer() {
1885 return mDragLayer;
1886 }
1887
Winson Chungb745afb2015-03-02 11:51:23 -08001888 public AppsContainerView getAppsView() {
1889 return mAppsView;
1890 }
1891
Hyunyoung Song3f471442015-04-08 19:01:34 -07001892 public WidgetsContainerView getWidgetsView() {
1893 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001894 }
1895
Winson Chunga6945242014-01-08 14:04:34 -08001896 public Workspace getWorkspace() {
1897 return mWorkspace;
1898 }
1899
1900 public Hotseat getHotseat() {
1901 return mHotseat;
1902 }
1903
Jorim Jaggid017f882014-01-14 17:08:48 -08001904 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001905 return mOverviewPanel;
1906 }
1907
1908 public SearchDropTargetBar getSearchBar() {
1909 return mSearchDropTargetBar;
1910 }
1911
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001912 public LauncherAppWidgetHost getAppWidgetHost() {
1913 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 }
Romain Guycbb89e42009-06-08 15:52:54 -07001915
Winson Chunga9abd0e2010-10-27 17:18:37 -07001916 public LauncherModel getModel() {
1917 return mModel;
1918 }
1919
Winson Chunga6945242014-01-08 14:04:34 -08001920 protected SharedPreferences getSharedPrefs() {
1921 return mSharedPrefs;
1922 }
1923
Bjorn Bringertc459e522013-06-07 19:36:01 +01001924 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001925 getWindow().closeAllPanels();
1926
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001927 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001928 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001929 }
1930
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 @Override
1932 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001933 long startTime = 0;
1934 if (DEBUG_RESUME_TIME) {
1935 startTime = System.currentTimeMillis();
1936 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 super.onNewIntent(intent);
1938
1939 // Close the menu
1940 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001941 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001942 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001943
Adam Cohened307df2013-10-02 09:37:31 -07001944 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1945 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1946 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001947
Adam Cohen6fecd412013-10-02 17:41:50 -07001948 if (mWorkspace == null) {
1949 // Can be cases where mWorkspace is null, this prevents a NPE
1950 return;
1951 }
1952 Folder openFolder = mWorkspace.getOpenFolder();
1953 // In all these cases, only animate if we're already on home
1954 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001955
1956 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1957 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001958 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001959 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001960 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001961 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001962
Adam Cohen6fecd412013-10-02 17:41:50 -07001963 closeFolder();
1964 exitSpringLoadedDragMode();
1965
1966 // If we are already on home, then just animate back to the workspace,
1967 // otherwise, just wait until onResume to set the state back to Workspace
1968 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001969 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001970 } else {
1971 mOnResumeState = State.WORKSPACE;
1972 }
1973
1974 final View v = getWindow().peekDecorView();
1975 if (v != null && v.getWindowToken() != null) {
1976 InputMethodManager imm = (InputMethodManager)getSystemService(
1977 INPUT_METHOD_SERVICE);
1978 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1979 }
1980
Winson Chungb745afb2015-03-02 11:51:23 -08001981 // Reset the apps view
1982 if (!alreadyOnHome && mAppsView != null) {
1983 mAppsView.scrollToTop();
1984 }
1985
Hyunyoung Song3f471442015-04-08 19:01:34 -07001986 // Reset the widgets view
1987 if (!alreadyOnHome && mWidgetsView != null) {
1988 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001989 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001990
Adam Cohen9211d422014-10-07 18:14:53 -07001991 if (mLauncherCallbacks != null) {
1992 mLauncherCallbacks.onHomeIntent();
1993 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 }
Adam Cohened307df2013-10-02 09:37:31 -07001995
Michael Jurka447bf842013-05-15 14:52:15 +02001996 if (DEBUG_RESUME_TIME) {
1997 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1998 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999
Adam Cohen9211d422014-10-07 18:14:53 -07002000 if (mLauncherCallbacks != null) {
2001 mLauncherCallbacks.onNewIntent(intent);
2002 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002003 }
2004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07002006 public void onRestoreInstanceState(Bundle state) {
2007 super.onRestoreInstanceState(state);
2008 for (int page: mSynchronouslyBoundPages) {
2009 mWorkspace.restoreInstanceStateForChild(page);
2010 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 }
2012
2013 @Override
2014 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002015 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002016 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2017 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002018 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002019 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020
Michael Jurka883f55b2010-10-21 15:47:14 -07002021 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002022 // We close any open folder since it will not be re-opened, and we need to make sure
2023 // this state is reflected.
2024 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025
Adam Cohendcd297f2013-06-18 13:13:40 -07002026 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002027 mWaitingForResult) {
2028 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002029 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002030 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2031 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002032 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2033 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2034 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002035 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 }
2037
2038 if (mFolderInfo != null && mWaitingForResult) {
2039 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2040 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2041 }
Michael Jurka946ad472010-07-09 18:05:18 -07002042
Hyunyoung Song3f471442015-04-08 19:01:34 -07002043 // Save the current widgets tray?
2044 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002045 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002046
2047 if (mLauncherCallbacks != null) {
2048 mLauncherCallbacks.onSaveInstanceState(outState);
2049 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 }
2051
2052 @Override
2053 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002055
Winson Chunge7a03942011-08-05 15:05:12 -07002056 // Remove all pending runnables
2057 mHandler.removeMessages(ADVANCE_MSG);
2058 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002059 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002060
Winson Chungcd2b0142011-06-08 16:02:26 -07002061 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002062 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002063
2064 // It's possible to receive onDestroy after a new Launcher activity has
2065 // been created. In this case, don't interfere with the new Launcher.
2066 if (mModel.isCurrentCallbacks(this)) {
2067 mModel.stopLoader();
2068 app.setLauncher(null);
2069 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002070
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002072 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002074 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002076 mAppWidgetHost = null;
2077
2078 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079
2080 TextKeyListener.getInstance().release();
2081
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002082 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002083 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002084
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002085 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002086 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002087 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002088 mWorkspace = null;
2089 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002090
Michael Jurka2ecf9952012-06-18 12:52:28 -07002091 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002092
2093 if (mLauncherCallbacks != null) {
2094 mLauncherCallbacks.onDestroy();
2095 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 }
2097
Adam Cohena9cf38f2011-05-02 15:36:58 -07002098 public DragController getDragController() {
2099 return mDragController;
2100 }
2101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 @Override
2103 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002104 onStartForResult(requestCode);
2105 super.startActivityForResult(intent, requestCode);
2106 }
2107
2108 @Override
2109 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2110 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2111 onStartForResult(requestCode);
2112 try {
2113 super.startIntentSenderForResult(intent, requestCode,
2114 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2115 } catch (IntentSender.SendIntentException e) {
2116 throw new ActivityNotFoundException();
2117 }
2118 }
2119
2120 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002121 if (requestCode >= 0) {
2122 setWaitingForResult(true);
2123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 }
2125
Winson Chung70d72102011-08-12 11:24:35 -07002126 /**
2127 * Indicates that we want global search for this activity by setting the globalSearch
2128 * argument for {@link #startSearch} to true.
2129 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002131 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002133
Karl Rosaen138a0412009-04-23 19:00:21 -07002134 if (initialQuery == null) {
2135 // Use any text typed in the launcher as the initial query
2136 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 if (appSearchData == null) {
2139 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002140 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 }
Winson Chungadf0c182012-08-23 14:59:07 -07002142 Rect sourceBounds = new Rect();
2143 if (mSearchDropTargetBar != null) {
2144 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2145 }
Romain Guycbb89e42009-06-08 15:52:54 -07002146
Ian Parkinson047036e2014-09-01 15:40:53 +01002147 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002148 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002149 if (clearTextImmediately) {
2150 clearTypedText();
2151 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002152
2153 // We need to show the workspace after starting the search
2154 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002155 }
2156
Ian Parkinson047036e2014-09-01 15:40:53 +01002157 /**
2158 * Start a text search.
2159 *
2160 * @return {@code true} if the search will start immediately, so any further keypresses
2161 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2162 * to buffer keypresses.
2163 */
2164 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002165 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002166 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2167 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2168 sourceBounds);
2169 }
2170
Michael Jurkaa33411c2012-06-14 16:18:21 -07002171 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002172 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002173 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002174 }
2175
2176 /**
2177 * Starts the global search activity. This code is a copied from SearchManager
2178 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002179 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002180 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002181 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002182 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2183 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2184 if (globalSearchActivity == null) {
2185 Log.w(TAG, "No global search activity found.");
2186 return;
2187 }
2188 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2189 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2190 intent.setComponent(globalSearchActivity);
2191 // Make sure that we have a Bundle to put source in
2192 if (appSearchData == null) {
2193 appSearchData = new Bundle();
2194 } else {
2195 appSearchData = new Bundle(appSearchData);
2196 }
Adam Cohen9211d422014-10-07 18:14:53 -07002197 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002198 if (!appSearchData.containsKey("source")) {
2199 appSearchData.putString("source", getPackageName());
2200 }
2201 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2202 if (!TextUtils.isEmpty(initialQuery)) {
2203 intent.putExtra(SearchManager.QUERY, initialQuery);
2204 }
2205 if (selectInitialQuery) {
2206 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2207 }
2208 intent.setSourceBounds(sourceBounds);
2209 try {
2210 startActivity(intent);
2211 } catch (ActivityNotFoundException ex) {
2212 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 }
2215
Yura4f93ec62014-02-04 14:15:21 +00002216 public boolean isOnCustomContent() {
2217 return mWorkspace.isOnOrMovingToCustomContent();
2218 }
2219
Winson Chung70d72102011-08-12 11:24:35 -07002220 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002221 public boolean onPrepareOptionsMenu(Menu menu) {
2222 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002223 if (!isOnCustomContent()) {
2224 // Close any open folders
2225 closeFolder();
2226 // Stop resizing any widgets
2227 mWorkspace.exitWidgetResizeMode();
2228 if (!mWorkspace.isInOverviewMode()) {
2229 // Show the overview mode
2230 showOverviewMode(true);
2231 } else {
2232 showWorkspace(true);
2233 }
Winson Chunge0298742014-01-17 12:03:00 -08002234 }
Adam Cohen9211d422014-10-07 18:14:53 -07002235 if (mLauncherCallbacks != null) {
2236 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2237 }
2238
Michael Jurkab94f3f82013-09-11 18:08:54 +02002239 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002240 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002242 @Override
2243 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002244 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002245 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002246 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002247 }
2248
Joe Onorato9c1289c2009-08-17 11:03:03 -04002249 public boolean isWorkspaceLocked() {
2250 return mWorkspaceLoading || mWaitingForResult;
2251 }
2252
Adam Cohen517a7f52014-03-01 12:12:59 -08002253 public boolean isWorkspaceLoading() {
2254 return mWorkspaceLoading;
2255 }
2256
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002257 private void setWorkspaceLoading(boolean value) {
2258 boolean isLocked = isWorkspaceLocked();
2259 mWorkspaceLoading = value;
2260 if (isLocked != isWorkspaceLocked()) {
2261 onWorkspaceLockedChanged();
2262 }
2263 }
2264
2265 private void setWaitingForResult(boolean value) {
2266 boolean isLocked = isWorkspaceLocked();
2267 mWaitingForResult = value;
2268 if (isLocked != isWorkspaceLocked()) {
2269 onWorkspaceLockedChanged();
2270 }
2271 }
2272
Adam Cohen9211d422014-10-07 18:14:53 -07002273 protected void onWorkspaceLockedChanged() {
2274 if (mLauncherCallbacks != null) {
2275 mLauncherCallbacks.onWorkspaceLockedChanged();
2276 }
2277 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002278
Michael Jurka0280c3b2010-09-17 15:00:07 -07002279 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002280 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002281 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002282 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2283 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002284 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002285 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002286 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002287
Sunny Goyale6b63a32015-01-16 16:14:29 -08002288 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002289 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002290 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2291 }
2292
Sunny Goyale6b63a32015-01-16 16:14:29 -08002293 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002294 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2295 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002296 if (appWidgetInfo.configure != null) {
2297 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002298 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002299
Bjorn Bringert7984c942009-12-09 15:38:25 +00002300 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002301 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2302 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002304 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002305 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002306 Runnable onComplete = new Runnable() {
2307 @Override
2308 public void run() {
2309 // Exit spring loaded mode if necessary after adding the widget
2310 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2311 null);
2312 }
2313 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002314 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002315 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002316 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 }
2318 }
Romain Guycbb89e42009-06-08 15:52:54 -07002319
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002320 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002321 // Close any folders that may be open.
2322 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002323 mWorkspace.moveToCustomContentScreen(animate);
2324 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002325
2326 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2327 int[] cell, int spanX, int spanY) {
2328 switch (info.itemType) {
2329 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2330 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2331 int span[] = new int[2];
2332 span[0] = spanX;
2333 span[1] = spanY;
2334 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2335 container, screenId, cell, span);
2336 break;
2337 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2338 processShortcutFromDrop(info.componentName, container, screenId, cell);
2339 break;
2340 default:
2341 throw new IllegalStateException("Unknown item type: " + info.itemType);
2342 }
2343 }
2344
Adam Cohenfbba09b2011-07-18 21:43:05 -07002345 /**
2346 * Process a shortcut drop.
2347 *
2348 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002349 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002350 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002352 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2353 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002354 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002355 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002356 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002357 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002358
2359 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002360 mPendingAddInfo.cellX = cell[0];
2361 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002362 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002363
2364 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2365 createShortcutIntent.setComponent(componentName);
2366 processShortcut(createShortcutIntent);
2367 }
2368
Adam Cohenfbba09b2011-07-18 21:43:05 -07002369 /**
2370 * Process a widget drop.
2371 *
2372 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002373 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002374 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002375 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002376 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2377 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002378 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002379 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002380 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002381 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002382 mPendingAddInfo.minSpanX = info.minSpanX;
2383 mPendingAddInfo.minSpanY = info.minSpanY;
2384
Adam Cohenfbba09b2011-07-18 21:43:05 -07002385 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002386 mPendingAddInfo.cellX = cell[0];
2387 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002388 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002389 if (span != null) {
2390 mPendingAddInfo.spanX = span[0];
2391 mPendingAddInfo.spanY = span[1];
2392 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002393
Adam Cohened66b2b2012-01-23 17:28:51 -08002394 AppWidgetHostView hostView = info.boundWidget;
2395 int appWidgetId;
2396 if (hostView != null) {
2397 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002398 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002399 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002400 // In this case, we either need to start an activity to get permission to bind
2401 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002402 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002403 Bundle options = info.bindOptions;
2404
Sunny Goyalffe83f12014-08-14 17:39:34 -07002405 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2406 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002407 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002408 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002409 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002410 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002411 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2412 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2413 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002414 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2415 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002416 // TODO: we need to make sure that this accounts for the options bundle.
2417 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002418 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2419 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002420 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002421 }
2422
Joe Onoratodeb98af2010-02-19 14:59:39 -08002423 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002424 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 }
2426
Winson Chung24ab2f12010-09-16 14:10:47 -07002427 void processWallpaper(Intent intent) {
2428 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2429 }
2430
Adam Cohendcd297f2013-06-18 13:13:40 -07002431 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002432 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002433 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 folderInfo.title = getText(R.string.folder_name);
2435
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002437 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2438 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002439 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440
2441 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002442 FolderIcon newFolder =
2443 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002444 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002445 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002446 // Force measure the new folder icon
2447 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2448 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002449 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 }
Romain Guycbb89e42009-06-08 15:52:54 -07002451
Joe Onorato9c1289c2009-08-17 11:03:03 -04002452 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002453 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002454 }
2455
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002456 /**
2457 * Registers various content observers. The current implementation registers
2458 * only a favorites observer to keep track of the favorites applications.
2459 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002460 private void registerContentObservers() {
2461 ContentResolver resolver = getContentResolver();
2462 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2463 true, mWidgetObserver);
2464 }
2465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 @Override
2467 public boolean dispatchKeyEvent(KeyEvent event) {
2468 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2469 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002471 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002472 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002473 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002474 dumpState();
2475 return true;
2476 }
2477 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002478 }
2479 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2480 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002481 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 return true;
2483 }
2484 }
2485
2486 return super.dispatchKeyEvent(event);
2487 }
2488
Joe Onorato88ec0992009-11-19 13:16:06 -08002489 @Override
2490 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002491 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2492 return;
2493 }
2494
Winson Chung3d9490a2015-03-24 17:38:42 -07002495 LauncherAccessibilityDelegate delegate =
2496 LauncherAppState.getInstance().getAccessibilityDelegate();
2497 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002498 return;
2499 }
2500
Winson Chungb745afb2015-03-02 11:51:23 -08002501 if (isAppsViewVisible()) {
2502 showWorkspace(true);
2503 } else if (isWidgetsViewVisible()) {
2504 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002505 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002506 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002507 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002508 Folder openFolder = mWorkspace.getOpenFolder();
2509 if (openFolder.isEditingName()) {
2510 openFolder.dismissEditingName();
2511 } else {
2512 closeFolder();
2513 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002514 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002515 mWorkspace.exitWidgetResizeMode();
2516
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002517 // Back button is a no-op here, but give at least some feedback for the button press
2518 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002519 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002520 }
2521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002523 * Re-listen when widgets are reset.
2524 */
Adam Cohen091440a2015-03-18 14:16:05 -07002525 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002526 if (mAppWidgetHost != null) {
2527 mAppWidgetHost.startListening();
2528 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002529 }
2530
2531 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002532 * Launches the intent referred by the clicked shortcut.
2533 *
2534 * @param v The view representing the clicked shortcut.
2535 */
2536 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002537 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2538 // view has detached (it's possible for this to happen if the view is removed mid touch).
2539 if (v.getWindowToken() == null) {
2540 return;
2541 }
2542
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002543 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002544 return;
2545 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002546
Adam Cohen1697b792013-09-17 19:08:21 -07002547 if (v instanceof Workspace) {
2548 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002549 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002550 }
2551 return;
2552 }
2553
2554 if (v instanceof CellLayout) {
2555 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002556 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002557 }
2558 }
2559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002561 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002562 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002564 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002565 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002566 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002567 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002568 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002569 } else if (tag instanceof AppInfo) {
2570 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002571 } else if (tag instanceof LauncherAppWidgetInfo) {
2572 if (v instanceof PendingAppWidgetHostView) {
2573 onClickPendingWidget((PendingAppWidgetHostView) v);
2574 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002575 }
2576 }
2577
Sunny Goyal508da152014-08-14 10:53:27 -07002578 public void onClickPagedViewIcon(View v) {
2579 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002580 if (mLauncherCallbacks != null) {
2581 mLauncherCallbacks.onClickPagedViewIcon(v);
2582 }
Sunny Goyal508da152014-08-14 10:53:27 -07002583 }
2584
Sunny Goyal70660032015-05-14 00:07:08 -07002585 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002586 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002587 return false;
2588 }
2589
Michael Jurkaaf442092010-06-10 17:01:57 -07002590 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002591 * Event handler for the app widget view which has not fully restored.
2592 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002593 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002594 if (mIsSafeModeEnabled) {
2595 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2596 return;
2597 }
2598
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002599 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002600 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002601 int widgetId = info.appWidgetId;
2602 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2603 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002604 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2605 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002606 mPendingAddInfo.copyFrom(info);
2607 mPendingAddWidgetId = widgetId;
2608
Sunny Goyalffe83f12014-08-14 17:39:34 -07002609 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2610 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002611 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002612 } else if (info.installProgress < 0) {
2613 // The install has not been queued
2614 final String packageName = info.providerName.getPackageName();
2615 showBrokenAppInstallDialog(packageName,
2616 new DialogInterface.OnClickListener() {
2617 public void onClick(DialogInterface dialog, int id) {
2618 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2619 }
2620 });
2621 } else {
2622 // Download has started.
2623 final String packageName = info.providerName.getPackageName();
2624 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002625 }
2626 }
2627
2628 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002629 * Event handler for the "grid" button that appears on the home screen, which
2630 * enters all apps mode.
2631 *
2632 * @param v The view that was clicked.
2633 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002634 protected void onClickAllAppsButton(View v) {
2635 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002636 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002637 showWorkspace(true);
2638 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002639 // Try and refresh the set of predicted apps before we enter launcher
2640 showAppsView(true /* animated */, false /* resetListToTop */,
2641 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002642 }
2643 }
2644
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002645 private void showBrokenAppInstallDialog(final String packageName,
2646 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002647 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002648 .setTitle(R.string.abandoned_promises_title)
2649 .setMessage(R.string.abandoned_promise_explanation)
2650 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2651 .setNeutralButton(R.string.abandoned_clean_this,
2652 new DialogInterface.OnClickListener() {
2653 public void onClick(DialogInterface dialog, int id) {
2654 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2655 mWorkspace.removeAbandonedPromise(packageName, user);
2656 }
2657 })
2658 .create().show();
2659 return;
2660 }
2661
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002662 /**
2663 * Event handler for an app shortcut click.
2664 *
2665 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2666 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002667 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002668 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2669 Object tag = v.getTag();
2670 if (!(tag instanceof ShortcutInfo)) {
2671 throw new IllegalArgumentException("Input must be a Shortcut");
2672 }
2673
2674 // Open shortcut
2675 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002676
2677 if (shortcut.isDisabled != 0) {
2678 int error = R.string.activity_not_available;
2679 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2680 error = R.string.safemode_shortcut_error;
2681 }
2682 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2683 return;
2684 }
2685
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002686 final Intent intent = shortcut.intent;
2687
2688 // Check for special shortcuts
2689 if (intent.getComponent() != null) {
2690 final String shortcutClass = intent.getComponent().getClassName();
2691
2692 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2693 MemoryDumpActivity.startDump(this);
2694 return;
2695 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2696 toggleShowWeightWatcher();
2697 return;
2698 }
2699 }
2700
Chris Wren40c5ed32014-06-24 18:24:23 -04002701 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002702 if ((v instanceof BubbleTextView)
2703 && shortcut.isPromise()
2704 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002705 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002706 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002707 new DialogInterface.OnClickListener() {
2708 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002709 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002710 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002711 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002712 return;
2713 }
2714
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002716 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002717
2718 if (mLauncherCallbacks != null) {
2719 mLauncherCallbacks.onClickAppShortcut(v);
2720 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002721 }
2722
Adam Cohen091440a2015-03-18 14:16:05 -07002723 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002724 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002725 final ShortcutInfo shortcut;
2726 final Intent intent;
2727 if (tag instanceof ShortcutInfo) {
2728 shortcut = (ShortcutInfo) tag;
2729 intent = shortcut.intent;
2730 int[] pos = new int[2];
2731 v.getLocationOnScreen(pos);
2732 intent.setSourceBounds(new Rect(pos[0], pos[1],
2733 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002734
Sunny Goyal508da152014-08-14 10:53:27 -07002735 } else if (tag instanceof AppInfo) {
2736 shortcut = null;
2737 intent = ((AppInfo) tag).intent;
2738 } else {
2739 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2740 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002741
2742 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002743 mStats.recordLaunch(intent, shortcut);
2744
2745 if (success && v instanceof BubbleTextView) {
2746 mWaitingForResume = (BubbleTextView) v;
2747 mWaitingForResume.setStayPressed(true);
2748 }
2749 }
2750
2751 /**
2752 * Event handler for a folder icon click.
2753 *
2754 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2755 */
2756 protected void onClickFolderIcon(View v) {
2757 if (LOGD) Log.d(TAG, "onClickFolder");
2758 if (!(v instanceof FolderIcon)){
2759 throw new IllegalArgumentException("Input must be a FolderIcon");
2760 }
2761
2762 FolderIcon folderIcon = (FolderIcon) v;
2763 final FolderInfo info = folderIcon.getFolderInfo();
2764 Folder openFolder = mWorkspace.getFolderForTag(info);
2765
2766 // If the folder info reports that the associated folder is open, then verify that
2767 // it is actually opened. There have been a few instances where this gets out of sync.
2768 if (info.opened && openFolder == null) {
2769 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2770 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2771 info.opened = false;
2772 }
2773
2774 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2775 // Close any open folder
2776 closeFolder();
2777 // Open the requested folder
2778 openFolder(folderIcon);
2779 } else {
2780 // Find the open folder...
2781 int folderScreen;
2782 if (openFolder != null) {
2783 folderScreen = mWorkspace.getPageForView(openFolder);
2784 // .. and close it
2785 closeFolder(openFolder);
2786 if (folderScreen != mWorkspace.getCurrentPage()) {
2787 // Close any folder open on the current screen
2788 closeFolder();
2789 // Pull the folder onto this screen
2790 openFolder(folderIcon);
2791 }
2792 }
2793 }
Adam Cohen9211d422014-10-07 18:14:53 -07002794
2795 if (mLauncherCallbacks != null) {
2796 mLauncherCallbacks.onClickFolderIcon(v);
2797 }
Michael Jurka5130e402011-10-13 04:55:35 -07002798 }
2799
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002800 /**
2801 * Event handler for the (Add) Widgets button that appears after a long press
2802 * on the home screen.
2803 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002804 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002805 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002806 if (mIsSafeModeEnabled) {
2807 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2808 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002809 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002810 if (mLauncherCallbacks != null) {
2811 mLauncherCallbacks.onClickAddWidgetButton(view);
2812 }
Adam Cohen9211d422014-10-07 18:14:53 -07002813 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002814 }
2815
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002816 /**
2817 * Event handler for the wallpaper picker button that appears after a long press
2818 * on the home screen.
2819 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002820 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002821 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002822 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2823 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002824
2825 if (mLauncherCallbacks != null) {
2826 mLauncherCallbacks.onClickWallpaperPicker(v);
2827 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002828 }
2829
2830 /**
2831 * Event handler for a click on the settings button that appears after a long press
2832 * on the home screen.
2833 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002834 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002835 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002836 if (mLauncherCallbacks != null) {
2837 mLauncherCallbacks.onClickSettingsButton(v);
2838 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002839 }
2840
Michael Jurka5130e402011-10-13 04:55:35 -07002841 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002842 // Provide the same haptic feedback that the system offers for virtual keys.
2843 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002844 }
2845
Adam Cohen61f560d2013-09-30 15:58:20 -07002846 public void performHapticFeedbackOnTouchDown(View v) {
2847 // Provide the same haptic feedback that the system offers for virtual keys.
2848 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2849 }
2850
2851 public View.OnTouchListener getHapticFeedbackTouchListener() {
2852 if (mHapticFeedbackTouchListener == null) {
2853 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002854 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002855 @Override
2856 public boolean onTouch(View v, MotionEvent event) {
2857 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2858 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2859 }
2860 return false;
2861 }
2862 };
2863 }
2864 return mHapticFeedbackTouchListener;
2865 }
2866
Adam Cohen9211d422014-10-07 18:14:53 -07002867 public void onDragStarted(View view) {
2868 if (isOnCustomContent()) {
2869 // Custom content screen doesn't participate in drag and drop. If on custom
2870 // content screen, move to default.
2871 moveWorkspaceToDefaultScreen();
2872 }
2873
2874 if (mLauncherCallbacks != null) {
2875 mLauncherCallbacks.onDragStarted(view);
2876 }
2877 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002878
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002879 /**
2880 * Called when the user stops interacting with the launcher.
2881 * This implies that the user is now on the homescreen and is not doing housekeeping.
2882 */
Adam Cohen9211d422014-10-07 18:14:53 -07002883 protected void onInteractionEnd() {
2884 if (mLauncherCallbacks != null) {
2885 mLauncherCallbacks.onInteractionEnd();
2886 }
2887 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002888
2889 /**
2890 * Called when the user starts interacting with the launcher.
2891 * The possible interactions are:
2892 * - open all apps
2893 * - reorder an app shortcut, or a widget
2894 * - open the overview mode.
2895 * This is a good time to stop doing things that only make sense
2896 * when the user is on the homescreen and not doing housekeeping.
2897 */
Adam Cohen9211d422014-10-07 18:14:53 -07002898 protected void onInteractionBegin() {
2899 if (mLauncherCallbacks != null) {
2900 mLauncherCallbacks.onInteractionBegin();
2901 }
2902 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002903
Winson Chungcd99cd32015-04-29 11:03:24 -07002904 /** Updates the interaction state. */
2905 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002906 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002907 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002908 boolean fromStateWithOverlay;
2909 boolean toStateWithOverlay;
2910 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2911 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2912 toStateWithOverlay = toState != Workspace.State.NORMAL;
2913 } else {
2914 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2915 fromState != Workspace.State.NORMAL_HIDDEN;
2916 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2917 toState != Workspace.State.NORMAL_HIDDEN;
2918 }
2919 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002920 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002921 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002922 onInteractionEnd();
2923 }
2924 }
2925
Kenny Guyf07af7b2014-07-31 11:39:16 +01002926 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002927 try {
2928 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002929 launcherApps.showAppDetailsForProfile(componentName, user);
2930 } catch (SecurityException e) {
2931 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2932 Log.e(TAG, "Launcher does not have permission to launch settings");
2933 } catch (ActivityNotFoundException e) {
2934 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2935 Log.e(TAG, "Unable to launch settings");
2936 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002937 }
2938
Michael Jurka1e2f4652013-07-08 18:03:46 -07002939 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002940 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2941 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002942 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002943 // System applications cannot be installed. For now, show a toast explaining that.
2944 // We may give them the option of disabling apps this way.
2945 int messageId = R.string.uninstall_system_app_text;
2946 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002947 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002948 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002949 String packageName = componentName.getPackageName();
2950 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002951 Intent intent = new Intent(
2952 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002953 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2954 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002955 if (user != null) {
2956 user.addToIntent(intent, Intent.EXTRA_USER);
2957 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002958 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002959 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002960 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002961 }
2962
Michael Jurka86a720e2012-05-09 11:23:23 -07002963 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002964 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002966 // Only launch using the new animation if the shortcut has not opted out (this is a
2967 // private contract between launcher and may be ignored in the future).
2968 boolean useLaunchAnimation = (v != null) &&
2969 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002970 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2971 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002972
Kenny Guy1317e2d2014-05-08 18:52:50 +01002973 UserHandleCompat user = null;
2974 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2975 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2976 user = userManager.getUserForSerialNumber(serialNumber);
2977 }
2978
2979 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002980 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002981 ActivityOptions opts = null;
2982 if (sClipRevealMethod != null) {
2983 // TODO: call method directly when Launcher3 can depend on M APIs
2984 int left = 0, top = 0;
2985 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2986 if (v instanceof TextView) {
2987 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002988 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2989 if (icon != null) {
2990 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002991 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002992 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002993 width = bounds.width();
2994 height = bounds.height();
2995 }
2996 }
2997 try {
2998 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2999 left, top, width, height);
3000 } catch (IllegalAccessException e) {
3001 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
3002 sClipRevealMethod = null;
3003 } catch (InvocationTargetException e) {
3004 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
3005 sClipRevealMethod = null;
3006 }
3007 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003008 if (opts == null && !Utilities.isLmpOrAbove()) {
3009 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08003010 v.getMeasuredWidth(), v.getMeasuredHeight());
3011 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003012 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07003013 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01003014
3015 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
3016 // Could be launching some bookkeeping activity
3017 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003018 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003019 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003020 launcherApps.startActivityForProfile(intent.getComponent(), user,
3021 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003022 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003023 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003024 } catch (SecurityException e) {
3025 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003026 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003027 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003028 "or use the exported attribute for this activity. "
3029 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003030 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003031 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003032 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003033
Michael Jurka86a720e2012-05-09 11:23:23 -07003034 boolean startActivitySafely(View v, Intent intent, Object tag) {
3035 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003036 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3037 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3038 return false;
3039 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003040 try {
3041 success = startActivity(v, intent, tag);
3042 } catch (ActivityNotFoundException e) {
3043 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3044 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3045 }
3046 return success;
3047 }
3048
Adam Cohen268c4752012-06-06 17:47:33 -07003049 /**
3050 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3051 * in the DragLayer in the exact absolute location of the original FolderIcon.
3052 */
3053 private void copyFolderIconToImage(FolderIcon fi) {
3054 final int width = fi.getMeasuredWidth();
3055 final int height = fi.getMeasuredHeight();
3056
3057 // Lazy load ImageView, Bitmap and Canvas
3058 if (mFolderIconImageView == null) {
3059 mFolderIconImageView = new ImageView(this);
3060 }
3061 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3062 mFolderIconBitmap.getHeight() != height) {
3063 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3064 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3065 }
3066
3067 DragLayer.LayoutParams lp;
3068 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3069 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3070 } else {
3071 lp = new DragLayer.LayoutParams(width, height);
3072 }
3073
Adam Cohen307fe232012-08-16 17:55:58 -07003074 // The layout from which the folder is being opened may be scaled, adjust the starting
3075 // view size by this scale factor.
3076 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003077 lp.customPosition = true;
3078 lp.x = mRectForFolderAnimation.left;
3079 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003080 lp.width = (int) (scale * width);
3081 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003082
3083 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3084 fi.draw(mFolderIconCanvas);
3085 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003086 if (fi.getFolder() != null) {
3087 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3088 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003089 }
Adam Cohen268c4752012-06-06 17:47:33 -07003090 // Just in case this image view is still in the drag layer from a previous animation,
3091 // we remove it and re-add it.
3092 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3093 mDragLayer.removeView(mFolderIconImageView);
3094 }
3095 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003096 if (fi.getFolder() != null) {
3097 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003098 }
Adam Cohen268c4752012-06-06 17:47:33 -07003099 }
3100
Adam Cohen2801caf2011-05-13 20:57:39 -07003101 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003102 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003103 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3104 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3105 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3106
Adam Cohenc51934b2011-07-26 21:07:43 -07003107 FolderInfo info = (FolderInfo) fi.getTag();
3108 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3109 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003110 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3111 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003112 }
3113
Adam Cohen268c4752012-06-06 17:47:33 -07003114 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3115 copyFolderIconToImage(fi);
3116 fi.setVisibility(View.INVISIBLE);
3117
Michael Jurka2ecf9952012-06-18 12:52:28 -07003118 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003119 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003120 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003121 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3122 }
3123 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003124 oa.start();
3125 }
3126
Adam Cohen268c4752012-06-06 17:47:33 -07003127 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003128 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003129 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3130 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3131 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3132
Adam Cohen268c4752012-06-06 17:47:33 -07003133 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003134
Adam Cohen268c4752012-06-06 17:47:33 -07003135 // We remove and re-draw the FolderIcon in-case it has changed
3136 mDragLayer.removeView(mFolderIconImageView);
3137 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003138 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003139 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003140 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003141 oa.addListener(new AnimatorListenerAdapter() {
3142 @Override
3143 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003144 if (cl != null) {
3145 cl.clearFolderLeaveBehind();
3146 // Remove the ImageView copy of the FolderIcon and make the original visible.
3147 mDragLayer.removeView(mFolderIconImageView);
3148 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003149 }
3150 }
3151 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003152 oa.start();
3153 }
3154
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003156 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003157 * is animated relative to the specified View. If the View is null, no animation
3158 * is played.
3159 *
3160 * @param folderInfo The FolderInfo describing the folder to open.
3161 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003162 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003163 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003164 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3165 if (openFolder != null && openFolder != folder) {
3166 // Close any open folder before opening a folder.
3167 closeFolder();
3168 }
3169
Adam Cohena9cf38f2011-05-02 15:36:58 -07003170 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171
Adam Cohena9cf38f2011-05-02 15:36:58 -07003172 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173
Sunny Goyalf4066152015-04-15 09:42:19 -07003174 // While the folder is open, the position of the icon cannot change.
3175 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3176
Adam Cohen4554ee12011-08-03 16:13:21 -07003177 // Just verify that the folder hasn't already been added to the DragLayer.
3178 // There was a one-off crash where the folder had a parent already.
3179 if (folder.getParent() == null) {
3180 mDragLayer.addView(folder);
3181 mDragController.addDropTarget((DropTarget) folder);
3182 } else {
3183 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3184 folder.getParent() + ").");
3185 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003186 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003187 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003188
3189 // Notify the accessibility manager that this folder "window" has appeared and occluded
3190 // the workspace items
3191 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3192 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003193 }
3194
3195 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003196 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003197 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003198 if (folder.isEditingName()) {
3199 folder.dismissEditingName();
3200 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003201 closeFolder(folder);
3202 }
3203 }
3204
3205 void closeFolder(Folder folder) {
3206 folder.getInfo().opened = false;
3207
3208 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3209 if (parent != null) {
3210 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3211 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003212 if (fi != null) {
3213 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3214 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003215 }
3216 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003217
3218 // Notify the accessibility manager that this folder "window" has disappeard and no
3219 // longer occludeds the workspace items
3220 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003221 }
3222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003223 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003224 if (!isDraggingEnabled()) return false;
3225 if (isWorkspaceLocked()) return false;
3226 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003227
Adam Cohen1697b792013-09-17 19:08:21 -07003228 if (v instanceof Workspace) {
3229 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003230 if (!mWorkspace.isTouchActive()) {
3231 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003232 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3233 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3234 return true;
3235 } else {
3236 return false;
3237 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003238 } else {
3239 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003240 }
Adam Cohen1697b792013-09-17 19:08:21 -07003241 }
3242
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003243 CellLayout.CellInfo longClickCellInfo = null;
3244 View itemUnderLongClick = null;
3245 if (v.getTag() instanceof ItemInfo) {
3246 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003247 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003248 itemUnderLongClick = longClickCellInfo.cell;
3249 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003250 }
3251
Winson Chung3d503fb2011-07-13 17:25:49 -07003252 // The hotseat touch handling does not go through Workspace, and we always allow long press
3253 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003254 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003255 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003256 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003257 // User long pressed on empty space
3258 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3259 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003260 if (mWorkspace.isInOverviewMode()) {
3261 mWorkspace.startReordering(v);
3262 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003263 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003264 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003265 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003266 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3267 mHotseat.getOrderInHotseat(
3268 longClickCellInfo.cellX,
3269 longClickCellInfo.cellY));
3270 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003271 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003272 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003273 }
3274 }
3275 }
3276 return true;
3277 }
3278
Winson Chung3d503fb2011-07-13 17:25:49 -07003279 boolean isHotseatLayout(View layout) {
3280 return mHotseat != null && layout != null &&
3281 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3282 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003283
Winson Chung3d503fb2011-07-13 17:25:49 -07003284 /**
3285 * Returns the CellLayout of the specified container at the specified screen.
3286 */
Sunny Goyal92820592015-03-02 11:31:35 -08003287 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003288 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3289 if (mHotseat != null) {
3290 return mHotseat.getLayout();
3291 } else {
3292 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003293 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003294 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003295 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003296 }
3297 }
3298
Winson Chungb745afb2015-03-02 11:51:23 -08003299 /**
3300 * For overridden classes.
3301 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003302 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003303 return isAppsViewVisible();
3304 }
3305
3306 public boolean isAppsViewVisible() {
3307 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3308 }
3309
3310 public boolean isWidgetsViewVisible() {
3311 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003312 }
3313
Craig Mautner360310b2012-10-26 15:13:08 -07003314 private void setWorkspaceBackground(boolean workspace) {
3315 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003316 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003317 }
3318
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003319 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003320 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3321 int curflags = getWindow().getAttributes().flags
3322 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3323 if (wpflags != curflags) {
3324 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3325 }
Craig Mautner360310b2012-10-26 15:13:08 -07003326 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003327 }
3328
Michael Jurkae326f182011-11-21 14:05:46 -08003329 @Override
3330 public void onTrimMemory(int level) {
3331 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003332 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3333 // The widget preview db can result in holding onto over
3334 // 3MB of memory for caching which isn't necessary.
3335 SQLiteDatabase.releaseMemory();
3336
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003337 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003338 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003339 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003340 if (mLauncherCallbacks != null) {
3341 mLauncherCallbacks.onTrimMemory(level);
3342 }
Michael Jurkae326f182011-11-21 14:05:46 -08003343 }
3344
Winson Chungb745afb2015-03-02 11:51:23 -08003345 @Override
3346 public void onStateTransitionHideSearchBar() {
3347 // Hide the search bar
3348 if (mSearchDropTargetBar != null) {
3349 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3350 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003351 }
3352
Winson Chungb745afb2015-03-02 11:51:23 -08003353 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003354 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3355 true);
Adam Cohened307df2013-10-02 09:37:31 -07003356 }
3357
Winson Chungdc61c4d2015-04-20 18:26:57 -07003358 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3359 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3360 onCompleteRunnable, true);
3361 }
3362
3363 protected void showWorkspace(int snapToPage, boolean animated) {
3364 showWorkspace(snapToPage, animated, null, true);
3365 }
3366
3367 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3368 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003369 boolean changed = mState != State.WORKSPACE ||
3370 mWorkspace.getState() != Workspace.State.NORMAL;
3371 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003372 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003373 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003374 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003375 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003376
Winson Chungc7d2b602012-05-16 17:02:20 -07003377 // Show the search bar (only animate if we were showing the drop target bar in spring
3378 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003379 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003380 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003381 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003382
Michael Jurkab3e22d92011-10-31 15:58:33 -07003383 // Set focus to the AppsCustomize button
3384 if (mAllAppsButton != null) {
3385 mAllAppsButton.requestFocus();
3386 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003387 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003388
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003389 // Change the state *after* we've called all the transition code
3390 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003391
Winson Chung5fb63472011-02-02 17:03:37 -08003392 // Resume the auto-advance of widgets
3393 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003394 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003395
Winson Chung0f785722015-04-08 10:27:49 -07003396 if (changed) {
3397 // Send an accessibility event to announce the context change
3398 getWindow().getDecorView()
3399 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003400 if (notifyLauncherCallbacks) {
3401 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003402 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003403 mLauncherCallbacks.onAllAppsHidden();
3404 }
3405 }
Winson Chung0f785722015-04-08 10:27:49 -07003406 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003407 }
3408
Adam Cohened307df2013-10-02 09:37:31 -07003409 void showOverviewMode(boolean animated) {
3410 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003411 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003412 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3413 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003414 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003415 }
3416
Winson Chungb745afb2015-03-02 11:51:23 -08003417 /**
3418 * Shows the apps view.
3419 */
Winson Chung4ac30062015-05-08 17:34:17 -07003420 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003421 if (resetListToTop) {
3422 mAppsView.scrollToTop();
3423 }
Winson Chung4ac30062015-05-08 17:34:17 -07003424 if (updatePredictedApps) {
3425 tryAndUpdatePredictedApps();
3426 }
Winson Chungb745afb2015-03-02 11:51:23 -08003427 showAppsOrWidgets(animated, State.APPS);
3428 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003429
Winson Chungb745afb2015-03-02 11:51:23 -08003430 /**
3431 * Shows the widgets view.
3432 */
3433 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003434 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003435 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003436 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003437 }
Winson Chungb745afb2015-03-02 11:51:23 -08003438 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003439
3440 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003441 @Override
3442 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003443 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003444 }
3445 });
Winson Chungb745afb2015-03-02 11:51:23 -08003446 }
3447
3448 /**
3449 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003450 *
3451 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003452 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003453 // TODO: calling method should use the return value so that when {@code false} is returned
3454 // the workspace transition doesn't fall into invalid state.
3455 private boolean showAppsOrWidgets(boolean animated, State toState) {
3456 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3457 mState != State.WIDGETS_SPRING_LOADED) {
3458 return false;
3459 }
3460 if (toState != State.APPS && toState != State.WIDGETS) {
3461 return false;
3462 }
Winson Chungb745afb2015-03-02 11:51:23 -08003463
3464 if (toState == State.APPS) {
3465 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003466 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003467 mLauncherCallbacks.onAllAppsShown();
3468 }
Winson Chungb745afb2015-03-02 11:51:23 -08003469 } else {
3470 mStateTransitionAnimation.startAnimationToWidgets(animated);
3471 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003472
Michael Jurkab3e22d92011-10-31 15:58:33 -07003473 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003474 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003475
3476 // Pause the auto-advance of widgets until we are out of AllApps
3477 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003478 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003479 closeFolder();
3480
3481 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003482 getWindow().getDecorView()
3483 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003484 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003485 }
3486
Winson Chungcd99cd32015-04-29 11:03:24 -07003487 /**
3488 * Updates the workspace and interaction state on state change, and return the animation to this
3489 * new state.
3490 */
3491 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3492 boolean animated, HashMap<View, Integer> layerViews) {
3493 Workspace.State fromState = mWorkspace.getState();
3494 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3495 updateInteraction(fromState, toState);
3496 return anim;
3497 }
3498
Hyunyoung Song3f471442015-04-08 19:01:34 -07003499 public void enterSpringLoadedDragMode() {
3500 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3501 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003502 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3503 mState == State.WIDGETS_SPRING_LOADED) {
3504 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003505 }
Winson Chungb745afb2015-03-02 11:51:23 -08003506
3507 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003508 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3509 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003510 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003511 }
Adam Cohen7777d962011-08-18 18:58:38 -07003512
Hyunyoung Song3f471442015-04-08 19:01:34 -07003513 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003514 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003515 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003516
Winson Chungcd99cd32015-04-29 11:03:24 -07003517 if (successfulDrop) {
3518 // We need to trigger all apps hidden to notify search to update itself before the
3519 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003520 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003521 mLauncherCallbacks.onAllAppsHidden();
3522 }
3523 }
3524
Winson Chunge7a03942011-08-05 15:05:12 -07003525 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003526 @Override
3527 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003528 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003529 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3530 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003531 // Before we show workspace, hide all apps again because
3532 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3533 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003534 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003535 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003536 } else {
3537 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003538 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003539 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003540 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003541 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003542
Michael Jurkad3ef3062010-11-23 16:23:58 -08003543 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003544 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003545 showAppsView(true /* animated */, false /* resetListToTop */,
3546 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003547 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003548 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003549 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003550 }
3551
Winson Chung4ac30062015-05-08 17:34:17 -07003552 /**
3553 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3554 * resumed.
3555 */
3556 private void tryAndUpdatePredictedApps() {
3557 if (mLauncherCallbacks != null) {
3558 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3559 if (!apps.isEmpty()) {
3560 mAppsView.setPredictedApps(apps);
3561 }
3562 }
3563 }
3564
Michael Jurkab3e22d92011-10-31 15:58:33 -07003565 void lockAllApps() {
3566 // TODO
3567 }
3568
3569 void unlockAllApps() {
3570 // TODO
3571 }
3572
Sunny Goyal594d76d2014-11-06 10:12:54 -08003573 protected void disableVoiceButtonProxy(boolean disable) {
3574 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003575 }
3576
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003577 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003578 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3579 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003580 }
3581
Adam Cohen24ce0b32014-01-14 16:18:14 -08003582 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003583 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3584 if (searchProvider == null) {
3585 return null;
3586 }
3587
3588 Bundle opts = new Bundle();
3589 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003590 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003591
3592 SharedPreferences sp = getSharedPreferences(
3593 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3594 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003595 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003596 if (!searchProvider.provider.flattenToString().equals(
3597 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003598 || (widgetInfo == null)
3599 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003600 // A valid widget is not already bound.
3601 if (widgetId > -1) {
3602 mAppWidgetHost.deleteAppWidgetId(widgetId);
3603 widgetId = -1;
3604 }
3605
3606 // Try to bind a new widget
3607 widgetId = mAppWidgetHost.allocateAppWidgetId();
3608
3609 if (!AppWidgetManagerCompat.getInstance(this)
3610 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3611 mAppWidgetHost.deleteAppWidgetId(widgetId);
3612 widgetId = -1;
3613 }
3614
3615 sp.edit()
3616 .putInt(QSB_WIDGET_ID, widgetId)
3617 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3618 .commit();
3619 }
3620
3621 if (widgetId != -1) {
3622 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3623 mQsb.updateAppWidgetOptions(opts);
3624 mQsb.setPadding(0, 0, 0, 0);
3625 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003626 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003627 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003628 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003629 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003630 }
3631
Sunny Goyal22235bc2015-04-03 09:23:43 -07003632 private void reinflateQSBIfNecessary() {
3633 if (mQsb instanceof LauncherAppWidgetHostView &&
3634 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3635 mSearchDropTargetBar.removeView(mQsb);
3636 mQsb = null;
3637 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3638 }
3639 }
3640
Michael Jurkad7c28052012-04-27 15:43:36 -07003641 @Override
3642 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003643 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003644 final List<CharSequence> text = event.getText();
3645 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003646 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003647 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003648 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003649 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003650 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003651 } else {
3652 text.add(getString(R.string.all_apps_home_button_label));
3653 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003654 return result;
3655 }
3656
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003657 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003658 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003659 */
Adam Cohen091440a2015-03-18 14:16:05 -07003660 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003661 @Override
3662 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003663 closeSystemDialogs();
3664 }
3665 }
3666
3667 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003668 * Receives notifications whenever the appwidgets are reset.
3669 */
3670 private class AppWidgetResetObserver extends ContentObserver {
3671 public AppWidgetResetObserver() {
3672 super(new Handler());
3673 }
3674
3675 @Override
3676 public void onChange(boolean selfChange) {
3677 onAppWidgetReset();
3678 }
3679 }
3680
3681 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003682 * If the activity is currently paused, signal that we need to run the passed Runnable
3683 * in onResume.
3684 *
3685 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003686 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3687 * wrong when we're not running, and if the activity comes back to what the configuration was
3688 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003689 *
3690 * Implementation of the method from LauncherModel.Callbacks.
3691 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003692 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003693 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003694 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003695 if (mPaused) {
3696 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003697 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003698 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003699 }
3700 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003701 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003702 return true;
3703 } else {
3704 return false;
3705 }
3706 }
3707
Michael Jurkac402cd92013-05-20 15:49:32 +02003708 private boolean waitUntilResume(Runnable run) {
3709 return waitUntilResume(run, false);
3710 }
3711
Michael Jurka1e2f4652013-07-08 18:03:46 -07003712 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003713 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003714 }
3715
Michael Jurka7607c2f2013-04-03 14:33:19 -07003716 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003717 * If the activity is currently paused, signal that we need to re-run the loader
3718 * in onResume.
3719 *
3720 * This needs to be called from incoming places where resources might have been loaded
3721 * while we are paused. That is becaues the Configuration might be wrong
3722 * when we're not running, and if it comes back to what it was when we
3723 * were paused, we are not restarted.
3724 *
3725 * Implementation of the method from LauncherModel.Callbacks.
3726 *
3727 * @return true if we are currently paused. The caller might be able to
3728 * skip some work in that case since we will come back again.
3729 */
3730 public boolean setLoadOnResume() {
3731 if (mPaused) {
3732 Log.i(TAG, "setLoadOnResume");
3733 mOnResumeNeedsLoad = true;
3734 return true;
3735 } else {
3736 return false;
3737 }
3738 }
3739
3740 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003741 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003742 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003743 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003744 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003745 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003746 } else {
3747 return SCREEN_COUNT / 2;
3748 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003749 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003750
Joe Onorato9c1289c2009-08-17 11:03:03 -04003751 /**
3752 * Refreshes the shortcuts shown on the workspace.
3753 *
3754 * Implementation of the method from LauncherModel.Callbacks.
3755 */
3756 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003757 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003758
Michael Jurka7607c2f2013-04-03 14:33:19 -07003759 // If we're starting binding all over again, clear any bind calls we'd postponed in
3760 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3761 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003762 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003763
Winson Chungd64d1762013-08-20 14:37:16 -07003764 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003765 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003766 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003767
Michael Jurka05bf644e2011-11-30 20:28:53 -08003768 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003769 if (mHotseat != null) {
3770 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003771 }
3772 }
3773
Adam Cohendcd297f2013-06-18 13:13:40 -07003774 @Override
3775 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003776 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003777
Adam Cohen5084cba2013-09-03 12:01:16 -07003778 // If there are no screens, we need to have an empty screen
3779 if (orderedScreenIds.size() == 0) {
3780 mWorkspace.addExtraEmptyScreen();
3781 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003782
3783 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003784 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003785 if (hasCustomContentToLeft()) {
3786 mWorkspace.createCustomContentContainer();
3787 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003788 }
Winson Chung64359a52013-07-08 17:17:08 -07003789 }
3790
3791 @Override
3792 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003793 // Log to disk
3794 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3795 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3796 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003797 int count = orderedScreenIds.size();
3798 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003799 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003800 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003801 }
3802
Adam Cohen39a06042013-07-19 14:30:12 -07003803 private boolean shouldShowWeightWatcher() {
3804 String spKey = LauncherAppState.getSharedPreferencesKey();
3805 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003806 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003807
3808 return show;
3809 }
3810
3811 private void toggleShowWeightWatcher() {
3812 String spKey = LauncherAppState.getSharedPreferencesKey();
3813 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3814 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3815
3816 show = !show;
3817
3818 SharedPreferences.Editor editor = sp.edit();
3819 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3820 editor.commit();
3821
3822 if (mWeightWatcher != null) {
3823 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3824 }
3825 }
3826
Winson Chungd64d1762013-08-20 14:37:16 -07003827 public void bindAppsAdded(final ArrayList<Long> newScreens,
3828 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003829 final ArrayList<ItemInfo> addAnimated,
3830 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003831 Runnable r = new Runnable() {
3832 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003833 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003834 }
3835 };
3836 if (waitUntilResume(r)) {
3837 return;
3838 }
3839
Winson Chungd64d1762013-08-20 14:37:16 -07003840 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003841 if (newScreens != null) {
3842 bindAddScreens(newScreens);
3843 }
Winson Chungd64d1762013-08-20 14:37:16 -07003844
3845 // We add the items without animation on non-visible pages, and with
3846 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003847 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003848 bindItems(addNotAnimated, 0,
3849 addNotAnimated.size(), false);
3850 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003851 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003852 bindItems(addAnimated, 0,
3853 addAnimated.size(), true);
3854 }
Winson Chungc58497e2013-09-03 17:48:37 -07003855
Winson Chung87412982013-10-03 18:34:14 -07003856 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003857 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003858
Winson Chungb745afb2015-03-02 11:51:23 -08003859 if (addedApps != null && mAppsView != null) {
3860 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003861 }
Winson Chungd64d1762013-08-20 14:37:16 -07003862 }
3863
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003864 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003865 * Bind the items start-end from the list.
3866 *
3867 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003868 */
Winson Chung64359a52013-07-08 17:17:08 -07003869 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3870 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003871 Runnable r = new Runnable() {
3872 public void run() {
3873 bindItems(shortcuts, start, end, forceAnimateIcons);
3874 }
3875 };
3876 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003877 return;
3878 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003879
Winson Chungf0c6ae02012-03-21 16:10:31 -07003880 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003881 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3882 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003883 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003884 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003885 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003886 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003887 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003888
3889 // Short circuit if we are loading dock items for a configuration which has no dock
3890 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3891 mHotseat == null) {
3892 continue;
3893 }
3894
Joe Onorato9c1289c2009-08-17 11:03:03 -04003895 switch (item.itemType) {
3896 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3897 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003898 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003899 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003900
Winson Chung64359a52013-07-08 17:17:08 -07003901 /*
3902 * TODO: FIX collision case
3903 */
Winson Chungce376632013-07-11 16:12:41 -07003904 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3905 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3906 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003907 View v = cl.getChildAt(item.cellX, item.cellY);
3908 Object tag = v.getTag();
3909 String desc = "Collision while binding workspace item: " + item
3910 + ". Collides with " + tag;
3911 if (LauncherAppState.isDogfoodBuild()) {
3912 throw (new RuntimeException(desc));
3913 } else {
3914 Log.d(TAG, desc);
3915 }
Winson Chungce376632013-07-11 16:12:41 -07003916 }
Winson Chung64359a52013-07-08 17:17:08 -07003917 }
3918
Adam Cohendcd297f2013-06-18 13:13:40 -07003919 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3920 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003921 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003922 // Animate all the applications up now
3923 shortcut.setAlpha(0f);
3924 shortcut.setScaleX(0f);
3925 shortcut.setScaleY(0f);
3926 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003927 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003928 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003929 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003930 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003931 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003932 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003933 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003934 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3935 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003936 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003937 default:
3938 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003940 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003941
Winson Chung997a9232013-07-24 15:33:46 -07003942 if (animateIcons) {
3943 // Animate to the correct page
3944 if (newShortcutsScreenId > -1) {
3945 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003946 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003947 final Runnable startBounceAnimRunnable = new Runnable() {
3948 public void run() {
3949 anim.playTogether(bounceAnims);
3950 anim.start();
3951 }
3952 };
Winson Chung997a9232013-07-24 15:33:46 -07003953 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003954 // We post the animation slightly delayed to prevent slowdowns
3955 // when we are loading right after we return to launcher.
3956 mWorkspace.postDelayed(new Runnable() {
3957 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003958 if (mWorkspace != null) {
3959 mWorkspace.snapToPage(newScreenIndex);
3960 mWorkspace.postDelayed(startBounceAnimRunnable,
3961 NEW_APPS_ANIMATION_DELAY);
3962 }
Winson Chung94d67682013-09-25 16:29:40 -07003963 }
3964 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003965 } else {
3966 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003967 }
3968 }
Winson Chung64359a52013-07-08 17:17:08 -07003969 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003970 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003971 }
3972
Joe Onorato9c1289c2009-08-17 11:03:03 -04003973 /**
3974 * Implementation of the method from LauncherModel.Callbacks.
3975 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003976 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003977 Runnable r = new Runnable() {
3978 public void run() {
3979 bindFolders(folders);
3980 }
3981 };
3982 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003983 return;
3984 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003985 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003986 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987
3988 /**
3989 * Add the views for a widget to the workspace.
3990 *
3991 * Implementation of the method from LauncherModel.Callbacks.
3992 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003993 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003994 Runnable r = new Runnable() {
3995 public void run() {
3996 bindAppWidget(item);
3997 }
3998 };
3999 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004000 return;
4001 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004002
Daniel Sandler843e8602010-06-07 14:59:01 -04004003 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4004 if (DEBUG_WIDGETS) {
4005 Log.d(TAG, "bindAppWidget: " + item);
4006 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004007 final Workspace workspace = mWorkspace;
4008
Adam Cohen59400422014-03-05 18:07:04 -08004009 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00004010 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08004011
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004012 if (!mIsSafeModeEnabled
4013 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4014 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004015 if (appWidgetInfo == null) {
4016 if (DEBUG_WIDGETS) {
4017 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4018 + " belongs to component " + item.providerName
4019 + ", as the povider is null");
4020 }
4021 LauncherModel.deleteItemFromDatabase(this, item);
4022 return;
4023 }
4024 // Note: This assumes that the id remap broadcast is received before this step.
4025 // If that is not the case, the id remap will be ignored and user may see the
4026 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004027 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004028 pendingInfo.spanX = item.spanX;
4029 pendingInfo.spanY = item.spanY;
4030 pendingInfo.minSpanX = item.minSpanX;
4031 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004032 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004033 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004034
Sunny Goyalff572272014-07-23 13:58:07 -07004035 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004036 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4037 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004038
4039 // TODO consider showing a permission dialog when the widget is clicked.
4040 if (!success) {
4041 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4042 if (DEBUG_WIDGETS) {
4043 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4044 + " belongs to component " + item.providerName
4045 + ", as the launcher is unable to bing a new widget id");
4046 }
4047 LauncherModel.deleteItemFromDatabase(this, item);
4048 return;
4049 }
4050
4051 item.appWidgetId = newWidgetId;
4052
4053 // If the widget has a configure activity, it is still needs to set it up, otherwise
4054 // the widget is ready to go.
4055 item.restoreStatus = (appWidgetInfo.configure == null)
4056 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4057 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4058
4059 LauncherModel.updateItemInDatabase(this, item);
4060 }
4061
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004062 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004063 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004064 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004065 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4066 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004067 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004068
Sunny Goyal651077b2014-06-30 14:15:31 -07004069 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4070 } else {
4071 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004072 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4073 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004074 view.updateIcon(mIconCache);
4075 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004076 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004077 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004078 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004079
Joe Onorato9c1289c2009-08-17 11:03:03 -04004080 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004081 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004082
Adam Cohendcd297f2013-06-18 13:13:40 -07004083 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004084 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004085 if (!item.isCustomWidget()) {
4086 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4087 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004088
Joe Onorato9c1289c2009-08-17 11:03:03 -04004089 workspace.requestLayout();
4090
Daniel Sandler843e8602010-06-07 14:59:01 -04004091 if (DEBUG_WIDGETS) {
4092 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4093 + (SystemClock.uptimeMillis()-start) + "ms");
4094 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004095 }
4096
Sunny Goyalff572272014-07-23 13:58:07 -07004097 /**
4098 * Restores a pending widget.
4099 *
4100 * @param appWidgetId The app widget id
4101 * @param cellInfo The position on screen where to create the widget.
4102 */
4103 private void completeRestoreAppWidget(final int appWidgetId) {
4104 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4105 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4106 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4107 return;
4108 }
4109
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004110 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004111 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4112
4113 mWorkspace.reinflateWidgetsIfNecessary();
4114 LauncherModel.updateItemInDatabase(this, info);
4115 }
4116
Adam Cohen1462de32012-07-24 22:34:36 -07004117 public void onPageBoundSynchronously(int page) {
4118 mSynchronouslyBoundPages.add(page);
4119 }
4120
Joe Onorato9c1289c2009-08-17 11:03:03 -04004121 /**
4122 * Callback saying that there aren't any more items to bind.
4123 *
4124 * Implementation of the method from LauncherModel.Callbacks.
4125 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004126 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004127 Runnable r = new Runnable() {
4128 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004129 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004130 }
4131 };
4132 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004133 return;
4134 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004135 if (mSavedState != null) {
4136 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004137 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004138 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004139 mSavedState = null;
4140 }
4141
Adam Cohen1462de32012-07-24 22:34:36 -07004142 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004143
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004144 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004145 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004146
4147 // If we received the result of any pending adds while the loader was running (e.g. the
4148 // widget configuration forced an orientation change), process them now.
4149 if (sPendingAddItem != null) {
4150 final long screenId = completeAdd(sPendingAddItem);
4151
4152 // TODO: this moves the user to the page where the pending item was added. Ideally,
4153 // the screen would be guaranteed to exist after bind, and the page would be set through
4154 // the workspace restore process.
4155 mWorkspace.post(new Runnable() {
4156 @Override
4157 public void run() {
4158 mWorkspace.snapToScreenId(screenId);
4159 }
4160 });
4161 sPendingAddItem = null;
4162 }
4163
Sunny Goyal756adbc2015-04-16 15:20:51 -07004164 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004165
4166 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004167 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004168 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004169 }
4170
Adam Cohen3ed316a2014-07-23 18:21:20 -07004171 private void sendLoadingCompleteBroadcastIfNecessary() {
4172 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4173 String permission =
4174 getResources().getString(R.string.receive_first_load_broadcast_permission);
4175 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4176 sendBroadcast(intent, permission);
4177 SharedPreferences.Editor editor = mSharedPrefs.edit();
4178 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4179 editor.apply();
4180 }
4181 }
4182
Winson Chunga0b7e862013-09-05 16:03:15 -07004183 public boolean isAllAppsButtonRank(int rank) {
4184 if (mHotseat != null) {
4185 return mHotseat.isAllAppsButtonRank(rank);
4186 }
4187 return false;
4188 }
4189
Winson Chunga2413752012-04-03 14:22:34 -07004190 private boolean canRunNewAppsAnimation() {
4191 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4192 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4193 }
4194
Winson Chungc9168342013-06-26 14:54:55 -07004195 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4196 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4197 PropertyValuesHolder.ofFloat("alpha", 1f),
4198 PropertyValuesHolder.ofFloat("scaleX", 1f),
4199 PropertyValuesHolder.ofFloat("scaleY", 1f));
4200 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4201 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4202 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4203 return bounceAnim;
4204 }
4205
Adam Cohen27772732013-11-11 14:00:56 +00004206 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004207 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004208 }
4209
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004210 protected Rect getSearchBarBounds() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004211 return mDeviceProfile.getSearchBarBounds();
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004212 }
4213
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004214 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004215 if (mSearchDropTargetBar == null) {
4216 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004217 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004218 if (mQsb != null) {
4219 mSearchDropTargetBar.removeView(mQsb);
4220 mQsb = null;
4221 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004222 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004223 }
4224
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004225 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004226 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4227 * multiple calls to bind the same list.)
4228 */
4229 @Thunk ArrayList<AppInfo> mTmpAppsList;
4230 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4231 public void run() {
4232 bindAllApplications(mTmpAppsList);
4233 mTmpAppsList = null;
4234 }
4235 };
4236
4237 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004238 * Add the icons for all apps.
4239 *
4240 * Implementation of the method from LauncherModel.Callbacks.
4241 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004242 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004243 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4244 mTmpAppsList = apps;
4245 return;
4246 }
4247
Winson Chungb745afb2015-03-02 11:51:23 -08004248 if (mAppsView != null) {
4249 mAppsView.setApps(apps);
4250 }
Adam Cohen9211d422014-10-07 18:14:53 -07004251 if (mLauncherCallbacks != null) {
4252 mLauncherCallbacks.bindAllApplications(apps);
4253 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004254 }
4255
4256 /**
4257 * A package was updated.
4258 *
4259 * Implementation of the method from LauncherModel.Callbacks.
4260 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004261 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004262 Runnable r = new Runnable() {
4263 public void run() {
4264 bindAppsUpdated(apps);
4265 }
4266 };
4267 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004268 return;
4269 }
4270
Winson Chungb745afb2015-03-02 11:51:23 -08004271 if (mAppsView != null) {
4272 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004273 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004274 }
4275
Sunny Goyal4390ace2014-10-13 11:33:11 -07004276 @Override
4277 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4278 Runnable r = new Runnable() {
4279 public void run() {
4280 bindWidgetsRestored(widgets);
4281 }
4282 };
4283 if (waitUntilResume(r)) {
4284 return;
4285 }
4286 mWorkspace.widgetsRestored(widgets);
4287 }
4288
Joe Onorato9c1289c2009-08-17 11:03:03 -04004289 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004290 * Some shortcuts were updated in the background.
4291 *
4292 * Implementation of the method from LauncherModel.Callbacks.
4293 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004294 @Override
4295 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4296 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004297 Runnable r = new Runnable() {
4298 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004299 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004300 }
4301 };
4302 if (waitUntilResume(r)) {
4303 return;
4304 }
4305
Sunny Goyal4390ace2014-10-13 11:33:11 -07004306 if (!updated.isEmpty()) {
4307 mWorkspace.updateShortcuts(updated);
4308 }
4309
4310 if (!removed.isEmpty()) {
4311 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4312 for (ShortcutInfo si : removed) {
4313 removedComponents.add(si.getTargetComponent());
4314 }
4315 mWorkspace.removeItemsByComponentName(removedComponents, user);
4316 // Notify the drag controller
4317 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004318 }
4319 }
4320
4321 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004322 * Update the state of a package, typically related to install state.
4323 *
4324 * Implementation of the method from LauncherModel.Callbacks.
4325 */
Sunny Goyale755d462014-07-22 13:48:29 -07004326 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004327 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4328 Runnable r = new Runnable() {
4329 public void run() {
4330 bindRestoreItemsChange(updates);
4331 }
4332 };
4333 if (waitUntilResume(r)) {
4334 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004335 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004336
Sunny Goyal756adbc2015-04-16 15:20:51 -07004337 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004338 }
4339
4340 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004341 * A package was uninstalled. We take both the super set of packageNames
4342 * in addition to specific applications to remove, the reason being that
4343 * this can be called when a package is updated as well. In that scenario,
4344 * we only remove specific components from the workspace, where as
4345 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004346 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004347 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004348 * Implementation of the method from LauncherModel.Callbacks.
4349 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004350 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004351 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004352 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004353 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004354 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004355 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004356 }
Winson Chungd64d1762013-08-20 14:37:16 -07004357 };
4358 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004359 return;
4360 }
4361
Sunny Goyal1a745e82014-10-02 15:58:31 -07004362 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004363 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4364 for (AppInfo info : appInfos) {
4365 removedComponents.add(info.componentName);
4366 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004367 if (!packageNames.isEmpty()) {
4368 mWorkspace.removeItemsByPackageName(packageNames, user);
4369 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004370 if (!removedComponents.isEmpty()) {
4371 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004372 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004373 // Notify the drag controller
4374 mDragController.onAppsRemoved(packageNames, removedComponents);
4375
Sunny Goyal1a745e82014-10-02 15:58:31 -07004376 } else {
4377 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004378 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004379
Winson Chungdf95eb12013-10-16 14:57:07 -07004380 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004381 if (mAppsView != null) {
4382 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004383 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004384 }
Joe Onoratobe386092009-11-17 17:32:16 -08004385
Adam Cohen091440a2015-03-18 14:16:05 -07004386 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004387 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004388 public void run() {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004389 bindAllPackages(mWidgetsAndShortcuts);
Winson Chung83892cc2013-05-01 16:53:33 -07004390 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004391 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004392
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004393 @Override
4394 public void bindAllPackages(final ArrayList<Object> widgetsAndShortcuts) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004395 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4396 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004397 return;
4398 }
4399
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004400 if (mWidgetsView != null && widgetsAndShortcuts != null) {
4401 mWidgetsView.addWidgets(widgetsAndShortcuts, getPackageManager());
4402 mWidgetsAndShortcuts = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004403 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004404 }
4405
Winson Chung400438b2011-01-16 17:53:48 -08004406 private int mapConfigurationOriActivityInfoOri(int configOri) {
4407 final Display d = getWindowManager().getDefaultDisplay();
4408 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4409 switch (d.getRotation()) {
4410 case Surface.ROTATION_0:
4411 case Surface.ROTATION_180:
4412 // We are currently in the same basic orientation as the natural orientation
4413 naturalOri = configOri;
4414 break;
4415 case Surface.ROTATION_90:
4416 case Surface.ROTATION_270:
4417 // We are currently in the other basic orientation to the natural orientation
4418 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4419 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4420 break;
4421 }
4422
4423 int[] oriMap = {
4424 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4425 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4426 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4427 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4428 };
4429 // Since the map starts at portrait, we need to offset if this device's natural orientation
4430 // is landscape.
4431 int indexOffset = 0;
4432 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4433 indexOffset = 1;
4434 }
4435 return oriMap[(d.getRotation() + indexOffset) % 4];
4436 }
Adam Cohen446e9402011-09-15 18:21:21 -07004437
Winson Chung4b919f82012-05-01 10:44:08 -07004438 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004439 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004440 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4441 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4442 .getConfiguration().orientation));
4443 } else {
4444 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4445 }
Winson Chung4b919f82012-05-01 10:44:08 -07004446 }
4447 }
4448 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004449 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004450 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004451 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004452 } else {
4453 mHandler.postDelayed(new Runnable() {
4454 public void run() {
4455 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4456 }
4457 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004458 }
Winson Chung4b919f82012-05-01 10:44:08 -07004459 }
Winson Chung400438b2011-01-16 17:53:48 -08004460 }
4461
Winson Chunge43a1e72014-01-15 10:33:02 -08004462 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004463 if (mLauncherCallbacks != null) {
4464 return mLauncherCallbacks.isLauncherPreinstalled();
4465 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004466 PackageManager pm = getPackageManager();
4467 try {
4468 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4469 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4470 return true;
4471 } else {
4472 return false;
4473 }
4474 } catch (NameNotFoundException e) {
4475 e.printStackTrace();
4476 return false;
4477 }
4478 }
4479
Adam Cohenea90f832014-05-21 15:03:34 -07004480 /**
4481 * This method indicates whether or not we should suggest default wallpaper dimensions
4482 * when our wallpaper cropper was not yet used to set a wallpaper.
4483 */
4484 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004485 if (mLauncherCallbacks != null) {
4486 return mLauncherCallbacks.overrideWallpaperDimensions();
4487 }
Adam Cohenea90f832014-05-21 15:03:34 -07004488 return true;
4489 }
4490
Adam Cohen432609a2014-03-13 17:03:22 -07004491 /**
4492 * To be overridden by subclasses to indicate that there is an activity to launch
4493 * before showing the standard launcher experience.
4494 */
4495 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004496 if (mLauncherCallbacks != null) {
4497 return mLauncherCallbacks.hasFirstRunActivity();
4498 }
Adam Cohen432609a2014-03-13 17:03:22 -07004499 return false;
4500 }
4501
4502 /**
4503 * To be overridden by subclasses to launch any first run activity
4504 */
4505 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004506 if (mLauncherCallbacks != null) {
4507 return mLauncherCallbacks.getFirstRunActivity();
4508 }
Adam Cohen432609a2014-03-13 17:03:22 -07004509 return null;
4510 }
4511
Winson Chung2826a402015-04-17 16:18:53 -07004512 /**
4513 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004514 */
4515 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004516 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4517 return false;
4518 }
4519
Winson Chung2826a402015-04-17 16:18:53 -07004520 if (mLauncherCallbacks != null) {
4521 return mLauncherCallbacks.overrideAllAppsSearch();
4522 }
4523 return false;
4524 }
4525
Winson Chunga6945242014-01-08 14:04:34 -08004526 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004527 return !ActivityManager.isRunningInTestHarness() &&
4528 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004529 }
4530
Adam Cohen4a9423d2014-03-28 14:22:31 -07004531 protected boolean hasRunFirstRunActivity() {
4532 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4533 }
4534
Adam Cohen432609a2014-03-13 17:03:22 -07004535 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004536 if (shouldRunFirstRunActivity() &&
4537 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004538 Intent firstRunIntent = getFirstRunActivity();
4539 if (firstRunIntent != null) {
4540 startActivity(firstRunIntent);
4541 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004542 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004543 }
4544 }
Adam Cohen432609a2014-03-13 17:03:22 -07004545 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004546 }
4547
4548 private void markFirstRunActivityShown() {
4549 SharedPreferences.Editor editor = mSharedPrefs.edit();
4550 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4551 editor.apply();
4552 }
4553
Adam Cohen432609a2014-03-13 17:03:22 -07004554 /**
4555 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4556 * screen that must be displayed and dismissed.
4557 */
4558 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004559 if (mLauncherCallbacks != null) {
4560 return mLauncherCallbacks.hasDismissableIntroScreen();
4561 }
Adam Cohen432609a2014-03-13 17:03:22 -07004562 return false;
4563 }
4564
4565 /**
4566 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4567 */
4568 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004569 if (mLauncherCallbacks != null) {
4570 return mLauncherCallbacks.getIntroScreen();
4571 }
Adam Cohen432609a2014-03-13 17:03:22 -07004572 return null;
4573 }
4574
4575 /**
4576 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4577 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4578 */
4579 private boolean shouldShowIntroScreen() {
4580 return hasDismissableIntroScreen() &&
4581 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4582 }
4583
4584 protected void showIntroScreen() {
4585 View introScreen = getIntroScreen();
4586 changeWallpaperVisiblity(false);
4587 if (introScreen != null) {
4588 mDragLayer.showOverlayView(introScreen);
4589 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004590 if (mLauncherOverlayContainer != null) {
4591 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4592 }
Adam Cohen432609a2014-03-13 17:03:22 -07004593 }
4594
4595 public void dismissIntroScreen() {
4596 markIntroScreenDismissed();
4597 if (showFirstRunActivity()) {
4598 // We delay hiding the intro view until the first run activity is showing. This
4599 // avoids a blip.
4600 mWorkspace.postDelayed(new Runnable() {
4601 @Override
4602 public void run() {
4603 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004604 if (mLauncherOverlayContainer != null) {
4605 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4606 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004607 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004608 }
4609 }, ACTIVITY_START_DELAY);
4610 } else {
4611 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004612 if (mLauncherOverlayContainer != null) {
4613 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4614 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004615 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004616 }
4617 changeWallpaperVisiblity(true);
4618 }
4619
4620 private void markIntroScreenDismissed() {
4621 SharedPreferences.Editor editor = mSharedPrefs.edit();
4622 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4623 editor.apply();
4624 }
4625
Adam Cohen091440a2015-03-18 14:16:05 -07004626 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004627 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4628 // on the device, then we always show the first run cling experience (or if there is no
4629 // launcher2). Otherwise, we prompt the user upon started for migration
4630 LauncherClings launcherClings = new LauncherClings(this);
4631 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4632 if (mModel.canMigrateFromOldLauncherDb(this)) {
4633 launcherClings.showMigrationCling();
4634 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004635 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004636 }
4637 }
4638 }
4639
Winson Chunga6945242014-01-08 14:04:34 -08004640 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004641 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4642 if (mHotseat != null) mHotseat.setAlpha(1f);
4643 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4644 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004645 }
4646
4647 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004648 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4649 if (mHotseat != null) mHotseat.setAlpha(0f);
4650 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4651 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004652 }
4653
Mathew Inwood72fbec12013-11-19 15:45:07 +00004654 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004655 // Called from search suggestion, not supported in other profiles.
4656 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4657 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4658 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4659 myUser);
4660 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004661 return null;
4662 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004663 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004664 }
4665
4666 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4667 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004668 // Called from search suggestion, not supported in other profiles.
4669 return createShortcutDragInfo(shortcutIntent, caption, icon,
4670 UserHandleCompat.myUserHandle());
4671 }
4672
4673 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4674 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004675 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004676 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004677 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004678 }
4679
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004680 protected void moveWorkspaceToDefaultScreen() {
4681 mWorkspace.moveToDefaultScreen(false);
4682 }
4683
Mathew Inwood72fbec12013-11-19 15:45:07 +00004684 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4685 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004686 mWorkspace.onExternalDragStartedWithItem(dragView);
4687 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004688 }
4689
Anjali Koppalf5d29132014-02-28 13:33:27 -08004690 @Override
4691 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004692 if (mLauncherCallbacks != null) {
4693 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4694 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004695 }
4696
Winson Chung80baf5a2010-08-09 16:03:15 -07004697 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004698 * Prints out out state for debugging.
4699 */
4700 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004701 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004702 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004703 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4704 Log.d(TAG, "mRestoring=" + mRestoring);
4705 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4706 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004707 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004708 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004709 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004710
Daniel Sandler325dc232013-06-05 22:57:57 -04004711 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004712 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004713
4714 @Override
4715 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4716 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004717 synchronized (sDumpLogs) {
4718 writer.println(" ");
4719 writer.println("Debug logs: ");
4720 for (int i = 0; i < sDumpLogs.size(); i++) {
4721 writer.println(" " + sDumpLogs.get(i));
4722 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004723 }
Adam Cohen9211d422014-10-07 18:14:53 -07004724 if (mLauncherCallbacks != null) {
4725 mLauncherCallbacks.dump(prefix, fd, writer, args);
4726 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004727 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004728
4729 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004730 if (DEBUG_DUMP_LOG) {
4731 synchronized (sDumpLogs) {
4732 Log.d(TAG, "");
4733 Log.d(TAG, "*********************");
4734 Log.d(TAG, "Launcher debug logs: ");
4735 for (int i = 0; i < sDumpLogs.size(); i++) {
4736 Log.d(TAG, " " + sDumpLogs.get(i));
4737 }
4738 Log.d(TAG, "*********************");
4739 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004740 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004741 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004742 }
4743
4744 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004745 addDumpLog(tag, log, null, debugLog);
4746 }
4747
4748 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004749 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004750 if (e != null) {
4751 Log.d(tag, log, e);
4752 } else {
4753 Log.d(tag, log);
4754 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004755 }
Winson Chungede41292013-09-19 16:27:36 -07004756 if (DEBUG_DUMP_LOG) {
4757 sDateStamp.setTime(System.currentTimeMillis());
4758 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004759 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4760 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004761 }
Winson Chungede41292013-09-19 16:27:36 -07004762 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004763 }
4764
Adam Cohen59400422014-03-05 18:07:04 -08004765 public static CustomAppWidget getCustomAppWidget(String name) {
4766 return sCustomAppWidgets.get(name);
4767 }
4768
4769 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4770 return sCustomAppWidgets;
4771 }
4772
Winson Chungede41292013-09-19 16:27:36 -07004773 public void dumpLogsToLocalData() {
4774 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004775 new AsyncTask<Void, Void, Void>() {
4776 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004777 boolean success = false;
4778 sDateStamp.setTime(sRunStart);
4779 String FILENAME = sDateStamp.getMonth() + "-"
4780 + sDateStamp.getDay() + "_"
4781 + sDateStamp.getHours() + "-"
4782 + sDateStamp.getMinutes() + "_"
4783 + sDateStamp.getSeconds() + ".txt";
4784
4785 FileOutputStream fos = null;
4786 File outFile = null;
4787 try {
4788 outFile = new File(getFilesDir(), FILENAME);
4789 outFile.createNewFile();
4790 fos = new FileOutputStream(outFile);
4791 } catch (Exception e) {
4792 e.printStackTrace();
4793 }
4794 if (fos != null) {
4795 PrintWriter writer = new PrintWriter(fos);
4796
4797 writer.println(" ");
4798 writer.println("Debug logs: ");
4799 synchronized (sDumpLogs) {
4800 for (int i = 0; i < sDumpLogs.size(); i++) {
4801 writer.println(" " + sDumpLogs.get(i));
4802 }
4803 }
4804 writer.close();
4805 }
4806 try {
4807 if (fos != null) {
4808 fos.close();
4809 success = true;
4810 }
4811 } catch (IOException e) {
4812 e.printStackTrace();
4813 }
Michael Jurka43467462013-11-14 12:03:58 +01004814 return null;
Winson Chungede41292013-09-19 16:27:36 -07004815 }
Michael Jurka43467462013-11-14 12:03:58 +01004816 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004817 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004818 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004819}
Michael Jurka2763be32011-02-24 11:19:57 -08004820
Michael Jurkaabded662011-03-04 12:06:57 -08004821interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004822 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004823 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004824 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004825 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004826 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004827}
Dan Sandlerd5024042014-01-09 15:01:33 -05004828
4829interface DebugIntents {
4830 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4831 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004832}