blob: c708b3c8e33f5ec1c5c35ac2d9d1d35ad3e39cdd [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070078import android.view.MotionEvent;
79import android.view.Surface;
80import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070081import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070084import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070086import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.widget.Advanceable;
91import android.widget.FrameLayout;
92import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080093import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070095
Sunny Goyal651077b2014-06-30 14:15:31 -070096import com.android.launcher3.DropTarget.DragObject;
97import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070098import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010099import com.android.launcher3.compat.LauncherActivityInfoCompat;
100import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700101import com.android.launcher3.compat.PackageInstallerCompat;
102import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
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
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700140 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700141 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400142 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700143 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700144
Dan Sandlerd5024042014-01-09 15:01:33 -0500145 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700150 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Michael Jurka8b805b12012-04-18 14:23:14 -0700152 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700153 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700154
Mathew Inwood876a8462013-06-14 14:12:41 +0100155 /**
156 * IntentStarter uses request codes starting with this. This must be greater than all activity
157 * request codes used internally.
158 */
159 protected static final int REQUEST_LAST = 100;
160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
162
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800163 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Michael Jurka0a457bf2012-11-19 14:05:05 -0800165 // To turn on these properties, type
166 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800167 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Winson Chung2672ff92012-05-04 16:22:30 -0700169 // The Intent extra that defines whether to ignore the launch animation
170 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400171 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 // Type: int
174 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700175 // Type: int
176 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700178 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
179 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
181 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700182 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700184 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: boolean
186 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
187 // Type: long
188 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700189 // Type: int
190 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
191 // Type: int
192 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
193 // Type: parcelable
194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000195 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000197 // Type: int[]
198 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Adam Cohen432609a2014-03-13 17:03:22 -0700200 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800201 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
202
Adam Cohen3ed316a2014-07-23 18:21:20 -0700203 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
204 static final String ACTION_FIRST_LOAD_COMPLETE =
205 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
206
Adam Cohen39a06042013-07-19 14:30:12 -0700207 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700208 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700209
Sunny Goyal594d76d2014-11-06 10:12:54 -0800210 private static final String QSB_WIDGET_ID = "qsb_widget_id";
211 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
212
Winson Chunga6945242014-01-08 14:04:34 -0800213 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
214
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700215 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800216 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700217 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700218 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700219
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700220 private boolean mIsSafeModeEnabled;
221
Adam Cohenc2d6e892014-10-16 09:49:24 -0700222 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
223 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700224 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700225
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700227 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
228 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700229 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000231 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
232 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
233
Winson Chunga2413752012-04-03 14:22:34 -0700234 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700235 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
236 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700237 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700238
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800239 private final BroadcastReceiver mCloseSystemDialogsReceiver
240 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800241 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 private LayoutInflater mInflater;
244
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700246 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800247 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700248 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800249 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700250 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700251
Sunny Goyalffe83f12014-08-14 17:39:34 -0700252 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700253 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700254
Adam Cohen091440a2015-03-18 14:16:05 -0700255 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800256 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800257 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700258
Michael Jurka0280c3b2010-09-17 15:00:07 -0700259 private int[] mTmpAddItemCellCoordinates = new int[2];
260
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 private FolderInfo mFolderInfo;
262
Winson Chung3d503fb2011-07-13 17:25:49 -0700263 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800264 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700265
Michael Jurka838a4ca2011-02-07 13:33:06 -0800266 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700267
Winson Chungc51db6a2011-10-05 11:44:49 -0700268 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700269
270 // Main container view for the all apps screen.
Adam Cohen091440a2015-03-18 14:16:05 -0700271 @Thunk AppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700272
273 // Main container view for the widget tray screen.
274 private WidgetsContainerView mWidgetsView;
275
Winson Chungf0ea4d32011-06-06 14:27:16 -0700276 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800277 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700280 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
281 // scroll issues (because the workspace may not have been measured yet) and extra work.
282 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
283 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284
285 private SpannableStringBuilder mDefaultKeySsb = null;
286
Adam Cohen091440a2015-03-18 14:16:05 -0700287 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400288
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800289 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 private boolean mRestoring;
291 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700292 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293
Michael Jurka1e2f4652013-07-08 18:03:46 -0700294 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700295 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
296
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 private Bundle mSavedInstanceState;
298
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800300 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700301 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800302 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700303 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305
Adam Cohen091440a2015-03-18 14:16:05 -0700306 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700307
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700308 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700309
Adam Cohen61f560d2013-09-30 15:58:20 -0700310 private View.OnTouchListener mHapticFeedbackTouchListener;
311
Adam Cohended9f8d2010-11-03 13:25:16 -0700312 // Related to the auto-advancing of widgets
313 private final int ADVANCE_MSG = 1;
314 private final int mAdvanceInterval = 20000;
315 private final int mAdvanceStagger = 250;
316 private long mAutoAdvanceSentTime;
317 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700318 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700319 new HashMap<View, AppWidgetProviderInfo>();
320
Winson Chung400438b2011-01-16 17:53:48 -0800321 // Determines how long to wait after a rotation before restoring the screen orientation to
322 // match the sensor state.
323 private final int mRestoreScreenOrientationDelay = 500;
324
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700326
Adam Cohen1462de32012-07-24 22:34:36 -0700327 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700328 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700329
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700330 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700331 static Date sDateStamp = new Date();
332 static DateFormat sDateFormat =
333 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
334 static long sRunStart = System.currentTimeMillis();
335 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700336
Winson Chung46353de2012-02-16 14:05:10 -0800337 // We only want to get the SharedPreferences once since it does an FS stat each time we get
338 // it from the context.
339 private SharedPreferences mSharedPrefs;
340
Winson Chungf0c6ae02012-03-21 16:10:31 -0700341 // Holds the page that we need to animate to, and the icon views that we need to animate up
342 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700343 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700344 private Bitmap mFolderIconBitmap;
345 private Canvas mFolderIconCanvas;
346 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700347
Michael Jurkaddd62e92011-02-16 17:49:14 -0800348 private BubbleTextView mWaitingForResume;
349
Adam Cohen59400422014-03-05 18:07:04 -0800350 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
351 new HashMap<String, CustomAppWidget>();
352
353 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
354 static {
355 if (ENABLE_CUSTOM_WIDGET_TEST) {
356 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
357 }
358 }
359
Chet Haasea8f996d2015-02-17 12:56:04 -0800360 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
361 private static Method sClipRevealMethod = null;
362 static {
363 Class<?> activityOptionsClass = ActivityOptions.class;
364 try {
365 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
366 View.class, int.class, int.class, int.class, int.class);
367 } catch (Exception e) {
368 // Earlier version
369 }
370 }
371
Adam Cohen091440a2015-03-18 14:16:05 -0700372 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700373 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700374 if (mWorkspace != null) {
375 mWorkspace.buildPageHardwareLayers();
376 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700377 }
378 };
379
Adam Cohendb364c32014-05-20 14:23:40 -0700380 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800381
Adam Cohen091440a2015-03-18 14:16:05 -0700382 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800383 int requestCode;
384 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700385 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700386 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800387 int cellX;
388 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700389 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800390 }
391
Daniel Sandlerff02d492013-08-05 02:12:05 -0400392 private Stats mStats;
393
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700394 FocusIndicatorView mFocusHandler;
395
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 @Override
397 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700398 if (DEBUG_STRICT_MODE) {
399 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
400 .detectDiskReads()
401 .detectDiskWrites()
402 .detectNetwork() // or .detectAll() for all detectable problems
403 .penaltyLog()
404 .build());
405 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
406 .detectLeakedSqlLiteObjects()
407 .detectLeakedClosableObjects()
408 .penaltyLog()
409 .penaltyDeath()
410 .build());
411 }
412
Adam Cohen9211d422014-10-07 18:14:53 -0700413 if (mLauncherCallbacks != null) {
414 mLauncherCallbacks.preOnCreate();
415 }
416
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400418
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400419 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400420 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700421 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700422
Winson Chung5f8afe62013-08-12 16:19:28 -0700423 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700424 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700425
426 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400427 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700428 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700429 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800430 mModel = app.setLauncher(this);
431 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700432 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400433 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800435 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700436
Daniel Sandlerff02d492013-08-05 02:12:05 -0400437 mStats = new Stats(this);
438
Sunny Goyalffe83f12014-08-14 17:39:34 -0700439 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700440
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700441 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
442 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700443
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700444 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
445 // this also ensures that any synchronous binding below doesn't re-trigger another
446 // LauncherModel load.
447 mPaused = false;
448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200450 android.os.Debug.startMethodTracing(
451 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 }
453
454 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100458 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800460 registerContentObservers();
461
Joe Onorato7c312c12009-08-13 21:36:53 -0700462 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700463
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 mSavedState = savedInstanceState;
465 restoreState(mSavedState);
466
467 if (PROFILE_STARTUP) {
468 android.os.Debug.stopMethodTracing();
469 }
470
471 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700472 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700473 // If the user leaves launcher, then we should just load items asynchronously when
474 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500475 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700476 } else {
477 // We only load the page synchronously if the user rotates (or triggers a
478 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800479 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700480 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 }
482
483 // For handling default keys
484 mDefaultKeySsb = new SpannableStringBuilder();
485 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800486
487 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
488 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800489
Adam Cohenf9426d52012-06-04 17:26:21 -0700490 // On large interfaces, we want the screen to auto-rotate based on the current orientation
491 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700492
Adam Cohen9211d422014-10-07 18:14:53 -0700493 if (mLauncherCallbacks != null) {
494 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700495 if (mLauncherCallbacks.hasLauncherOverlay()) {
496 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700497 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
498 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
499 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700500 mWorkspace.setLauncherOverlay(mLauncherOverlay);
501 }
Adam Cohen9211d422014-10-07 18:14:53 -0700502 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700503
504 if (shouldShowIntroScreen()) {
505 showIntroScreen();
506 } else {
507 showFirstRunActivity();
508 showFirstRunClings();
509 }
Adam Cohen9211d422014-10-07 18:14:53 -0700510 }
511
512 private LauncherCallbacks mLauncherCallbacks;
513
514 public void onPostCreate(Bundle savedInstanceState) {
515 super.onPostCreate(savedInstanceState);
516 if (mLauncherCallbacks != null) {
517 mLauncherCallbacks.onPostCreate(savedInstanceState);
518 }
519 }
520
521 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
522 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700523 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
524 @Override
525 public void onAllAppsBoundsChanged(Rect bounds) {
526 mAppsView.setFixedBounds(Launcher.this, bounds);
527 }
528
529 @Override
530 public void dismissAllApps() {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700531 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true, null,
532 false /* notifyLauncherCallbacks */);
Winson Chung0f785722015-04-08 10:27:49 -0700533 }
534 });
Adam Cohen9211d422014-10-07 18:14:53 -0700535 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700536 }
537
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700538 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700539 public void onLauncherProviderChange() {
540 if (mLauncherCallbacks != null) {
541 mLauncherCallbacks.onLauncherProviderChange();
542 }
543 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700544
Adam Cohen9211d422014-10-07 18:14:53 -0700545 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700546 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700547 if (mLauncherCallbacks != null) {
548 return mLauncherCallbacks.hasCustomContentToLeft();
549 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700550 return false;
551 }
552
Dave Hawkeya8881582013-09-17 15:55:33 -0600553 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500554 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600555 * {@link #addToCustomContentPage}. This will only be invoked if
556 * {@link #hasCustomContentToLeft()} is {@code true}.
557 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500558 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700559 if (mLauncherCallbacks != null) {
560 mLauncherCallbacks.populateCustomContentContainer();
561 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600562 }
563
564 /**
565 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
566 * ensure the custom content page is added or removed if necessary.
567 */
568 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600569 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600570 // Not bound yet, wait for bindScreens to be called.
571 return;
572 }
573
574 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
575 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500576 mWorkspace.createCustomContentContainer();
577 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600578 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
579 mWorkspace.removeCustomContentPage();
580 }
581 }
582
Adam Cohen091440a2015-03-18 14:16:05 -0700583 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700584 if (sLocaleConfiguration == null) {
585 new AsyncTask<Void, Void, LocaleConfiguration>() {
586 @Override
587 protected LocaleConfiguration doInBackground(Void... unused) {
588 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
589 readConfiguration(Launcher.this, localeConfiguration);
590 return localeConfiguration;
591 }
592
593 @Override
594 protected void onPostExecute(LocaleConfiguration result) {
595 sLocaleConfiguration = result;
596 checkForLocaleChange(); // recursive, but now with a locale configuration
597 }
598 }.execute();
599 return;
600 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700601
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 final Configuration configuration = getResources().getConfiguration();
603
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 final String locale = configuration.locale.toString();
606
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700607 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 final int mcc = configuration.mcc;
609
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700610 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 final int mnc = configuration.mnc;
612
Romain Guy84f296c2009-11-04 15:00:44 -0800613 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614
Romain Guy84f296c2009-11-04 15:00:44 -0800615 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700616 sLocaleConfiguration.locale = locale;
617 sLocaleConfiguration.mcc = mcc;
618 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700619
Joe Onorato0589f0f2010-02-08 13:44:00 -0800620 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700621
622 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100623 new AsyncTask<Void, Void, Void>() {
624 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700625 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100626 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700627 }
Michael Jurka43467462013-11-14 12:03:58 +0100628 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700629 }
630 }
631
Adam Cohen091440a2015-03-18 14:16:05 -0700632 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700633 public String locale;
634 public int mcc = -1;
635 public int mnc = -1;
636 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700637
Adam Cohen091440a2015-03-18 14:16:05 -0700638 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700639 DataInputStream in = null;
640 try {
Helena Josol28db2802014-10-09 17:04:09 +0100641 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700642 configuration.locale = in.readUTF();
643 configuration.mcc = in.readInt();
644 configuration.mnc = in.readInt();
645 } catch (FileNotFoundException e) {
646 // Ignore
647 } catch (IOException e) {
648 // Ignore
649 } finally {
650 if (in != null) {
651 try {
652 in.close();
653 } catch (IOException e) {
654 // Ignore
655 }
656 }
657 }
658 }
659
Adam Cohen091440a2015-03-18 14:16:05 -0700660 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700661 DataOutputStream out = null;
662 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100663 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100664 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700665 out.writeUTF(configuration.locale);
666 out.writeInt(configuration.mcc);
667 out.writeInt(configuration.mnc);
668 out.flush();
669 } catch (FileNotFoundException e) {
670 // Ignore
671 } catch (IOException e) {
672 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100673 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700674 } finally {
675 if (out != null) {
676 try {
677 out.close();
678 } catch (IOException e) {
679 // Ignore
680 }
681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
683 }
684
Anton Hanssona2f665f2013-09-26 12:18:32 +0100685 public Stats getStats() {
686 return mStats;
687 }
688
Bjorn Bringertc459e522013-06-07 19:36:01 +0100689 public LayoutInflater getInflater() {
690 return mInflater;
691 }
692
Hyunyoung Song3f471442015-04-08 19:01:34 -0700693 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700694 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
695 // that is subsequently removed from the workspace in startBinding().
696 return !mModel.isLoadingWorkspace();
697 }
698
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800699 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000700 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100701 if (Build.VERSION.SDK_INT >= 17) {
702 return View.generateViewId();
703 } else {
704 // View.generateViewId() is not available. The following fallback logic is a copy
705 // of its implementation.
706 for (;;) {
707 final int result = sNextGeneratedId.get();
708 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
709 int newValue = result + 1;
710 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
711 if (sNextGeneratedId.compareAndSet(result, newValue)) {
712 return result;
713 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000714 }
715 }
716 }
717
718 public int getViewIdForItem(ItemInfo info) {
719 // This cast is safe given the > 2B range for int.
720 int itemId = (int) info.id;
721 if (mItemIdToViewId.containsKey(itemId)) {
722 return mItemIdToViewId.get(itemId);
723 }
724 int viewId = generateViewId();
725 mItemIdToViewId.put(itemId, viewId);
726 return viewId;
727 }
728
729 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700730 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
731 * a configuration step, this allows the proper animations to run after other transitions.
732 */
Adam Cohendb364c32014-05-20 14:23:40 -0700733 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700734 long screenId = args.screenId;
735 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
736 // When the screen id represents an actual screen (as opposed to a rank) we make sure
737 // that the drop page actually exists.
738 screenId = ensurePendingDropLayoutExists(args.screenId);
739 }
Adam Cohendb364c32014-05-20 14:23:40 -0700740
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800741 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800742 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700743 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700744 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700745 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800746 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700747 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700748 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700749 case REQUEST_RECONFIGURE_APPWIDGET:
750 completeRestoreAppWidget(args.appWidgetId);
751 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800752 }
Michael Jurka27614d22012-04-02 06:40:22 -0700753 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
754 // if you turned the screen off and then back while in All Apps, Launcher would not
755 // return to the workspace. Clearing mAddInfo.container here fixes this issue
756 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700757 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800758 }
759
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800760 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700761 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700762 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700763 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700764 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800765 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700766
Adam Cohenad4e15c2013-10-17 16:21:35 -0700767 Runnable exitSpringLoaded = new Runnable() {
768 @Override
769 public void run() {
770 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
771 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
772 }
773 };
774
Michael Jurka8b805b12012-04-18 14:23:14 -0700775 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700776 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700777 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
778 if (resultCode == RESULT_CANCELED) {
779 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700780 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700782 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800783 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700784 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700785 }
786 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200787 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
788 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700789 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200790 }
791 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700792 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200793
Adam Cohened66b2b2012-01-23 17:28:51 -0800794 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
795 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700796
Adam Cohendb364c32014-05-20 14:23:40 -0700797 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800798 // We have special handling for widgets
799 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800800 final int appWidgetId;
801 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
802 : -1;
803 if (widgetId < 0) {
804 appWidgetId = pendingAddWidgetId;
805 } else {
806 appWidgetId = widgetId;
807 }
808
Adam Cohenad4e15c2013-10-17 16:21:35 -0700809 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800810 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700811 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
812 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700813 result = RESULT_CANCELED;
814 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700815 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700816 @Override
817 public void run() {
818 exitSpringLoadedDragModeDelayed(false, 0, null);
819 }
820 };
Adam Cohendb364c32014-05-20 14:23:40 -0700821 if (workspaceLocked) {
822 // No need to remove the empty screen if we're mid-binding, as the
823 // the bind will not add the empty screen.
824 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
825 } else {
826 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
827 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
828 }
Winson Chung5aaab772012-04-27 15:24:02 -0700829 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700830 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700831 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
832 // When the screen id represents an actual screen (as opposed to a rank)
833 // we make sure that the drop page actually exists.
834 mPendingAddInfo.screenId =
835 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
836 }
Adam Cohendb364c32014-05-20 14:23:40 -0700837 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
838
839 dropLayout.setDropPending(true);
840 final Runnable onComplete = new Runnable() {
841 @Override
842 public void run() {
843 completeTwoStageWidgetDrop(resultCode, appWidgetId);
844 dropLayout.setDropPending(false);
845 }
846 };
847 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
848 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
849 } else {
850 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
851 mPendingAddInfo);
852 sPendingAddItem = args;
853 }
Winson Chung5aaab772012-04-27 15:24:02 -0700854 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800855 return;
856 }
857
Sunny Goyalff572272014-07-23 13:58:07 -0700858 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
859 if (resultCode == RESULT_OK) {
860 // Update the widget view.
861 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
862 pendingAddWidgetId, mPendingAddInfo);
863 if (workspaceLocked) {
864 sPendingAddItem = args;
865 } else {
866 completeAdd(args);
867 }
868 }
869 // Leave the widget in the pending state if the user canceled the configure.
870 return;
871 }
872
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 // The pattern used here is that a user PICKs a specific application,
874 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700875
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
877 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700878 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700879 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
880 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800881 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700882 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800883 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700884 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700885 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
886 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 }
Adam Cohen00074722013-10-11 17:46:09 -0700888 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700889 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700890 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800892 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800893
894 }
895
896 @Override
897 protected void onActivityResult(
898 final int requestCode, final int resultCode, final Intent data) {
899 handleActivityResult(requestCode, resultCode, data);
900 if (mLauncherCallbacks != null) {
901 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
902 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800903 }
904
Adam Cohendb364c32014-05-20 14:23:40 -0700905 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
906 appWidgetId, ItemInfo info) {
907 PendingAddArguments args = new PendingAddArguments();
908 args.requestCode = requestCode;
909 args.intent = data;
910 args.container = info.container;
911 args.screenId = info.screenId;
912 args.cellX = info.cellX;
913 args.cellY = info.cellY;
914 args.appWidgetId = appWidgetId;
915 return args;
916 }
917
918 /**
919 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
920 *
921 * @param screenId the screen id to check
922 * @return the new screen, or screenId if it exists
923 */
924 private long ensurePendingDropLayoutExists(long screenId) {
925 CellLayout dropLayout =
926 (CellLayout) mWorkspace.getScreenWithId(screenId);
927 if (dropLayout == null) {
928 // it's possible that the add screen was removed because it was
929 // empty and a re-bind occurred
930 mWorkspace.addExtraEmptyScreen();
931 return mWorkspace.commitExtraEmptyScreen();
932 } else {
933 return screenId;
934 }
935 }
936
Adam Cohen091440a2015-03-18 14:16:05 -0700937 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700938 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700939 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800940 Runnable onCompleteRunnable = null;
941 int animationType = 0;
942
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700943 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800944 if (resultCode == RESULT_OK) {
945 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
946 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700947 mPendingAddWidgetInfo);
948 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800949 onCompleteRunnable = new Runnable() {
950 @Override
951 public void run() {
952 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700953 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700954 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
955 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800956 }
957 };
958 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800959 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800960 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800961 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700962 if (mDragLayer.getAnimatedView() != null) {
963 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
964 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
965 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700966 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700967 // The animated view may be null in the case of a rotation during widget configuration
968 onCompleteRunnable.run();
969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 }
971
972 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700973 protected void onStop() {
974 super.onStop();
975 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700976
977 if (mLauncherCallbacks != null) {
978 mLauncherCallbacks.onStop();
979 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700980 }
981
982 @Override
983 protected void onStart() {
984 super.onStart();
985 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700986
987 if (mLauncherCallbacks != null) {
988 mLauncherCallbacks.onStart();
989 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700990 }
991
992 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200994 long startTime = 0;
995 if (DEBUG_RESUME_TIME) {
996 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400997 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200998 }
Adam Cohen9211d422014-10-07 18:14:53 -0700999
1000 if (mLauncherCallbacks != null) {
1001 mLauncherCallbacks.preOnResume();
1002 }
1003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001005
Winson Chung4a2afa32012-07-19 14:53:05 -07001006 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001007 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001008 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001009 } else if (mOnResumeState == State.APPS) {
1010 showAppsView(false /* animated */, false /* resetListToTop */);
1011 } else if (mOnResumeState == State.WIDGETS) {
1012 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001013 }
1014 mOnResumeState = State.NONE;
1015
Craig Mautner360310b2012-10-26 15:13:08 -07001016 // Background was set to gradient in onPause(), restore to black if in all apps.
1017 setWorkspaceBackground(mState == State.WORKSPACE);
1018
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001019 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001020 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001021 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001022 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001024 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001026 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001027 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1028 // execute them here
1029 long startTimeCallbacks = 0;
1030 if (DEBUG_RESUME_TIME) {
1031 startTimeCallbacks = System.currentTimeMillis();
1032 }
1033
Michael Jurka1e2f4652013-07-08 18:03:46 -07001034 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1035 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001036 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001037 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001038 if (DEBUG_RESUME_TIME) {
1039 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1040 (System.currentTimeMillis() - startTimeCallbacks));
1041 }
Michael Jurka447bf842013-05-15 14:52:15 +02001042 }
Michael Jurka54554252013-08-01 12:52:23 +02001043 if (mOnResumeCallbacks.size() > 0) {
1044 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1045 mOnResumeCallbacks.get(i).run();
1046 }
1047 mOnResumeCallbacks.clear();
1048 }
Winson Chunge4e50662012-01-23 14:45:13 -08001049
1050 // Reset the pressed state of icons that were locked in the press state while activities
1051 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001052 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001053 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001054 mWaitingForResume.setStayPressed(false);
1055 }
Winson Chung82963d52013-10-09 11:20:57 -07001056
Adam Cohen06dff352012-06-01 17:17:08 -07001057 // It is possible that widgets can receive updates while launcher is not in the foreground.
1058 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1059 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1060 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001061 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001062 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001063
Winson Chung780fe592013-09-26 14:48:44 -07001064 // Process any items that were added while Launcher was away.
1065 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1066
Michael Jurka447bf842013-05-15 14:52:15 +02001067 if (DEBUG_RESUME_TIME) {
1068 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1069 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001070
1071 if (mWorkspace.getCustomContentCallbacks() != null) {
1072 // If we are resuming and the custom content is the current page, we call onShow().
1073 // It is also poassible that onShow will instead be called slightly after first layout
1074 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1075 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001076 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001077 }
1078 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001079 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001080 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001081
1082 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001083
1084 if (mLauncherCallbacks != null) {
1085 mLauncherCallbacks.onResume();
1086 }
Adam Cohen06dff352012-06-01 17:17:08 -07001087 }
1088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001090 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001091 // Ensure that items added to Launcher are queued until Launcher returns
1092 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001093 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001094
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001096 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001097 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001098 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001099
1100 // We call onHide() aggressively. The custom content callbacks should be able to
1101 // debounce excess onHide calls.
1102 if (mWorkspace.getCustomContentCallbacks() != null) {
1103 mWorkspace.getCustomContentCallbacks().onHide();
1104 }
Romain Guycbb89e42009-06-08 15:52:54 -07001105
Adam Cohen9211d422014-10-07 18:14:53 -07001106 if (mLauncherCallbacks != null) {
1107 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001108 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001109 }
1110
1111 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001112 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1113 // by a onResume or by scrolling otherwise.
1114 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001115
1116 // Custom content is completely hidden
1117 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001118
1119 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1120 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001121
1122 // Indicates whether the user is allowed to scroll away from the custom content.
1123 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001124 }
1125
Adam Cohenc2d6e892014-10-16 09:49:24 -07001126 public interface LauncherOverlay {
1127
1128 /**
1129 * Touch interaction leading to overscroll has begun
1130 */
1131 public void onScrollInteractionBegin();
1132
1133 /**
1134 * Touch interaction related to overscroll has ended
1135 */
1136 public void onScrollInteractionEnd();
1137
1138 /**
1139 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1140 * screen (or in the case of RTL, the rightmost screen).
1141 */
1142 public void onScrollChange(int progress, boolean rtl);
1143
1144 /**
1145 * Screen has stopped scrolling
1146 */
1147 public void onScrollSettled();
1148
1149 /**
1150 * This method can be called by the Launcher in order to force the LauncherOverlay
1151 * to exit fully immersive mode.
1152 */
1153 public void forceExitFullImmersion();
1154 }
1155
Winson Chung0f785722015-04-08 10:27:49 -07001156 public interface LauncherAppsCallbacks {
1157 /**
1158 * Updates launcher to the available space that AllApps can take so as not to overlap with
1159 * any other views.
1160 */
1161 public void onAllAppsBoundsChanged(Rect bounds);
1162
1163 /**
1164 * Called to dismiss all apps if it is showing.
1165 */
1166 public void dismissAllApps();
1167 }
1168
Adam Cohenc2d6e892014-10-16 09:49:24 -07001169 public interface LauncherOverlayCallbacks {
1170 /**
1171 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1172 * however it doesn't modify any state within the launcher.
1173 */
1174 public boolean canEnterFullImmersion();
1175
1176 /**
1177 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1178 * eg. by occupying the full screen and handling all touch events.
1179 *
1180 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1181 * case, Launcher will modify any necessary state and assumes the overlay is
1182 * handling all interaction. If false, the LauncherOverlay should cancel any
1183 *
1184 */
1185 public boolean enterFullImmersion();
1186
1187 /**
1188 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1189 * full control over UI and state.
1190 */
1191 public void exitFullImmersion();
1192 }
1193
1194 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1195
1196 @Override
1197 public boolean canEnterFullImmersion() {
1198 return mState == State.WORKSPACE;
1199 }
1200
1201 @Override
1202 public boolean enterFullImmersion() {
1203 if (mState == State.WORKSPACE) {
1204 // When fully immersed, disregard any touches which fall through.
1205 mDragLayer.setBlockTouch(true);
1206 return true;
1207 }
1208 return false;
1209 }
1210
1211 @Override
1212 public void exitFullImmersion() {
1213 mDragLayer.setBlockTouch(false);
1214 }
1215 }
1216
Jorim Jaggid017f882014-01-14 17:08:48 -08001217 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001218 if (mLauncherCallbacks != null) {
1219 return mLauncherCallbacks.hasSettings();
1220 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001221 return false;
1222 }
1223
Adam Cohen9211d422014-10-07 18:14:53 -07001224 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001225 CustomContentCallbacks callbacks, String description) {
1226 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001227 }
1228
Adam Cohenedb40762013-07-18 16:45:45 -07001229 // The custom content needs to offset its content to account for the QSB
1230 public int getTopOffsetForCustomContent() {
1231 return mWorkspace.getPaddingTop();
1232 }
1233
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001234 @Override
1235 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001236 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001237 if (mModel.isCurrentCallbacks(this)) {
1238 mModel.stopLoader();
1239 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001240 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1241
Romain Guy13c2e7b2010-03-10 19:45:00 -08001242 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001243 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001244
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001245 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001246 @Override
1247 public void onWindowFocusChanged(boolean hasFocus) {
1248 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001249 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001250
1251 if (mLauncherCallbacks != null) {
1252 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1253 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001254 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 private boolean acceptFilter() {
1257 final InputMethodManager inputManager = (InputMethodManager)
1258 getSystemService(Context.INPUT_METHOD_SERVICE);
1259 return !inputManager.isFullscreenMode();
1260 }
1261
1262 @Override
1263 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001264 final int uniChar = event.getUnicodeChar();
1265 final boolean handled = super.onKeyDown(keyCode, event);
1266 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1267 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1269 keyCode, event);
1270 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001271 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001272 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001273 // showSearchDialog()
1274 // If there are multiple keystrokes before the search dialog takes focus,
1275 // onSearchRequested() will be called for every keystroke,
1276 // but it is idempotent, so it's fine.
1277 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 }
1279 }
1280
Joe Onorato8a9625e2010-01-28 15:55:35 -08001281 // Eat the long press event so the keyboard doesn't come up.
1282 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1283 return true;
1284 }
1285
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 return handled;
1287 }
1288
Karl Rosaen138a0412009-04-23 19:00:21 -07001289 private String getTypedText() {
1290 return mDefaultKeySsb.toString();
1291 }
1292
1293 private void clearTypedText() {
1294 mDefaultKeySsb.clear();
1295 mDefaultKeySsb.clearSpans();
1296 Selection.setSelection(mDefaultKeySsb, 0);
1297 }
1298
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001300 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1301 * State
1302 */
1303 private static State intToState(int stateOrdinal) {
1304 State state = State.WORKSPACE;
1305 final State[] stateValues = State.values();
1306 for (int i = 0; i < stateValues.length; i++) {
1307 if (stateValues[i].ordinal() == stateOrdinal) {
1308 state = stateValues[i];
1309 break;
1310 }
1311 }
1312 return state;
1313 }
1314
1315 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 * Restores the previous state, if it exists.
1317 *
1318 * @param savedState The previous state.
1319 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001320 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 private void restoreState(Bundle savedState) {
1322 if (savedState == null) {
1323 return;
1324 }
1325
Michael Jurka883f55b2010-10-21 15:47:14 -07001326 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001327 if (state == State.APPS || state == State.WIDGETS) {
1328 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001329 }
1330
Adam Cohen21cd0022013-10-09 18:57:02 -07001331 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1332 PagedView.INVALID_RESTORE_PAGE);
1333 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001334 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 }
1336
Winson Chung3d503fb2011-07-13 17:25:49 -07001337 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001338 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001339
Winson Chung3d503fb2011-07-13 17:25:49 -07001340 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1341 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001342 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001343 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1344 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001345 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1346 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001347 AppWidgetProviderInfo info = savedState.getParcelable(
1348 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
1349 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001350 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001351 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 mRestoring = true;
1353 }
1354
1355 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1356 if (renameFolder) {
1357 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001358 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 mRestoring = true;
1360 }
Winson Chunga12a2502010-12-20 14:41:35 -08001361
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001362 mItemIdToViewId = (HashMap<Integer, Integer>)
1363 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 }
1365
1366 /**
1367 * Finds all the views we need and configure them properly.
1368 */
1369 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001370 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001371
Craig Mautner360310b2012-10-26 15:13:08 -07001372 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001373 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001374 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1375 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001376 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001377 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001378
John Spurlock77e1f472013-09-11 10:09:51 -04001379 mLauncherView.setSystemUiVisibility(
1380 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001381 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382
Winson Chung4c98d922011-05-31 16:50:48 -07001383 // Setup the drag layer
1384 mDragLayer.setup(this, dragController);
1385
Winson Chung3d503fb2011-07-13 17:25:49 -07001386 // Setup the hotseat
1387 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1388 if (mHotseat != null) {
1389 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001390 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001391 }
1392
Jorim Jaggid017f882014-01-14 17:08:48 -08001393 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001394 View widgetButton = findViewById(R.id.widget_button);
1395 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001396 @Override
1397 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001398 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001399 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001400 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001401 }
1402 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001403 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1404
1405 View wallpaperButton = findViewById(R.id.wallpaper_button);
1406 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001407 @Override
1408 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001409 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001410 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001411 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001412 }
1413 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001414 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1415
1416 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001417 if (hasSettings()) {
1418 settingsButton.setOnClickListener(new OnClickListener() {
1419 @Override
1420 public void onClick(View arg0) {
1421 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001422 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001423 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001424 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001425 });
1426 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1427 } else {
1428 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001429 }
1430
Adam Cohendbdff6b2013-09-18 19:09:15 -07001431 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001432
Winson Chung4c98d922011-05-31 16:50:48 -07001433 // Setup the workspace
1434 mWorkspace.setHapticFeedbackEnabled(false);
1435 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001436 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001437 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001438
Winson Chungf0ea4d32011-06-06 14:27:16 -07001439 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001440 mSearchDropTargetBar = (SearchDropTargetBar)
1441 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001442
Winson Chungb745afb2015-03-02 11:51:23 -08001443 // Setup Apps
1444 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
Winson Chung0f785722015-04-08 10:27:49 -07001445 if (mLauncherCallbacks != null && mLauncherCallbacks.overrideAllAppsSearch()) {
1446 mAppsView.hideSearchBar();
1447 }
Winson Chungb745afb2015-03-02 11:51:23 -08001448
Winson Chungf0ea4d32011-06-06 14:27:16 -07001449 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001450 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001451
Winson Chung3d503fb2011-07-13 17:25:49 -07001452 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001453 dragController.setDragScoller(mWorkspace);
1454 dragController.setScrollView(mDragLayer);
1455 dragController.setMoveTarget(mWorkspace);
1456 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001457 if (mSearchDropTargetBar != null) {
1458 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001459 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001460 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001461
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001462 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001463 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001464 mWeightWatcher = new WeightWatcher(this);
1465 mWeightWatcher.setAlpha(0.5f);
1466 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001467 new FrameLayout.LayoutParams(
1468 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001469 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001470 Gravity.BOTTOM)
1471 );
Adam Cohen39a06042013-07-19 14:30:12 -07001472
1473 boolean show = shouldShowWeightWatcher();
1474 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001475 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477
1478 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001479 * Sets the all apps button. This method is called from {@link Hotseat}.
1480 */
1481 public void setAllAppsButton(View allAppsButton) {
1482 mAllAppsButton = allAppsButton;
1483 }
1484
1485 public View getAllAppsButton() {
1486 return mAllAppsButton;
1487 }
1488
1489 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 * Creates a view representing a shortcut.
1491 *
1492 * @param info The data structure describing the shortcut.
1493 *
1494 * @return A View inflated from R.layout.application.
1495 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001496 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001498 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 }
1500
1501 /**
1502 * Creates a view representing a shortcut inflated from the specified resource.
1503 *
1504 * @param layoutResId The id of the XML layout used to create the shortcut.
1505 * @param parent The group the shortcut belongs to.
1506 * @param info The data structure describing the shortcut.
1507 *
1508 * @return A View inflated from layoutResId.
1509 */
Adam Cohen59400422014-03-05 18:07:04 -08001510 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001511 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001512 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001514 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 return favorite;
1516 }
1517
1518 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 * Add a shortcut to the workspace.
1520 *
1521 * @param data The intent describing the shortcut.
1522 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001524 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001525 int cellY) {
1526 int[] cellXY = mTmpAddItemCellCoordinates;
1527 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001528 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001529
Michael Jurkad3ef3062010-11-23 16:23:58 -08001530 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001531
Sunny Goyal2350bc92014-10-14 16:42:54 -07001532 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001533 if (info == null) {
1534 return;
1535 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001536 final View view = createShortcut(info);
1537
Adam Cohenfbba09b2011-07-18 21:43:05 -07001538 // First we check if we already know the exact location where we want to add this item.
1539 if (cellX >= 0 && cellY >= 0) {
1540 cellXY[0] = cellX;
1541 cellXY[1] = cellY;
1542 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001543
1544 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001545 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001546 true, null,null)) {
1547 return;
1548 }
1549 DragObject dragObject = new DragObject();
1550 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001551 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1552 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001553 return;
1554 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001555 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001556 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001557 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001558 foundCellSpan = (result != null);
1559 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001560 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001561 }
1562
1563 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001564 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001565 return;
1566 }
1567
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001568 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569
1570 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001571 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001572 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
1574 }
1575
Adam Cohen2f093b62012-04-30 18:59:53 -07001576 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1577 int minHeight) {
1578 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001579 // We want to account for the extra amount of padding that we are adding to the widget
1580 // to ensure that it gets the full amount of space that it has requested
1581 int requiredWidth = minWidth + padding.left + padding.right;
1582 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001583 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001584 }
1585
Adam Cohen2f093b62012-04-30 18:59:53 -07001586 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1587 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001588 }
1589
Adam Cohen2f093b62012-04-30 18:59:53 -07001590 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1591 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001592 }
1593
Adam Cohen2f093b62012-04-30 18:59:53 -07001594 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1595 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001596 }
1597
Adam Cohen2f093b62012-04-30 18:59:53 -07001598 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1599 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001600 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001601 }
1602
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001604 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001606 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 */
Adam Cohen091440a2015-03-18 14:16:05 -07001608 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001609 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1610
1611 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001612 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001613 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1614 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001615 }
Romain Guycbb89e42009-06-08 15:52:54 -07001616
Adam Cohen59400422014-03-05 18:07:04 -08001617 if (appWidgetInfo.isCustomWidget) {
1618 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001619 }
1620
Adam Cohen59400422014-03-05 18:07:04 -08001621 LauncherAppWidgetInfo launcherInfo;
1622 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1623 launcherInfo.spanX = info.spanX;
1624 launcherInfo.spanY = info.spanY;
1625 launcherInfo.minSpanX = info.minSpanX;
1626 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001627 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001628
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001630 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631
1632 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001633 if (hostView == null) {
1634 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001635 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1636 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001637 } else {
1638 // The AppWidgetHostView has already been inflated and instantiated
1639 launcherInfo.hostView = hostView;
1640 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001642 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001643 launcherInfo.notifyWidgetSizeChanged(this);
1644
Adam Cohen59400422014-03-05 18:07:04 -08001645 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1646 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001647
1648 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001650 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 }
Romain Guycbb89e42009-06-08 15:52:54 -07001652
Adam Cohended9f8d2010-11-03 13:25:16 -07001653 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1654 @Override
1655 public void onReceive(Context context, Intent intent) {
1656 final String action = intent.getAction();
1657 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1658 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001659 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001660 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001661
Winson Chungc100e8e2011-08-09 16:02:43 -07001662 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001663 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001664 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001665 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001666 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001667 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001668 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1669 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001670 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001671 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1672 mModel.resetLoadedState(false, true);
1673 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1674 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1675 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1676 mModel.resetLoadedState(false, true);
1677 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1678 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1679 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001680 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1681 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1682 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001683 }
1684 }
1685 };
1686
1687 @Override
1688 public void onAttachedToWindow() {
1689 super.onAttachedToWindow();
1690
1691 // Listen for broadcasts related to user-presence
1692 final IntentFilter filter = new IntentFilter();
1693 filter.addAction(Intent.ACTION_SCREEN_OFF);
1694 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001695 // For handling managed profiles
1696 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1697 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001698 if (ENABLE_DEBUG_INTENTS) {
1699 filter.addAction(DebugIntents.DELETE_DATABASE);
1700 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1701 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001703 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001704 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001705 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001706 mVisible = true;
1707 }
1708
Adam Cohen0b395352014-06-09 22:54:36 +00001709 /**
1710 * Sets up transparent navigation and status bars in LMP.
1711 * This method is a no-op for other platform versions.
1712 */
Sunny Goyald0091012015-01-20 15:55:34 -08001713 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001714 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001715 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001716 Window window = getWindow();
1717 window.getAttributes().systemUiVisibility |=
1718 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1719 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1720 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1721 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1722 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1723 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1724 window.setStatusBarColor(Color.TRANSPARENT);
1725 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001726 }
1727 }
1728
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 @Override
1730 public void onDetachedFromWindow() {
1731 super.onDetachedFromWindow();
1732 mVisible = false;
1733
Adam Cohend113e0c2010-11-11 10:48:05 -08001734 if (mAttached) {
1735 unregisterReceiver(mReceiver);
1736 mAttached = false;
1737 }
Winson Chungb745afb2015-03-02 11:51:23 -08001738 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 }
1740
1741 public void onWindowVisibilityChanged(int visibility) {
1742 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001743 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001744 // The following code used to be in onResume, but it turns out onResume is called when
1745 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1746 // is a more appropriate event to handle
1747 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001748 if (!mWorkspaceLoading) {
1749 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001750 // We want to let Launcher draw itself at least once before we force it to build
1751 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001752 // apps is nice and speedy.
1753 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001754 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001755 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001756 if (mStarted) return;
1757 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001758 // We delay the layer building a bit in order to give
1759 // other message processing a time to run. In particular
1760 // this avoids a delay in hiding the IME if it was
1761 // currently shown, because doing that may involve
1762 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001763 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001764 final ViewTreeObserver.OnDrawListener listener = this;
1765 mWorkspace.post(new Runnable() {
1766 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001767 if (mWorkspace != null &&
1768 mWorkspace.getViewTreeObserver() != null) {
1769 mWorkspace.getViewTreeObserver().
1770 removeOnDrawListener(listener);
1771 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001772 }
1773 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001774 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001775 }
1776 });
1777 }
1778 clearTypedText();
1779 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001780 }
1781
Adam Cohen091440a2015-03-18 14:16:05 -07001782 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001783 mHandler.removeMessages(ADVANCE_MSG);
1784 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1785 mHandler.sendMessageDelayed(msg, delay);
1786 mAutoAdvanceSentTime = System.currentTimeMillis();
1787 }
1788
Adam Cohen091440a2015-03-18 14:16:05 -07001789 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001790 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1791 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1792 mAutoAdvanceRunning = autoAdvanceRunning;
1793 if (autoAdvanceRunning) {
1794 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1795 sendAdvanceMessage(delay);
1796 } else {
1797 if (!mWidgetsToAdvance.isEmpty()) {
1798 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1799 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1800 }
1801 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001802 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001803 }
1804 }
1805 }
1806
1807 private final Handler mHandler = new Handler() {
1808 @Override
1809 public void handleMessage(Message msg) {
1810 if (msg.what == ADVANCE_MSG) {
1811 int i = 0;
1812 for (View key: mWidgetsToAdvance.keySet()) {
1813 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1814 final int delay = mAdvanceStagger * i;
1815 if (v instanceof Advanceable) {
1816 postDelayed(new Runnable() {
1817 public void run() {
1818 ((Advanceable) v).advance();
1819 }
1820 }, delay);
1821 }
1822 i++;
1823 }
1824 sendAdvanceMessage(mAdvanceInterval);
1825 }
1826 }
1827 };
1828
1829 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001830 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001831 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1832 if (v instanceof Advanceable) {
1833 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001834 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001835 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001836 }
1837 }
1838
1839 void removeWidgetToAutoAdvance(View hostView) {
1840 if (mWidgetsToAdvance.containsKey(hostView)) {
1841 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001842 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001843 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001844 }
1845
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001847 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848 launcherInfo.hostView = null;
1849 }
1850
Hyunyoung Song3f471442015-04-08 19:01:34 -07001851 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001852 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1853 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001854 }
1855
Winson Chunga6945242014-01-08 14:04:34 -08001856 public DragLayer getDragLayer() {
1857 return mDragLayer;
1858 }
1859
Winson Chungb745afb2015-03-02 11:51:23 -08001860 public AppsContainerView getAppsView() {
1861 return mAppsView;
1862 }
1863
Hyunyoung Song3f471442015-04-08 19:01:34 -07001864 public WidgetsContainerView getWidgetsView() {
1865 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001866 }
1867
Winson Chunga6945242014-01-08 14:04:34 -08001868 public Workspace getWorkspace() {
1869 return mWorkspace;
1870 }
1871
1872 public Hotseat getHotseat() {
1873 return mHotseat;
1874 }
1875
Jorim Jaggid017f882014-01-14 17:08:48 -08001876 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001877 return mOverviewPanel;
1878 }
1879
1880 public SearchDropTargetBar getSearchBar() {
1881 return mSearchDropTargetBar;
1882 }
1883
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001884 public LauncherAppWidgetHost getAppWidgetHost() {
1885 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 }
Romain Guycbb89e42009-06-08 15:52:54 -07001887
Winson Chunga9abd0e2010-10-27 17:18:37 -07001888 public LauncherModel getModel() {
1889 return mModel;
1890 }
1891
Winson Chunga6945242014-01-08 14:04:34 -08001892 protected SharedPreferences getSharedPrefs() {
1893 return mSharedPrefs;
1894 }
1895
Bjorn Bringertc459e522013-06-07 19:36:01 +01001896 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001897 getWindow().closeAllPanels();
1898
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001899 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001900 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001901 }
1902
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 @Override
1904 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001905 long startTime = 0;
1906 if (DEBUG_RESUME_TIME) {
1907 startTime = System.currentTimeMillis();
1908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 super.onNewIntent(intent);
1910
1911 // Close the menu
1912 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001913 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001914 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001915
Adam Cohened307df2013-10-02 09:37:31 -07001916 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1917 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1918 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001919
Adam Cohen6fecd412013-10-02 17:41:50 -07001920 if (mWorkspace == null) {
1921 // Can be cases where mWorkspace is null, this prevents a NPE
1922 return;
1923 }
1924 Folder openFolder = mWorkspace.getOpenFolder();
1925 // In all these cases, only animate if we're already on home
1926 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001927
1928 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1929 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001930 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001931 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001932 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001933 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001934
Adam Cohen6fecd412013-10-02 17:41:50 -07001935 closeFolder();
1936 exitSpringLoadedDragMode();
1937
1938 // If we are already on home, then just animate back to the workspace,
1939 // otherwise, just wait until onResume to set the state back to Workspace
1940 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001941 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001942 } else {
1943 mOnResumeState = State.WORKSPACE;
1944 }
1945
1946 final View v = getWindow().peekDecorView();
1947 if (v != null && v.getWindowToken() != null) {
1948 InputMethodManager imm = (InputMethodManager)getSystemService(
1949 INPUT_METHOD_SERVICE);
1950 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1951 }
1952
Winson Chungb745afb2015-03-02 11:51:23 -08001953 // Reset the apps view
1954 if (!alreadyOnHome && mAppsView != null) {
1955 mAppsView.scrollToTop();
1956 }
1957
Hyunyoung Song3f471442015-04-08 19:01:34 -07001958 // Reset the widgets view
1959 if (!alreadyOnHome && mWidgetsView != null) {
1960 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001961 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001962
Adam Cohen9211d422014-10-07 18:14:53 -07001963 if (mLauncherCallbacks != null) {
1964 mLauncherCallbacks.onHomeIntent();
1965 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 }
Adam Cohened307df2013-10-02 09:37:31 -07001967
Michael Jurka447bf842013-05-15 14:52:15 +02001968 if (DEBUG_RESUME_TIME) {
1969 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971
Adam Cohen9211d422014-10-07 18:14:53 -07001972 if (mLauncherCallbacks != null) {
1973 mLauncherCallbacks.onNewIntent(intent);
1974 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001975 }
1976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001978 public void onRestoreInstanceState(Bundle state) {
1979 super.onRestoreInstanceState(state);
1980 for (int page: mSynchronouslyBoundPages) {
1981 mWorkspace.restoreInstanceStateForChild(page);
1982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
1984
1985 @Override
1986 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001987 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001988 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1989 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001990 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001991 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992
Michael Jurka883f55b2010-10-21 15:47:14 -07001993 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001994 // We close any open folder since it will not be re-opened, and we need to make sure
1995 // this state is reflected.
1996 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997
Adam Cohendcd297f2013-06-18 13:13:40 -07001998 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001999 mWaitingForResult) {
2000 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002001 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002002 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2003 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002004 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2005 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2006 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002007 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 }
2009
2010 if (mFolderInfo != null && mWaitingForResult) {
2011 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2012 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2013 }
Michael Jurka946ad472010-07-09 18:05:18 -07002014
Hyunyoung Song3f471442015-04-08 19:01:34 -07002015 // Save the current widgets tray?
2016 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002017 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002018
2019 if (mLauncherCallbacks != null) {
2020 mLauncherCallbacks.onSaveInstanceState(outState);
2021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
2023
2024 @Override
2025 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002027
Winson Chunge7a03942011-08-05 15:05:12 -07002028 // Remove all pending runnables
2029 mHandler.removeMessages(ADVANCE_MSG);
2030 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002031 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002032
Winson Chungcd2b0142011-06-08 16:02:26 -07002033 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002034 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002035
2036 // It's possible to receive onDestroy after a new Launcher activity has
2037 // been created. In this case, don't interfere with the new Launcher.
2038 if (mModel.isCurrentCallbacks(this)) {
2039 mModel.stopLoader();
2040 app.setLauncher(null);
2041 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002044 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002046 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002048 mAppWidgetHost = null;
2049
2050 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051
2052 TextKeyListener.getInstance().release();
2053
Winson Chung81b52252012-08-27 15:34:29 -07002054 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2055 // to prevent leaking Launcher activities on orientation change.
2056 if (mModel != null) {
2057 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2058 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002059
2060 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002061 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002062
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002063 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002064 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002065 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002066 mWorkspace = null;
2067 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002068
Michael Jurka2ecf9952012-06-18 12:52:28 -07002069 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002070
2071 if (mLauncherCallbacks != null) {
2072 mLauncherCallbacks.onDestroy();
2073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
2075
Adam Cohena9cf38f2011-05-02 15:36:58 -07002076 public DragController getDragController() {
2077 return mDragController;
2078 }
2079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 @Override
2081 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002082 onStartForResult(requestCode);
2083 super.startActivityForResult(intent, requestCode);
2084 }
2085
2086 @Override
2087 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2088 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2089 onStartForResult(requestCode);
2090 try {
2091 super.startIntentSenderForResult(intent, requestCode,
2092 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2093 } catch (IntentSender.SendIntentException e) {
2094 throw new ActivityNotFoundException();
2095 }
2096 }
2097
2098 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002099 if (requestCode >= 0) {
2100 setWaitingForResult(true);
2101 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 }
2103
Winson Chung70d72102011-08-12 11:24:35 -07002104 /**
2105 * Indicates that we want global search for this activity by setting the globalSearch
2106 * argument for {@link #startSearch} to true.
2107 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002109 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002111
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002112 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002113
Karl Rosaen138a0412009-04-23 19:00:21 -07002114 if (initialQuery == null) {
2115 // Use any text typed in the launcher as the initial query
2116 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 if (appSearchData == null) {
2119 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002120 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 }
Winson Chungadf0c182012-08-23 14:59:07 -07002122 Rect sourceBounds = new Rect();
2123 if (mSearchDropTargetBar != null) {
2124 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2125 }
Romain Guycbb89e42009-06-08 15:52:54 -07002126
Ian Parkinson047036e2014-09-01 15:40:53 +01002127 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002128 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002129 if (clearTextImmediately) {
2130 clearTypedText();
2131 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002132 }
2133
Ian Parkinson047036e2014-09-01 15:40:53 +01002134 /**
2135 * Start a text search.
2136 *
2137 * @return {@code true} if the search will start immediately, so any further keypresses
2138 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2139 * to buffer keypresses.
2140 */
2141 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002142 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002143 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2144 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2145 sourceBounds);
2146 }
2147
Michael Jurkaa33411c2012-06-14 16:18:21 -07002148 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002149 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002150 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002151 }
2152
2153 /**
2154 * Starts the global search activity. This code is a copied from SearchManager
2155 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002156 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002157 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002158 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002159 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2160 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2161 if (globalSearchActivity == null) {
2162 Log.w(TAG, "No global search activity found.");
2163 return;
2164 }
2165 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2166 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2167 intent.setComponent(globalSearchActivity);
2168 // Make sure that we have a Bundle to put source in
2169 if (appSearchData == null) {
2170 appSearchData = new Bundle();
2171 } else {
2172 appSearchData = new Bundle(appSearchData);
2173 }
Adam Cohen9211d422014-10-07 18:14:53 -07002174 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002175 if (!appSearchData.containsKey("source")) {
2176 appSearchData.putString("source", getPackageName());
2177 }
2178 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2179 if (!TextUtils.isEmpty(initialQuery)) {
2180 intent.putExtra(SearchManager.QUERY, initialQuery);
2181 }
2182 if (selectInitialQuery) {
2183 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2184 }
2185 intent.setSourceBounds(sourceBounds);
2186 try {
2187 startActivity(intent);
2188 } catch (ActivityNotFoundException ex) {
2189 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 }
2192
Yura4f93ec62014-02-04 14:15:21 +00002193 public boolean isOnCustomContent() {
2194 return mWorkspace.isOnOrMovingToCustomContent();
2195 }
2196
Winson Chung70d72102011-08-12 11:24:35 -07002197 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002198 public boolean onPrepareOptionsMenu(Menu menu) {
2199 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002200 if (!isOnCustomContent()) {
2201 // Close any open folders
2202 closeFolder();
2203 // Stop resizing any widgets
2204 mWorkspace.exitWidgetResizeMode();
2205 if (!mWorkspace.isInOverviewMode()) {
2206 // Show the overview mode
2207 showOverviewMode(true);
2208 } else {
2209 showWorkspace(true);
2210 }
Winson Chunge0298742014-01-17 12:03:00 -08002211 }
Adam Cohen9211d422014-10-07 18:14:53 -07002212 if (mLauncherCallbacks != null) {
2213 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2214 }
2215
Michael Jurkab94f3f82013-09-11 18:08:54 +02002216 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002219 @Override
2220 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002221 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002222 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002223 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002224 }
2225
Joe Onorato9c1289c2009-08-17 11:03:03 -04002226 public boolean isWorkspaceLocked() {
2227 return mWorkspaceLoading || mWaitingForResult;
2228 }
2229
Adam Cohen517a7f52014-03-01 12:12:59 -08002230 public boolean isWorkspaceLoading() {
2231 return mWorkspaceLoading;
2232 }
2233
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002234 private void setWorkspaceLoading(boolean value) {
2235 boolean isLocked = isWorkspaceLocked();
2236 mWorkspaceLoading = value;
2237 if (isLocked != isWorkspaceLocked()) {
2238 onWorkspaceLockedChanged();
2239 }
2240 }
2241
2242 private void setWaitingForResult(boolean value) {
2243 boolean isLocked = isWorkspaceLocked();
2244 mWaitingForResult = value;
2245 if (isLocked != isWorkspaceLocked()) {
2246 onWorkspaceLockedChanged();
2247 }
2248 }
2249
Adam Cohen9211d422014-10-07 18:14:53 -07002250 protected void onWorkspaceLockedChanged() {
2251 if (mLauncherCallbacks != null) {
2252 mLauncherCallbacks.onWorkspaceLockedChanged();
2253 }
2254 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002255
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002258 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002259 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2260 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002261 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002262 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002263 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002264
Sunny Goyale6b63a32015-01-16 16:14:29 -08002265 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002266 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002267 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2268 }
2269
Sunny Goyale6b63a32015-01-16 16:14:29 -08002270 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002271 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2272 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002273 if (appWidgetInfo.configure != null) {
2274 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002275 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002276
Bjorn Bringert7984c942009-12-09 15:38:25 +00002277 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002278 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2279 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002282 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002283 Runnable onComplete = new Runnable() {
2284 @Override
2285 public void run() {
2286 // Exit spring loaded mode if necessary after adding the widget
2287 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2288 null);
2289 }
2290 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002291 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002292 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002293 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002294 }
2295 }
Romain Guycbb89e42009-06-08 15:52:54 -07002296
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002297 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002298 // Close any folders that may be open.
2299 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002300 mWorkspace.moveToCustomContentScreen(animate);
2301 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002302
2303 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2304 int[] cell, int spanX, int spanY) {
2305 switch (info.itemType) {
2306 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2307 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2308 int span[] = new int[2];
2309 span[0] = spanX;
2310 span[1] = spanY;
2311 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2312 container, screenId, cell, span);
2313 break;
2314 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2315 processShortcutFromDrop(info.componentName, container, screenId, cell);
2316 break;
2317 default:
2318 throw new IllegalStateException("Unknown item type: " + info.itemType);
2319 }
2320 }
2321
Adam Cohenfbba09b2011-07-18 21:43:05 -07002322 /**
2323 * Process a shortcut drop.
2324 *
2325 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002326 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002328 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002329 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2330 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002331 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002332 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002333 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002334 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002335
2336 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 mPendingAddInfo.cellX = cell[0];
2338 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002339 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002340
2341 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2342 createShortcutIntent.setComponent(componentName);
2343 processShortcut(createShortcutIntent);
2344 }
2345
Adam Cohenfbba09b2011-07-18 21:43:05 -07002346 /**
2347 * Process a widget drop.
2348 *
2349 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002350 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002352 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002353 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2354 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002355 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002356 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002357 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002358 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002359 mPendingAddInfo.minSpanX = info.minSpanX;
2360 mPendingAddInfo.minSpanY = info.minSpanY;
2361
Adam Cohenfbba09b2011-07-18 21:43:05 -07002362 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002363 mPendingAddInfo.cellX = cell[0];
2364 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002365 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002366 if (span != null) {
2367 mPendingAddInfo.spanX = span[0];
2368 mPendingAddInfo.spanY = span[1];
2369 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002370
Adam Cohened66b2b2012-01-23 17:28:51 -08002371 AppWidgetHostView hostView = info.boundWidget;
2372 int appWidgetId;
2373 if (hostView != null) {
2374 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002375 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002376 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002377 // In this case, we either need to start an activity to get permission to bind
2378 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002379 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002380 Bundle options = info.bindOptions;
2381
Sunny Goyalffe83f12014-08-14 17:39:34 -07002382 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2383 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002384 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002385 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002386 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002387 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002388 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2389 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2390 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002391 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2392 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002393 // TODO: we need to make sure that this accounts for the options bundle.
2394 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002395 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2396 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002397 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002398 }
2399
Joe Onoratodeb98af2010-02-19 14:59:39 -08002400 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002401 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 }
2403
Winson Chung24ab2f12010-09-16 14:10:47 -07002404 void processWallpaper(Intent intent) {
2405 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2406 }
2407
Adam Cohendcd297f2013-06-18 13:13:40 -07002408 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002409 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002410 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 folderInfo.title = getText(R.string.folder_name);
2412
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002414 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2415 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002416 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417
2418 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002419 FolderIcon newFolder =
2420 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002421 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002422 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002423 // Force measure the new folder icon
2424 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2425 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002426 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 }
Romain Guycbb89e42009-06-08 15:52:54 -07002428
Joe Onorato9c1289c2009-08-17 11:03:03 -04002429 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002430 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002431 }
2432
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002433 /**
2434 * Registers various content observers. The current implementation registers
2435 * only a favorites observer to keep track of the favorites applications.
2436 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002437 private void registerContentObservers() {
2438 ContentResolver resolver = getContentResolver();
2439 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2440 true, mWidgetObserver);
2441 }
2442
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 @Override
2444 public boolean dispatchKeyEvent(KeyEvent event) {
2445 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2446 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002448 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002449 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002450 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002451 dumpState();
2452 return true;
2453 }
2454 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002455 }
2456 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2457 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002458 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 return true;
2460 }
2461 }
2462
2463 return super.dispatchKeyEvent(event);
2464 }
2465
Joe Onorato88ec0992009-11-19 13:16:06 -08002466 @Override
2467 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002468 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2469 return;
2470 }
2471
Winson Chung3d9490a2015-03-24 17:38:42 -07002472 LauncherAccessibilityDelegate delegate =
2473 LauncherAppState.getInstance().getAccessibilityDelegate();
2474 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002475 return;
2476 }
2477
Winson Chungb745afb2015-03-02 11:51:23 -08002478 if (isAppsViewVisible()) {
2479 showWorkspace(true);
2480 } else if (isWidgetsViewVisible()) {
2481 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002482 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002483 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002484 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002485 Folder openFolder = mWorkspace.getOpenFolder();
2486 if (openFolder.isEditingName()) {
2487 openFolder.dismissEditingName();
2488 } else {
2489 closeFolder();
2490 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002491 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002492 mWorkspace.exitWidgetResizeMode();
2493
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002494 // Back button is a no-op here, but give at least some feedback for the button press
2495 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002496 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002497 }
2498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002500 * Re-listen when widgets are reset.
2501 */
Adam Cohen091440a2015-03-18 14:16:05 -07002502 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002503 if (mAppWidgetHost != null) {
2504 mAppWidgetHost.startListening();
2505 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002506 }
2507
2508 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002509 * Launches the intent referred by the clicked shortcut.
2510 *
2511 * @param v The view representing the clicked shortcut.
2512 */
2513 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002514 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2515 // view has detached (it's possible for this to happen if the view is removed mid touch).
2516 if (v.getWindowToken() == null) {
2517 return;
2518 }
2519
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002520 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002521 return;
2522 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002523
Adam Cohen1697b792013-09-17 19:08:21 -07002524 if (v instanceof Workspace) {
2525 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002526 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002527 }
2528 return;
2529 }
2530
2531 if (v instanceof CellLayout) {
2532 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002533 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002534 }
2535 }
2536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002538 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002539 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002541 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002542 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002543 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002544 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002545 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002546 } else if (tag instanceof AppInfo) {
2547 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002548 } else if (tag instanceof LauncherAppWidgetInfo) {
2549 if (v instanceof PendingAppWidgetHostView) {
2550 onClickPendingWidget((PendingAppWidgetHostView) v);
2551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 }
2553 }
2554
Sunny Goyal508da152014-08-14 10:53:27 -07002555 public void onClickPagedViewIcon(View v) {
2556 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002557 if (mLauncherCallbacks != null) {
2558 mLauncherCallbacks.onClickPagedViewIcon(v);
2559 }
Sunny Goyal508da152014-08-14 10:53:27 -07002560 }
2561
Michael Jurka0e260592010-06-30 17:07:39 -07002562 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002563 return false;
2564 }
2565
Michael Jurkaaf442092010-06-10 17:01:57 -07002566 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002567 * Event handler for the app widget view which has not fully restored.
2568 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002569 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002570 if (mIsSafeModeEnabled) {
2571 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2572 return;
2573 }
2574
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002575 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002576 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002577 int widgetId = info.appWidgetId;
2578 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2579 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002580 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2581 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002582 mPendingAddInfo.copyFrom(info);
2583 mPendingAddWidgetId = widgetId;
2584
Sunny Goyalffe83f12014-08-14 17:39:34 -07002585 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2586 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002587 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002588 } else if (info.installProgress < 0) {
2589 // The install has not been queued
2590 final String packageName = info.providerName.getPackageName();
2591 showBrokenAppInstallDialog(packageName,
2592 new DialogInterface.OnClickListener() {
2593 public void onClick(DialogInterface dialog, int id) {
2594 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2595 }
2596 });
2597 } else {
2598 // Download has started.
2599 final String packageName = info.providerName.getPackageName();
2600 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002601 }
2602 }
2603
2604 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002605 * Event handler for the "grid" button that appears on the home screen, which
2606 * enters all apps mode.
2607 *
2608 * @param v The view that was clicked.
2609 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002610 protected void onClickAllAppsButton(View v) {
2611 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002612 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002613 showWorkspace(true);
2614 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002615 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002616 }
2617 }
2618
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002619 private void showBrokenAppInstallDialog(final String packageName,
2620 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002621 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002622 .setTitle(R.string.abandoned_promises_title)
2623 .setMessage(R.string.abandoned_promise_explanation)
2624 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2625 .setNeutralButton(R.string.abandoned_clean_this,
2626 new DialogInterface.OnClickListener() {
2627 public void onClick(DialogInterface dialog, int id) {
2628 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2629 mWorkspace.removeAbandonedPromise(packageName, user);
2630 }
2631 })
2632 .create().show();
2633 return;
2634 }
2635
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002636 /**
2637 * Event handler for an app shortcut click.
2638 *
2639 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2640 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002641 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002642 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2643 Object tag = v.getTag();
2644 if (!(tag instanceof ShortcutInfo)) {
2645 throw new IllegalArgumentException("Input must be a Shortcut");
2646 }
2647
2648 // Open shortcut
2649 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002650
2651 if (shortcut.isDisabled != 0) {
2652 int error = R.string.activity_not_available;
2653 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2654 error = R.string.safemode_shortcut_error;
2655 }
2656 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2657 return;
2658 }
2659
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002660 final Intent intent = shortcut.intent;
2661
2662 // Check for special shortcuts
2663 if (intent.getComponent() != null) {
2664 final String shortcutClass = intent.getComponent().getClassName();
2665
2666 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2667 MemoryDumpActivity.startDump(this);
2668 return;
2669 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2670 toggleShowWeightWatcher();
2671 return;
2672 }
2673 }
2674
Chris Wren40c5ed32014-06-24 18:24:23 -04002675 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002676 if ((v instanceof BubbleTextView)
2677 && shortcut.isPromise()
2678 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002679 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002680 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002681 new DialogInterface.OnClickListener() {
2682 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002683 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002685 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 return;
2687 }
2688
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002689 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002690 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002691
2692 if (mLauncherCallbacks != null) {
2693 mLauncherCallbacks.onClickAppShortcut(v);
2694 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002695 }
2696
Adam Cohen091440a2015-03-18 14:16:05 -07002697 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002698 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002699 final ShortcutInfo shortcut;
2700 final Intent intent;
2701 if (tag instanceof ShortcutInfo) {
2702 shortcut = (ShortcutInfo) tag;
2703 intent = shortcut.intent;
2704 int[] pos = new int[2];
2705 v.getLocationOnScreen(pos);
2706 intent.setSourceBounds(new Rect(pos[0], pos[1],
2707 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002708
Sunny Goyal508da152014-08-14 10:53:27 -07002709 } else if (tag instanceof AppInfo) {
2710 shortcut = null;
2711 intent = ((AppInfo) tag).intent;
2712 } else {
2713 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2714 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715
2716 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002717 mStats.recordLaunch(intent, shortcut);
2718
2719 if (success && v instanceof BubbleTextView) {
2720 mWaitingForResume = (BubbleTextView) v;
2721 mWaitingForResume.setStayPressed(true);
2722 }
2723 }
2724
2725 /**
2726 * Event handler for a folder icon click.
2727 *
2728 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2729 */
2730 protected void onClickFolderIcon(View v) {
2731 if (LOGD) Log.d(TAG, "onClickFolder");
2732 if (!(v instanceof FolderIcon)){
2733 throw new IllegalArgumentException("Input must be a FolderIcon");
2734 }
2735
2736 FolderIcon folderIcon = (FolderIcon) v;
2737 final FolderInfo info = folderIcon.getFolderInfo();
2738 Folder openFolder = mWorkspace.getFolderForTag(info);
2739
2740 // If the folder info reports that the associated folder is open, then verify that
2741 // it is actually opened. There have been a few instances where this gets out of sync.
2742 if (info.opened && openFolder == null) {
2743 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2744 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2745 info.opened = false;
2746 }
2747
2748 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2749 // Close any open folder
2750 closeFolder();
2751 // Open the requested folder
2752 openFolder(folderIcon);
2753 } else {
2754 // Find the open folder...
2755 int folderScreen;
2756 if (openFolder != null) {
2757 folderScreen = mWorkspace.getPageForView(openFolder);
2758 // .. and close it
2759 closeFolder(openFolder);
2760 if (folderScreen != mWorkspace.getCurrentPage()) {
2761 // Close any folder open on the current screen
2762 closeFolder();
2763 // Pull the folder onto this screen
2764 openFolder(folderIcon);
2765 }
2766 }
2767 }
Adam Cohen9211d422014-10-07 18:14:53 -07002768
2769 if (mLauncherCallbacks != null) {
2770 mLauncherCallbacks.onClickFolderIcon(v);
2771 }
Michael Jurka5130e402011-10-13 04:55:35 -07002772 }
2773
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002774 /**
2775 * Event handler for the (Add) Widgets button that appears after a long press
2776 * on the home screen.
2777 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002778 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002779 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002780 if (mIsSafeModeEnabled) {
2781 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2782 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002783 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002784 if (mLauncherCallbacks != null) {
2785 mLauncherCallbacks.onClickAddWidgetButton(view);
2786 }
Adam Cohen9211d422014-10-07 18:14:53 -07002787 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002788 }
2789
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002790 /**
2791 * Event handler for the wallpaper picker button that appears after a long press
2792 * on the home screen.
2793 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002794 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002795 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002796 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2797 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002798
2799 if (mLauncherCallbacks != null) {
2800 mLauncherCallbacks.onClickWallpaperPicker(v);
2801 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002802 }
2803
2804 /**
2805 * Event handler for a click on the settings button that appears after a long press
2806 * on the home screen.
2807 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002808 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002809 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002810 if (mLauncherCallbacks != null) {
2811 mLauncherCallbacks.onClickSettingsButton(v);
2812 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002813 }
2814
Michael Jurka5130e402011-10-13 04:55:35 -07002815 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002816 // Provide the same haptic feedback that the system offers for virtual keys.
2817 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002818 }
2819
Adam Cohen61f560d2013-09-30 15:58:20 -07002820 public void performHapticFeedbackOnTouchDown(View v) {
2821 // Provide the same haptic feedback that the system offers for virtual keys.
2822 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2823 }
2824
2825 public View.OnTouchListener getHapticFeedbackTouchListener() {
2826 if (mHapticFeedbackTouchListener == null) {
2827 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2828 @Override
2829 public boolean onTouch(View v, MotionEvent event) {
2830 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2831 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2832 }
2833 return false;
2834 }
2835 };
2836 }
2837 return mHapticFeedbackTouchListener;
2838 }
2839
Adam Cohen9211d422014-10-07 18:14:53 -07002840 public void onDragStarted(View view) {
2841 if (isOnCustomContent()) {
2842 // Custom content screen doesn't participate in drag and drop. If on custom
2843 // content screen, move to default.
2844 moveWorkspaceToDefaultScreen();
2845 }
2846
2847 if (mLauncherCallbacks != null) {
2848 mLauncherCallbacks.onDragStarted(view);
2849 }
2850 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002851
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002852 /**
2853 * Called when the user stops interacting with the launcher.
2854 * This implies that the user is now on the homescreen and is not doing housekeeping.
2855 */
Adam Cohen9211d422014-10-07 18:14:53 -07002856 protected void onInteractionEnd() {
2857 if (mLauncherCallbacks != null) {
2858 mLauncherCallbacks.onInteractionEnd();
2859 }
2860 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002861
2862 /**
2863 * Called when the user starts interacting with the launcher.
2864 * The possible interactions are:
2865 * - open all apps
2866 * - reorder an app shortcut, or a widget
2867 * - open the overview mode.
2868 * This is a good time to stop doing things that only make sense
2869 * when the user is on the homescreen and not doing housekeeping.
2870 */
Adam Cohen9211d422014-10-07 18:14:53 -07002871 protected void onInteractionBegin() {
2872 if (mLauncherCallbacks != null) {
2873 mLauncherCallbacks.onInteractionBegin();
2874 }
2875 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002876
Kenny Guyf07af7b2014-07-31 11:39:16 +01002877 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002878 try {
2879 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002880 launcherApps.showAppDetailsForProfile(componentName, user);
2881 } catch (SecurityException e) {
2882 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2883 Log.e(TAG, "Launcher does not have permission to launch settings");
2884 } catch (ActivityNotFoundException e) {
2885 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2886 Log.e(TAG, "Unable to launch settings");
2887 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002888 }
2889
Michael Jurka1e2f4652013-07-08 18:03:46 -07002890 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002891 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2892 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002893 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002894 // System applications cannot be installed. For now, show a toast explaining that.
2895 // We may give them the option of disabling apps this way.
2896 int messageId = R.string.uninstall_system_app_text;
2897 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002898 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002899 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002900 String packageName = componentName.getPackageName();
2901 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002902 Intent intent = new Intent(
2903 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002904 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2905 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002906 if (user != null) {
2907 user.addToIntent(intent, Intent.EXTRA_USER);
2908 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002909 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002910 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002911 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002912 }
2913
Michael Jurka86a720e2012-05-09 11:23:23 -07002914 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002915 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002916 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002917 // Only launch using the new animation if the shortcut has not opted out (this is a
2918 // private contract between launcher and may be ignored in the future).
2919 boolean useLaunchAnimation = (v != null) &&
2920 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002921 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2922 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002923
Kenny Guy1317e2d2014-05-08 18:52:50 +01002924 UserHandleCompat user = null;
2925 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2926 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2927 user = userManager.getUserForSerialNumber(serialNumber);
2928 }
2929
2930 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002931 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002932 ActivityOptions opts = null;
2933 if (sClipRevealMethod != null) {
2934 // TODO: call method directly when Launcher3 can depend on M APIs
2935 int left = 0, top = 0;
2936 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2937 if (v instanceof TextView) {
2938 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002939 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2940 if (icon != null) {
2941 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002942 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002943 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002944 width = bounds.width();
2945 height = bounds.height();
2946 }
2947 }
2948 try {
2949 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2950 left, top, width, height);
2951 } catch (IllegalAccessException e) {
2952 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2953 sClipRevealMethod = null;
2954 } catch (InvocationTargetException e) {
2955 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2956 sClipRevealMethod = null;
2957 }
2958 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002959 if (opts == null && !Utilities.isLmpOrAbove()) {
2960 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002961 v.getMeasuredWidth(), v.getMeasuredHeight());
2962 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002963 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002964 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002965
2966 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2967 // Could be launching some bookkeeping activity
2968 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002969 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002970 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002971 launcherApps.startActivityForProfile(intent.getComponent(), user,
2972 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002973 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002974 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 } catch (SecurityException e) {
2976 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002977 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002979 "or use the exported attribute for this activity. "
2980 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002981 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002982 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002983 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002984
Michael Jurka86a720e2012-05-09 11:23:23 -07002985 boolean startActivitySafely(View v, Intent intent, Object tag) {
2986 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002987 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2988 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2989 return false;
2990 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002991 try {
2992 success = startActivity(v, intent, tag);
2993 } catch (ActivityNotFoundException e) {
2994 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2995 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2996 }
2997 return success;
2998 }
2999
Adam Cohen268c4752012-06-06 17:47:33 -07003000 /**
3001 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3002 * in the DragLayer in the exact absolute location of the original FolderIcon.
3003 */
3004 private void copyFolderIconToImage(FolderIcon fi) {
3005 final int width = fi.getMeasuredWidth();
3006 final int height = fi.getMeasuredHeight();
3007
3008 // Lazy load ImageView, Bitmap and Canvas
3009 if (mFolderIconImageView == null) {
3010 mFolderIconImageView = new ImageView(this);
3011 }
3012 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3013 mFolderIconBitmap.getHeight() != height) {
3014 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3015 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3016 }
3017
3018 DragLayer.LayoutParams lp;
3019 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3020 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3021 } else {
3022 lp = new DragLayer.LayoutParams(width, height);
3023 }
3024
Adam Cohen307fe232012-08-16 17:55:58 -07003025 // The layout from which the folder is being opened may be scaled, adjust the starting
3026 // view size by this scale factor.
3027 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003028 lp.customPosition = true;
3029 lp.x = mRectForFolderAnimation.left;
3030 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003031 lp.width = (int) (scale * width);
3032 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003033
3034 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3035 fi.draw(mFolderIconCanvas);
3036 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003037 if (fi.getFolder() != null) {
3038 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3039 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003040 }
Adam Cohen268c4752012-06-06 17:47:33 -07003041 // Just in case this image view is still in the drag layer from a previous animation,
3042 // we remove it and re-add it.
3043 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3044 mDragLayer.removeView(mFolderIconImageView);
3045 }
3046 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003047 if (fi.getFolder() != null) {
3048 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003049 }
Adam Cohen268c4752012-06-06 17:47:33 -07003050 }
3051
Adam Cohen2801caf2011-05-13 20:57:39 -07003052 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003053 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003054 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3055 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3056 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3057
Adam Cohenc51934b2011-07-26 21:07:43 -07003058 FolderInfo info = (FolderInfo) fi.getTag();
3059 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3060 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003061 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3062 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003063 }
3064
Adam Cohen268c4752012-06-06 17:47:33 -07003065 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3066 copyFolderIconToImage(fi);
3067 fi.setVisibility(View.INVISIBLE);
3068
Michael Jurka2ecf9952012-06-18 12:52:28 -07003069 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003070 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003071 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003072 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3073 }
3074 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003075 oa.start();
3076 }
3077
Adam Cohen268c4752012-06-06 17:47:33 -07003078 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003079 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003080 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3081 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3082 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3083
Adam Cohen268c4752012-06-06 17:47:33 -07003084 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003085
Adam Cohen268c4752012-06-06 17:47:33 -07003086 // We remove and re-draw the FolderIcon in-case it has changed
3087 mDragLayer.removeView(mFolderIconImageView);
3088 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003089 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003090 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003091 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003092 oa.addListener(new AnimatorListenerAdapter() {
3093 @Override
3094 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003095 if (cl != null) {
3096 cl.clearFolderLeaveBehind();
3097 // Remove the ImageView copy of the FolderIcon and make the original visible.
3098 mDragLayer.removeView(mFolderIconImageView);
3099 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003100 }
3101 }
3102 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003103 oa.start();
3104 }
3105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003107 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108 * is animated relative to the specified View. If the View is null, no animation
3109 * is played.
3110 *
3111 * @param folderInfo The FolderInfo describing the folder to open.
3112 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003113 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003114 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003115 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3116 if (openFolder != null && openFolder != folder) {
3117 // Close any open folder before opening a folder.
3118 closeFolder();
3119 }
3120
Adam Cohena9cf38f2011-05-02 15:36:58 -07003121 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122
Adam Cohena9cf38f2011-05-02 15:36:58 -07003123 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124
Sunny Goyalf4066152015-04-15 09:42:19 -07003125 // While the folder is open, the position of the icon cannot change.
3126 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3127
Adam Cohen4554ee12011-08-03 16:13:21 -07003128 // Just verify that the folder hasn't already been added to the DragLayer.
3129 // There was a one-off crash where the folder had a parent already.
3130 if (folder.getParent() == null) {
3131 mDragLayer.addView(folder);
3132 mDragController.addDropTarget((DropTarget) folder);
3133 } else {
3134 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3135 folder.getParent() + ").");
3136 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003137 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003138 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003139
3140 // Notify the accessibility manager that this folder "window" has appeared and occluded
3141 // the workspace items
3142 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3143 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003144 }
3145
3146 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003147 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003148 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003149 if (folder.isEditingName()) {
3150 folder.dismissEditingName();
3151 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003152 closeFolder(folder);
3153 }
3154 }
3155
3156 void closeFolder(Folder folder) {
3157 folder.getInfo().opened = false;
3158
3159 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3160 if (parent != null) {
3161 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3162 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003163 if (fi != null) {
3164 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3165 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003166 }
3167 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003168
3169 // Notify the accessibility manager that this folder "window" has disappeard and no
3170 // longer occludeds the workspace items
3171 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 }
3173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003175 if (!isDraggingEnabled()) return false;
3176 if (isWorkspaceLocked()) return false;
3177 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178
Adam Cohen1697b792013-09-17 19:08:21 -07003179 if (v instanceof Workspace) {
3180 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003181
3182 if (!mWorkspace.isTouchActive()) {
3183 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003184 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3185 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3186 return true;
3187 } else {
3188 return false;
3189 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003190 } else {
3191 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003192 }
Adam Cohen1697b792013-09-17 19:08:21 -07003193 }
3194
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003195 CellLayout.CellInfo longClickCellInfo = null;
3196 View itemUnderLongClick = null;
3197 if (v.getTag() instanceof ItemInfo) {
3198 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003199 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003200 itemUnderLongClick = longClickCellInfo.cell;
3201 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003202 }
3203
Winson Chung3d503fb2011-07-13 17:25:49 -07003204 // The hotseat touch handling does not go through Workspace, and we always allow long press
3205 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003206 final boolean inHotseat = isHotseatLayout(v);
3207 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003208 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003209 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003210 // User long pressed on empty space
3211 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3212 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003213 if (mWorkspace.isInOverviewMode()) {
3214 mWorkspace.startReordering(v);
3215 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003216 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003217 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003218 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003219 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3220 mHotseat.getOrderInHotseat(
3221 longClickCellInfo.cellX,
3222 longClickCellInfo.cellY));
3223 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003224 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003225 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003226 }
3227 }
3228 }
3229 return true;
3230 }
3231
Winson Chung3d503fb2011-07-13 17:25:49 -07003232 boolean isHotseatLayout(View layout) {
3233 return mHotseat != null && layout != null &&
3234 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3235 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003236
Winson Chung3d503fb2011-07-13 17:25:49 -07003237 /**
3238 * Returns the CellLayout of the specified container at the specified screen.
3239 */
Sunny Goyal92820592015-03-02 11:31:35 -08003240 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003241 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3242 if (mHotseat != null) {
3243 return mHotseat.getLayout();
3244 } else {
3245 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003246 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003247 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003248 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003249 }
3250 }
3251
Winson Chungb745afb2015-03-02 11:51:23 -08003252 /**
3253 * For overridden classes.
3254 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003255 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003256 return isAppsViewVisible();
3257 }
3258
3259 public boolean isAppsViewVisible() {
3260 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3261 }
3262
3263 public boolean isWidgetsViewVisible() {
3264 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003265 }
3266
Craig Mautner360310b2012-10-26 15:13:08 -07003267 private void setWorkspaceBackground(boolean workspace) {
3268 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003269 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003270 }
3271
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003272 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003273 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3274 int curflags = getWindow().getAttributes().flags
3275 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3276 if (wpflags != curflags) {
3277 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3278 }
Craig Mautner360310b2012-10-26 15:13:08 -07003279 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003280 }
3281
Michael Jurkae326f182011-11-21 14:05:46 -08003282 @Override
3283 public void onTrimMemory(int level) {
3284 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003285 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3286 // The widget preview db can result in holding onto over
3287 // 3MB of memory for caching which isn't necessary.
3288 SQLiteDatabase.releaseMemory();
3289
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003290 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003291 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003292 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003293 if (mLauncherCallbacks != null) {
3294 mLauncherCallbacks.onTrimMemory(level);
3295 }
Michael Jurkae326f182011-11-21 14:05:46 -08003296 }
3297
Winson Chungb745afb2015-03-02 11:51:23 -08003298 @Override
3299 public void onStateTransitionHideSearchBar() {
3300 // Hide the search bar
3301 if (mSearchDropTargetBar != null) {
3302 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3303 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003304 }
3305
Winson Chungb745afb2015-03-02 11:51:23 -08003306 protected void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003307 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3308 true);
Adam Cohened307df2013-10-02 09:37:31 -07003309 }
3310
Winson Chungdc61c4d2015-04-20 18:26:57 -07003311 protected void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
3312 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3313 onCompleteRunnable, true);
3314 }
3315
3316 protected void showWorkspace(int snapToPage, boolean animated) {
3317 showWorkspace(snapToPage, animated, null, true);
3318 }
3319
3320 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3321 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003322 boolean changed = mState != State.WORKSPACE ||
3323 mWorkspace.getState() != Workspace.State.NORMAL;
3324 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003325 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003326 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003327 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003328 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003329
Winson Chungc7d2b602012-05-16 17:02:20 -07003330 // Show the search bar (only animate if we were showing the drop target bar in spring
3331 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003332 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003333 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003334 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003335
Michael Jurkab3e22d92011-10-31 15:58:33 -07003336 // Set focus to the AppsCustomize button
3337 if (mAllAppsButton != null) {
3338 mAllAppsButton.requestFocus();
3339 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003340 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003341
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003342 // Change the state *after* we've called all the transition code
3343 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003344
Winson Chung5fb63472011-02-02 17:03:37 -08003345 // Resume the auto-advance of widgets
3346 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003347 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003348
Winson Chung0f785722015-04-08 10:27:49 -07003349 if (changed) {
3350 // Send an accessibility event to announce the context change
3351 getWindow().getDecorView()
3352 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003353
Winson Chung0f785722015-04-08 10:27:49 -07003354 onWorkspaceShown(animated);
3355 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003356 }
3357
Adam Cohened307df2013-10-02 09:37:31 -07003358 void showOverviewMode(boolean animated) {
3359 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003360 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003361 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3362 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003363 mState = State.WORKSPACE;
3364 onWorkspaceShown(animated);
3365 }
3366
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003367 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003368 }
3369
Winson Chungb745afb2015-03-02 11:51:23 -08003370 /**
3371 * Shows the apps view.
3372 */
3373 void showAppsView(boolean animated, boolean resetListToTop) {
3374 if (resetListToTop) {
3375 mAppsView.scrollToTop();
3376 }
3377 showAppsOrWidgets(animated, State.APPS);
3378 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003379
Winson Chungb745afb2015-03-02 11:51:23 -08003380 /**
3381 * Shows the widgets view.
3382 */
3383 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003384 Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003385 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003386 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003387 }
Winson Chungb745afb2015-03-02 11:51:23 -08003388 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003389
3390 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003391 @Override
3392 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003393 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003394 }
3395 });
Winson Chungb745afb2015-03-02 11:51:23 -08003396 }
3397
3398 /**
3399 * Sets up the transition to show the apps/widgets view.
3400 */
3401 private void showAppsOrWidgets(boolean animated, State toState) {
3402 if (mState != State.WORKSPACE) return;
3403 if (toState != State.APPS && toState != State.WIDGETS) return;
3404
3405 if (toState == State.APPS) {
3406 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung0f785722015-04-08 10:27:49 -07003407 if (mLauncherCallbacks != null) {
3408 mLauncherCallbacks.onAllAppsShown();
3409 }
Winson Chungb745afb2015-03-02 11:51:23 -08003410 } else {
3411 mStateTransitionAnimation.startAnimationToWidgets(animated);
3412 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003413
Michael Jurkab3e22d92011-10-31 15:58:33 -07003414 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003415 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003416
3417 // Pause the auto-advance of widgets until we are out of AllApps
3418 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003419 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003420 closeFolder();
3421
3422 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003423 getWindow().getDecorView()
3424 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003425 }
3426
Hyunyoung Song3f471442015-04-08 19:01:34 -07003427 public void enterSpringLoadedDragMode() {
3428 Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s",
3429 mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003430 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3431 mState == State.WIDGETS_SPRING_LOADED) {
3432 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003433 }
Winson Chungb745afb2015-03-02 11:51:23 -08003434
3435 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003436 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3437 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003438 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003439 }
Adam Cohen7777d962011-08-18 18:58:38 -07003440
Hyunyoung Song3f471442015-04-08 19:01:34 -07003441 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003442 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003443 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003444
Winson Chunge7a03942011-08-05 15:05:12 -07003445 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003446 @Override
3447 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003448 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003449 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3450 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003451 // Before we show workspace, hide all apps again because
3452 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3453 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003454 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003455 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003456 } else {
3457 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003458 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003459 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003460 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003461 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003462
Michael Jurkad3ef3062010-11-23 16:23:58 -08003463 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003464 if (mState == State.APPS_SPRING_LOADED) {
3465 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3466 mState = State.APPS;
3467 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3468 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3469 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003470 }
3471 // Otherwise, we are not in spring loaded mode, so don't do anything.
3472 }
3473
Michael Jurkab3e22d92011-10-31 15:58:33 -07003474 void lockAllApps() {
3475 // TODO
3476 }
3477
3478 void unlockAllApps() {
3479 // TODO
3480 }
3481
Sunny Goyal594d76d2014-11-06 10:12:54 -08003482 protected void disableVoiceButtonProxy(boolean disable) {
3483 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003484 }
3485
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003486 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003487 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3488 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003489 }
3490
Adam Cohen24ce0b32014-01-14 16:18:14 -08003491 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003492 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3493 if (searchProvider == null) {
3494 return null;
3495 }
3496
3497 Bundle opts = new Bundle();
3498 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003499 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003500
3501 SharedPreferences sp = getSharedPreferences(
3502 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3503 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003504 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003505 if (!searchProvider.provider.flattenToString().equals(
3506 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003507 || (widgetInfo == null)
3508 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003509 // A valid widget is not already bound.
3510 if (widgetId > -1) {
3511 mAppWidgetHost.deleteAppWidgetId(widgetId);
3512 widgetId = -1;
3513 }
3514
3515 // Try to bind a new widget
3516 widgetId = mAppWidgetHost.allocateAppWidgetId();
3517
3518 if (!AppWidgetManagerCompat.getInstance(this)
3519 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3520 mAppWidgetHost.deleteAppWidgetId(widgetId);
3521 widgetId = -1;
3522 }
3523
3524 sp.edit()
3525 .putInt(QSB_WIDGET_ID, widgetId)
3526 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3527 .commit();
3528 }
3529
3530 if (widgetId != -1) {
3531 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3532 mQsb.updateAppWidgetOptions(opts);
3533 mQsb.setPadding(0, 0, 0, 0);
3534 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003535 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003536 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003537 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003538 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003539 }
3540
Sunny Goyal22235bc2015-04-03 09:23:43 -07003541 private void reinflateQSBIfNecessary() {
3542 if (mQsb instanceof LauncherAppWidgetHostView &&
3543 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3544 mSearchDropTargetBar.removeView(mQsb);
3545 mQsb = null;
3546 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3547 }
3548 }
3549
Michael Jurkad7c28052012-04-27 15:43:36 -07003550 @Override
3551 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003552 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003553 final List<CharSequence> text = event.getText();
3554 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003555 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003556 if (mState == State.APPS) {
3557 text.add("Apps");
3558 } else if (mState == State.WIDGETS) {
3559 text.add("Widgets");
alanv1d4fde62012-10-17 13:15:47 -07003560 } else {
3561 text.add(getString(R.string.all_apps_home_button_label));
3562 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003563 return result;
3564 }
3565
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003566 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003567 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003568 */
Adam Cohen091440a2015-03-18 14:16:05 -07003569 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003570 @Override
3571 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003572 closeSystemDialogs();
3573 }
3574 }
3575
3576 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003577 * Receives notifications whenever the appwidgets are reset.
3578 */
3579 private class AppWidgetResetObserver extends ContentObserver {
3580 public AppWidgetResetObserver() {
3581 super(new Handler());
3582 }
3583
3584 @Override
3585 public void onChange(boolean selfChange) {
3586 onAppWidgetReset();
3587 }
3588 }
3589
3590 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003591 * If the activity is currently paused, signal that we need to run the passed Runnable
3592 * in onResume.
3593 *
3594 * This needs to be called from incoming places where resources might have been loaded
3595 * while we are paused. That is becaues the Configuration might be wrong
3596 * when we're not running, and if it comes back to what it was when we
3597 * were paused, we are not restarted.
3598 *
3599 * Implementation of the method from LauncherModel.Callbacks.
3600 *
3601 * @return true if we are currently paused. The caller might be able to
3602 * skip some work in that case since we will come back again.
3603 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003604 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003605 if (mPaused) {
3606 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003607 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003608 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003609 }
3610 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003611 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003612 return true;
3613 } else {
3614 return false;
3615 }
3616 }
3617
Michael Jurkac402cd92013-05-20 15:49:32 +02003618 private boolean waitUntilResume(Runnable run) {
3619 return waitUntilResume(run, false);
3620 }
3621
Michael Jurka1e2f4652013-07-08 18:03:46 -07003622 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003623 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003624 }
3625
Michael Jurka7607c2f2013-04-03 14:33:19 -07003626 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003627 * If the activity is currently paused, signal that we need to re-run the loader
3628 * in onResume.
3629 *
3630 * This needs to be called from incoming places where resources might have been loaded
3631 * while we are paused. That is becaues the Configuration might be wrong
3632 * when we're not running, and if it comes back to what it was when we
3633 * were paused, we are not restarted.
3634 *
3635 * Implementation of the method from LauncherModel.Callbacks.
3636 *
3637 * @return true if we are currently paused. The caller might be able to
3638 * skip some work in that case since we will come back again.
3639 */
3640 public boolean setLoadOnResume() {
3641 if (mPaused) {
3642 Log.i(TAG, "setLoadOnResume");
3643 mOnResumeNeedsLoad = true;
3644 return true;
3645 } else {
3646 return false;
3647 }
3648 }
3649
3650 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003651 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003652 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003654 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003655 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003656 } else {
3657 return SCREEN_COUNT / 2;
3658 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003659 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003660
Joe Onorato9c1289c2009-08-17 11:03:03 -04003661 /**
3662 * Refreshes the shortcuts shown on the workspace.
3663 *
3664 * Implementation of the method from LauncherModel.Callbacks.
3665 */
3666 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003667 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003668
Michael Jurka7607c2f2013-04-03 14:33:19 -07003669 // If we're starting binding all over again, clear any bind calls we'd postponed in
3670 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3671 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003672 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003673
Winson Chungd64d1762013-08-20 14:37:16 -07003674 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003675 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003676 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003677
Michael Jurka05bf6442011-11-30 20:28:53 -08003678 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003679 if (mHotseat != null) {
3680 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003681 }
3682 }
3683
Adam Cohendcd297f2013-06-18 13:13:40 -07003684 @Override
3685 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003686 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003687
Adam Cohen5084cba2013-09-03 12:01:16 -07003688 // If there are no screens, we need to have an empty screen
3689 if (orderedScreenIds.size() == 0) {
3690 mWorkspace.addExtraEmptyScreen();
3691 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003692
3693 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003694 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003695 if (hasCustomContentToLeft()) {
3696 mWorkspace.createCustomContentContainer();
3697 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003698 }
Winson Chung64359a52013-07-08 17:17:08 -07003699 }
3700
3701 @Override
3702 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003703 // Log to disk
3704 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3705 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3706 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003707 int count = orderedScreenIds.size();
3708 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003709 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003710 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003711 }
3712
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003713 @Override
3714 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3715 final long screenId, final int[] cell, final int spanX, final int spanY) {
3716 showWorkspace(true, new Runnable() {
3717
3718 @Override
3719 public void run() {
3720 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3721 addPendingItem(info, container, screenId, cell, spanX, spanY);
3722 }
3723 });
3724 }
3725
Adam Cohen39a06042013-07-19 14:30:12 -07003726 private boolean shouldShowWeightWatcher() {
3727 String spKey = LauncherAppState.getSharedPreferencesKey();
3728 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003729 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003730
3731 return show;
3732 }
3733
3734 private void toggleShowWeightWatcher() {
3735 String spKey = LauncherAppState.getSharedPreferencesKey();
3736 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3737 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3738
3739 show = !show;
3740
3741 SharedPreferences.Editor editor = sp.edit();
3742 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3743 editor.commit();
3744
3745 if (mWeightWatcher != null) {
3746 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3747 }
3748 }
3749
Winson Chungd64d1762013-08-20 14:37:16 -07003750 public void bindAppsAdded(final ArrayList<Long> newScreens,
3751 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003752 final ArrayList<ItemInfo> addAnimated,
3753 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003754 Runnable r = new Runnable() {
3755 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003756 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003757 }
3758 };
3759 if (waitUntilResume(r)) {
3760 return;
3761 }
3762
Winson Chungd64d1762013-08-20 14:37:16 -07003763 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003764 if (newScreens != null) {
3765 bindAddScreens(newScreens);
3766 }
Winson Chungd64d1762013-08-20 14:37:16 -07003767
3768 // We add the items without animation on non-visible pages, and with
3769 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003770 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003771 bindItems(addNotAnimated, 0,
3772 addNotAnimated.size(), false);
3773 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003774 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003775 bindItems(addAnimated, 0,
3776 addAnimated.size(), true);
3777 }
Winson Chungc58497e2013-09-03 17:48:37 -07003778
Winson Chung87412982013-10-03 18:34:14 -07003779 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003780 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003781
Winson Chungb745afb2015-03-02 11:51:23 -08003782 if (addedApps != null && mAppsView != null) {
3783 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003784 }
Winson Chungd64d1762013-08-20 14:37:16 -07003785 }
3786
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003787 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 * Bind the items start-end from the list.
3789 *
3790 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003791 */
Winson Chung64359a52013-07-08 17:17:08 -07003792 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3793 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003794 Runnable r = new Runnable() {
3795 public void run() {
3796 bindItems(shortcuts, start, end, forceAnimateIcons);
3797 }
3798 };
3799 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003800 return;
3801 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003802
Winson Chungf0c6ae02012-03-21 16:10:31 -07003803 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003804 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3805 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003806 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003807 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003808 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003809 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003810 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003811
3812 // Short circuit if we are loading dock items for a configuration which has no dock
3813 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3814 mHotseat == null) {
3815 continue;
3816 }
3817
Joe Onorato9c1289c2009-08-17 11:03:03 -04003818 switch (item.itemType) {
3819 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3820 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003821 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003822 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003823
Winson Chung64359a52013-07-08 17:17:08 -07003824 /*
3825 * TODO: FIX collision case
3826 */
Winson Chungce376632013-07-11 16:12:41 -07003827 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3828 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3829 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003830 View v = cl.getChildAt(item.cellX, item.cellY);
3831 Object tag = v.getTag();
3832 String desc = "Collision while binding workspace item: " + item
3833 + ". Collides with " + tag;
3834 if (LauncherAppState.isDogfoodBuild()) {
3835 throw (new RuntimeException(desc));
3836 } else {
3837 Log.d(TAG, desc);
3838 }
Winson Chungce376632013-07-11 16:12:41 -07003839 }
Winson Chung64359a52013-07-08 17:17:08 -07003840 }
3841
Adam Cohendcd297f2013-06-18 13:13:40 -07003842 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3843 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003844 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003845 // Animate all the applications up now
3846 shortcut.setAlpha(0f);
3847 shortcut.setScaleX(0f);
3848 shortcut.setScaleY(0f);
3849 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003850 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003851 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003853 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003854 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003855 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003856 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003857 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3858 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003859 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003860 default:
3861 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003862 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003863 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003864
Winson Chung997a9232013-07-24 15:33:46 -07003865 if (animateIcons) {
3866 // Animate to the correct page
3867 if (newShortcutsScreenId > -1) {
3868 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003869 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003870 final Runnable startBounceAnimRunnable = new Runnable() {
3871 public void run() {
3872 anim.playTogether(bounceAnims);
3873 anim.start();
3874 }
3875 };
Winson Chung997a9232013-07-24 15:33:46 -07003876 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003877 // We post the animation slightly delayed to prevent slowdowns
3878 // when we are loading right after we return to launcher.
3879 mWorkspace.postDelayed(new Runnable() {
3880 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003881 if (mWorkspace != null) {
3882 mWorkspace.snapToPage(newScreenIndex);
3883 mWorkspace.postDelayed(startBounceAnimRunnable,
3884 NEW_APPS_ANIMATION_DELAY);
3885 }
Winson Chung94d67682013-09-25 16:29:40 -07003886 }
3887 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003888 } else {
3889 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003890 }
3891 }
Winson Chung64359a52013-07-08 17:17:08 -07003892 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003893 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003894 }
3895
Joe Onorato9c1289c2009-08-17 11:03:03 -04003896 /**
3897 * Implementation of the method from LauncherModel.Callbacks.
3898 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003899 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003900 Runnable r = new Runnable() {
3901 public void run() {
3902 bindFolders(folders);
3903 }
3904 };
3905 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003906 return;
3907 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003908 sFolders.clear();
3909 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003910 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003911
3912 /**
3913 * Add the views for a widget to the workspace.
3914 *
3915 * Implementation of the method from LauncherModel.Callbacks.
3916 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003917 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003918 Runnable r = new Runnable() {
3919 public void run() {
3920 bindAppWidget(item);
3921 }
3922 };
3923 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003924 return;
3925 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003926
Daniel Sandler843e8602010-06-07 14:59:01 -04003927 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3928 if (DEBUG_WIDGETS) {
3929 Log.d(TAG, "bindAppWidget: " + item);
3930 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003931 final Workspace workspace = mWorkspace;
3932
Adam Cohen59400422014-03-05 18:07:04 -08003933 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003934 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003935
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003936 if (!mIsSafeModeEnabled
3937 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3938 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003939 if (appWidgetInfo == null) {
3940 if (DEBUG_WIDGETS) {
3941 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3942 + " belongs to component " + item.providerName
3943 + ", as the povider is null");
3944 }
3945 LauncherModel.deleteItemFromDatabase(this, item);
3946 return;
3947 }
3948 // Note: This assumes that the id remap broadcast is received before this step.
3949 // If that is not the case, the id remap will be ignored and user may see the
3950 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003951 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003952 pendingInfo.spanX = item.spanX;
3953 pendingInfo.spanY = item.spanY;
3954 pendingInfo.minSpanX = item.minSpanX;
3955 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003956 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07003957 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003958
Sunny Goyalff572272014-07-23 13:58:07 -07003959 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003960 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3961 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003962
3963 // TODO consider showing a permission dialog when the widget is clicked.
3964 if (!success) {
3965 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3966 if (DEBUG_WIDGETS) {
3967 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3968 + " belongs to component " + item.providerName
3969 + ", as the launcher is unable to bing a new widget id");
3970 }
3971 LauncherModel.deleteItemFromDatabase(this, item);
3972 return;
3973 }
3974
3975 item.appWidgetId = newWidgetId;
3976
3977 // If the widget has a configure activity, it is still needs to set it up, otherwise
3978 // the widget is ready to go.
3979 item.restoreStatus = (appWidgetInfo.configure == null)
3980 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3981 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3982
3983 LauncherModel.updateItemInDatabase(this, item);
3984 }
3985
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003986 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003987 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003988 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003989 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3990 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003991 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003992
Sunny Goyal651077b2014-06-30 14:15:31 -07003993 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3994 } else {
3995 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003996 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3997 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003998 view.updateIcon(mIconCache);
3999 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004000 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004001 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004002 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004003
Joe Onorato9c1289c2009-08-17 11:03:03 -04004004 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004005 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004006
Adam Cohendcd297f2013-06-18 13:13:40 -07004007 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004008 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004009 if (!item.isCustomWidget()) {
4010 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4011 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004012
Joe Onorato9c1289c2009-08-17 11:03:03 -04004013 workspace.requestLayout();
4014
Daniel Sandler843e8602010-06-07 14:59:01 -04004015 if (DEBUG_WIDGETS) {
4016 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4017 + (SystemClock.uptimeMillis()-start) + "ms");
4018 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004019 }
4020
Sunny Goyalff572272014-07-23 13:58:07 -07004021 /**
4022 * Restores a pending widget.
4023 *
4024 * @param appWidgetId The app widget id
4025 * @param cellInfo The position on screen where to create the widget.
4026 */
4027 private void completeRestoreAppWidget(final int appWidgetId) {
4028 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4029 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4030 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4031 return;
4032 }
4033
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004034 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004035 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4036
4037 mWorkspace.reinflateWidgetsIfNecessary();
4038 LauncherModel.updateItemInDatabase(this, info);
4039 }
4040
Adam Cohen1462de32012-07-24 22:34:36 -07004041 public void onPageBoundSynchronously(int page) {
4042 mSynchronouslyBoundPages.add(page);
4043 }
4044
Joe Onorato9c1289c2009-08-17 11:03:03 -04004045 /**
4046 * Callback saying that there aren't any more items to bind.
4047 *
4048 * Implementation of the method from LauncherModel.Callbacks.
4049 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004050 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004051 Runnable r = new Runnable() {
4052 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004053 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004054 }
4055 };
4056 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004057 return;
4058 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004059 if (mSavedState != null) {
4060 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004061 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004062 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004063 mSavedState = null;
4064 }
4065
Adam Cohen1462de32012-07-24 22:34:36 -07004066 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004067
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004068 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004069 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004070
4071 // If we received the result of any pending adds while the loader was running (e.g. the
4072 // widget configuration forced an orientation change), process them now.
4073 if (sPendingAddItem != null) {
4074 final long screenId = completeAdd(sPendingAddItem);
4075
4076 // TODO: this moves the user to the page where the pending item was added. Ideally,
4077 // the screen would be guaranteed to exist after bind, and the page would be set through
4078 // the workspace restore process.
4079 mWorkspace.post(new Runnable() {
4080 @Override
4081 public void run() {
4082 mWorkspace.snapToScreenId(screenId);
4083 }
4084 });
4085 sPendingAddItem = null;
4086 }
4087
Sunny Goyale755d462014-07-22 13:48:29 -07004088 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004089
4090 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004091 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004092 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004093 }
4094
Adam Cohen3ed316a2014-07-23 18:21:20 -07004095 private void sendLoadingCompleteBroadcastIfNecessary() {
4096 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4097 String permission =
4098 getResources().getString(R.string.receive_first_load_broadcast_permission);
4099 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4100 sendBroadcast(intent, permission);
4101 SharedPreferences.Editor editor = mSharedPrefs.edit();
4102 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4103 editor.apply();
4104 }
4105 }
4106
Winson Chunga0b7e862013-09-05 16:03:15 -07004107 public boolean isAllAppsButtonRank(int rank) {
4108 if (mHotseat != null) {
4109 return mHotseat.isAllAppsButtonRank(rank);
4110 }
4111 return false;
4112 }
4113
Winson Chunga2413752012-04-03 14:22:34 -07004114 private boolean canRunNewAppsAnimation() {
4115 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4116 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4117 }
4118
Winson Chungc9168342013-06-26 14:54:55 -07004119 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4120 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4121 PropertyValuesHolder.ofFloat("alpha", 1f),
4122 PropertyValuesHolder.ofFloat("scaleX", 1f),
4123 PropertyValuesHolder.ofFloat("scaleY", 1f));
4124 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4125 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4126 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4127 return bounceAnim;
4128 }
4129
Adam Cohen27772732013-11-11 14:00:56 +00004130 public boolean useVerticalBarLayout() {
4131 return LauncherAppState.getInstance().getDynamicGrid().
4132 getDeviceProfile().isVerticalBarLayout();
4133 }
4134
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004135 protected Rect getSearchBarBounds() {
4136 return LauncherAppState.getInstance().getDynamicGrid().
4137 getDeviceProfile().getSearchBarBounds();
4138 }
4139
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004140 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004141 if (mSearchDropTargetBar == null) {
4142 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004143 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004144 if (mQsb != null) {
4145 mSearchDropTargetBar.removeView(mQsb);
4146 mQsb = null;
4147 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004148 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004149 }
4150
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004151 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004152 * Add the icons for all apps.
4153 *
4154 * Implementation of the method from LauncherModel.Callbacks.
4155 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004156 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004157 if (mAppsView != null) {
4158 mAppsView.setApps(apps);
4159 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07004160 if (mWidgetsView != null) {
4161 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4162 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004163 }
Adam Cohen9211d422014-10-07 18:14:53 -07004164 if (mLauncherCallbacks != null) {
4165 mLauncherCallbacks.bindAllApplications(apps);
4166 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004167 }
4168
4169 /**
4170 * A package was updated.
4171 *
4172 * Implementation of the method from LauncherModel.Callbacks.
4173 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004174 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004175 Runnable r = new Runnable() {
4176 public void run() {
4177 bindAppsUpdated(apps);
4178 }
4179 };
4180 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004181 return;
4182 }
4183
Winson Chungb745afb2015-03-02 11:51:23 -08004184 if (mAppsView != null) {
4185 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004186 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004187 }
4188
Sunny Goyal4390ace2014-10-13 11:33:11 -07004189 @Override
4190 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4191 Runnable r = new Runnable() {
4192 public void run() {
4193 bindWidgetsRestored(widgets);
4194 }
4195 };
4196 if (waitUntilResume(r)) {
4197 return;
4198 }
4199 mWorkspace.widgetsRestored(widgets);
4200 }
4201
Joe Onorato9c1289c2009-08-17 11:03:03 -04004202 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004203 * Some shortcuts were updated in the background.
4204 *
4205 * Implementation of the method from LauncherModel.Callbacks.
4206 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004207 @Override
4208 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4209 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004210 Runnable r = new Runnable() {
4211 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004212 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004213 }
4214 };
4215 if (waitUntilResume(r)) {
4216 return;
4217 }
4218
Sunny Goyal4390ace2014-10-13 11:33:11 -07004219 if (!updated.isEmpty()) {
4220 mWorkspace.updateShortcuts(updated);
4221 }
4222
4223 if (!removed.isEmpty()) {
4224 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4225 for (ShortcutInfo si : removed) {
4226 removedComponents.add(si.getTargetComponent());
4227 }
4228 mWorkspace.removeItemsByComponentName(removedComponents, user);
4229 // Notify the drag controller
4230 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004231 }
4232 }
4233
4234 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004235 * Update the state of a package, typically related to install state.
4236 *
4237 * Implementation of the method from LauncherModel.Callbacks.
4238 */
Sunny Goyale755d462014-07-22 13:48:29 -07004239 @Override
4240 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004241 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004242 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004243 }
4244 }
4245
4246 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004247 * Update the label and icon of all the icons in a package
4248 *
4249 * Implementation of the method from LauncherModel.Callbacks.
4250 */
4251 @Override
4252 public void updatePackageBadge(String packageName) {
4253 if (mWorkspace != null) {
4254 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4255 }
4256 }
4257
4258 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004259 * A package was uninstalled. We take both the super set of packageNames
4260 * in addition to specific applications to remove, the reason being that
4261 * this can be called when a package is updated as well. In that scenario,
4262 * we only remove specific components from the workspace, where as
4263 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004264 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004265 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004266 * Implementation of the method from LauncherModel.Callbacks.
4267 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004268 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004269 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004270 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004271 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004272 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004273 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004274 }
Winson Chungd64d1762013-08-20 14:37:16 -07004275 };
4276 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004277 return;
4278 }
4279
Sunny Goyal1a745e82014-10-02 15:58:31 -07004280 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004281 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4282 for (AppInfo info : appInfos) {
4283 removedComponents.add(info.componentName);
4284 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004285 if (!packageNames.isEmpty()) {
4286 mWorkspace.removeItemsByPackageName(packageNames, user);
4287 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004288 if (!removedComponents.isEmpty()) {
4289 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004290 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004291 // Notify the drag controller
4292 mDragController.onAppsRemoved(packageNames, removedComponents);
4293
Sunny Goyal1a745e82014-10-02 15:58:31 -07004294 } else {
4295 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004296 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004297
Winson Chungdf95eb12013-10-16 14:57:07 -07004298 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004299 if (mAppsView != null) {
4300 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004301 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004302 }
Joe Onoratobe386092009-11-17 17:32:16 -08004303
4304 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004305 * A number of packages were updated.
4306 */
Adam Cohen091440a2015-03-18 14:16:05 -07004307 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004308 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004309 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004310 bindPackagesUpdated(mWidgetsAndShortcuts);
4311 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004312 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004313 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004314
Michael Jurkac402cd92013-05-20 15:49:32 +02004315 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4316 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4317 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004318 return;
4319 }
4320
Hyunyoung Song3f471442015-04-08 19:01:34 -07004321 if (mWidgetsView != null) {
4322 mWidgetsView.addWidgets(LauncherModel.getSortedWidgetsAndShortcuts(this, false),
4323 getPackageManager());
Winson Chung785d2eb2011-04-14 16:08:02 -07004324 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004325 }
4326
Winson Chung400438b2011-01-16 17:53:48 -08004327 private int mapConfigurationOriActivityInfoOri(int configOri) {
4328 final Display d = getWindowManager().getDefaultDisplay();
4329 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4330 switch (d.getRotation()) {
4331 case Surface.ROTATION_0:
4332 case Surface.ROTATION_180:
4333 // We are currently in the same basic orientation as the natural orientation
4334 naturalOri = configOri;
4335 break;
4336 case Surface.ROTATION_90:
4337 case Surface.ROTATION_270:
4338 // We are currently in the other basic orientation to the natural orientation
4339 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4340 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4341 break;
4342 }
4343
4344 int[] oriMap = {
4345 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4346 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4347 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4348 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4349 };
4350 // Since the map starts at portrait, we need to offset if this device's natural orientation
4351 // is landscape.
4352 int indexOffset = 0;
4353 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4354 indexOffset = 1;
4355 }
4356 return oriMap[(d.getRotation() + indexOffset) % 4];
4357 }
Adam Cohen446e9402011-09-15 18:21:21 -07004358
Winson Chung4b919f82012-05-01 10:44:08 -07004359 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004360 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004361 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4362 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4363 .getConfiguration().orientation));
4364 } else {
4365 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4366 }
Winson Chung4b919f82012-05-01 10:44:08 -07004367 }
4368 }
4369 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004370 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004371 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004372 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004373 } else {
4374 mHandler.postDelayed(new Runnable() {
4375 public void run() {
4376 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4377 }
4378 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004379 }
Winson Chung4b919f82012-05-01 10:44:08 -07004380 }
Winson Chung400438b2011-01-16 17:53:48 -08004381 }
4382
Winson Chunge43a1e72014-01-15 10:33:02 -08004383 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004384 if (mLauncherCallbacks != null) {
4385 return mLauncherCallbacks.isLauncherPreinstalled();
4386 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004387 PackageManager pm = getPackageManager();
4388 try {
4389 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4390 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4391 return true;
4392 } else {
4393 return false;
4394 }
4395 } catch (NameNotFoundException e) {
4396 e.printStackTrace();
4397 return false;
4398 }
4399 }
4400
Adam Cohenea90f832014-05-21 15:03:34 -07004401 /**
4402 * This method indicates whether or not we should suggest default wallpaper dimensions
4403 * when our wallpaper cropper was not yet used to set a wallpaper.
4404 */
4405 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004406 if (mLauncherCallbacks != null) {
4407 return mLauncherCallbacks.overrideWallpaperDimensions();
4408 }
Adam Cohenea90f832014-05-21 15:03:34 -07004409 return true;
4410 }
4411
Adam Cohen432609a2014-03-13 17:03:22 -07004412 /**
4413 * To be overridden by subclasses to indicate that there is an activity to launch
4414 * before showing the standard launcher experience.
4415 */
4416 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004417 if (mLauncherCallbacks != null) {
4418 return mLauncherCallbacks.hasFirstRunActivity();
4419 }
Adam Cohen432609a2014-03-13 17:03:22 -07004420 return false;
4421 }
4422
4423 /**
4424 * To be overridden by subclasses to launch any first run activity
4425 */
4426 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004427 if (mLauncherCallbacks != null) {
4428 return mLauncherCallbacks.getFirstRunActivity();
4429 }
Adam Cohen432609a2014-03-13 17:03:22 -07004430 return null;
4431 }
4432
Winson Chung2826a402015-04-17 16:18:53 -07004433 /**
4434 * Returns whether the launcher callbacks overrides search in all apps.
4435 * @return
4436 */
4437 @Thunk boolean isAllAppsSearchOverridden() {
4438 if (mLauncherCallbacks != null) {
4439 return mLauncherCallbacks.overrideAllAppsSearch();
4440 }
4441 return false;
4442 }
4443
Winson Chunga6945242014-01-08 14:04:34 -08004444 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004445 return !ActivityManager.isRunningInTestHarness() &&
4446 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004447 }
4448
Adam Cohen4a9423d2014-03-28 14:22:31 -07004449 protected boolean hasRunFirstRunActivity() {
4450 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4451 }
4452
Adam Cohen432609a2014-03-13 17:03:22 -07004453 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004454 if (shouldRunFirstRunActivity() &&
4455 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004456 Intent firstRunIntent = getFirstRunActivity();
4457 if (firstRunIntent != null) {
4458 startActivity(firstRunIntent);
4459 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004460 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004461 }
4462 }
Adam Cohen432609a2014-03-13 17:03:22 -07004463 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004464 }
4465
4466 private void markFirstRunActivityShown() {
4467 SharedPreferences.Editor editor = mSharedPrefs.edit();
4468 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4469 editor.apply();
4470 }
4471
Adam Cohen432609a2014-03-13 17:03:22 -07004472 /**
4473 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4474 * screen that must be displayed and dismissed.
4475 */
4476 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004477 if (mLauncherCallbacks != null) {
4478 return mLauncherCallbacks.hasDismissableIntroScreen();
4479 }
Adam Cohen432609a2014-03-13 17:03:22 -07004480 return false;
4481 }
4482
4483 /**
4484 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4485 */
4486 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004487 if (mLauncherCallbacks != null) {
4488 return mLauncherCallbacks.getIntroScreen();
4489 }
Adam Cohen432609a2014-03-13 17:03:22 -07004490 return null;
4491 }
4492
4493 /**
4494 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4495 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4496 */
4497 private boolean shouldShowIntroScreen() {
4498 return hasDismissableIntroScreen() &&
4499 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4500 }
4501
4502 protected void showIntroScreen() {
4503 View introScreen = getIntroScreen();
4504 changeWallpaperVisiblity(false);
4505 if (introScreen != null) {
4506 mDragLayer.showOverlayView(introScreen);
4507 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004508 if (mLauncherOverlayContainer != null) {
4509 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4510 }
Adam Cohen432609a2014-03-13 17:03:22 -07004511 }
4512
4513 public void dismissIntroScreen() {
4514 markIntroScreenDismissed();
4515 if (showFirstRunActivity()) {
4516 // We delay hiding the intro view until the first run activity is showing. This
4517 // avoids a blip.
4518 mWorkspace.postDelayed(new Runnable() {
4519 @Override
4520 public void run() {
4521 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004522 if (mLauncherOverlayContainer != null) {
4523 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4524 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004525 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004526 }
4527 }, ACTIVITY_START_DELAY);
4528 } else {
4529 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004530 if (mLauncherOverlayContainer != null) {
4531 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4532 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004533 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004534 }
4535 changeWallpaperVisiblity(true);
4536 }
4537
4538 private void markIntroScreenDismissed() {
4539 SharedPreferences.Editor editor = mSharedPrefs.edit();
4540 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4541 editor.apply();
4542 }
4543
Adam Cohen091440a2015-03-18 14:16:05 -07004544 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004545 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4546 // on the device, then we always show the first run cling experience (or if there is no
4547 // launcher2). Otherwise, we prompt the user upon started for migration
4548 LauncherClings launcherClings = new LauncherClings(this);
4549 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4550 if (mModel.canMigrateFromOldLauncherDb(this)) {
4551 launcherClings.showMigrationCling();
4552 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004553 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004554 }
4555 }
4556 }
4557
Winson Chunga6945242014-01-08 14:04:34 -08004558 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004559 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4560 if (mHotseat != null) mHotseat.setAlpha(1f);
4561 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4562 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004563 }
4564
4565 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004566 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4567 if (mHotseat != null) mHotseat.setAlpha(0f);
4568 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4569 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004570 }
4571
Mathew Inwood72fbec12013-11-19 15:45:07 +00004572 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004573 // Called from search suggestion, not supported in other profiles.
4574 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4575 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4576 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4577 myUser);
4578 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004579 return null;
4580 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004581 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004582 }
4583
4584 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4585 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004586 // Called from search suggestion, not supported in other profiles.
4587 return createShortcutDragInfo(shortcutIntent, caption, icon,
4588 UserHandleCompat.myUserHandle());
4589 }
4590
4591 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4592 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004593 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004594 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004595 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004596 }
4597
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004598 protected void moveWorkspaceToDefaultScreen() {
4599 mWorkspace.moveToDefaultScreen(false);
4600 }
4601
Mathew Inwood72fbec12013-11-19 15:45:07 +00004602 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4603 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004604 mWorkspace.onExternalDragStartedWithItem(dragView);
4605 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004606 }
4607
Anjali Koppalf5d29132014-02-28 13:33:27 -08004608 @Override
4609 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004610 if (mLauncherCallbacks != null) {
4611 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4612 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004613 }
4614
Winson Chung80baf5a2010-08-09 16:03:15 -07004615 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004616 * Prints out out state for debugging.
4617 */
4618 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004619 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004620 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004621 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4622 Log.d(TAG, "mRestoring=" + mRestoring);
4623 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4624 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004625 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004626 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004627 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004628
Daniel Sandler325dc232013-06-05 22:57:57 -04004629 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004630 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004631
4632 @Override
4633 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4634 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004635 synchronized (sDumpLogs) {
4636 writer.println(" ");
4637 writer.println("Debug logs: ");
4638 for (int i = 0; i < sDumpLogs.size(); i++) {
4639 writer.println(" " + sDumpLogs.get(i));
4640 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004641 }
Adam Cohen9211d422014-10-07 18:14:53 -07004642 if (mLauncherCallbacks != null) {
4643 mLauncherCallbacks.dump(prefix, fd, writer, args);
4644 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004645 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004646
4647 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004648 if (DEBUG_DUMP_LOG) {
4649 synchronized (sDumpLogs) {
4650 Log.d(TAG, "");
4651 Log.d(TAG, "*********************");
4652 Log.d(TAG, "Launcher debug logs: ");
4653 for (int i = 0; i < sDumpLogs.size(); i++) {
4654 Log.d(TAG, " " + sDumpLogs.get(i));
4655 }
4656 Log.d(TAG, "*********************");
4657 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004658 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004659 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004660 }
4661
4662 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004663 addDumpLog(tag, log, null, debugLog);
4664 }
4665
4666 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004667 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004668 if (e != null) {
4669 Log.d(tag, log, e);
4670 } else {
4671 Log.d(tag, log);
4672 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004673 }
Winson Chungede41292013-09-19 16:27:36 -07004674 if (DEBUG_DUMP_LOG) {
4675 sDateStamp.setTime(System.currentTimeMillis());
4676 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004677 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4678 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004679 }
Winson Chungede41292013-09-19 16:27:36 -07004680 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004681 }
4682
Adam Cohen59400422014-03-05 18:07:04 -08004683 public static CustomAppWidget getCustomAppWidget(String name) {
4684 return sCustomAppWidgets.get(name);
4685 }
4686
4687 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4688 return sCustomAppWidgets;
4689 }
4690
Winson Chungede41292013-09-19 16:27:36 -07004691 public void dumpLogsToLocalData() {
4692 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004693 new AsyncTask<Void, Void, Void>() {
4694 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004695 boolean success = false;
4696 sDateStamp.setTime(sRunStart);
4697 String FILENAME = sDateStamp.getMonth() + "-"
4698 + sDateStamp.getDay() + "_"
4699 + sDateStamp.getHours() + "-"
4700 + sDateStamp.getMinutes() + "_"
4701 + sDateStamp.getSeconds() + ".txt";
4702
4703 FileOutputStream fos = null;
4704 File outFile = null;
4705 try {
4706 outFile = new File(getFilesDir(), FILENAME);
4707 outFile.createNewFile();
4708 fos = new FileOutputStream(outFile);
4709 } catch (Exception e) {
4710 e.printStackTrace();
4711 }
4712 if (fos != null) {
4713 PrintWriter writer = new PrintWriter(fos);
4714
4715 writer.println(" ");
4716 writer.println("Debug logs: ");
4717 synchronized (sDumpLogs) {
4718 for (int i = 0; i < sDumpLogs.size(); i++) {
4719 writer.println(" " + sDumpLogs.get(i));
4720 }
4721 }
4722 writer.close();
4723 }
4724 try {
4725 if (fos != null) {
4726 fos.close();
4727 success = true;
4728 }
4729 } catch (IOException e) {
4730 e.printStackTrace();
4731 }
Michael Jurka43467462013-11-14 12:03:58 +01004732 return null;
Winson Chungede41292013-09-19 16:27:36 -07004733 }
Michael Jurka43467462013-11-14 12:03:58 +01004734 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004735 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004736 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004737}
Michael Jurka2763be32011-02-24 11:19:57 -08004738
Michael Jurkaabded662011-03-04 12:06:57 -08004739interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004740 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004741 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004742 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004743 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004744 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004745}
Dan Sandlerd5024042014-01-09 15:01:33 -05004746
4747interface DebugIntents {
4748 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4749 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004750}