blob: a289fce8742b68225f4d9f4cc827bbad7dce7668 [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;
Adam Cohen0b395352014-06-09 22:54:36 +000025import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070028import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040040import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070043import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080046import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020060import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020062import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070064import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070065import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
Winson Chung4ac30062015-05-08 17:34:17 -070067import android.preference.PreferenceManager;
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 ||
812 requestCode == REQUEST_CREATE_APPWIDGET);
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);
1380 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001381 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001382 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 mRestoring = true;
1384 }
1385
1386 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1387 if (renameFolder) {
1388 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001389 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 mRestoring = true;
1391 }
Winson Chunga12a2502010-12-20 14:41:35 -08001392
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001393 mItemIdToViewId = (HashMap<Integer, Integer>)
1394 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 }
1396
1397 /**
1398 * Finds all the views we need and configure them properly.
1399 */
1400 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001401 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001402
Craig Mautner360310b2012-10-26 15:13:08 -07001403 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001404 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001405 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1406 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001407 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001408 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001409
John Spurlock77e1f472013-09-11 10:09:51 -04001410 mLauncherView.setSystemUiVisibility(
1411 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001412 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413
Winson Chung4c98d922011-05-31 16:50:48 -07001414 // Setup the drag layer
1415 mDragLayer.setup(this, dragController);
1416
Winson Chung3d503fb2011-07-13 17:25:49 -07001417 // Setup the hotseat
1418 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1419 if (mHotseat != null) {
1420 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001421 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001422 }
1423
Jorim Jaggid017f882014-01-14 17:08:48 -08001424 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001425 View widgetButton = findViewById(R.id.widget_button);
1426 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001427 @Override
1428 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001429 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001430 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001431 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001432 }
1433 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001434 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1435
1436 View wallpaperButton = findViewById(R.id.wallpaper_button);
1437 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001438 @Override
1439 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001440 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001441 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001442 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001443 }
1444 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001445 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1446
1447 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001448 if (hasSettings()) {
1449 settingsButton.setOnClickListener(new OnClickListener() {
1450 @Override
1451 public void onClick(View arg0) {
1452 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001453 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001454 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001455 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001456 });
1457 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1458 } else {
1459 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001460 }
1461
Adam Cohendbdff6b2013-09-18 19:09:15 -07001462 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001463
Winson Chung4c98d922011-05-31 16:50:48 -07001464 // Setup the workspace
1465 mWorkspace.setHapticFeedbackEnabled(false);
1466 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001467 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001468 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001469
Winson Chungf0ea4d32011-06-06 14:27:16 -07001470 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001471 mSearchDropTargetBar = (SearchDropTargetBar)
1472 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001473
Winson Chungb745afb2015-03-02 11:51:23 -08001474 // Setup Apps
1475 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001476 if (isAllAppsSearchOverridden()) {
1477 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001478 }
Winson Chungb745afb2015-03-02 11:51:23 -08001479
Winson Chungf0ea4d32011-06-06 14:27:16 -07001480 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001481 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001482
Winson Chung3d503fb2011-07-13 17:25:49 -07001483 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001484 dragController.setDragScoller(mWorkspace);
1485 dragController.setScrollView(mDragLayer);
1486 dragController.setMoveTarget(mWorkspace);
1487 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001488 if (mSearchDropTargetBar != null) {
1489 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001490 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001491 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001492
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001493 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001494 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001495 mWeightWatcher = new WeightWatcher(this);
1496 mWeightWatcher.setAlpha(0.5f);
1497 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001498 new FrameLayout.LayoutParams(
1499 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001500 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001501 Gravity.BOTTOM)
1502 );
Adam Cohen39a06042013-07-19 14:30:12 -07001503
1504 boolean show = shouldShowWeightWatcher();
1505 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001506 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
1508
1509 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001510 * Sets the all apps button. This method is called from {@link Hotseat}.
1511 */
1512 public void setAllAppsButton(View allAppsButton) {
1513 mAllAppsButton = allAppsButton;
1514 }
1515
1516 public View getAllAppsButton() {
1517 return mAllAppsButton;
1518 }
1519
1520 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 * Creates a view representing a shortcut.
1522 *
1523 * @param info The data structure describing the shortcut.
1524 *
1525 * @return A View inflated from R.layout.application.
1526 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001527 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001528 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 }
1530
1531 /**
1532 * Creates a view representing a shortcut inflated from the specified resource.
1533 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 * @param parent The group the shortcut belongs to.
1535 * @param info The data structure describing the shortcut.
1536 *
1537 * @return A View inflated from layoutResId.
1538 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001539 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
1540 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.application,
1541 parent, false);
1542 favorite.applyFromShortcutInfo(info, mIconCache);
1543 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001545 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 return favorite;
1547 }
1548
1549 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 * Add a shortcut to the workspace.
1551 *
1552 * @param data The intent describing the shortcut.
1553 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001555 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001556 int cellY) {
1557 int[] cellXY = mTmpAddItemCellCoordinates;
1558 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001559 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001560
Michael Jurkad3ef3062010-11-23 16:23:58 -08001561 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001562
Sunny Goyal2350bc92014-10-14 16:42:54 -07001563 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001564 if (info == null) {
1565 return;
1566 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001567 final View view = createShortcut(info);
1568
Adam Cohenfbba09b2011-07-18 21:43:05 -07001569 // First we check if we already know the exact location where we want to add this item.
1570 if (cellX >= 0 && cellY >= 0) {
1571 cellXY[0] = cellX;
1572 cellXY[1] = cellY;
1573 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001574
1575 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001576 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001577 true, null,null)) {
1578 return;
1579 }
1580 DragObject dragObject = new DragObject();
1581 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001582 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1583 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001584 return;
1585 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001586 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001587 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001588 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001589 foundCellSpan = (result != null);
1590 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001591 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001592 }
1593
1594 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001595 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001596 return;
1597 }
1598
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001599 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600
1601 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001602 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001603 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 }
1605 }
1606
Adam Cohen2f093b62012-04-30 18:59:53 -07001607 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1608 int minHeight) {
1609 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001610 // We want to account for the extra amount of padding that we are adding to the widget
1611 // to ensure that it gets the full amount of space that it has requested
1612 int requiredWidth = minWidth + padding.left + padding.right;
1613 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001614 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001615 }
1616
Adam Cohen2f093b62012-04-30 18:59:53 -07001617 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1618 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001619 }
1620
Adam Cohen2f093b62012-04-30 18:59:53 -07001621 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1622 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001623 }
1624
Adam Cohen2f093b62012-04-30 18:59:53 -07001625 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1626 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001627 }
1628
Adam Cohen2f093b62012-04-30 18:59:53 -07001629 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1630 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001631 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001632 }
1633
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001635 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001637 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 */
Adam Cohen091440a2015-03-18 14:16:05 -07001639 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001640 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1641
1642 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001643 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001644 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1645 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001646 }
Romain Guycbb89e42009-06-08 15:52:54 -07001647
Adam Cohen59400422014-03-05 18:07:04 -08001648 if (appWidgetInfo.isCustomWidget) {
1649 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001650 }
1651
Adam Cohen59400422014-03-05 18:07:04 -08001652 LauncherAppWidgetInfo launcherInfo;
1653 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1654 launcherInfo.spanX = info.spanX;
1655 launcherInfo.spanY = info.spanY;
1656 launcherInfo.minSpanX = info.minSpanX;
1657 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001658 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001659
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001661 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662
1663 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001664 if (hostView == null) {
1665 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001666 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1667 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001668 } else {
1669 // The AppWidgetHostView has already been inflated and instantiated
1670 launcherInfo.hostView = hostView;
1671 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001673 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001674 launcherInfo.notifyWidgetSizeChanged(this);
1675
Adam Cohen59400422014-03-05 18:07:04 -08001676 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1677 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001678
1679 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001681 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 }
Romain Guycbb89e42009-06-08 15:52:54 -07001683
Adam Cohended9f8d2010-11-03 13:25:16 -07001684 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1685 @Override
1686 public void onReceive(Context context, Intent intent) {
1687 final String action = intent.getAction();
1688 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1689 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001690 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001691 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001692
Winson Chungc100e8e2011-08-09 16:02:43 -07001693 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001694 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001695 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001696 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001697 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001698 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001699 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1700 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001701 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001702 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1703 mModel.resetLoadedState(false, true);
1704 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1705 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1706 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1707 mModel.resetLoadedState(false, true);
1708 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1709 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1710 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 }
1712 }
1713 };
1714
1715 @Override
1716 public void onAttachedToWindow() {
1717 super.onAttachedToWindow();
1718
1719 // Listen for broadcasts related to user-presence
1720 final IntentFilter filter = new IntentFilter();
1721 filter.addAction(Intent.ACTION_SCREEN_OFF);
1722 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001723 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001724 if (ENABLE_DEBUG_INTENTS) {
1725 filter.addAction(DebugIntents.DELETE_DATABASE);
1726 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1727 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001728 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001729 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001730 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001731 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 mVisible = true;
1733 }
1734
Adam Cohen0b395352014-06-09 22:54:36 +00001735 /**
1736 * Sets up transparent navigation and status bars in LMP.
1737 * This method is a no-op for other platform versions.
1738 */
Sunny Goyald0091012015-01-20 15:55:34 -08001739 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001740 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001741 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001742 Window window = getWindow();
1743 window.getAttributes().systemUiVisibility |=
1744 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1745 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1746 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1747 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1748 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1749 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1750 window.setStatusBarColor(Color.TRANSPARENT);
1751 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001752 }
1753 }
1754
Adam Cohended9f8d2010-11-03 13:25:16 -07001755 @Override
1756 public void onDetachedFromWindow() {
1757 super.onDetachedFromWindow();
1758 mVisible = false;
1759
Adam Cohend113e0c2010-11-11 10:48:05 -08001760 if (mAttached) {
1761 unregisterReceiver(mReceiver);
1762 mAttached = false;
1763 }
Winson Chungb745afb2015-03-02 11:51:23 -08001764 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 }
1766
1767 public void onWindowVisibilityChanged(int visibility) {
1768 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001769 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001770 // The following code used to be in onResume, but it turns out onResume is called when
1771 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1772 // is a more appropriate event to handle
1773 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001774 if (!mWorkspaceLoading) {
1775 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001776 // We want to let Launcher draw itself at least once before we force it to build
1777 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001778 // apps is nice and speedy.
1779 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001780 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001781 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001782 if (mStarted) return;
1783 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001784 // We delay the layer building a bit in order to give
1785 // other message processing a time to run. In particular
1786 // this avoids a delay in hiding the IME if it was
1787 // currently shown, because doing that may involve
1788 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001789 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001790 final ViewTreeObserver.OnDrawListener listener = this;
1791 mWorkspace.post(new Runnable() {
1792 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001793 if (mWorkspace != null &&
1794 mWorkspace.getViewTreeObserver() != null) {
1795 mWorkspace.getViewTreeObserver().
1796 removeOnDrawListener(listener);
1797 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001798 }
1799 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001800 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001801 }
1802 });
1803 }
1804 clearTypedText();
1805 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001806 }
1807
Adam Cohen091440a2015-03-18 14:16:05 -07001808 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001809 mHandler.removeMessages(ADVANCE_MSG);
1810 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1811 mHandler.sendMessageDelayed(msg, delay);
1812 mAutoAdvanceSentTime = System.currentTimeMillis();
1813 }
1814
Adam Cohen091440a2015-03-18 14:16:05 -07001815 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001816 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1817 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1818 mAutoAdvanceRunning = autoAdvanceRunning;
1819 if (autoAdvanceRunning) {
1820 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1821 sendAdvanceMessage(delay);
1822 } else {
1823 if (!mWidgetsToAdvance.isEmpty()) {
1824 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1825 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1826 }
1827 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001828 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001829 }
1830 }
1831 }
1832
1833 private final Handler mHandler = new Handler() {
1834 @Override
1835 public void handleMessage(Message msg) {
1836 if (msg.what == ADVANCE_MSG) {
1837 int i = 0;
1838 for (View key: mWidgetsToAdvance.keySet()) {
1839 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1840 final int delay = mAdvanceStagger * i;
1841 if (v instanceof Advanceable) {
1842 postDelayed(new Runnable() {
1843 public void run() {
1844 ((Advanceable) v).advance();
1845 }
1846 }, delay);
1847 }
1848 i++;
1849 }
1850 sendAdvanceMessage(mAdvanceInterval);
1851 }
1852 }
1853 };
1854
1855 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001856 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001857 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1858 if (v instanceof Advanceable) {
1859 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001860 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001861 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001862 }
1863 }
1864
1865 void removeWidgetToAutoAdvance(View hostView) {
1866 if (mWidgetsToAdvance.containsKey(hostView)) {
1867 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001868 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001869 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001870 }
1871
Joe Onorato9c1289c2009-08-17 11:03:03 -04001872 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001873 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001874 launcherInfo.hostView = null;
1875 }
1876
Hyunyoung Song3f471442015-04-08 19:01:34 -07001877 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001878 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1879 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001880 }
1881
Winson Chunga6945242014-01-08 14:04:34 -08001882 public DragLayer getDragLayer() {
1883 return mDragLayer;
1884 }
1885
Winson Chungb745afb2015-03-02 11:51:23 -08001886 public AppsContainerView getAppsView() {
1887 return mAppsView;
1888 }
1889
Hyunyoung Song3f471442015-04-08 19:01:34 -07001890 public WidgetsContainerView getWidgetsView() {
1891 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001892 }
1893
Winson Chunga6945242014-01-08 14:04:34 -08001894 public Workspace getWorkspace() {
1895 return mWorkspace;
1896 }
1897
1898 public Hotseat getHotseat() {
1899 return mHotseat;
1900 }
1901
Jorim Jaggid017f882014-01-14 17:08:48 -08001902 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001903 return mOverviewPanel;
1904 }
1905
1906 public SearchDropTargetBar getSearchBar() {
1907 return mSearchDropTargetBar;
1908 }
1909
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001910 public LauncherAppWidgetHost getAppWidgetHost() {
1911 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
Romain Guycbb89e42009-06-08 15:52:54 -07001913
Winson Chunga9abd0e2010-10-27 17:18:37 -07001914 public LauncherModel getModel() {
1915 return mModel;
1916 }
1917
Winson Chunga6945242014-01-08 14:04:34 -08001918 protected SharedPreferences getSharedPrefs() {
1919 return mSharedPrefs;
1920 }
1921
Bjorn Bringertc459e522013-06-07 19:36:01 +01001922 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001923 getWindow().closeAllPanels();
1924
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001925 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001926 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001927 }
1928
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 @Override
1930 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001931 long startTime = 0;
1932 if (DEBUG_RESUME_TIME) {
1933 startTime = System.currentTimeMillis();
1934 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 super.onNewIntent(intent);
1936
1937 // Close the menu
1938 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001939 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001940 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001941
Adam Cohened307df2013-10-02 09:37:31 -07001942 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1943 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1944 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001945
Adam Cohen6fecd412013-10-02 17:41:50 -07001946 if (mWorkspace == null) {
1947 // Can be cases where mWorkspace is null, this prevents a NPE
1948 return;
1949 }
1950 Folder openFolder = mWorkspace.getOpenFolder();
1951 // In all these cases, only animate if we're already on home
1952 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001953
1954 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1955 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001956 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001957 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001958 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001959 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001960
Adam Cohen6fecd412013-10-02 17:41:50 -07001961 closeFolder();
1962 exitSpringLoadedDragMode();
1963
1964 // If we are already on home, then just animate back to the workspace,
1965 // otherwise, just wait until onResume to set the state back to Workspace
1966 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001967 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001968 } else {
1969 mOnResumeState = State.WORKSPACE;
1970 }
1971
1972 final View v = getWindow().peekDecorView();
1973 if (v != null && v.getWindowToken() != null) {
1974 InputMethodManager imm = (InputMethodManager)getSystemService(
1975 INPUT_METHOD_SERVICE);
1976 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1977 }
1978
Winson Chungb745afb2015-03-02 11:51:23 -08001979 // Reset the apps view
1980 if (!alreadyOnHome && mAppsView != null) {
1981 mAppsView.scrollToTop();
1982 }
1983
Hyunyoung Song3f471442015-04-08 19:01:34 -07001984 // Reset the widgets view
1985 if (!alreadyOnHome && mWidgetsView != null) {
1986 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001987 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001988
Adam Cohen9211d422014-10-07 18:14:53 -07001989 if (mLauncherCallbacks != null) {
1990 mLauncherCallbacks.onHomeIntent();
1991 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 }
Adam Cohened307df2013-10-02 09:37:31 -07001993
Michael Jurka447bf842013-05-15 14:52:15 +02001994 if (DEBUG_RESUME_TIME) {
1995 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1996 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997
Adam Cohen9211d422014-10-07 18:14:53 -07001998 if (mLauncherCallbacks != null) {
1999 mLauncherCallbacks.onNewIntent(intent);
2000 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002001 }
2002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07002004 public void onRestoreInstanceState(Bundle state) {
2005 super.onRestoreInstanceState(state);
2006 for (int page: mSynchronouslyBoundPages) {
2007 mWorkspace.restoreInstanceStateForChild(page);
2008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010
2011 @Override
2012 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002013 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002014 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2015 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002016 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002017 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018
Michael Jurka883f55b2010-10-21 15:47:14 -07002019 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002020 // We close any open folder since it will not be re-opened, and we need to make sure
2021 // this state is reflected.
2022 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023
Adam Cohendcd297f2013-06-18 13:13:40 -07002024 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002025 mWaitingForResult) {
2026 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002027 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002028 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2029 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002030 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2031 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2032 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002033 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
2035
2036 if (mFolderInfo != null && mWaitingForResult) {
2037 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2038 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2039 }
Michael Jurka946ad472010-07-09 18:05:18 -07002040
Hyunyoung Song3f471442015-04-08 19:01:34 -07002041 // Save the current widgets tray?
2042 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002043 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002044
2045 if (mLauncherCallbacks != null) {
2046 mLauncherCallbacks.onSaveInstanceState(outState);
2047 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 }
2049
2050 @Override
2051 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002053
Winson Chunge7a03942011-08-05 15:05:12 -07002054 // Remove all pending runnables
2055 mHandler.removeMessages(ADVANCE_MSG);
2056 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002057 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002058
Winson Chungcd2b0142011-06-08 16:02:26 -07002059 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002060 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002061
2062 // It's possible to receive onDestroy after a new Launcher activity has
2063 // been created. In this case, don't interfere with the new Launcher.
2064 if (mModel.isCurrentCallbacks(this)) {
2065 mModel.stopLoader();
2066 app.setLauncher(null);
2067 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002070 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002072 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002074 mAppWidgetHost = null;
2075
2076 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077
2078 TextKeyListener.getInstance().release();
2079
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002080 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002081 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002082
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002083 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002084 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002085 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002086 mWorkspace = null;
2087 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002088
Michael Jurka2ecf9952012-06-18 12:52:28 -07002089 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002090
2091 if (mLauncherCallbacks != null) {
2092 mLauncherCallbacks.onDestroy();
2093 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 }
2095
Adam Cohena9cf38f2011-05-02 15:36:58 -07002096 public DragController getDragController() {
2097 return mDragController;
2098 }
2099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 @Override
2101 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002102 onStartForResult(requestCode);
2103 super.startActivityForResult(intent, requestCode);
2104 }
2105
2106 @Override
2107 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2108 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2109 onStartForResult(requestCode);
2110 try {
2111 super.startIntentSenderForResult(intent, requestCode,
2112 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2113 } catch (IntentSender.SendIntentException e) {
2114 throw new ActivityNotFoundException();
2115 }
2116 }
2117
2118 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002119 if (requestCode >= 0) {
2120 setWaitingForResult(true);
2121 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 }
2123
Winson Chung70d72102011-08-12 11:24:35 -07002124 /**
2125 * Indicates that we want global search for this activity by setting the globalSearch
2126 * argument for {@link #startSearch} to true.
2127 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002129 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002131
Karl Rosaen138a0412009-04-23 19:00:21 -07002132 if (initialQuery == null) {
2133 // Use any text typed in the launcher as the initial query
2134 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002135 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 if (appSearchData == null) {
2137 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002138 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 }
Winson Chungadf0c182012-08-23 14:59:07 -07002140 Rect sourceBounds = new Rect();
2141 if (mSearchDropTargetBar != null) {
2142 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2143 }
Romain Guycbb89e42009-06-08 15:52:54 -07002144
Ian Parkinson047036e2014-09-01 15:40:53 +01002145 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002146 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002147 if (clearTextImmediately) {
2148 clearTypedText();
2149 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002150
2151 // We need to show the workspace after starting the search
2152 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002153 }
2154
Ian Parkinson047036e2014-09-01 15:40:53 +01002155 /**
2156 * Start a text search.
2157 *
2158 * @return {@code true} if the search will start immediately, so any further keypresses
2159 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2160 * to buffer keypresses.
2161 */
2162 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002163 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002164 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2165 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2166 sourceBounds);
2167 }
2168
Michael Jurkaa33411c2012-06-14 16:18:21 -07002169 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002170 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002171 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002172 }
2173
2174 /**
2175 * Starts the global search activity. This code is a copied from SearchManager
2176 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002177 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002178 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002179 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002180 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2181 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2182 if (globalSearchActivity == null) {
2183 Log.w(TAG, "No global search activity found.");
2184 return;
2185 }
2186 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2187 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2188 intent.setComponent(globalSearchActivity);
2189 // Make sure that we have a Bundle to put source in
2190 if (appSearchData == null) {
2191 appSearchData = new Bundle();
2192 } else {
2193 appSearchData = new Bundle(appSearchData);
2194 }
Adam Cohen9211d422014-10-07 18:14:53 -07002195 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002196 if (!appSearchData.containsKey("source")) {
2197 appSearchData.putString("source", getPackageName());
2198 }
2199 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2200 if (!TextUtils.isEmpty(initialQuery)) {
2201 intent.putExtra(SearchManager.QUERY, initialQuery);
2202 }
2203 if (selectInitialQuery) {
2204 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2205 }
2206 intent.setSourceBounds(sourceBounds);
2207 try {
2208 startActivity(intent);
2209 } catch (ActivityNotFoundException ex) {
2210 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 }
2213
Yura4f93ec62014-02-04 14:15:21 +00002214 public boolean isOnCustomContent() {
2215 return mWorkspace.isOnOrMovingToCustomContent();
2216 }
2217
Winson Chung70d72102011-08-12 11:24:35 -07002218 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002219 public boolean onPrepareOptionsMenu(Menu menu) {
2220 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002221 if (!isOnCustomContent()) {
2222 // Close any open folders
2223 closeFolder();
2224 // Stop resizing any widgets
2225 mWorkspace.exitWidgetResizeMode();
2226 if (!mWorkspace.isInOverviewMode()) {
2227 // Show the overview mode
2228 showOverviewMode(true);
2229 } else {
2230 showWorkspace(true);
2231 }
Winson Chunge0298742014-01-17 12:03:00 -08002232 }
Adam Cohen9211d422014-10-07 18:14:53 -07002233 if (mLauncherCallbacks != null) {
2234 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2235 }
2236
Michael Jurkab94f3f82013-09-11 18:08:54 +02002237 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002238 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002240 @Override
2241 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002242 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002243 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002244 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002245 }
2246
Joe Onorato9c1289c2009-08-17 11:03:03 -04002247 public boolean isWorkspaceLocked() {
2248 return mWorkspaceLoading || mWaitingForResult;
2249 }
2250
Adam Cohen517a7f52014-03-01 12:12:59 -08002251 public boolean isWorkspaceLoading() {
2252 return mWorkspaceLoading;
2253 }
2254
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002255 private void setWorkspaceLoading(boolean value) {
2256 boolean isLocked = isWorkspaceLocked();
2257 mWorkspaceLoading = value;
2258 if (isLocked != isWorkspaceLocked()) {
2259 onWorkspaceLockedChanged();
2260 }
2261 }
2262
2263 private void setWaitingForResult(boolean value) {
2264 boolean isLocked = isWorkspaceLocked();
2265 mWaitingForResult = value;
2266 if (isLocked != isWorkspaceLocked()) {
2267 onWorkspaceLockedChanged();
2268 }
2269 }
2270
Adam Cohen9211d422014-10-07 18:14:53 -07002271 protected void onWorkspaceLockedChanged() {
2272 if (mLauncherCallbacks != null) {
2273 mLauncherCallbacks.onWorkspaceLockedChanged();
2274 }
2275 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002276
Michael Jurka0280c3b2010-09-17 15:00:07 -07002277 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002278 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002279 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002280 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2281 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002282 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002283 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002284 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002285
Sunny Goyale6b63a32015-01-16 16:14:29 -08002286 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002287 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002288 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2289 }
2290
Sunny Goyale6b63a32015-01-16 16:14:29 -08002291 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002292 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2293 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002294 if (appWidgetInfo.configure != null) {
2295 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002296 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002297
Bjorn Bringert7984c942009-12-09 15:38:25 +00002298 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002299 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2300 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2301
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002302 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002303 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002304 Runnable onComplete = new Runnable() {
2305 @Override
2306 public void run() {
2307 // Exit spring loaded mode if necessary after adding the widget
2308 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2309 null);
2310 }
2311 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002312 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002313 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002314 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315 }
2316 }
Romain Guycbb89e42009-06-08 15:52:54 -07002317
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002318 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002319 // Close any folders that may be open.
2320 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002321 mWorkspace.moveToCustomContentScreen(animate);
2322 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002323
2324 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2325 int[] cell, int spanX, int spanY) {
2326 switch (info.itemType) {
2327 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2328 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2329 int span[] = new int[2];
2330 span[0] = spanX;
2331 span[1] = spanY;
2332 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2333 container, screenId, cell, span);
2334 break;
2335 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2336 processShortcutFromDrop(info.componentName, container, screenId, cell);
2337 break;
2338 default:
2339 throw new IllegalStateException("Unknown item type: " + info.itemType);
2340 }
2341 }
2342
Adam Cohenfbba09b2011-07-18 21:43:05 -07002343 /**
2344 * Process a shortcut drop.
2345 *
2346 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002347 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002348 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002349 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002350 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2351 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002352 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002353 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002354 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002355 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002356
2357 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002358 mPendingAddInfo.cellX = cell[0];
2359 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002360 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002361
2362 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2363 createShortcutIntent.setComponent(componentName);
2364 processShortcut(createShortcutIntent);
2365 }
2366
Adam Cohenfbba09b2011-07-18 21:43:05 -07002367 /**
2368 * Process a widget drop.
2369 *
2370 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002371 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002372 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002373 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002374 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2375 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002376 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002377 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002378 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002379 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002380 mPendingAddInfo.minSpanX = info.minSpanX;
2381 mPendingAddInfo.minSpanY = info.minSpanY;
2382
Adam Cohenfbba09b2011-07-18 21:43:05 -07002383 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002384 mPendingAddInfo.cellX = cell[0];
2385 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002386 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002387 if (span != null) {
2388 mPendingAddInfo.spanX = span[0];
2389 mPendingAddInfo.spanY = span[1];
2390 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002391
Adam Cohened66b2b2012-01-23 17:28:51 -08002392 AppWidgetHostView hostView = info.boundWidget;
2393 int appWidgetId;
2394 if (hostView != null) {
2395 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002396 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002397 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002398 // In this case, we either need to start an activity to get permission to bind
2399 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002400 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002401 Bundle options = info.bindOptions;
2402
Sunny Goyalffe83f12014-08-14 17:39:34 -07002403 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2404 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002405 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002406 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002407 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002408 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002409 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2410 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2411 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002412 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2413 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002414 // TODO: we need to make sure that this accounts for the options bundle.
2415 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002416 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2417 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002418 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002419 }
2420
Joe Onoratodeb98af2010-02-19 14:59:39 -08002421 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002422 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 }
2424
Winson Chung24ab2f12010-09-16 14:10:47 -07002425 void processWallpaper(Intent intent) {
2426 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2427 }
2428
Adam Cohendcd297f2013-06-18 13:13:40 -07002429 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002430 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002431 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 folderInfo.title = getText(R.string.folder_name);
2433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002435 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2436 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002437 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438
2439 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002440 FolderIcon newFolder =
2441 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002442 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002443 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002444 // Force measure the new folder icon
2445 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2446 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002447 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448 }
Romain Guycbb89e42009-06-08 15:52:54 -07002449
Joe Onorato9c1289c2009-08-17 11:03:03 -04002450 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002451 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002452 }
2453
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002454 /**
2455 * Registers various content observers. The current implementation registers
2456 * only a favorites observer to keep track of the favorites applications.
2457 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002458 private void registerContentObservers() {
2459 ContentResolver resolver = getContentResolver();
2460 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2461 true, mWidgetObserver);
2462 }
2463
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 @Override
2465 public boolean dispatchKeyEvent(KeyEvent event) {
2466 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2467 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002469 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002470 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002471 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002472 dumpState();
2473 return true;
2474 }
2475 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002476 }
2477 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2478 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002479 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 return true;
2481 }
2482 }
2483
2484 return super.dispatchKeyEvent(event);
2485 }
2486
Joe Onorato88ec0992009-11-19 13:16:06 -08002487 @Override
2488 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002489 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2490 return;
2491 }
2492
Winson Chung3d9490a2015-03-24 17:38:42 -07002493 LauncherAccessibilityDelegate delegate =
2494 LauncherAppState.getInstance().getAccessibilityDelegate();
2495 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002496 return;
2497 }
2498
Winson Chungb745afb2015-03-02 11:51:23 -08002499 if (isAppsViewVisible()) {
2500 showWorkspace(true);
2501 } else if (isWidgetsViewVisible()) {
2502 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002503 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002504 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002505 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002506 Folder openFolder = mWorkspace.getOpenFolder();
2507 if (openFolder.isEditingName()) {
2508 openFolder.dismissEditingName();
2509 } else {
2510 closeFolder();
2511 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002512 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002513 mWorkspace.exitWidgetResizeMode();
2514
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002515 // Back button is a no-op here, but give at least some feedback for the button press
2516 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002517 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002518 }
2519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002521 * Re-listen when widgets are reset.
2522 */
Adam Cohen091440a2015-03-18 14:16:05 -07002523 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002524 if (mAppWidgetHost != null) {
2525 mAppWidgetHost.startListening();
2526 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002527 }
2528
2529 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530 * Launches the intent referred by the clicked shortcut.
2531 *
2532 * @param v The view representing the clicked shortcut.
2533 */
2534 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002535 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2536 // view has detached (it's possible for this to happen if the view is removed mid touch).
2537 if (v.getWindowToken() == null) {
2538 return;
2539 }
2540
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002541 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002542 return;
2543 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002544
Adam Cohen1697b792013-09-17 19:08:21 -07002545 if (v instanceof Workspace) {
2546 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002547 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002548 }
2549 return;
2550 }
2551
2552 if (v instanceof CellLayout) {
2553 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002554 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002555 }
2556 }
2557
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002559 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002560 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002562 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002563 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002564 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002565 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002566 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002567 } else if (tag instanceof AppInfo) {
2568 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002569 } else if (tag instanceof LauncherAppWidgetInfo) {
2570 if (v instanceof PendingAppWidgetHostView) {
2571 onClickPendingWidget((PendingAppWidgetHostView) v);
2572 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002573 }
2574 }
2575
Sunny Goyal508da152014-08-14 10:53:27 -07002576 public void onClickPagedViewIcon(View v) {
2577 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002578 if (mLauncherCallbacks != null) {
2579 mLauncherCallbacks.onClickPagedViewIcon(v);
2580 }
Sunny Goyal508da152014-08-14 10:53:27 -07002581 }
2582
Michael Jurka0e260592010-06-30 17:07:39 -07002583 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002584 return false;
2585 }
2586
Michael Jurkaaf442092010-06-10 17:01:57 -07002587 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002588 * Event handler for the app widget view which has not fully restored.
2589 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002590 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002591 if (mIsSafeModeEnabled) {
2592 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2593 return;
2594 }
2595
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002596 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002597 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002598 int widgetId = info.appWidgetId;
2599 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2600 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002601 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2602 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002603 mPendingAddInfo.copyFrom(info);
2604 mPendingAddWidgetId = widgetId;
2605
Sunny Goyalffe83f12014-08-14 17:39:34 -07002606 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2607 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002608 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002609 } else if (info.installProgress < 0) {
2610 // The install has not been queued
2611 final String packageName = info.providerName.getPackageName();
2612 showBrokenAppInstallDialog(packageName,
2613 new DialogInterface.OnClickListener() {
2614 public void onClick(DialogInterface dialog, int id) {
2615 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2616 }
2617 });
2618 } else {
2619 // Download has started.
2620 final String packageName = info.providerName.getPackageName();
2621 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002622 }
2623 }
2624
2625 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002626 * Event handler for the "grid" button that appears on the home screen, which
2627 * enters all apps mode.
2628 *
2629 * @param v The view that was clicked.
2630 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002631 protected void onClickAllAppsButton(View v) {
2632 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002633 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002634 showWorkspace(true);
2635 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002636 // Try and refresh the set of predicted apps before we enter launcher
2637 showAppsView(true /* animated */, false /* resetListToTop */,
2638 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002639 }
2640 }
2641
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002642 private void showBrokenAppInstallDialog(final String packageName,
2643 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002644 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002645 .setTitle(R.string.abandoned_promises_title)
2646 .setMessage(R.string.abandoned_promise_explanation)
2647 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2648 .setNeutralButton(R.string.abandoned_clean_this,
2649 new DialogInterface.OnClickListener() {
2650 public void onClick(DialogInterface dialog, int id) {
2651 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2652 mWorkspace.removeAbandonedPromise(packageName, user);
2653 }
2654 })
2655 .create().show();
2656 return;
2657 }
2658
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002659 /**
2660 * Event handler for an app shortcut click.
2661 *
2662 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2663 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002664 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002665 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2666 Object tag = v.getTag();
2667 if (!(tag instanceof ShortcutInfo)) {
2668 throw new IllegalArgumentException("Input must be a Shortcut");
2669 }
2670
2671 // Open shortcut
2672 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002673
2674 if (shortcut.isDisabled != 0) {
2675 int error = R.string.activity_not_available;
2676 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2677 error = R.string.safemode_shortcut_error;
2678 }
2679 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2680 return;
2681 }
2682
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002683 final Intent intent = shortcut.intent;
2684
2685 // Check for special shortcuts
2686 if (intent.getComponent() != null) {
2687 final String shortcutClass = intent.getComponent().getClassName();
2688
2689 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2690 MemoryDumpActivity.startDump(this);
2691 return;
2692 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2693 toggleShowWeightWatcher();
2694 return;
2695 }
2696 }
2697
Chris Wren40c5ed32014-06-24 18:24:23 -04002698 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002699 if ((v instanceof BubbleTextView)
2700 && shortcut.isPromise()
2701 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002702 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002703 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002704 new DialogInterface.OnClickListener() {
2705 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002706 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002707 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002708 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002709 return;
2710 }
2711
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002712 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002713 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002714
2715 if (mLauncherCallbacks != null) {
2716 mLauncherCallbacks.onClickAppShortcut(v);
2717 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002718 }
2719
Adam Cohen091440a2015-03-18 14:16:05 -07002720 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002721 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002722 final ShortcutInfo shortcut;
2723 final Intent intent;
2724 if (tag instanceof ShortcutInfo) {
2725 shortcut = (ShortcutInfo) tag;
2726 intent = shortcut.intent;
2727 int[] pos = new int[2];
2728 v.getLocationOnScreen(pos);
2729 intent.setSourceBounds(new Rect(pos[0], pos[1],
2730 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002731
Sunny Goyal508da152014-08-14 10:53:27 -07002732 } else if (tag instanceof AppInfo) {
2733 shortcut = null;
2734 intent = ((AppInfo) tag).intent;
2735 } else {
2736 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2737 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002738
2739 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002740 mStats.recordLaunch(intent, shortcut);
2741
2742 if (success && v instanceof BubbleTextView) {
2743 mWaitingForResume = (BubbleTextView) v;
2744 mWaitingForResume.setStayPressed(true);
2745 }
2746 }
2747
2748 /**
2749 * Event handler for a folder icon click.
2750 *
2751 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2752 */
2753 protected void onClickFolderIcon(View v) {
2754 if (LOGD) Log.d(TAG, "onClickFolder");
2755 if (!(v instanceof FolderIcon)){
2756 throw new IllegalArgumentException("Input must be a FolderIcon");
2757 }
2758
2759 FolderIcon folderIcon = (FolderIcon) v;
2760 final FolderInfo info = folderIcon.getFolderInfo();
2761 Folder openFolder = mWorkspace.getFolderForTag(info);
2762
2763 // If the folder info reports that the associated folder is open, then verify that
2764 // it is actually opened. There have been a few instances where this gets out of sync.
2765 if (info.opened && openFolder == null) {
2766 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2767 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2768 info.opened = false;
2769 }
2770
2771 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2772 // Close any open folder
2773 closeFolder();
2774 // Open the requested folder
2775 openFolder(folderIcon);
2776 } else {
2777 // Find the open folder...
2778 int folderScreen;
2779 if (openFolder != null) {
2780 folderScreen = mWorkspace.getPageForView(openFolder);
2781 // .. and close it
2782 closeFolder(openFolder);
2783 if (folderScreen != mWorkspace.getCurrentPage()) {
2784 // Close any folder open on the current screen
2785 closeFolder();
2786 // Pull the folder onto this screen
2787 openFolder(folderIcon);
2788 }
2789 }
2790 }
Adam Cohen9211d422014-10-07 18:14:53 -07002791
2792 if (mLauncherCallbacks != null) {
2793 mLauncherCallbacks.onClickFolderIcon(v);
2794 }
Michael Jurka5130e402011-10-13 04:55:35 -07002795 }
2796
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002797 /**
2798 * Event handler for the (Add) Widgets button that appears after a long press
2799 * on the home screen.
2800 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002801 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002802 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002803 if (mIsSafeModeEnabled) {
2804 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2805 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002806 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002807 if (mLauncherCallbacks != null) {
2808 mLauncherCallbacks.onClickAddWidgetButton(view);
2809 }
Adam Cohen9211d422014-10-07 18:14:53 -07002810 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002811 }
2812
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002813 /**
2814 * Event handler for the wallpaper picker button that appears after a long press
2815 * on the home screen.
2816 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002817 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002818 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002819 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2820 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002821
2822 if (mLauncherCallbacks != null) {
2823 mLauncherCallbacks.onClickWallpaperPicker(v);
2824 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002825 }
2826
2827 /**
2828 * Event handler for a click on the settings button that appears after a long press
2829 * on the home screen.
2830 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002831 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002832 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002833 if (mLauncherCallbacks != null) {
2834 mLauncherCallbacks.onClickSettingsButton(v);
2835 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002836 }
2837
Michael Jurka5130e402011-10-13 04:55:35 -07002838 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002839 // Provide the same haptic feedback that the system offers for virtual keys.
2840 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002841 }
2842
Adam Cohen61f560d2013-09-30 15:58:20 -07002843 public void performHapticFeedbackOnTouchDown(View v) {
2844 // Provide the same haptic feedback that the system offers for virtual keys.
2845 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2846 }
2847
2848 public View.OnTouchListener getHapticFeedbackTouchListener() {
2849 if (mHapticFeedbackTouchListener == null) {
2850 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2851 @Override
2852 public boolean onTouch(View v, MotionEvent event) {
2853 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2854 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2855 }
2856 return false;
2857 }
2858 };
2859 }
2860 return mHapticFeedbackTouchListener;
2861 }
2862
Adam Cohen9211d422014-10-07 18:14:53 -07002863 public void onDragStarted(View view) {
2864 if (isOnCustomContent()) {
2865 // Custom content screen doesn't participate in drag and drop. If on custom
2866 // content screen, move to default.
2867 moveWorkspaceToDefaultScreen();
2868 }
2869
2870 if (mLauncherCallbacks != null) {
2871 mLauncherCallbacks.onDragStarted(view);
2872 }
2873 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002874
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002875 /**
2876 * Called when the user stops interacting with the launcher.
2877 * This implies that the user is now on the homescreen and is not doing housekeeping.
2878 */
Adam Cohen9211d422014-10-07 18:14:53 -07002879 protected void onInteractionEnd() {
2880 if (mLauncherCallbacks != null) {
2881 mLauncherCallbacks.onInteractionEnd();
2882 }
2883 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002884
2885 /**
2886 * Called when the user starts interacting with the launcher.
2887 * The possible interactions are:
2888 * - open all apps
2889 * - reorder an app shortcut, or a widget
2890 * - open the overview mode.
2891 * This is a good time to stop doing things that only make sense
2892 * when the user is on the homescreen and not doing housekeeping.
2893 */
Adam Cohen9211d422014-10-07 18:14:53 -07002894 protected void onInteractionBegin() {
2895 if (mLauncherCallbacks != null) {
2896 mLauncherCallbacks.onInteractionBegin();
2897 }
2898 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002899
Winson Chungcd99cd32015-04-29 11:03:24 -07002900 /** Updates the interaction state. */
2901 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002902 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002903 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002904 boolean fromStateWithOverlay;
2905 boolean toStateWithOverlay;
2906 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2907 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2908 toStateWithOverlay = toState != Workspace.State.NORMAL;
2909 } else {
2910 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2911 fromState != Workspace.State.NORMAL_HIDDEN;
2912 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2913 toState != Workspace.State.NORMAL_HIDDEN;
2914 }
2915 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002916 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002917 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002918 onInteractionEnd();
2919 }
2920 }
2921
Kenny Guyf07af7b2014-07-31 11:39:16 +01002922 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002923 try {
2924 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002925 launcherApps.showAppDetailsForProfile(componentName, user);
2926 } catch (SecurityException e) {
2927 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2928 Log.e(TAG, "Launcher does not have permission to launch settings");
2929 } catch (ActivityNotFoundException e) {
2930 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2931 Log.e(TAG, "Unable to launch settings");
2932 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002933 }
2934
Michael Jurka1e2f4652013-07-08 18:03:46 -07002935 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002936 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2937 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002938 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002939 // System applications cannot be installed. For now, show a toast explaining that.
2940 // We may give them the option of disabling apps this way.
2941 int messageId = R.string.uninstall_system_app_text;
2942 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002943 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002944 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002945 String packageName = componentName.getPackageName();
2946 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002947 Intent intent = new Intent(
2948 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002949 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2950 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002951 if (user != null) {
2952 user.addToIntent(intent, Intent.EXTRA_USER);
2953 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002954 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002955 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002956 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002957 }
2958
Michael Jurka86a720e2012-05-09 11:23:23 -07002959 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002960 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002961 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002962 // Only launch using the new animation if the shortcut has not opted out (this is a
2963 // private contract between launcher and may be ignored in the future).
2964 boolean useLaunchAnimation = (v != null) &&
2965 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002966 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2967 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002968
Kenny Guy1317e2d2014-05-08 18:52:50 +01002969 UserHandleCompat user = null;
2970 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2971 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2972 user = userManager.getUserForSerialNumber(serialNumber);
2973 }
2974
2975 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002976 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002977 ActivityOptions opts = null;
2978 if (sClipRevealMethod != null) {
2979 // TODO: call method directly when Launcher3 can depend on M APIs
2980 int left = 0, top = 0;
2981 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2982 if (v instanceof TextView) {
2983 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002984 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2985 if (icon != null) {
2986 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002987 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002988 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002989 width = bounds.width();
2990 height = bounds.height();
2991 }
2992 }
2993 try {
2994 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2995 left, top, width, height);
2996 } catch (IllegalAccessException e) {
2997 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2998 sClipRevealMethod = null;
2999 } catch (InvocationTargetException e) {
3000 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
3001 sClipRevealMethod = null;
3002 }
3003 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003004 if (opts == null && !Utilities.isLmpOrAbove()) {
3005 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08003006 v.getMeasuredWidth(), v.getMeasuredHeight());
3007 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003008 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07003009 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01003010
3011 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
3012 // Could be launching some bookkeeping activity
3013 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003014 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003015 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003016 launcherApps.startActivityForProfile(intent.getComponent(), user,
3017 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003018 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003019 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020 } catch (SecurityException e) {
3021 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003022 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003023 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003024 "or use the exported attribute for this activity. "
3025 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003026 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003027 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003028 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003029
Michael Jurka86a720e2012-05-09 11:23:23 -07003030 boolean startActivitySafely(View v, Intent intent, Object tag) {
3031 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003032 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3033 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3034 return false;
3035 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003036 try {
3037 success = startActivity(v, intent, tag);
3038 } catch (ActivityNotFoundException e) {
3039 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3040 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3041 }
3042 return success;
3043 }
3044
Adam Cohen268c4752012-06-06 17:47:33 -07003045 /**
3046 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3047 * in the DragLayer in the exact absolute location of the original FolderIcon.
3048 */
3049 private void copyFolderIconToImage(FolderIcon fi) {
3050 final int width = fi.getMeasuredWidth();
3051 final int height = fi.getMeasuredHeight();
3052
3053 // Lazy load ImageView, Bitmap and Canvas
3054 if (mFolderIconImageView == null) {
3055 mFolderIconImageView = new ImageView(this);
3056 }
3057 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3058 mFolderIconBitmap.getHeight() != height) {
3059 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3060 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3061 }
3062
3063 DragLayer.LayoutParams lp;
3064 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3065 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3066 } else {
3067 lp = new DragLayer.LayoutParams(width, height);
3068 }
3069
Adam Cohen307fe232012-08-16 17:55:58 -07003070 // The layout from which the folder is being opened may be scaled, adjust the starting
3071 // view size by this scale factor.
3072 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003073 lp.customPosition = true;
3074 lp.x = mRectForFolderAnimation.left;
3075 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003076 lp.width = (int) (scale * width);
3077 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003078
3079 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3080 fi.draw(mFolderIconCanvas);
3081 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003082 if (fi.getFolder() != null) {
3083 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3084 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003085 }
Adam Cohen268c4752012-06-06 17:47:33 -07003086 // Just in case this image view is still in the drag layer from a previous animation,
3087 // we remove it and re-add it.
3088 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3089 mDragLayer.removeView(mFolderIconImageView);
3090 }
3091 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003092 if (fi.getFolder() != null) {
3093 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003094 }
Adam Cohen268c4752012-06-06 17:47:33 -07003095 }
3096
Adam Cohen2801caf2011-05-13 20:57:39 -07003097 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003098 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003099 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3100 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3101 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3102
Adam Cohenc51934b2011-07-26 21:07:43 -07003103 FolderInfo info = (FolderInfo) fi.getTag();
3104 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3105 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003106 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3107 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003108 }
3109
Adam Cohen268c4752012-06-06 17:47:33 -07003110 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3111 copyFolderIconToImage(fi);
3112 fi.setVisibility(View.INVISIBLE);
3113
Michael Jurka2ecf9952012-06-18 12:52:28 -07003114 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003115 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003116 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003117 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3118 }
3119 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003120 oa.start();
3121 }
3122
Adam Cohen268c4752012-06-06 17:47:33 -07003123 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003124 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003125 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3126 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3127 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3128
Adam Cohen268c4752012-06-06 17:47:33 -07003129 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003130
Adam Cohen268c4752012-06-06 17:47:33 -07003131 // We remove and re-draw the FolderIcon in-case it has changed
3132 mDragLayer.removeView(mFolderIconImageView);
3133 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003134 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003135 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003136 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003137 oa.addListener(new AnimatorListenerAdapter() {
3138 @Override
3139 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003140 if (cl != null) {
3141 cl.clearFolderLeaveBehind();
3142 // Remove the ImageView copy of the FolderIcon and make the original visible.
3143 mDragLayer.removeView(mFolderIconImageView);
3144 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003145 }
3146 }
3147 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003148 oa.start();
3149 }
3150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003152 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153 * is animated relative to the specified View. If the View is null, no animation
3154 * is played.
3155 *
3156 * @param folderInfo The FolderInfo describing the folder to open.
3157 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003158 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003159 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003160 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3161 if (openFolder != null && openFolder != folder) {
3162 // Close any open folder before opening a folder.
3163 closeFolder();
3164 }
3165
Adam Cohena9cf38f2011-05-02 15:36:58 -07003166 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167
Adam Cohena9cf38f2011-05-02 15:36:58 -07003168 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003169
Sunny Goyalf4066152015-04-15 09:42:19 -07003170 // While the folder is open, the position of the icon cannot change.
3171 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3172
Adam Cohen4554ee12011-08-03 16:13:21 -07003173 // Just verify that the folder hasn't already been added to the DragLayer.
3174 // There was a one-off crash where the folder had a parent already.
3175 if (folder.getParent() == null) {
3176 mDragLayer.addView(folder);
3177 mDragController.addDropTarget((DropTarget) folder);
3178 } else {
3179 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3180 folder.getParent() + ").");
3181 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003182 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003183 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003184
3185 // Notify the accessibility manager that this folder "window" has appeared and occluded
3186 // the workspace items
3187 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3188 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003189 }
3190
3191 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003192 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003193 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003194 if (folder.isEditingName()) {
3195 folder.dismissEditingName();
3196 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003197 closeFolder(folder);
3198 }
3199 }
3200
3201 void closeFolder(Folder folder) {
3202 folder.getInfo().opened = false;
3203
3204 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3205 if (parent != null) {
3206 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3207 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003208 if (fi != null) {
3209 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3210 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003211 }
3212 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003213
3214 // Notify the accessibility manager that this folder "window" has disappeard and no
3215 // longer occludeds the workspace items
3216 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003217 }
3218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003219 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003220 if (!isDraggingEnabled()) return false;
3221 if (isWorkspaceLocked()) return false;
3222 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003223
Adam Cohen1697b792013-09-17 19:08:21 -07003224 if (v instanceof Workspace) {
3225 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003226 if (!mWorkspace.isTouchActive()) {
3227 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003228 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3229 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3230 return true;
3231 } else {
3232 return false;
3233 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003234 } else {
3235 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003236 }
Adam Cohen1697b792013-09-17 19:08:21 -07003237 }
3238
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003239 CellLayout.CellInfo longClickCellInfo = null;
3240 View itemUnderLongClick = null;
3241 if (v.getTag() instanceof ItemInfo) {
3242 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003243 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003244 itemUnderLongClick = longClickCellInfo.cell;
3245 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003246 }
3247
Winson Chung3d503fb2011-07-13 17:25:49 -07003248 // The hotseat touch handling does not go through Workspace, and we always allow long press
3249 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003250 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003251 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003252 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003253 // User long pressed on empty space
3254 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3255 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003256 if (mWorkspace.isInOverviewMode()) {
3257 mWorkspace.startReordering(v);
3258 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003259 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003260 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003261 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003262 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3263 mHotseat.getOrderInHotseat(
3264 longClickCellInfo.cellX,
3265 longClickCellInfo.cellY));
3266 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003267 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003268 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003269 }
3270 }
3271 }
3272 return true;
3273 }
3274
Winson Chung3d503fb2011-07-13 17:25:49 -07003275 boolean isHotseatLayout(View layout) {
3276 return mHotseat != null && layout != null &&
3277 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3278 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003279
Winson Chung3d503fb2011-07-13 17:25:49 -07003280 /**
3281 * Returns the CellLayout of the specified container at the specified screen.
3282 */
Sunny Goyal92820592015-03-02 11:31:35 -08003283 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003284 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3285 if (mHotseat != null) {
3286 return mHotseat.getLayout();
3287 } else {
3288 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003289 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003290 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003291 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003292 }
3293 }
3294
Winson Chungb745afb2015-03-02 11:51:23 -08003295 /**
3296 * For overridden classes.
3297 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003298 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003299 return isAppsViewVisible();
3300 }
3301
3302 public boolean isAppsViewVisible() {
3303 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3304 }
3305
3306 public boolean isWidgetsViewVisible() {
3307 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003308 }
3309
Craig Mautner360310b2012-10-26 15:13:08 -07003310 private void setWorkspaceBackground(boolean workspace) {
3311 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003312 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003313 }
3314
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003315 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003316 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3317 int curflags = getWindow().getAttributes().flags
3318 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3319 if (wpflags != curflags) {
3320 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3321 }
Craig Mautner360310b2012-10-26 15:13:08 -07003322 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003323 }
3324
Michael Jurkae326f182011-11-21 14:05:46 -08003325 @Override
3326 public void onTrimMemory(int level) {
3327 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003328 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3329 // The widget preview db can result in holding onto over
3330 // 3MB of memory for caching which isn't necessary.
3331 SQLiteDatabase.releaseMemory();
3332
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003333 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003334 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003335 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003336 if (mLauncherCallbacks != null) {
3337 mLauncherCallbacks.onTrimMemory(level);
3338 }
Michael Jurkae326f182011-11-21 14:05:46 -08003339 }
3340
Winson Chungb745afb2015-03-02 11:51:23 -08003341 @Override
3342 public void onStateTransitionHideSearchBar() {
3343 // Hide the search bar
3344 if (mSearchDropTargetBar != null) {
3345 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3346 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003347 }
3348
Winson Chungb745afb2015-03-02 11:51:23 -08003349 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003350 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3351 true);
Adam Cohened307df2013-10-02 09:37:31 -07003352 }
3353
Winson Chungdc61c4d2015-04-20 18:26:57 -07003354 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3355 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3356 onCompleteRunnable, true);
3357 }
3358
3359 protected void showWorkspace(int snapToPage, boolean animated) {
3360 showWorkspace(snapToPage, animated, null, true);
3361 }
3362
3363 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3364 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003365 boolean changed = mState != State.WORKSPACE ||
3366 mWorkspace.getState() != Workspace.State.NORMAL;
3367 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003368 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003369 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003370 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003371 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003372
Winson Chungc7d2b602012-05-16 17:02:20 -07003373 // Show the search bar (only animate if we were showing the drop target bar in spring
3374 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003375 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003376 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003377 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003378
Michael Jurkab3e22d92011-10-31 15:58:33 -07003379 // Set focus to the AppsCustomize button
3380 if (mAllAppsButton != null) {
3381 mAllAppsButton.requestFocus();
3382 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003383 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003384
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003385 // Change the state *after* we've called all the transition code
3386 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003387
Winson Chung5fb63472011-02-02 17:03:37 -08003388 // Resume the auto-advance of widgets
3389 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003390 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003391
Winson Chung0f785722015-04-08 10:27:49 -07003392 if (changed) {
3393 // Send an accessibility event to announce the context change
3394 getWindow().getDecorView()
3395 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003396 if (notifyLauncherCallbacks) {
3397 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003398 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003399 mLauncherCallbacks.onAllAppsHidden();
3400 }
3401 }
Winson Chung0f785722015-04-08 10:27:49 -07003402 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003403 }
3404
Adam Cohened307df2013-10-02 09:37:31 -07003405 void showOverviewMode(boolean animated) {
3406 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003407 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003408 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3409 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003410 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003411 }
3412
Winson Chungb745afb2015-03-02 11:51:23 -08003413 /**
3414 * Shows the apps view.
3415 */
Winson Chung4ac30062015-05-08 17:34:17 -07003416 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003417 if (resetListToTop) {
3418 mAppsView.scrollToTop();
3419 }
Winson Chung4ac30062015-05-08 17:34:17 -07003420 if (updatePredictedApps) {
3421 tryAndUpdatePredictedApps();
3422 }
Winson Chungb745afb2015-03-02 11:51:23 -08003423 showAppsOrWidgets(animated, State.APPS);
3424 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003425
Winson Chungb745afb2015-03-02 11:51:23 -08003426 /**
3427 * Shows the widgets view.
3428 */
3429 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003430 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003431 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003432 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003433 }
Winson Chungb745afb2015-03-02 11:51:23 -08003434 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003435
3436 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003437 @Override
3438 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003439 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003440 }
3441 });
Winson Chungb745afb2015-03-02 11:51:23 -08003442 }
3443
3444 /**
3445 * Sets up the transition to show the apps/widgets view.
3446 */
3447 private void showAppsOrWidgets(boolean animated, State toState) {
3448 if (mState != State.WORKSPACE) return;
3449 if (toState != State.APPS && toState != State.WIDGETS) return;
3450
3451 if (toState == State.APPS) {
3452 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003453 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003454 mLauncherCallbacks.onAllAppsShown();
3455 }
Winson Chungb745afb2015-03-02 11:51:23 -08003456 } else {
3457 mStateTransitionAnimation.startAnimationToWidgets(animated);
3458 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003459
Michael Jurkab3e22d92011-10-31 15:58:33 -07003460 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003461 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003462
3463 // Pause the auto-advance of widgets until we are out of AllApps
3464 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003465 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003466 closeFolder();
3467
3468 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003469 getWindow().getDecorView()
3470 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003471 }
3472
Winson Chungcd99cd32015-04-29 11:03:24 -07003473 /**
3474 * Updates the workspace and interaction state on state change, and return the animation to this
3475 * new state.
3476 */
3477 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3478 boolean animated, HashMap<View, Integer> layerViews) {
3479 Workspace.State fromState = mWorkspace.getState();
3480 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3481 updateInteraction(fromState, toState);
3482 return anim;
3483 }
3484
Hyunyoung Song3f471442015-04-08 19:01:34 -07003485 public void enterSpringLoadedDragMode() {
3486 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3487 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003488 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3489 mState == State.WIDGETS_SPRING_LOADED) {
3490 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003491 }
Winson Chungb745afb2015-03-02 11:51:23 -08003492
3493 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003494 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3495 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003496 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003497 }
Adam Cohen7777d962011-08-18 18:58:38 -07003498
Hyunyoung Song3f471442015-04-08 19:01:34 -07003499 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003500 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003501 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003502
Winson Chungcd99cd32015-04-29 11:03:24 -07003503 if (successfulDrop) {
3504 // We need to trigger all apps hidden to notify search to update itself before the
3505 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003506 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003507 mLauncherCallbacks.onAllAppsHidden();
3508 }
3509 }
3510
Winson Chunge7a03942011-08-05 15:05:12 -07003511 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003512 @Override
3513 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003514 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003515 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3516 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003517 // Before we show workspace, hide all apps again because
3518 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3519 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003520 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003521 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003522 } else {
3523 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003524 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003525 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003526 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003527 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003528
Michael Jurkad3ef3062010-11-23 16:23:58 -08003529 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003530 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003531 showAppsView(true /* animated */, false /* resetListToTop */,
3532 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003533 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003534 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003535 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003536 }
3537
Winson Chung4ac30062015-05-08 17:34:17 -07003538 /**
3539 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3540 * resumed.
3541 */
3542 private void tryAndUpdatePredictedApps() {
3543 if (mLauncherCallbacks != null) {
3544 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3545 if (!apps.isEmpty()) {
3546 mAppsView.setPredictedApps(apps);
3547 }
3548 }
3549 }
3550
Michael Jurkab3e22d92011-10-31 15:58:33 -07003551 void lockAllApps() {
3552 // TODO
3553 }
3554
3555 void unlockAllApps() {
3556 // TODO
3557 }
3558
Sunny Goyal594d76d2014-11-06 10:12:54 -08003559 protected void disableVoiceButtonProxy(boolean disable) {
3560 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003561 }
3562
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003563 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003564 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3565 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003566 }
3567
Adam Cohen24ce0b32014-01-14 16:18:14 -08003568 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003569 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3570 if (searchProvider == null) {
3571 return null;
3572 }
3573
3574 Bundle opts = new Bundle();
3575 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003576 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003577
3578 SharedPreferences sp = getSharedPreferences(
3579 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3580 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003581 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003582 if (!searchProvider.provider.flattenToString().equals(
3583 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003584 || (widgetInfo == null)
3585 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003586 // A valid widget is not already bound.
3587 if (widgetId > -1) {
3588 mAppWidgetHost.deleteAppWidgetId(widgetId);
3589 widgetId = -1;
3590 }
3591
3592 // Try to bind a new widget
3593 widgetId = mAppWidgetHost.allocateAppWidgetId();
3594
3595 if (!AppWidgetManagerCompat.getInstance(this)
3596 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3597 mAppWidgetHost.deleteAppWidgetId(widgetId);
3598 widgetId = -1;
3599 }
3600
3601 sp.edit()
3602 .putInt(QSB_WIDGET_ID, widgetId)
3603 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3604 .commit();
3605 }
3606
3607 if (widgetId != -1) {
3608 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3609 mQsb.updateAppWidgetOptions(opts);
3610 mQsb.setPadding(0, 0, 0, 0);
3611 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003612 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003613 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003614 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003615 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003616 }
3617
Sunny Goyal22235bc2015-04-03 09:23:43 -07003618 private void reinflateQSBIfNecessary() {
3619 if (mQsb instanceof LauncherAppWidgetHostView &&
3620 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3621 mSearchDropTargetBar.removeView(mQsb);
3622 mQsb = null;
3623 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3624 }
3625 }
3626
Michael Jurkad7c28052012-04-27 15:43:36 -07003627 @Override
3628 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003629 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003630 final List<CharSequence> text = event.getText();
3631 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003632 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003633 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003634 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003635 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003636 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003637 } else {
3638 text.add(getString(R.string.all_apps_home_button_label));
3639 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003640 return result;
3641 }
3642
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003643 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003644 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003645 */
Adam Cohen091440a2015-03-18 14:16:05 -07003646 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003647 @Override
3648 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003649 closeSystemDialogs();
3650 }
3651 }
3652
3653 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003654 * Receives notifications whenever the appwidgets are reset.
3655 */
3656 private class AppWidgetResetObserver extends ContentObserver {
3657 public AppWidgetResetObserver() {
3658 super(new Handler());
3659 }
3660
3661 @Override
3662 public void onChange(boolean selfChange) {
3663 onAppWidgetReset();
3664 }
3665 }
3666
3667 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003668 * If the activity is currently paused, signal that we need to run the passed Runnable
3669 * in onResume.
3670 *
3671 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003672 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3673 * wrong when we're not running, and if the activity comes back to what the configuration was
3674 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003675 *
3676 * Implementation of the method from LauncherModel.Callbacks.
3677 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003678 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003679 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003680 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003681 if (mPaused) {
3682 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003683 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003684 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003685 }
3686 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003687 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003688 return true;
3689 } else {
3690 return false;
3691 }
3692 }
3693
Michael Jurkac402cd92013-05-20 15:49:32 +02003694 private boolean waitUntilResume(Runnable run) {
3695 return waitUntilResume(run, false);
3696 }
3697
Michael Jurka1e2f4652013-07-08 18:03:46 -07003698 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003699 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003700 }
3701
Michael Jurka7607c2f2013-04-03 14:33:19 -07003702 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003703 * If the activity is currently paused, signal that we need to re-run the loader
3704 * in onResume.
3705 *
3706 * This needs to be called from incoming places where resources might have been loaded
3707 * while we are paused. That is becaues the Configuration might be wrong
3708 * when we're not running, and if it comes back to what it was when we
3709 * were paused, we are not restarted.
3710 *
3711 * Implementation of the method from LauncherModel.Callbacks.
3712 *
3713 * @return true if we are currently paused. The caller might be able to
3714 * skip some work in that case since we will come back again.
3715 */
3716 public boolean setLoadOnResume() {
3717 if (mPaused) {
3718 Log.i(TAG, "setLoadOnResume");
3719 mOnResumeNeedsLoad = true;
3720 return true;
3721 } else {
3722 return false;
3723 }
3724 }
3725
3726 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003728 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003729 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003730 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003731 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003732 } else {
3733 return SCREEN_COUNT / 2;
3734 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003735 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003736
Joe Onorato9c1289c2009-08-17 11:03:03 -04003737 /**
3738 * Refreshes the shortcuts shown on the workspace.
3739 *
3740 * Implementation of the method from LauncherModel.Callbacks.
3741 */
3742 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003743 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003744
Michael Jurka7607c2f2013-04-03 14:33:19 -07003745 // If we're starting binding all over again, clear any bind calls we'd postponed in
3746 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3747 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003748 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003749
Winson Chungd64d1762013-08-20 14:37:16 -07003750 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003751 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003752 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003753
Michael Jurka05bf644e2011-11-30 20:28:53 -08003754 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003755 if (mHotseat != null) {
3756 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003757 }
3758 }
3759
Adam Cohendcd297f2013-06-18 13:13:40 -07003760 @Override
3761 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003762 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003763
Adam Cohen5084cba2013-09-03 12:01:16 -07003764 // If there are no screens, we need to have an empty screen
3765 if (orderedScreenIds.size() == 0) {
3766 mWorkspace.addExtraEmptyScreen();
3767 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003768
3769 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003770 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003771 if (hasCustomContentToLeft()) {
3772 mWorkspace.createCustomContentContainer();
3773 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003774 }
Winson Chung64359a52013-07-08 17:17:08 -07003775 }
3776
3777 @Override
3778 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003779 // Log to disk
3780 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3781 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3782 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003783 int count = orderedScreenIds.size();
3784 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003785 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003786 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003787 }
3788
Adam Cohen39a06042013-07-19 14:30:12 -07003789 private boolean shouldShowWeightWatcher() {
3790 String spKey = LauncherAppState.getSharedPreferencesKey();
3791 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003792 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003793
3794 return show;
3795 }
3796
3797 private void toggleShowWeightWatcher() {
3798 String spKey = LauncherAppState.getSharedPreferencesKey();
3799 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3800 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3801
3802 show = !show;
3803
3804 SharedPreferences.Editor editor = sp.edit();
3805 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3806 editor.commit();
3807
3808 if (mWeightWatcher != null) {
3809 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3810 }
3811 }
3812
Winson Chungd64d1762013-08-20 14:37:16 -07003813 public void bindAppsAdded(final ArrayList<Long> newScreens,
3814 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003815 final ArrayList<ItemInfo> addAnimated,
3816 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003817 Runnable r = new Runnable() {
3818 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003819 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003820 }
3821 };
3822 if (waitUntilResume(r)) {
3823 return;
3824 }
3825
Winson Chungd64d1762013-08-20 14:37:16 -07003826 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003827 if (newScreens != null) {
3828 bindAddScreens(newScreens);
3829 }
Winson Chungd64d1762013-08-20 14:37:16 -07003830
3831 // We add the items without animation on non-visible pages, and with
3832 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003833 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003834 bindItems(addNotAnimated, 0,
3835 addNotAnimated.size(), false);
3836 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003837 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003838 bindItems(addAnimated, 0,
3839 addAnimated.size(), true);
3840 }
Winson Chungc58497e2013-09-03 17:48:37 -07003841
Winson Chung87412982013-10-03 18:34:14 -07003842 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003843 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003844
Winson Chungb745afb2015-03-02 11:51:23 -08003845 if (addedApps != null && mAppsView != null) {
3846 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003847 }
Winson Chungd64d1762013-08-20 14:37:16 -07003848 }
3849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003850 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003851 * Bind the items start-end from the list.
3852 *
3853 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003854 */
Winson Chung64359a52013-07-08 17:17:08 -07003855 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3856 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003857 Runnable r = new Runnable() {
3858 public void run() {
3859 bindItems(shortcuts, start, end, forceAnimateIcons);
3860 }
3861 };
3862 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003863 return;
3864 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003865
Winson Chungf0c6ae02012-03-21 16:10:31 -07003866 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003867 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3868 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003869 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003870 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003871 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003872 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003873 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003874
3875 // Short circuit if we are loading dock items for a configuration which has no dock
3876 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3877 mHotseat == null) {
3878 continue;
3879 }
3880
Joe Onorato9c1289c2009-08-17 11:03:03 -04003881 switch (item.itemType) {
3882 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3883 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003884 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003885 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003886
Winson Chung64359a52013-07-08 17:17:08 -07003887 /*
3888 * TODO: FIX collision case
3889 */
Winson Chungce376632013-07-11 16:12:41 -07003890 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3891 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3892 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003893 View v = cl.getChildAt(item.cellX, item.cellY);
3894 Object tag = v.getTag();
3895 String desc = "Collision while binding workspace item: " + item
3896 + ". Collides with " + tag;
3897 if (LauncherAppState.isDogfoodBuild()) {
3898 throw (new RuntimeException(desc));
3899 } else {
3900 Log.d(TAG, desc);
3901 }
Winson Chungce376632013-07-11 16:12:41 -07003902 }
Winson Chung64359a52013-07-08 17:17:08 -07003903 }
3904
Adam Cohendcd297f2013-06-18 13:13:40 -07003905 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3906 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003907 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003908 // Animate all the applications up now
3909 shortcut.setAlpha(0f);
3910 shortcut.setScaleX(0f);
3911 shortcut.setScaleY(0f);
3912 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003913 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003914 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003915 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003916 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003917 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003918 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003919 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003920 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3921 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003922 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003923 default:
3924 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003926 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003927
Winson Chung997a9232013-07-24 15:33:46 -07003928 if (animateIcons) {
3929 // Animate to the correct page
3930 if (newShortcutsScreenId > -1) {
3931 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003932 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003933 final Runnable startBounceAnimRunnable = new Runnable() {
3934 public void run() {
3935 anim.playTogether(bounceAnims);
3936 anim.start();
3937 }
3938 };
Winson Chung997a9232013-07-24 15:33:46 -07003939 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003940 // We post the animation slightly delayed to prevent slowdowns
3941 // when we are loading right after we return to launcher.
3942 mWorkspace.postDelayed(new Runnable() {
3943 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003944 if (mWorkspace != null) {
3945 mWorkspace.snapToPage(newScreenIndex);
3946 mWorkspace.postDelayed(startBounceAnimRunnable,
3947 NEW_APPS_ANIMATION_DELAY);
3948 }
Winson Chung94d67682013-09-25 16:29:40 -07003949 }
3950 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003951 } else {
3952 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003953 }
3954 }
Winson Chung64359a52013-07-08 17:17:08 -07003955 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003956 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003957 }
3958
Joe Onorato9c1289c2009-08-17 11:03:03 -04003959 /**
3960 * Implementation of the method from LauncherModel.Callbacks.
3961 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003962 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003963 Runnable r = new Runnable() {
3964 public void run() {
3965 bindFolders(folders);
3966 }
3967 };
3968 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003969 return;
3970 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003971 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003972 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003973
3974 /**
3975 * Add the views for a widget to the workspace.
3976 *
3977 * Implementation of the method from LauncherModel.Callbacks.
3978 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003979 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003980 Runnable r = new Runnable() {
3981 public void run() {
3982 bindAppWidget(item);
3983 }
3984 };
3985 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003986 return;
3987 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003988
Daniel Sandler843e8602010-06-07 14:59:01 -04003989 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3990 if (DEBUG_WIDGETS) {
3991 Log.d(TAG, "bindAppWidget: " + item);
3992 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003993 final Workspace workspace = mWorkspace;
3994
Adam Cohen59400422014-03-05 18:07:04 -08003995 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003996 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003997
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003998 if (!mIsSafeModeEnabled
3999 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4000 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004001 if (appWidgetInfo == null) {
4002 if (DEBUG_WIDGETS) {
4003 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4004 + " belongs to component " + item.providerName
4005 + ", as the povider is null");
4006 }
4007 LauncherModel.deleteItemFromDatabase(this, item);
4008 return;
4009 }
4010 // Note: This assumes that the id remap broadcast is received before this step.
4011 // If that is not the case, the id remap will be ignored and user may see the
4012 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004013 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004014 pendingInfo.spanX = item.spanX;
4015 pendingInfo.spanY = item.spanY;
4016 pendingInfo.minSpanX = item.minSpanX;
4017 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004018 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004019 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004020
Sunny Goyalff572272014-07-23 13:58:07 -07004021 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004022 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4023 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004024
4025 // TODO consider showing a permission dialog when the widget is clicked.
4026 if (!success) {
4027 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4028 if (DEBUG_WIDGETS) {
4029 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4030 + " belongs to component " + item.providerName
4031 + ", as the launcher is unable to bing a new widget id");
4032 }
4033 LauncherModel.deleteItemFromDatabase(this, item);
4034 return;
4035 }
4036
4037 item.appWidgetId = newWidgetId;
4038
4039 // If the widget has a configure activity, it is still needs to set it up, otherwise
4040 // the widget is ready to go.
4041 item.restoreStatus = (appWidgetInfo.configure == null)
4042 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4043 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4044
4045 LauncherModel.updateItemInDatabase(this, item);
4046 }
4047
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004048 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004049 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004050 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004051 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4052 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004053 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004054
Sunny Goyal651077b2014-06-30 14:15:31 -07004055 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4056 } else {
4057 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004058 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4059 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004060 view.updateIcon(mIconCache);
4061 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004062 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004063 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004064 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004067 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004068
Adam Cohendcd297f2013-06-18 13:13:40 -07004069 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004070 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004071 if (!item.isCustomWidget()) {
4072 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4073 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004074
Joe Onorato9c1289c2009-08-17 11:03:03 -04004075 workspace.requestLayout();
4076
Daniel Sandler843e8602010-06-07 14:59:01 -04004077 if (DEBUG_WIDGETS) {
4078 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4079 + (SystemClock.uptimeMillis()-start) + "ms");
4080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004081 }
4082
Sunny Goyalff572272014-07-23 13:58:07 -07004083 /**
4084 * Restores a pending widget.
4085 *
4086 * @param appWidgetId The app widget id
4087 * @param cellInfo The position on screen where to create the widget.
4088 */
4089 private void completeRestoreAppWidget(final int appWidgetId) {
4090 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4091 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4092 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4093 return;
4094 }
4095
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004096 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004097 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4098
4099 mWorkspace.reinflateWidgetsIfNecessary();
4100 LauncherModel.updateItemInDatabase(this, info);
4101 }
4102
Adam Cohen1462de32012-07-24 22:34:36 -07004103 public void onPageBoundSynchronously(int page) {
4104 mSynchronouslyBoundPages.add(page);
4105 }
4106
Joe Onorato9c1289c2009-08-17 11:03:03 -04004107 /**
4108 * Callback saying that there aren't any more items to bind.
4109 *
4110 * Implementation of the method from LauncherModel.Callbacks.
4111 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004112 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004113 Runnable r = new Runnable() {
4114 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004115 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004116 }
4117 };
4118 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004119 return;
4120 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004121 if (mSavedState != null) {
4122 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004123 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004124 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004125 mSavedState = null;
4126 }
4127
Adam Cohen1462de32012-07-24 22:34:36 -07004128 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004129
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004130 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004131 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004132
4133 // If we received the result of any pending adds while the loader was running (e.g. the
4134 // widget configuration forced an orientation change), process them now.
4135 if (sPendingAddItem != null) {
4136 final long screenId = completeAdd(sPendingAddItem);
4137
4138 // TODO: this moves the user to the page where the pending item was added. Ideally,
4139 // the screen would be guaranteed to exist after bind, and the page would be set through
4140 // the workspace restore process.
4141 mWorkspace.post(new Runnable() {
4142 @Override
4143 public void run() {
4144 mWorkspace.snapToScreenId(screenId);
4145 }
4146 });
4147 sPendingAddItem = null;
4148 }
4149
Sunny Goyal756adbc2015-04-16 15:20:51 -07004150 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004151
4152 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004153 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004154 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004155 }
4156
Adam Cohen3ed316a2014-07-23 18:21:20 -07004157 private void sendLoadingCompleteBroadcastIfNecessary() {
4158 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4159 String permission =
4160 getResources().getString(R.string.receive_first_load_broadcast_permission);
4161 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4162 sendBroadcast(intent, permission);
4163 SharedPreferences.Editor editor = mSharedPrefs.edit();
4164 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4165 editor.apply();
4166 }
4167 }
4168
Winson Chunga0b7e862013-09-05 16:03:15 -07004169 public boolean isAllAppsButtonRank(int rank) {
4170 if (mHotseat != null) {
4171 return mHotseat.isAllAppsButtonRank(rank);
4172 }
4173 return false;
4174 }
4175
Winson Chunga2413752012-04-03 14:22:34 -07004176 private boolean canRunNewAppsAnimation() {
4177 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4178 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4179 }
4180
Winson Chungc9168342013-06-26 14:54:55 -07004181 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4182 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4183 PropertyValuesHolder.ofFloat("alpha", 1f),
4184 PropertyValuesHolder.ofFloat("scaleX", 1f),
4185 PropertyValuesHolder.ofFloat("scaleY", 1f));
4186 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4187 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4188 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4189 return bounceAnim;
4190 }
4191
Adam Cohen27772732013-11-11 14:00:56 +00004192 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004193 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004194 }
4195
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004196 protected Rect getSearchBarBounds() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004197 return mDeviceProfile.getSearchBarBounds();
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004198 }
4199
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004200 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004201 if (mSearchDropTargetBar == null) {
4202 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004203 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004204 if (mQsb != null) {
4205 mSearchDropTargetBar.removeView(mQsb);
4206 mQsb = null;
4207 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004208 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004209 }
4210
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004211 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004212 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4213 * multiple calls to bind the same list.)
4214 */
4215 @Thunk ArrayList<AppInfo> mTmpAppsList;
4216 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4217 public void run() {
4218 bindAllApplications(mTmpAppsList);
4219 mTmpAppsList = null;
4220 }
4221 };
4222
4223 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004224 * Add the icons for all apps.
4225 *
4226 * Implementation of the method from LauncherModel.Callbacks.
4227 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004228 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004229 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4230 mTmpAppsList = apps;
4231 return;
4232 }
4233
Winson Chungb745afb2015-03-02 11:51:23 -08004234 if (mAppsView != null) {
4235 mAppsView.setApps(apps);
4236 }
Adam Cohen9211d422014-10-07 18:14:53 -07004237 if (mLauncherCallbacks != null) {
4238 mLauncherCallbacks.bindAllApplications(apps);
4239 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004240 }
4241
4242 /**
4243 * A package was updated.
4244 *
4245 * Implementation of the method from LauncherModel.Callbacks.
4246 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004247 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004248 Runnable r = new Runnable() {
4249 public void run() {
4250 bindAppsUpdated(apps);
4251 }
4252 };
4253 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004254 return;
4255 }
4256
Winson Chungb745afb2015-03-02 11:51:23 -08004257 if (mAppsView != null) {
4258 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004259 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004260 }
4261
Sunny Goyal4390ace2014-10-13 11:33:11 -07004262 @Override
4263 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4264 Runnable r = new Runnable() {
4265 public void run() {
4266 bindWidgetsRestored(widgets);
4267 }
4268 };
4269 if (waitUntilResume(r)) {
4270 return;
4271 }
4272 mWorkspace.widgetsRestored(widgets);
4273 }
4274
Joe Onorato9c1289c2009-08-17 11:03:03 -04004275 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004276 * Some shortcuts were updated in the background.
4277 *
4278 * Implementation of the method from LauncherModel.Callbacks.
4279 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004280 @Override
4281 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4282 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004283 Runnable r = new Runnable() {
4284 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004285 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004286 }
4287 };
4288 if (waitUntilResume(r)) {
4289 return;
4290 }
4291
Sunny Goyal4390ace2014-10-13 11:33:11 -07004292 if (!updated.isEmpty()) {
4293 mWorkspace.updateShortcuts(updated);
4294 }
4295
4296 if (!removed.isEmpty()) {
4297 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4298 for (ShortcutInfo si : removed) {
4299 removedComponents.add(si.getTargetComponent());
4300 }
4301 mWorkspace.removeItemsByComponentName(removedComponents, user);
4302 // Notify the drag controller
4303 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004304 }
4305 }
4306
4307 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004308 * Update the state of a package, typically related to install state.
4309 *
4310 * Implementation of the method from LauncherModel.Callbacks.
4311 */
Sunny Goyale755d462014-07-22 13:48:29 -07004312 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004313 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4314 Runnable r = new Runnable() {
4315 public void run() {
4316 bindRestoreItemsChange(updates);
4317 }
4318 };
4319 if (waitUntilResume(r)) {
4320 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004321 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004322
Sunny Goyal756adbc2015-04-16 15:20:51 -07004323 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004324 }
4325
4326 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004327 * A package was uninstalled. We take both the super set of packageNames
4328 * in addition to specific applications to remove, the reason being that
4329 * this can be called when a package is updated as well. In that scenario,
4330 * we only remove specific components from the workspace, where as
4331 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004332 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004333 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004334 * Implementation of the method from LauncherModel.Callbacks.
4335 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004336 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004337 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004338 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004339 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004340 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004341 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004342 }
Winson Chungd64d1762013-08-20 14:37:16 -07004343 };
4344 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004345 return;
4346 }
4347
Sunny Goyal1a745e82014-10-02 15:58:31 -07004348 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004349 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4350 for (AppInfo info : appInfos) {
4351 removedComponents.add(info.componentName);
4352 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004353 if (!packageNames.isEmpty()) {
4354 mWorkspace.removeItemsByPackageName(packageNames, user);
4355 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004356 if (!removedComponents.isEmpty()) {
4357 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004358 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004359 // Notify the drag controller
4360 mDragController.onAppsRemoved(packageNames, removedComponents);
4361
Sunny Goyal1a745e82014-10-02 15:58:31 -07004362 } else {
4363 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004364 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004365
Winson Chungdf95eb12013-10-16 14:57:07 -07004366 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004367 if (mAppsView != null) {
4368 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004369 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004370 }
Joe Onoratobe386092009-11-17 17:32:16 -08004371
Adam Cohen091440a2015-03-18 14:16:05 -07004372 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004373 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004374 public void run() {
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004375 bindAllPackages(mWidgetsAndShortcuts);
Winson Chung83892cc2013-05-01 16:53:33 -07004376 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004377 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004378
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004379 @Override
4380 public void bindAllPackages(final ArrayList<Object> widgetsAndShortcuts) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004381 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4382 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004383 return;
4384 }
4385
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004386 if (mWidgetsView != null && widgetsAndShortcuts != null) {
4387 mWidgetsView.addWidgets(widgetsAndShortcuts, getPackageManager());
4388 mWidgetsAndShortcuts = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004389 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004390 }
4391
Winson Chung400438b2011-01-16 17:53:48 -08004392 private int mapConfigurationOriActivityInfoOri(int configOri) {
4393 final Display d = getWindowManager().getDefaultDisplay();
4394 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4395 switch (d.getRotation()) {
4396 case Surface.ROTATION_0:
4397 case Surface.ROTATION_180:
4398 // We are currently in the same basic orientation as the natural orientation
4399 naturalOri = configOri;
4400 break;
4401 case Surface.ROTATION_90:
4402 case Surface.ROTATION_270:
4403 // We are currently in the other basic orientation to the natural orientation
4404 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4405 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4406 break;
4407 }
4408
4409 int[] oriMap = {
4410 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4411 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4412 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4413 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4414 };
4415 // Since the map starts at portrait, we need to offset if this device's natural orientation
4416 // is landscape.
4417 int indexOffset = 0;
4418 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4419 indexOffset = 1;
4420 }
4421 return oriMap[(d.getRotation() + indexOffset) % 4];
4422 }
Adam Cohen446e9402011-09-15 18:21:21 -07004423
Winson Chung4b919f82012-05-01 10:44:08 -07004424 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004425 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004426 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4427 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4428 .getConfiguration().orientation));
4429 } else {
4430 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4431 }
Winson Chung4b919f82012-05-01 10:44:08 -07004432 }
4433 }
4434 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004435 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004436 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004437 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004438 } else {
4439 mHandler.postDelayed(new Runnable() {
4440 public void run() {
4441 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4442 }
4443 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004444 }
Winson Chung4b919f82012-05-01 10:44:08 -07004445 }
Winson Chung400438b2011-01-16 17:53:48 -08004446 }
4447
Winson Chunge43a1e72014-01-15 10:33:02 -08004448 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004449 if (mLauncherCallbacks != null) {
4450 return mLauncherCallbacks.isLauncherPreinstalled();
4451 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004452 PackageManager pm = getPackageManager();
4453 try {
4454 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4455 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4456 return true;
4457 } else {
4458 return false;
4459 }
4460 } catch (NameNotFoundException e) {
4461 e.printStackTrace();
4462 return false;
4463 }
4464 }
4465
Adam Cohenea90f832014-05-21 15:03:34 -07004466 /**
4467 * This method indicates whether or not we should suggest default wallpaper dimensions
4468 * when our wallpaper cropper was not yet used to set a wallpaper.
4469 */
4470 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004471 if (mLauncherCallbacks != null) {
4472 return mLauncherCallbacks.overrideWallpaperDimensions();
4473 }
Adam Cohenea90f832014-05-21 15:03:34 -07004474 return true;
4475 }
4476
Adam Cohen432609a2014-03-13 17:03:22 -07004477 /**
4478 * To be overridden by subclasses to indicate that there is an activity to launch
4479 * before showing the standard launcher experience.
4480 */
4481 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004482 if (mLauncherCallbacks != null) {
4483 return mLauncherCallbacks.hasFirstRunActivity();
4484 }
Adam Cohen432609a2014-03-13 17:03:22 -07004485 return false;
4486 }
4487
4488 /**
4489 * To be overridden by subclasses to launch any first run activity
4490 */
4491 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004492 if (mLauncherCallbacks != null) {
4493 return mLauncherCallbacks.getFirstRunActivity();
4494 }
Adam Cohen432609a2014-03-13 17:03:22 -07004495 return null;
4496 }
4497
Winson Chung2826a402015-04-17 16:18:53 -07004498 /**
4499 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004500 */
4501 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004502 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4503 return false;
4504 }
4505
Winson Chung2826a402015-04-17 16:18:53 -07004506 if (mLauncherCallbacks != null) {
4507 return mLauncherCallbacks.overrideAllAppsSearch();
4508 }
4509 return false;
4510 }
4511
Winson Chunga6945242014-01-08 14:04:34 -08004512 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004513 return !ActivityManager.isRunningInTestHarness() &&
4514 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004515 }
4516
Adam Cohen4a9423d2014-03-28 14:22:31 -07004517 protected boolean hasRunFirstRunActivity() {
4518 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4519 }
4520
Adam Cohen432609a2014-03-13 17:03:22 -07004521 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004522 if (shouldRunFirstRunActivity() &&
4523 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004524 Intent firstRunIntent = getFirstRunActivity();
4525 if (firstRunIntent != null) {
4526 startActivity(firstRunIntent);
4527 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004528 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004529 }
4530 }
Adam Cohen432609a2014-03-13 17:03:22 -07004531 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004532 }
4533
4534 private void markFirstRunActivityShown() {
4535 SharedPreferences.Editor editor = mSharedPrefs.edit();
4536 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4537 editor.apply();
4538 }
4539
Adam Cohen432609a2014-03-13 17:03:22 -07004540 /**
4541 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4542 * screen that must be displayed and dismissed.
4543 */
4544 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004545 if (mLauncherCallbacks != null) {
4546 return mLauncherCallbacks.hasDismissableIntroScreen();
4547 }
Adam Cohen432609a2014-03-13 17:03:22 -07004548 return false;
4549 }
4550
4551 /**
4552 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4553 */
4554 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004555 if (mLauncherCallbacks != null) {
4556 return mLauncherCallbacks.getIntroScreen();
4557 }
Adam Cohen432609a2014-03-13 17:03:22 -07004558 return null;
4559 }
4560
4561 /**
4562 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4563 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4564 */
4565 private boolean shouldShowIntroScreen() {
4566 return hasDismissableIntroScreen() &&
4567 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4568 }
4569
4570 protected void showIntroScreen() {
4571 View introScreen = getIntroScreen();
4572 changeWallpaperVisiblity(false);
4573 if (introScreen != null) {
4574 mDragLayer.showOverlayView(introScreen);
4575 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004576 if (mLauncherOverlayContainer != null) {
4577 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4578 }
Adam Cohen432609a2014-03-13 17:03:22 -07004579 }
4580
4581 public void dismissIntroScreen() {
4582 markIntroScreenDismissed();
4583 if (showFirstRunActivity()) {
4584 // We delay hiding the intro view until the first run activity is showing. This
4585 // avoids a blip.
4586 mWorkspace.postDelayed(new Runnable() {
4587 @Override
4588 public void run() {
4589 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004590 if (mLauncherOverlayContainer != null) {
4591 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4592 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004593 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004594 }
4595 }, ACTIVITY_START_DELAY);
4596 } else {
4597 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004598 if (mLauncherOverlayContainer != null) {
4599 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4600 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004601 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004602 }
4603 changeWallpaperVisiblity(true);
4604 }
4605
4606 private void markIntroScreenDismissed() {
4607 SharedPreferences.Editor editor = mSharedPrefs.edit();
4608 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4609 editor.apply();
4610 }
4611
Adam Cohen091440a2015-03-18 14:16:05 -07004612 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004613 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4614 // on the device, then we always show the first run cling experience (or if there is no
4615 // launcher2). Otherwise, we prompt the user upon started for migration
4616 LauncherClings launcherClings = new LauncherClings(this);
4617 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4618 if (mModel.canMigrateFromOldLauncherDb(this)) {
4619 launcherClings.showMigrationCling();
4620 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004621 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004622 }
4623 }
4624 }
4625
Winson Chunga6945242014-01-08 14:04:34 -08004626 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004627 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4628 if (mHotseat != null) mHotseat.setAlpha(1f);
4629 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4630 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004631 }
4632
4633 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004634 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4635 if (mHotseat != null) mHotseat.setAlpha(0f);
4636 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4637 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004638 }
4639
Mathew Inwood72fbec12013-11-19 15:45:07 +00004640 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004641 // Called from search suggestion, not supported in other profiles.
4642 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4643 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4644 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4645 myUser);
4646 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004647 return null;
4648 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004649 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004650 }
4651
4652 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4653 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004654 // Called from search suggestion, not supported in other profiles.
4655 return createShortcutDragInfo(shortcutIntent, caption, icon,
4656 UserHandleCompat.myUserHandle());
4657 }
4658
4659 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4660 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004661 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004662 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004663 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004664 }
4665
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004666 protected void moveWorkspaceToDefaultScreen() {
4667 mWorkspace.moveToDefaultScreen(false);
4668 }
4669
Mathew Inwood72fbec12013-11-19 15:45:07 +00004670 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4671 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004672 mWorkspace.onExternalDragStartedWithItem(dragView);
4673 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004674 }
4675
Anjali Koppalf5d29132014-02-28 13:33:27 -08004676 @Override
4677 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004678 if (mLauncherCallbacks != null) {
4679 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4680 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004681 }
4682
Winson Chung80baf5a2010-08-09 16:03:15 -07004683 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004684 * Prints out out state for debugging.
4685 */
4686 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004687 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004688 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004689 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4690 Log.d(TAG, "mRestoring=" + mRestoring);
4691 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4692 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004693 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004694 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004695 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004696
Daniel Sandler325dc232013-06-05 22:57:57 -04004697 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004698 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004699
4700 @Override
4701 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4702 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004703 synchronized (sDumpLogs) {
4704 writer.println(" ");
4705 writer.println("Debug logs: ");
4706 for (int i = 0; i < sDumpLogs.size(); i++) {
4707 writer.println(" " + sDumpLogs.get(i));
4708 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004709 }
Adam Cohen9211d422014-10-07 18:14:53 -07004710 if (mLauncherCallbacks != null) {
4711 mLauncherCallbacks.dump(prefix, fd, writer, args);
4712 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004713 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004714
4715 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004716 if (DEBUG_DUMP_LOG) {
4717 synchronized (sDumpLogs) {
4718 Log.d(TAG, "");
4719 Log.d(TAG, "*********************");
4720 Log.d(TAG, "Launcher debug logs: ");
4721 for (int i = 0; i < sDumpLogs.size(); i++) {
4722 Log.d(TAG, " " + sDumpLogs.get(i));
4723 }
4724 Log.d(TAG, "*********************");
4725 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004726 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004727 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004728 }
4729
4730 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004731 addDumpLog(tag, log, null, debugLog);
4732 }
4733
4734 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004735 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004736 if (e != null) {
4737 Log.d(tag, log, e);
4738 } else {
4739 Log.d(tag, log);
4740 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004741 }
Winson Chungede41292013-09-19 16:27:36 -07004742 if (DEBUG_DUMP_LOG) {
4743 sDateStamp.setTime(System.currentTimeMillis());
4744 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004745 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4746 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004747 }
Winson Chungede41292013-09-19 16:27:36 -07004748 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004749 }
4750
Adam Cohen59400422014-03-05 18:07:04 -08004751 public static CustomAppWidget getCustomAppWidget(String name) {
4752 return sCustomAppWidgets.get(name);
4753 }
4754
4755 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4756 return sCustomAppWidgets;
4757 }
4758
Winson Chungede41292013-09-19 16:27:36 -07004759 public void dumpLogsToLocalData() {
4760 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004761 new AsyncTask<Void, Void, Void>() {
4762 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004763 boolean success = false;
4764 sDateStamp.setTime(sRunStart);
4765 String FILENAME = sDateStamp.getMonth() + "-"
4766 + sDateStamp.getDay() + "_"
4767 + sDateStamp.getHours() + "-"
4768 + sDateStamp.getMinutes() + "_"
4769 + sDateStamp.getSeconds() + ".txt";
4770
4771 FileOutputStream fos = null;
4772 File outFile = null;
4773 try {
4774 outFile = new File(getFilesDir(), FILENAME);
4775 outFile.createNewFile();
4776 fos = new FileOutputStream(outFile);
4777 } catch (Exception e) {
4778 e.printStackTrace();
4779 }
4780 if (fos != null) {
4781 PrintWriter writer = new PrintWriter(fos);
4782
4783 writer.println(" ");
4784 writer.println("Debug logs: ");
4785 synchronized (sDumpLogs) {
4786 for (int i = 0; i < sDumpLogs.size(); i++) {
4787 writer.println(" " + sDumpLogs.get(i));
4788 }
4789 }
4790 writer.close();
4791 }
4792 try {
4793 if (fos != null) {
4794 fos.close();
4795 success = true;
4796 }
4797 } catch (IOException e) {
4798 e.printStackTrace();
4799 }
Michael Jurka43467462013-11-14 12:03:58 +01004800 return null;
Winson Chungede41292013-09-19 16:27:36 -07004801 }
Michael Jurka43467462013-11-14 12:03:58 +01004802 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004803 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004804 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004805}
Michael Jurka2763be32011-02-24 11:19:57 -08004806
Michael Jurkaabded662011-03-04 12:06:57 -08004807interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004808 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004809 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004810 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004811 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004812 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004813}
Dan Sandlerd5024042014-01-09 15:01:33 -05004814
4815interface DebugIntents {
4816 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4817 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004818}