blob: f2610d6e41e159af4be5d4221ca9bf77a01e8ecc [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;
Winson Chung82f55532011-08-09 14:14:23 -070043import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080045import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080049import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070050import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020059import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070068import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070070import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070071import android.view.Display;
72import android.view.Gravity;
73import android.view.HapticFeedbackConstants;
74import android.view.KeyEvent;
75import android.view.LayoutInflater;
76import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070077import android.view.MotionEvent;
78import android.view.Surface;
79import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070080import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080081import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070083import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070085import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.widget.Advanceable;
90import android.widget.FrameLayout;
91import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080092import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070094
Sunny Goyal651077b2014-06-30 14:15:31 -070095import com.android.launcher3.DropTarget.DragObject;
96import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -070097import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +010098import com.android.launcher3.compat.LauncherActivityInfoCompat;
99import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700100import com.android.launcher3.compat.PackageInstallerCompat;
101import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100102import com.android.launcher3.compat.UserHandleCompat;
103import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen091440a2015-03-18 14:16:05 -0700104import com.android.launcher3.util.Thunk;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700105
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.io.DataInputStream;
107import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700108import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700109import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700111import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700113import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800114import java.lang.reflect.InvocationTargetException;
115import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700118import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700119import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700121import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700122import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000123import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125/**
126 * Default launcher application.
127 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100128public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700129 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800130 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
131 LauncherStateTransitionAnimation.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800132 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700133 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Daniel Sandlerf061f822013-06-27 13:59:36 -0400135 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400136 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700137 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400138 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700139 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700140
Dan Sandlerd5024042014-01-09 15:01:33 -0500141 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700144 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700146 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Michael Jurka8b805b12012-04-18 14:23:14 -0700148 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700149 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700150
Mathew Inwood876a8462013-06-14 14:12:41 +0100151 /**
152 * IntentStarter uses request codes starting with this. This must be greater than all activity
153 * request codes used internally.
154 */
155 protected static final int REQUEST_LAST = 100;
156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
158
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800159 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Michael Jurka0a457bf2012-11-19 14:05:05 -0800161 // To turn on these properties, type
162 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800163 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Winson Chung2672ff92012-05-04 16:22:30 -0700165 // The Intent extra that defines whether to ignore the launch animation
166 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400167 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700168
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
170 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700171 // Type: int
172 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
175 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
177 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700178 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700180 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: boolean
182 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
183 // Type: long
184 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700185 // Type: int
186 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
187 // Type: int
188 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
189 // Type: parcelable
190 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000191 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000193 // Type: int[]
194 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Adam Cohen432609a2014-03-13 17:03:22 -0700196 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800197 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
198
Adam Cohen3ed316a2014-07-23 18:21:20 -0700199 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
200 static final String ACTION_FIRST_LOAD_COMPLETE =
201 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
202
Adam Cohen39a06042013-07-19 14:30:12 -0700203 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700204 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700205
Sunny Goyal594d76d2014-11-06 10:12:54 -0800206 private static final String QSB_WIDGET_ID = "qsb_widget_id";
207 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
208
Winson Chunga6945242014-01-08 14:04:34 -0800209 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
210
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800212 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700213 @Thunk State mState = State.WORKSPACE;
214 @Thunk AnimatorSet mStateAnimation;
215 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700216
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700217 private boolean mIsSafeModeEnabled;
218
Adam Cohenc2d6e892014-10-16 09:49:24 -0700219 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
220 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700221 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700222
Joe Onorato9c1289c2009-08-17 11:03:03 -0400223 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700224 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
225 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700226 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000228 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
229 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
230
Winson Chunga2413752012-04-03 14:22:34 -0700231 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700232 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
233 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700234 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700235
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800236 private final BroadcastReceiver mCloseSystemDialogsReceiver
237 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800238 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
239
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 private LayoutInflater mInflater;
241
Adam Cohen091440a2015-03-18 14:16:05 -0700242 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700243 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800244 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800246 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700247 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700248
Sunny Goyalffe83f12014-08-14 17:39:34 -0700249 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700250 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700251
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800253 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800254 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700255
Michael Jurka0280c3b2010-09-17 15:00:07 -0700256 private int[] mTmpAddItemCellCoordinates = new int[2];
257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 private FolderInfo mFolderInfo;
259
Winson Chung3d503fb2011-07-13 17:25:49 -0700260 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800261 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700262
Michael Jurka838a4ca2011-02-07 13:33:06 -0800263 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700264
Winson Chungc51db6a2011-10-05 11:44:49 -0700265 private SearchDropTargetBar mSearchDropTargetBar;
Adam Cohen091440a2015-03-18 14:16:05 -0700266 @Thunk AppsContainerView mAppsView;
267 @Thunk AppsCustomizeTabHost mAppsCustomizeTabHost;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700268 private AppsCustomizePagedView mAppsCustomizeContent;
269 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800270 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700273 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
274 // scroll issues (because the workspace may not have been measured yet) and extra work.
275 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
276 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
278 private SpannableStringBuilder mDefaultKeySsb = null;
279
Adam Cohen091440a2015-03-18 14:16:05 -0700280 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400281
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800282 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 private boolean mRestoring;
284 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700285 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286
Michael Jurka1e2f4652013-07-08 18:03:46 -0700287 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700288 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
289
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 private Bundle mSavedInstanceState;
291
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800293 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700294 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800295 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700296 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800297 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400298
Adam Cohen091440a2015-03-18 14:16:05 -0700299 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700300
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700301 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700302
Adam Cohen61f560d2013-09-30 15:58:20 -0700303 private View.OnTouchListener mHapticFeedbackTouchListener;
304
Adam Cohended9f8d2010-11-03 13:25:16 -0700305 // Related to the auto-advancing of widgets
306 private final int ADVANCE_MSG = 1;
307 private final int mAdvanceInterval = 20000;
308 private final int mAdvanceStagger = 250;
309 private long mAutoAdvanceSentTime;
310 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700311 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700312 new HashMap<View, AppWidgetProviderInfo>();
313
Winson Chung400438b2011-01-16 17:53:48 -0800314 // Determines how long to wait after a rotation before restoring the screen orientation to
315 // match the sensor state.
316 private final int mRestoreScreenOrientationDelay = 500;
317
Adam Cohen091440a2015-03-18 14:16:05 -0700318 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700319
Adam Cohen1462de32012-07-24 22:34:36 -0700320 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700321 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700322
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700323 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700324 static Date sDateStamp = new Date();
325 static DateFormat sDateFormat =
326 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
327 static long sRunStart = System.currentTimeMillis();
328 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700329
Winson Chung46353de2012-02-16 14:05:10 -0800330 // We only want to get the SharedPreferences once since it does an FS stat each time we get
331 // it from the context.
332 private SharedPreferences mSharedPrefs;
333
Winson Chungf0c6ae02012-03-21 16:10:31 -0700334 // Holds the page that we need to animate to, and the icon views that we need to animate up
335 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700336 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700337 private Bitmap mFolderIconBitmap;
338 private Canvas mFolderIconCanvas;
339 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700340
Michael Jurkaddd62e92011-02-16 17:49:14 -0800341 private BubbleTextView mWaitingForResume;
342
Adam Cohen59400422014-03-05 18:07:04 -0800343 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
344 new HashMap<String, CustomAppWidget>();
345
346 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
347 static {
348 if (ENABLE_CUSTOM_WIDGET_TEST) {
349 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
350 }
351 }
352
Chet Haasea8f996d2015-02-17 12:56:04 -0800353 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
354 private static Method sClipRevealMethod = null;
355 static {
356 Class<?> activityOptionsClass = ActivityOptions.class;
357 try {
358 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
359 View.class, int.class, int.class, int.class, int.class);
360 } catch (Exception e) {
361 // Earlier version
362 }
363 }
364
Adam Cohen091440a2015-03-18 14:16:05 -0700365 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700366 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700367 if (mWorkspace != null) {
368 mWorkspace.buildPageHardwareLayers();
369 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700370 }
371 };
372
Adam Cohendb364c32014-05-20 14:23:40 -0700373 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800374
Adam Cohen091440a2015-03-18 14:16:05 -0700375 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800376 int requestCode;
377 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700378 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700379 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800380 int cellX;
381 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700382 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800383 }
384
Daniel Sandlerff02d492013-08-05 02:12:05 -0400385 private Stats mStats;
386
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700387 FocusIndicatorView mFocusHandler;
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 @Override
390 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700391 if (DEBUG_STRICT_MODE) {
392 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
393 .detectDiskReads()
394 .detectDiskWrites()
395 .detectNetwork() // or .detectAll() for all detectable problems
396 .penaltyLog()
397 .build());
398 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
399 .detectLeakedSqlLiteObjects()
400 .detectLeakedClosableObjects()
401 .penaltyLog()
402 .penaltyDeath()
403 .build());
404 }
405
Adam Cohen9211d422014-10-07 18:14:53 -0700406 if (mLauncherCallbacks != null) {
407 mLauncherCallbacks.preOnCreate();
408 }
409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400411
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400412 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400413 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700414 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700415
Winson Chung5f8afe62013-08-12 16:19:28 -0700416 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700417 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700418
419 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400420 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700421 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700422 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800423 mModel = app.setLauncher(this);
424 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700425 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400426 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800428 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Daniel Sandlerff02d492013-08-05 02:12:05 -0400430 mStats = new Stats(this);
431
Sunny Goyalffe83f12014-08-14 17:39:34 -0700432 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700433
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700434 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
435 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700436
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700437 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
438 // this also ensures that any synchronous binding below doesn't re-trigger another
439 // LauncherModel load.
440 mPaused = false;
441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200443 android.os.Debug.startMethodTracing(
444 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 }
446
447 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100451 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800453 registerContentObservers();
454
Joe Onorato7c312c12009-08-13 21:36:53 -0700455 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 mSavedState = savedInstanceState;
458 restoreState(mSavedState);
459
460 if (PROFILE_STARTUP) {
461 android.os.Debug.stopMethodTracing();
462 }
463
464 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700465 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 // If the user leaves launcher, then we should just load items asynchronously when
467 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500468 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 } else {
470 // We only load the page synchronously if the user rotates (or triggers a
471 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800472 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 }
475
476 // For handling default keys
477 mDefaultKeySsb = new SpannableStringBuilder();
478 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800479
480 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
481 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800482
Adam Cohenf9426d52012-06-04 17:26:21 -0700483 // On large interfaces, we want the screen to auto-rotate based on the current orientation
484 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700485
Adam Cohen9211d422014-10-07 18:14:53 -0700486 if (mLauncherCallbacks != null) {
487 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700488 if (mLauncherCallbacks.hasLauncherOverlay()) {
489 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700490 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
491 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
492 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700493 mWorkspace.setLauncherOverlay(mLauncherOverlay);
494 }
Adam Cohen9211d422014-10-07 18:14:53 -0700495 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700496
497 if (shouldShowIntroScreen()) {
498 showIntroScreen();
499 } else {
500 showFirstRunActivity();
501 showFirstRunClings();
502 }
Adam Cohen9211d422014-10-07 18:14:53 -0700503 }
504
505 private LauncherCallbacks mLauncherCallbacks;
506
507 public void onPostCreate(Bundle savedInstanceState) {
508 super.onPostCreate(savedInstanceState);
509 if (mLauncherCallbacks != null) {
510 mLauncherCallbacks.onPostCreate(savedInstanceState);
511 }
512 }
513
514 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
515 mLauncherCallbacks = callbacks;
516 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700517 }
518
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700519 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700520 public void onLauncherProviderChange() {
521 if (mLauncherCallbacks != null) {
522 mLauncherCallbacks.onLauncherProviderChange();
523 }
524 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700525
Adam Cohen9211d422014-10-07 18:14:53 -0700526 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700527 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700528 if (mLauncherCallbacks != null) {
529 return mLauncherCallbacks.hasCustomContentToLeft();
530 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700531 return false;
532 }
533
Dave Hawkeya8881582013-09-17 15:55:33 -0600534 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500535 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600536 * {@link #addToCustomContentPage}. This will only be invoked if
537 * {@link #hasCustomContentToLeft()} is {@code true}.
538 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500539 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700540 if (mLauncherCallbacks != null) {
541 mLauncherCallbacks.populateCustomContentContainer();
542 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600543 }
544
545 /**
546 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
547 * ensure the custom content page is added or removed if necessary.
548 */
549 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600550 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600551 // Not bound yet, wait for bindScreens to be called.
552 return;
553 }
554
555 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
556 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500557 mWorkspace.createCustomContentContainer();
558 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600559 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
560 mWorkspace.removeCustomContentPage();
561 }
562 }
563
Adam Cohen091440a2015-03-18 14:16:05 -0700564 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700565 if (sLocaleConfiguration == null) {
566 new AsyncTask<Void, Void, LocaleConfiguration>() {
567 @Override
568 protected LocaleConfiguration doInBackground(Void... unused) {
569 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
570 readConfiguration(Launcher.this, localeConfiguration);
571 return localeConfiguration;
572 }
573
574 @Override
575 protected void onPostExecute(LocaleConfiguration result) {
576 sLocaleConfiguration = result;
577 checkForLocaleChange(); // recursive, but now with a locale configuration
578 }
579 }.execute();
580 return;
581 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700582
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 final Configuration configuration = getResources().getConfiguration();
584
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700585 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 final String locale = configuration.locale.toString();
587
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700588 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 final int mcc = configuration.mcc;
590
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700591 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 final int mnc = configuration.mnc;
593
Romain Guy84f296c2009-11-04 15:00:44 -0800594 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595
Romain Guy84f296c2009-11-04 15:00:44 -0800596 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700597 sLocaleConfiguration.locale = locale;
598 sLocaleConfiguration.mcc = mcc;
599 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700600
Joe Onorato0589f0f2010-02-08 13:44:00 -0800601 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700602
603 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100604 new AsyncTask<Void, Void, Void>() {
605 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700606 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100607 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700608 }
Michael Jurka43467462013-11-14 12:03:58 +0100609 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700610 }
611 }
612
Adam Cohen091440a2015-03-18 14:16:05 -0700613 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700614 public String locale;
615 public int mcc = -1;
616 public int mnc = -1;
617 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700618
Adam Cohen091440a2015-03-18 14:16:05 -0700619 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700620 DataInputStream in = null;
621 try {
Helena Josol28db2802014-10-09 17:04:09 +0100622 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700623 configuration.locale = in.readUTF();
624 configuration.mcc = in.readInt();
625 configuration.mnc = in.readInt();
626 } catch (FileNotFoundException e) {
627 // Ignore
628 } catch (IOException e) {
629 // Ignore
630 } finally {
631 if (in != null) {
632 try {
633 in.close();
634 } catch (IOException e) {
635 // Ignore
636 }
637 }
638 }
639 }
640
Adam Cohen091440a2015-03-18 14:16:05 -0700641 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700642 DataOutputStream out = null;
643 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100644 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100645 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700646 out.writeUTF(configuration.locale);
647 out.writeInt(configuration.mcc);
648 out.writeInt(configuration.mnc);
649 out.flush();
650 } catch (FileNotFoundException e) {
651 // Ignore
652 } catch (IOException e) {
653 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100654 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700655 } finally {
656 if (out != null) {
657 try {
658 out.close();
659 } catch (IOException e) {
660 // Ignore
661 }
662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
664 }
665
Anton Hanssona2f665f2013-09-26 12:18:32 +0100666 public Stats getStats() {
667 return mStats;
668 }
669
Bjorn Bringertc459e522013-06-07 19:36:01 +0100670 public LayoutInflater getInflater() {
671 return mInflater;
672 }
673
Winson Chung36a62fe2012-05-06 18:04:42 -0700674 boolean isDraggingEnabled() {
675 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
676 // that is subsequently removed from the workspace in startBinding().
677 return !mModel.isLoadingWorkspace();
678 }
679
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800680 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000681 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100682 if (Build.VERSION.SDK_INT >= 17) {
683 return View.generateViewId();
684 } else {
685 // View.generateViewId() is not available. The following fallback logic is a copy
686 // of its implementation.
687 for (;;) {
688 final int result = sNextGeneratedId.get();
689 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
690 int newValue = result + 1;
691 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
692 if (sNextGeneratedId.compareAndSet(result, newValue)) {
693 return result;
694 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000695 }
696 }
697 }
698
699 public int getViewIdForItem(ItemInfo info) {
700 // This cast is safe given the > 2B range for int.
701 int itemId = (int) info.id;
702 if (mItemIdToViewId.containsKey(itemId)) {
703 return mItemIdToViewId.get(itemId);
704 }
705 int viewId = generateViewId();
706 mItemIdToViewId.put(itemId, viewId);
707 return viewId;
708 }
709
710 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700711 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
712 * a configuration step, this allows the proper animations to run after other transitions.
713 */
Adam Cohendb364c32014-05-20 14:23:40 -0700714 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700715 long screenId = args.screenId;
716 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
717 // When the screen id represents an actual screen (as opposed to a rank) we make sure
718 // that the drop page actually exists.
719 screenId = ensurePendingDropLayoutExists(args.screenId);
720 }
Adam Cohendb364c32014-05-20 14:23:40 -0700721
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800722 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800723 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700724 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700725 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700726 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800727 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700728 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700729 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700730 case REQUEST_RECONFIGURE_APPWIDGET:
731 completeRestoreAppWidget(args.appWidgetId);
732 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800733 }
Michael Jurka27614d22012-04-02 06:40:22 -0700734 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
735 // if you turned the screen off and then back while in All Apps, Launcher would not
736 // return to the workspace. Clearing mAddInfo.container here fixes this issue
737 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700738 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 }
740
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800741 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700742 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700743 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700744 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700745 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800746 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700747
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 Runnable exitSpringLoaded = new Runnable() {
749 @Override
750 public void run() {
751 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
752 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
753 }
754 };
755
Michael Jurka8b805b12012-04-18 14:23:14 -0700756 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700757 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700758 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
759 if (resultCode == RESULT_CANCELED) {
760 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700761 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700763 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800764 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700766 }
767 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200768 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
769 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
770 mWorkspace.exitOverviewMode(false);
771 }
772 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200774
Adam Cohened66b2b2012-01-23 17:28:51 -0800775 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
776 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700777
Adam Cohendb364c32014-05-20 14:23:40 -0700778 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800779 // We have special handling for widgets
780 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800781 final int appWidgetId;
782 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
783 : -1;
784 if (widgetId < 0) {
785 appWidgetId = pendingAddWidgetId;
786 } else {
787 appWidgetId = widgetId;
788 }
789
Adam Cohenad4e15c2013-10-17 16:21:35 -0700790 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800791 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700792 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
793 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700794 result = RESULT_CANCELED;
795 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700796 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700797 @Override
798 public void run() {
799 exitSpringLoadedDragModeDelayed(false, 0, null);
800 }
801 };
Adam Cohendb364c32014-05-20 14:23:40 -0700802 if (workspaceLocked) {
803 // No need to remove the empty screen if we're mid-binding, as the
804 // the bind will not add the empty screen.
805 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
806 } else {
807 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
808 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
809 }
Winson Chung5aaab772012-04-27 15:24:02 -0700810 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700811 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700812 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
813 // When the screen id represents an actual screen (as opposed to a rank)
814 // we make sure that the drop page actually exists.
815 mPendingAddInfo.screenId =
816 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
817 }
Adam Cohendb364c32014-05-20 14:23:40 -0700818 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
819
820 dropLayout.setDropPending(true);
821 final Runnable onComplete = new Runnable() {
822 @Override
823 public void run() {
824 completeTwoStageWidgetDrop(resultCode, appWidgetId);
825 dropLayout.setDropPending(false);
826 }
827 };
828 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
829 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
830 } else {
831 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
832 mPendingAddInfo);
833 sPendingAddItem = args;
834 }
Winson Chung5aaab772012-04-27 15:24:02 -0700835 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800836 return;
837 }
838
Sunny Goyalff572272014-07-23 13:58:07 -0700839 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
840 if (resultCode == RESULT_OK) {
841 // Update the widget view.
842 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
843 pendingAddWidgetId, mPendingAddInfo);
844 if (workspaceLocked) {
845 sPendingAddItem = args;
846 } else {
847 completeAdd(args);
848 }
849 }
850 // Leave the widget in the pending state if the user canceled the configure.
851 return;
852 }
853
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 // The pattern used here is that a user PICKs a specific application,
855 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700856
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
858 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700859 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700860 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
861 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800862 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700863 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800864 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700865 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700866 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
867 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 }
Adam Cohen00074722013-10-11 17:46:09 -0700869 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700870 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700871 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800873 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800874
875 }
876
877 @Override
878 protected void onActivityResult(
879 final int requestCode, final int resultCode, final Intent data) {
880 handleActivityResult(requestCode, resultCode, data);
881 if (mLauncherCallbacks != null) {
882 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
883 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800884 }
885
Adam Cohendb364c32014-05-20 14:23:40 -0700886 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
887 appWidgetId, ItemInfo info) {
888 PendingAddArguments args = new PendingAddArguments();
889 args.requestCode = requestCode;
890 args.intent = data;
891 args.container = info.container;
892 args.screenId = info.screenId;
893 args.cellX = info.cellX;
894 args.cellY = info.cellY;
895 args.appWidgetId = appWidgetId;
896 return args;
897 }
898
899 /**
900 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
901 *
902 * @param screenId the screen id to check
903 * @return the new screen, or screenId if it exists
904 */
905 private long ensurePendingDropLayoutExists(long screenId) {
906 CellLayout dropLayout =
907 (CellLayout) mWorkspace.getScreenWithId(screenId);
908 if (dropLayout == null) {
909 // it's possible that the add screen was removed because it was
910 // empty and a re-bind occurred
911 mWorkspace.addExtraEmptyScreen();
912 return mWorkspace.commitExtraEmptyScreen();
913 } else {
914 return screenId;
915 }
916 }
917
Adam Cohen091440a2015-03-18 14:16:05 -0700918 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700919 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700920 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800921 Runnable onCompleteRunnable = null;
922 int animationType = 0;
923
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700924 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800925 if (resultCode == RESULT_OK) {
926 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
927 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700928 mPendingAddWidgetInfo);
929 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 onCompleteRunnable = new Runnable() {
931 @Override
932 public void run() {
933 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700934 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700935 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
936 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 }
938 };
939 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800940 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800942 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700943 if (mDragLayer.getAnimatedView() != null) {
944 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
945 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
946 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700947 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700948 // The animated view may be null in the case of a rotation during widget configuration
949 onCompleteRunnable.run();
950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
952
953 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700954 protected void onStop() {
955 super.onStop();
956 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700957
958 if (mLauncherCallbacks != null) {
959 mLauncherCallbacks.onStop();
960 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700961 }
962
963 @Override
964 protected void onStart() {
965 super.onStart();
966 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700967
968 if (mLauncherCallbacks != null) {
969 mLauncherCallbacks.onStart();
970 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700971 }
972
973 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200975 long startTime = 0;
976 if (DEBUG_RESUME_TIME) {
977 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400978 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200979 }
Adam Cohen9211d422014-10-07 18:14:53 -0700980
981 if (mLauncherCallbacks != null) {
982 mLauncherCallbacks.preOnResume();
983 }
984
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700986
Winson Chung4a2afa32012-07-19 14:53:05 -0700987 // Restore the previous launcher state
Winson Chungb745afb2015-03-02 11:51:23 -0800988 if (mOnResumeState == State.WORKSPACE || mOnResumeState == State.NONE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700989 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800990 } else if (mOnResumeState == State.APPS) {
991 showAppsView(false /* animated */, false /* resetListToTop */);
992 } else if (mOnResumeState == State.WIDGETS) {
993 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700994 }
995 mOnResumeState = State.NONE;
996
Craig Mautner360310b2012-10-26 15:13:08 -0700997 // Background was set to gradient in onPause(), restore to black if in all apps.
998 setWorkspaceBackground(mState == State.WORKSPACE);
999
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001000 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001001 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001002 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001003 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001004 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001005 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001007 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001008 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1009 // execute them here
1010 long startTimeCallbacks = 0;
1011 if (DEBUG_RESUME_TIME) {
1012 startTimeCallbacks = System.currentTimeMillis();
1013 }
1014
1015 if (mAppsCustomizeContent != null) {
1016 mAppsCustomizeContent.setBulkBind(true);
1017 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001018 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1019 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001020 }
1021 if (mAppsCustomizeContent != null) {
1022 mAppsCustomizeContent.setBulkBind(false);
1023 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001024 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001025 if (DEBUG_RESUME_TIME) {
1026 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1027 (System.currentTimeMillis() - startTimeCallbacks));
1028 }
Michael Jurka447bf842013-05-15 14:52:15 +02001029 }
Michael Jurka54554252013-08-01 12:52:23 +02001030 if (mOnResumeCallbacks.size() > 0) {
1031 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1032 mOnResumeCallbacks.get(i).run();
1033 }
1034 mOnResumeCallbacks.clear();
1035 }
Winson Chunge4e50662012-01-23 14:45:13 -08001036
1037 // Reset the pressed state of icons that were locked in the press state while activities
1038 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001039 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001040 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001041 mWaitingForResume.setStayPressed(false);
1042 }
Winson Chung82963d52013-10-09 11:20:57 -07001043
Adam Cohen06dff352012-06-01 17:17:08 -07001044 // It is possible that widgets can receive updates while launcher is not in the foreground.
1045 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1046 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1047 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001048 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001049
Winson Chung780fe592013-09-26 14:48:44 -07001050 // Process any items that were added while Launcher was away.
1051 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1052
Michael Jurka447bf842013-05-15 14:52:15 +02001053 if (DEBUG_RESUME_TIME) {
1054 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1055 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001056
1057 if (mWorkspace.getCustomContentCallbacks() != null) {
1058 // If we are resuming and the custom content is the current page, we call onShow().
1059 // It is also poassible that onShow will instead be called slightly after first layout
1060 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1061 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001062 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001063 }
1064 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001065 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001066 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001067
1068 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001069
1070 if (mLauncherCallbacks != null) {
1071 mLauncherCallbacks.onResume();
1072 }
Adam Cohen06dff352012-06-01 17:17:08 -07001073 }
1074
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001076 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001077 // Ensure that items added to Launcher are queued until Launcher returns
1078 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001079 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001080
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001081 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001082 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001083 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001084 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001085
1086 // We call onHide() aggressively. The custom content callbacks should be able to
1087 // debounce excess onHide calls.
1088 if (mWorkspace.getCustomContentCallbacks() != null) {
1089 mWorkspace.getCustomContentCallbacks().onHide();
1090 }
Romain Guycbb89e42009-06-08 15:52:54 -07001091
Adam Cohen9211d422014-10-07 18:14:53 -07001092 if (mLauncherCallbacks != null) {
1093 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001094 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001095 }
1096
1097 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001098 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1099 // by a onResume or by scrolling otherwise.
1100 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001101
1102 // Custom content is completely hidden
1103 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001104
1105 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1106 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001107
1108 // Indicates whether the user is allowed to scroll away from the custom content.
1109 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001110 }
1111
Adam Cohenc2d6e892014-10-16 09:49:24 -07001112 public interface LauncherOverlay {
1113
1114 /**
1115 * Touch interaction leading to overscroll has begun
1116 */
1117 public void onScrollInteractionBegin();
1118
1119 /**
1120 * Touch interaction related to overscroll has ended
1121 */
1122 public void onScrollInteractionEnd();
1123
1124 /**
1125 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1126 * screen (or in the case of RTL, the rightmost screen).
1127 */
1128 public void onScrollChange(int progress, boolean rtl);
1129
1130 /**
1131 * Screen has stopped scrolling
1132 */
1133 public void onScrollSettled();
1134
1135 /**
1136 * This method can be called by the Launcher in order to force the LauncherOverlay
1137 * to exit fully immersive mode.
1138 */
1139 public void forceExitFullImmersion();
1140 }
1141
1142 public interface LauncherOverlayCallbacks {
1143 /**
1144 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1145 * however it doesn't modify any state within the launcher.
1146 */
1147 public boolean canEnterFullImmersion();
1148
1149 /**
1150 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1151 * eg. by occupying the full screen and handling all touch events.
1152 *
1153 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1154 * case, Launcher will modify any necessary state and assumes the overlay is
1155 * handling all interaction. If false, the LauncherOverlay should cancel any
1156 *
1157 */
1158 public boolean enterFullImmersion();
1159
1160 /**
1161 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1162 * full control over UI and state.
1163 */
1164 public void exitFullImmersion();
1165 }
1166
1167 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1168
1169 @Override
1170 public boolean canEnterFullImmersion() {
1171 return mState == State.WORKSPACE;
1172 }
1173
1174 @Override
1175 public boolean enterFullImmersion() {
1176 if (mState == State.WORKSPACE) {
1177 // When fully immersed, disregard any touches which fall through.
1178 mDragLayer.setBlockTouch(true);
1179 return true;
1180 }
1181 return false;
1182 }
1183
1184 @Override
1185 public void exitFullImmersion() {
1186 mDragLayer.setBlockTouch(false);
1187 }
1188 }
1189
Jorim Jaggid017f882014-01-14 17:08:48 -08001190 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001191 if (mLauncherCallbacks != null) {
1192 return mLauncherCallbacks.hasSettings();
1193 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001194 return false;
1195 }
1196
Adam Cohenbffe7452013-07-22 18:21:45 -07001197
Adam Cohen9211d422014-10-07 18:14:53 -07001198 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001199 CustomContentCallbacks callbacks, String description) {
1200 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001201 }
1202
Adam Cohenedb40762013-07-18 16:45:45 -07001203 // The custom content needs to offset its content to account for the QSB
1204 public int getTopOffsetForCustomContent() {
1205 return mWorkspace.getPaddingTop();
1206 }
1207
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001208 @Override
1209 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001211 if (mModel.isCurrentCallbacks(this)) {
1212 mModel.stopLoader();
1213 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001214 if (mAppsCustomizeContent != null) {
1215 mAppsCustomizeContent.surrender();
1216 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001217 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001218 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001219
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221 @Override
1222 public void onWindowFocusChanged(boolean hasFocus) {
1223 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001224 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001225
1226 if (mLauncherCallbacks != null) {
1227 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1228 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001229 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 private boolean acceptFilter() {
1232 final InputMethodManager inputManager = (InputMethodManager)
1233 getSystemService(Context.INPUT_METHOD_SERVICE);
1234 return !inputManager.isFullscreenMode();
1235 }
1236
1237 @Override
1238 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001239 final int uniChar = event.getUnicodeChar();
1240 final boolean handled = super.onKeyDown(keyCode, event);
1241 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1242 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1244 keyCode, event);
1245 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001246 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001247 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001248 // showSearchDialog()
1249 // If there are multiple keystrokes before the search dialog takes focus,
1250 // onSearchRequested() will be called for every keystroke,
1251 // but it is idempotent, so it's fine.
1252 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
1254 }
1255
Joe Onorato8a9625e2010-01-28 15:55:35 -08001256 // Eat the long press event so the keyboard doesn't come up.
1257 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1258 return true;
1259 }
1260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 return handled;
1262 }
1263
Karl Rosaen138a0412009-04-23 19:00:21 -07001264 private String getTypedText() {
1265 return mDefaultKeySsb.toString();
1266 }
1267
1268 private void clearTypedText() {
1269 mDefaultKeySsb.clear();
1270 mDefaultKeySsb.clearSpans();
1271 Selection.setSelection(mDefaultKeySsb, 0);
1272 }
1273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001275 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1276 * State
1277 */
1278 private static State intToState(int stateOrdinal) {
1279 State state = State.WORKSPACE;
1280 final State[] stateValues = State.values();
1281 for (int i = 0; i < stateValues.length; i++) {
1282 if (stateValues[i].ordinal() == stateOrdinal) {
1283 state = stateValues[i];
1284 break;
1285 }
1286 }
1287 return state;
1288 }
1289
1290 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 * Restores the previous state, if it exists.
1292 *
1293 * @param savedState The previous state.
1294 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001295 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 private void restoreState(Bundle savedState) {
1297 if (savedState == null) {
1298 return;
1299 }
1300
Michael Jurka883f55b2010-10-21 15:47:14 -07001301 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001302 if (state == State.APPS || state == State.WIDGETS) {
1303 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001304 }
1305
Adam Cohen21cd0022013-10-09 18:57:02 -07001306 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1307 PagedView.INVALID_RESTORE_PAGE);
1308 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001309 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
1311
Winson Chung3d503fb2011-07-13 17:25:49 -07001312 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001313 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001314
Winson Chung3d503fb2011-07-13 17:25:49 -07001315 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1316 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001317 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001318 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1319 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001320 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1321 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1322 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001323 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001324 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 mRestoring = true;
1326 }
1327
1328 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1329 if (renameFolder) {
1330 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001331 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 mRestoring = true;
1333 }
Winson Chunga12a2502010-12-20 14:41:35 -08001334
Winson Chung785d2eb2011-04-14 16:08:02 -07001335 // Restore the AppsCustomize tab
1336 if (mAppsCustomizeTabHost != null) {
1337 String curTab = savedState.getString("apps_customize_currentTab");
1338 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001339 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001340 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001341 mAppsCustomizeContent.loadAssociatedPages(
1342 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001343 }
1344
Winson Chung5afbf7b2011-07-25 11:53:08 -07001345 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1346 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001347 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001348 mItemIdToViewId = (HashMap<Integer, Integer>)
1349 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 }
1351
1352 /**
1353 * Finds all the views we need and configure them properly.
1354 */
1355 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001356 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001357
Craig Mautner360310b2012-10-26 15:13:08 -07001358 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001359 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001360 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1361 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001362 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001363 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001364
John Spurlock77e1f472013-09-11 10:09:51 -04001365 mLauncherView.setSystemUiVisibility(
1366 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001367 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368
Winson Chung4c98d922011-05-31 16:50:48 -07001369 // Setup the drag layer
1370 mDragLayer.setup(this, dragController);
1371
Winson Chung3d503fb2011-07-13 17:25:49 -07001372 // Setup the hotseat
1373 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1374 if (mHotseat != null) {
1375 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001376 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001377 }
1378
Jorim Jaggid017f882014-01-14 17:08:48 -08001379 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001380 View widgetButton = findViewById(R.id.widget_button);
1381 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001382 @Override
1383 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001384 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001385 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001386 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001387 }
1388 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001389 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1390
1391 View wallpaperButton = findViewById(R.id.wallpaper_button);
1392 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001393 @Override
1394 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001395 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001396 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001397 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001398 }
1399 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001400 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1401
1402 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001403 if (hasSettings()) {
1404 settingsButton.setOnClickListener(new OnClickListener() {
1405 @Override
1406 public void onClick(View arg0) {
1407 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001408 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001409 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001410 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001411 });
1412 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1413 } else {
1414 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001415 }
1416
Adam Cohendbdff6b2013-09-18 19:09:15 -07001417 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001418
Winson Chung4c98d922011-05-31 16:50:48 -07001419 // Setup the workspace
1420 mWorkspace.setHapticFeedbackEnabled(false);
1421 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001422 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001423 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001424
Winson Chungf0ea4d32011-06-06 14:27:16 -07001425 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001426 mSearchDropTargetBar = (SearchDropTargetBar)
1427 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001428
Winson Chungb745afb2015-03-02 11:51:23 -08001429 // Setup Apps
1430 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
1431
Winson Chungf0ea4d32011-06-06 14:27:16 -07001432 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001433 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001434 mAppsCustomizeContent = (AppsCustomizePagedView)
1435 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1436 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001437
Winson Chung3d503fb2011-07-13 17:25:49 -07001438 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001439 dragController.setDragScoller(mWorkspace);
1440 dragController.setScrollView(mDragLayer);
1441 dragController.setMoveTarget(mWorkspace);
1442 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001443 if (mSearchDropTargetBar != null) {
1444 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001445 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001446 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001447
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001448 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001449 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001450 mWeightWatcher = new WeightWatcher(this);
1451 mWeightWatcher.setAlpha(0.5f);
1452 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001453 new FrameLayout.LayoutParams(
1454 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001455 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001456 Gravity.BOTTOM)
1457 );
Adam Cohen39a06042013-07-19 14:30:12 -07001458
1459 boolean show = shouldShowWeightWatcher();
1460 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 }
1463
1464 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001465 * Sets the all apps button. This method is called from {@link Hotseat}.
1466 */
1467 public void setAllAppsButton(View allAppsButton) {
1468 mAllAppsButton = allAppsButton;
1469 }
1470
1471 public View getAllAppsButton() {
1472 return mAllAppsButton;
1473 }
1474
1475 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 * Creates a view representing a shortcut.
1477 *
1478 * @param info The data structure describing the shortcut.
1479 *
1480 * @return A View inflated from R.layout.application.
1481 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001482 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001484 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 }
1486
1487 /**
1488 * Creates a view representing a shortcut inflated from the specified resource.
1489 *
1490 * @param layoutResId The id of the XML layout used to create the shortcut.
1491 * @param parent The group the shortcut belongs to.
1492 * @param info The data structure describing the shortcut.
1493 *
1494 * @return A View inflated from layoutResId.
1495 */
Adam Cohen59400422014-03-05 18:07:04 -08001496 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001497 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001498 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001500 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 return favorite;
1502 }
1503
1504 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 * Add a shortcut to the workspace.
1506 *
1507 * @param data The intent describing the shortcut.
1508 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001510 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001511 int cellY) {
1512 int[] cellXY = mTmpAddItemCellCoordinates;
1513 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001514 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001515
Michael Jurkad3ef3062010-11-23 16:23:58 -08001516 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001517
Sunny Goyal2350bc92014-10-14 16:42:54 -07001518 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001519 if (info == null) {
1520 return;
1521 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001522 final View view = createShortcut(info);
1523
Adam Cohenfbba09b2011-07-18 21:43:05 -07001524 // First we check if we already know the exact location where we want to add this item.
1525 if (cellX >= 0 && cellY >= 0) {
1526 cellXY[0] = cellX;
1527 cellXY[1] = cellY;
1528 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001529
1530 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001531 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001532 true, null,null)) {
1533 return;
1534 }
1535 DragObject dragObject = new DragObject();
1536 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001537 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1538 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001539 return;
1540 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001541 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001542 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001543 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001544 foundCellSpan = (result != null);
1545 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001546 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001547 }
1548
1549 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001550 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001551 return;
1552 }
1553
Adam Cohendcd297f2013-06-18 13:13:40 -07001554 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555
1556 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001558 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 }
1560 }
1561
Adam Cohen2f093b62012-04-30 18:59:53 -07001562 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1563 int minHeight) {
1564 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001565 // We want to account for the extra amount of padding that we are adding to the widget
1566 // to ensure that it gets the full amount of space that it has requested
1567 int requiredWidth = minWidth + padding.left + padding.right;
1568 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001569 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001570 }
1571
Adam Cohen2f093b62012-04-30 18:59:53 -07001572 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1573 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001574 }
1575
Adam Cohen2f093b62012-04-30 18:59:53 -07001576 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1577 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001578 }
1579
Adam Cohen2f093b62012-04-30 18:59:53 -07001580 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1581 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001582 }
1583
Adam Cohen2f093b62012-04-30 18:59:53 -07001584 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1585 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001586 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001587 }
1588
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001590 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001592 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 */
Adam Cohen091440a2015-03-18 14:16:05 -07001594 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001595 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1596
1597 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001598 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001599 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1600 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001601 }
Romain Guycbb89e42009-06-08 15:52:54 -07001602
Adam Cohen59400422014-03-05 18:07:04 -08001603 if (appWidgetInfo.isCustomWidget) {
1604 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001605 }
1606
Adam Cohen59400422014-03-05 18:07:04 -08001607 LauncherAppWidgetInfo launcherInfo;
1608 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1609 launcherInfo.spanX = info.spanX;
1610 launcherInfo.spanY = info.spanY;
1611 launcherInfo.minSpanX = info.minSpanX;
1612 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001613 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001614
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001616 container, screenId, info.cellX, info.cellY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617
1618 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001619 if (hostView == null) {
1620 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001621 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1622 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001623 } else {
1624 // The AppWidgetHostView has already been inflated and instantiated
1625 launcherInfo.hostView = hostView;
1626 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001628 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001629 launcherInfo.notifyWidgetSizeChanged(this);
1630
Adam Cohen59400422014-03-05 18:07:04 -08001631 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1632 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001633
1634 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001636 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 }
Romain Guycbb89e42009-06-08 15:52:54 -07001638
Adam Cohended9f8d2010-11-03 13:25:16 -07001639 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1640 @Override
1641 public void onReceive(Context context, Intent intent) {
1642 final String action = intent.getAction();
1643 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1644 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001645 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001646 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001647
Winson Chungc100e8e2011-08-09 16:02:43 -07001648 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001649 // processing a multi-step drop
Winson Chungb745afb2015-03-02 11:51:23 -08001650 if (mAppsView != null && mAppsCustomizeTabHost != null &&
1651 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001652 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001653 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001654 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1655 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001656 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001657 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1658 mModel.resetLoadedState(false, true);
1659 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1660 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1661 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1662 mModel.resetLoadedState(false, true);
1663 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1664 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1665 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001666 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1667 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1668 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001669 }
1670 }
1671 };
1672
1673 @Override
1674 public void onAttachedToWindow() {
1675 super.onAttachedToWindow();
1676
1677 // Listen for broadcasts related to user-presence
1678 final IntentFilter filter = new IntentFilter();
1679 filter.addAction(Intent.ACTION_SCREEN_OFF);
1680 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001681 // For handling managed profiles
1682 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1683 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001684 if (ENABLE_DEBUG_INTENTS) {
1685 filter.addAction(DebugIntents.DELETE_DATABASE);
1686 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1687 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001688 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001689 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001690 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001691 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001692 mVisible = true;
1693 }
1694
Adam Cohen0b395352014-06-09 22:54:36 +00001695 /**
1696 * Sets up transparent navigation and status bars in LMP.
1697 * This method is a no-op for other platform versions.
1698 */
Sunny Goyald0091012015-01-20 15:55:34 -08001699 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001700 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001701 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001702 Window window = getWindow();
1703 window.getAttributes().systemUiVisibility |=
1704 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1705 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1706 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1707 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1708 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1709 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1710 window.setStatusBarColor(Color.TRANSPARENT);
1711 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001712 }
1713 }
1714
Adam Cohended9f8d2010-11-03 13:25:16 -07001715 @Override
1716 public void onDetachedFromWindow() {
1717 super.onDetachedFromWindow();
1718 mVisible = false;
1719
Adam Cohend113e0c2010-11-11 10:48:05 -08001720 if (mAttached) {
1721 unregisterReceiver(mReceiver);
1722 mAttached = false;
1723 }
Winson Chungb745afb2015-03-02 11:51:23 -08001724 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 }
1726
1727 public void onWindowVisibilityChanged(int visibility) {
1728 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001729 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001730 // The following code used to be in onResume, but it turns out onResume is called when
1731 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1732 // is a more appropriate event to handle
1733 if (mVisible) {
1734 mAppsCustomizeTabHost.onWindowVisible();
1735 if (!mWorkspaceLoading) {
1736 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001737 // We want to let Launcher draw itself at least once before we force it to build
1738 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001739 // apps is nice and speedy.
1740 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001741 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001742 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001743 if (mStarted) return;
1744 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001745 // We delay the layer building a bit in order to give
1746 // other message processing a time to run. In particular
1747 // this avoids a delay in hiding the IME if it was
1748 // currently shown, because doing that may involve
1749 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001750 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001751 final ViewTreeObserver.OnDrawListener listener = this;
1752 mWorkspace.post(new Runnable() {
1753 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001754 if (mWorkspace != null &&
1755 mWorkspace.getViewTreeObserver() != null) {
1756 mWorkspace.getViewTreeObserver().
1757 removeOnDrawListener(listener);
1758 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001759 }
1760 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001761 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001762 }
1763 });
1764 }
1765 clearTypedText();
1766 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001767 }
1768
Adam Cohen091440a2015-03-18 14:16:05 -07001769 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001770 mHandler.removeMessages(ADVANCE_MSG);
1771 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1772 mHandler.sendMessageDelayed(msg, delay);
1773 mAutoAdvanceSentTime = System.currentTimeMillis();
1774 }
1775
Adam Cohen091440a2015-03-18 14:16:05 -07001776 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001777 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1778 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1779 mAutoAdvanceRunning = autoAdvanceRunning;
1780 if (autoAdvanceRunning) {
1781 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1782 sendAdvanceMessage(delay);
1783 } else {
1784 if (!mWidgetsToAdvance.isEmpty()) {
1785 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1786 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1787 }
1788 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001789 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001790 }
1791 }
1792 }
1793
1794 private final Handler mHandler = new Handler() {
1795 @Override
1796 public void handleMessage(Message msg) {
1797 if (msg.what == ADVANCE_MSG) {
1798 int i = 0;
1799 for (View key: mWidgetsToAdvance.keySet()) {
1800 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1801 final int delay = mAdvanceStagger * i;
1802 if (v instanceof Advanceable) {
1803 postDelayed(new Runnable() {
1804 public void run() {
1805 ((Advanceable) v).advance();
1806 }
1807 }, delay);
1808 }
1809 i++;
1810 }
1811 sendAdvanceMessage(mAdvanceInterval);
1812 }
1813 }
1814 };
1815
1816 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001817 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001818 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1819 if (v instanceof Advanceable) {
1820 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001821 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001822 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001823 }
1824 }
1825
1826 void removeWidgetToAutoAdvance(View hostView) {
1827 if (mWidgetsToAdvance.containsKey(hostView)) {
1828 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001829 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001830 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001831 }
1832
Joe Onorato9c1289c2009-08-17 11:03:03 -04001833 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001834 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835 launcherInfo.hostView = null;
1836 }
1837
Winson Chung93eef082012-03-23 15:59:27 -07001838 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1839 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1840 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001841 }
1842
Winson Chunga6945242014-01-08 14:04:34 -08001843 public DragLayer getDragLayer() {
1844 return mDragLayer;
1845 }
1846
Winson Chungb745afb2015-03-02 11:51:23 -08001847 public AppsContainerView getAppsView() {
1848 return mAppsView;
1849 }
1850
1851 public AppsCustomizeTabHost getWidgetsView() {
1852 return mAppsCustomizeTabHost;
1853 }
1854
Winson Chunga6945242014-01-08 14:04:34 -08001855 public Workspace getWorkspace() {
1856 return mWorkspace;
1857 }
1858
1859 public Hotseat getHotseat() {
1860 return mHotseat;
1861 }
1862
Jorim Jaggid017f882014-01-14 17:08:48 -08001863 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001864 return mOverviewPanel;
1865 }
1866
1867 public SearchDropTargetBar getSearchBar() {
1868 return mSearchDropTargetBar;
1869 }
1870
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001871 public LauncherAppWidgetHost getAppWidgetHost() {
1872 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 }
Romain Guycbb89e42009-06-08 15:52:54 -07001874
Winson Chunga9abd0e2010-10-27 17:18:37 -07001875 public LauncherModel getModel() {
1876 return mModel;
1877 }
1878
Winson Chunga6945242014-01-08 14:04:34 -08001879 protected SharedPreferences getSharedPrefs() {
1880 return mSharedPrefs;
1881 }
1882
Bjorn Bringertc459e522013-06-07 19:36:01 +01001883 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001884 getWindow().closeAllPanels();
1885
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001886 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001887 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001888 }
1889
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 @Override
1891 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001892 long startTime = 0;
1893 if (DEBUG_RESUME_TIME) {
1894 startTime = System.currentTimeMillis();
1895 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 super.onNewIntent(intent);
1897
1898 // Close the menu
1899 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001900 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001901 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001902
Adam Cohened307df2013-10-02 09:37:31 -07001903 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1904 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1905 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001906
Adam Cohen6fecd412013-10-02 17:41:50 -07001907 if (mWorkspace == null) {
1908 // Can be cases where mWorkspace is null, this prevents a NPE
1909 return;
1910 }
1911 Folder openFolder = mWorkspace.getOpenFolder();
1912 // In all these cases, only animate if we're already on home
1913 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001914
1915 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1916 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001917 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001918 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001919 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001920 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001921
Adam Cohen6fecd412013-10-02 17:41:50 -07001922 closeFolder();
1923 exitSpringLoadedDragMode();
1924
1925 // If we are already on home, then just animate back to the workspace,
1926 // otherwise, just wait until onResume to set the state back to Workspace
1927 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001928 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001929 } else {
1930 mOnResumeState = State.WORKSPACE;
1931 }
1932
1933 final View v = getWindow().peekDecorView();
1934 if (v != null && v.getWindowToken() != null) {
1935 InputMethodManager imm = (InputMethodManager)getSystemService(
1936 INPUT_METHOD_SERVICE);
1937 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1938 }
1939
Winson Chungb745afb2015-03-02 11:51:23 -08001940 // Reset the apps view
1941 if (!alreadyOnHome && mAppsView != null) {
1942 mAppsView.scrollToTop();
1943 }
1944
Adam Cohen6fecd412013-10-02 17:41:50 -07001945 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001946 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001947 mAppsCustomizeTabHost.reset();
1948 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001949
Adam Cohen9211d422014-10-07 18:14:53 -07001950 if (mLauncherCallbacks != null) {
1951 mLauncherCallbacks.onHomeIntent();
1952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
Adam Cohened307df2013-10-02 09:37:31 -07001954
Michael Jurka447bf842013-05-15 14:52:15 +02001955 if (DEBUG_RESUME_TIME) {
1956 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958
Adam Cohen9211d422014-10-07 18:14:53 -07001959 if (mLauncherCallbacks != null) {
1960 mLauncherCallbacks.onNewIntent(intent);
1961 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001962 }
1963
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001965 public void onRestoreInstanceState(Bundle state) {
1966 super.onRestoreInstanceState(state);
1967 for (int page: mSynchronouslyBoundPages) {
1968 mWorkspace.restoreInstanceStateForChild(page);
1969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 }
1971
1972 @Override
1973 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001974 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001975 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1976 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001977 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001978 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979
Michael Jurka883f55b2010-10-21 15:47:14 -07001980 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001981 // We close any open folder since it will not be re-opened, and we need to make sure
1982 // this state is reflected.
1983 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984
Adam Cohendcd297f2013-06-18 13:13:40 -07001985 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001986 mWaitingForResult) {
1987 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001988 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001989 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1990 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001991 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1992 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1993 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001994 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
1996
1997 if (mFolderInfo != null && mWaitingForResult) {
1998 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1999 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2000 }
Michael Jurka946ad472010-07-09 18:05:18 -07002001
Winson Chung785d2eb2011-04-14 16:08:02 -07002002 // Save the current AppsCustomize tab
2003 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08002004 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2005 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002006 if (currentTabTag != null) {
2007 outState.putString("apps_customize_currentTab", currentTabTag);
2008 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002009 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2010 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002011 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002012 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002013
2014 if (mLauncherCallbacks != null) {
2015 mLauncherCallbacks.onSaveInstanceState(outState);
2016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
2018
2019 @Override
2020 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002022
Winson Chunge7a03942011-08-05 15:05:12 -07002023 // Remove all pending runnables
2024 mHandler.removeMessages(ADVANCE_MSG);
2025 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002026 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002027
Winson Chungcd2b0142011-06-08 16:02:26 -07002028 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002029 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002030
2031 // It's possible to receive onDestroy after a new Launcher activity has
2032 // been created. In this case, don't interfere with the new Launcher.
2033 if (mModel.isCurrentCallbacks(this)) {
2034 mModel.stopLoader();
2035 app.setLauncher(null);
2036 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002039 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002041 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002043 mAppWidgetHost = null;
2044
2045 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046
2047 TextKeyListener.getInstance().release();
2048
Winson Chung81b52252012-08-27 15:34:29 -07002049 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2050 // to prevent leaking Launcher activities on orientation change.
2051 if (mModel != null) {
2052 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2053 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002054
2055 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002056 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002057
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002058 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002059 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002060 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002061 mWorkspace = null;
2062 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002063
Michael Jurka2ecf9952012-06-18 12:52:28 -07002064 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002065
2066 if (mLauncherCallbacks != null) {
2067 mLauncherCallbacks.onDestroy();
2068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 }
2070
Adam Cohena9cf38f2011-05-02 15:36:58 -07002071 public DragController getDragController() {
2072 return mDragController;
2073 }
2074
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 @Override
2076 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002077 if (requestCode >= 0) {
2078 setWaitingForResult(true);
2079 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 super.startActivityForResult(intent, requestCode);
2081 }
2082
Winson Chung70d72102011-08-12 11:24:35 -07002083 /**
2084 * Indicates that we want global search for this activity by setting the globalSearch
2085 * argument for {@link #startSearch} to true.
2086 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002088 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002090
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002091 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002092
Karl Rosaen138a0412009-04-23 19:00:21 -07002093 if (initialQuery == null) {
2094 // Use any text typed in the launcher as the initial query
2095 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 if (appSearchData == null) {
2098 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002099 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 }
Winson Chungadf0c182012-08-23 14:59:07 -07002101 Rect sourceBounds = new Rect();
2102 if (mSearchDropTargetBar != null) {
2103 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2104 }
Romain Guycbb89e42009-06-08 15:52:54 -07002105
Ian Parkinson047036e2014-09-01 15:40:53 +01002106 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002107 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002108 if (clearTextImmediately) {
2109 clearTypedText();
2110 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002111 }
2112
Ian Parkinson047036e2014-09-01 15:40:53 +01002113 /**
2114 * Start a text search.
2115 *
2116 * @return {@code true} if the search will start immediately, so any further keypresses
2117 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2118 * to buffer keypresses.
2119 */
2120 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002121 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002122 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2123 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2124 sourceBounds);
2125 }
2126
Michael Jurkaa33411c2012-06-14 16:18:21 -07002127 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002128 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002129 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002130 }
2131
2132 /**
2133 * Starts the global search activity. This code is a copied from SearchManager
2134 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002135 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002136 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002137 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002138 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2139 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2140 if (globalSearchActivity == null) {
2141 Log.w(TAG, "No global search activity found.");
2142 return;
2143 }
2144 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2145 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2146 intent.setComponent(globalSearchActivity);
2147 // Make sure that we have a Bundle to put source in
2148 if (appSearchData == null) {
2149 appSearchData = new Bundle();
2150 } else {
2151 appSearchData = new Bundle(appSearchData);
2152 }
Adam Cohen9211d422014-10-07 18:14:53 -07002153 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002154 if (!appSearchData.containsKey("source")) {
2155 appSearchData.putString("source", getPackageName());
2156 }
2157 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2158 if (!TextUtils.isEmpty(initialQuery)) {
2159 intent.putExtra(SearchManager.QUERY, initialQuery);
2160 }
2161 if (selectInitialQuery) {
2162 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2163 }
2164 intent.setSourceBounds(sourceBounds);
2165 try {
2166 startActivity(intent);
2167 } catch (ActivityNotFoundException ex) {
2168 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2169 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 }
2171
Yura4f93ec62014-02-04 14:15:21 +00002172 public boolean isOnCustomContent() {
2173 return mWorkspace.isOnOrMovingToCustomContent();
2174 }
2175
Winson Chung70d72102011-08-12 11:24:35 -07002176 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002177 public boolean onPrepareOptionsMenu(Menu menu) {
2178 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002179 if (!isOnCustomContent()) {
2180 // Close any open folders
2181 closeFolder();
2182 // Stop resizing any widgets
2183 mWorkspace.exitWidgetResizeMode();
2184 if (!mWorkspace.isInOverviewMode()) {
2185 // Show the overview mode
2186 showOverviewMode(true);
2187 } else {
2188 showWorkspace(true);
2189 }
Winson Chunge0298742014-01-17 12:03:00 -08002190 }
Adam Cohen9211d422014-10-07 18:14:53 -07002191 if (mLauncherCallbacks != null) {
2192 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2193 }
2194
Michael Jurkab94f3f82013-09-11 18:08:54 +02002195 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002198 @Override
2199 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002200 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002201 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002202 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002203 }
2204
Joe Onorato9c1289c2009-08-17 11:03:03 -04002205 public boolean isWorkspaceLocked() {
2206 return mWorkspaceLoading || mWaitingForResult;
2207 }
2208
Adam Cohen517a7f52014-03-01 12:12:59 -08002209 public boolean isWorkspaceLoading() {
2210 return mWorkspaceLoading;
2211 }
2212
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002213 private void setWorkspaceLoading(boolean value) {
2214 boolean isLocked = isWorkspaceLocked();
2215 mWorkspaceLoading = value;
2216 if (isLocked != isWorkspaceLocked()) {
2217 onWorkspaceLockedChanged();
2218 }
2219 }
2220
2221 private void setWaitingForResult(boolean value) {
2222 boolean isLocked = isWorkspaceLocked();
2223 mWaitingForResult = value;
2224 if (isLocked != isWorkspaceLocked()) {
2225 onWorkspaceLockedChanged();
2226 }
2227 }
2228
Adam Cohen9211d422014-10-07 18:14:53 -07002229 protected void onWorkspaceLockedChanged() {
2230 if (mLauncherCallbacks != null) {
2231 mLauncherCallbacks.onWorkspaceLockedChanged();
2232 }
2233 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002234
Michael Jurka0280c3b2010-09-17 15:00:07 -07002235 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002236 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002237 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002238 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2239 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002240 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002241 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002242 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002243
Sunny Goyale6b63a32015-01-16 16:14:29 -08002244 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002245 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002246 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2247 }
2248
Sunny Goyale6b63a32015-01-16 16:14:29 -08002249 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002250 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2251 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002252 if (appWidgetInfo.configure != null) {
2253 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002254 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002255
Bjorn Bringert7984c942009-12-09 15:38:25 +00002256 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002257 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2258 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002261 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002262 Runnable onComplete = new Runnable() {
2263 @Override
2264 public void run() {
2265 // Exit spring loaded mode if necessary after adding the widget
2266 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2267 null);
2268 }
2269 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002270 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002271 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002272 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 }
2274 }
Romain Guycbb89e42009-06-08 15:52:54 -07002275
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002276 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002277 // Close any folders that may be open.
2278 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002279 mWorkspace.moveToCustomContentScreen(animate);
2280 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002281
2282 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2283 int[] cell, int spanX, int spanY) {
2284 switch (info.itemType) {
2285 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2286 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2287 int span[] = new int[2];
2288 span[0] = spanX;
2289 span[1] = spanY;
2290 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2291 container, screenId, cell, span);
2292 break;
2293 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2294 processShortcutFromDrop(info.componentName, container, screenId, cell);
2295 break;
2296 default:
2297 throw new IllegalStateException("Unknown item type: " + info.itemType);
2298 }
2299 }
2300
Adam Cohenfbba09b2011-07-18 21:43:05 -07002301 /**
2302 * Process a shortcut drop.
2303 *
2304 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002305 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002306 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002307 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002308 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2309 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002310 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002311 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002312 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002313 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002314
2315 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002316 mPendingAddInfo.cellX = cell[0];
2317 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002318 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002319
2320 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2321 createShortcutIntent.setComponent(componentName);
2322 processShortcut(createShortcutIntent);
2323 }
2324
Adam Cohenfbba09b2011-07-18 21:43:05 -07002325 /**
2326 * Process a widget drop.
2327 *
2328 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002330 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002331 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002332 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2333 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002334 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002335 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002336 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002337 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002338 mPendingAddInfo.minSpanX = info.minSpanX;
2339 mPendingAddInfo.minSpanY = info.minSpanY;
2340
Adam Cohenfbba09b2011-07-18 21:43:05 -07002341 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002342 mPendingAddInfo.cellX = cell[0];
2343 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002344 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002345 if (span != null) {
2346 mPendingAddInfo.spanX = span[0];
2347 mPendingAddInfo.spanY = span[1];
2348 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002349
Adam Cohened66b2b2012-01-23 17:28:51 -08002350 AppWidgetHostView hostView = info.boundWidget;
2351 int appWidgetId;
2352 if (hostView != null) {
2353 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002354 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002355 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002356 // In this case, we either need to start an activity to get permission to bind
2357 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002358 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002359 Bundle options = info.bindOptions;
2360
Sunny Goyalffe83f12014-08-14 17:39:34 -07002361 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2362 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002363 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002364 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002365 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002366 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002367 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2368 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2369 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002370 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2371 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002372 // TODO: we need to make sure that this accounts for the options bundle.
2373 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002374 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2375 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002376 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002377 }
2378
Joe Onoratodeb98af2010-02-19 14:59:39 -08002379 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002380 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 }
2382
Winson Chung24ab2f12010-09-16 14:10:47 -07002383 void processWallpaper(Intent intent) {
2384 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2385 }
2386
Adam Cohendcd297f2013-06-18 13:13:40 -07002387 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002388 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002389 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 folderInfo.title = getText(R.string.folder_name);
2391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002392 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002393 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002394 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002395 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396
2397 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002398 FolderIcon newFolder =
2399 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002400 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002401 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002402 // Force measure the new folder icon
2403 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2404 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002405 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 }
Romain Guycbb89e42009-06-08 15:52:54 -07002407
Joe Onorato9c1289c2009-08-17 11:03:03 -04002408 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002409 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002410 }
2411
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002412 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002413 if (mLauncherCallbacks != null) {
2414 return mLauncherCallbacks.getWallpaperPickerComponent();
2415 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002416 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002417 }
2418
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002419 /**
2420 * Registers various content observers. The current implementation registers
2421 * only a favorites observer to keep track of the favorites applications.
2422 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002423 private void registerContentObservers() {
2424 ContentResolver resolver = getContentResolver();
2425 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2426 true, mWidgetObserver);
2427 }
2428
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 @Override
2430 public boolean dispatchKeyEvent(KeyEvent event) {
2431 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2432 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002434 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002435 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002436 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002437 dumpState();
2438 return true;
2439 }
2440 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002441 }
2442 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2443 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002444 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002445 return true;
2446 }
2447 }
2448
2449 return super.dispatchKeyEvent(event);
2450 }
2451
Joe Onorato88ec0992009-11-19 13:16:06 -08002452 @Override
2453 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002454 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2455 return;
2456 }
2457
Adam Cohenc9735cf2015-01-23 16:11:55 -08002458 if (LauncherAppState.getInstance().getAccessibilityDelegate().onBackPressed()) {
2459 return;
2460 }
2461
Winson Chungb745afb2015-03-02 11:51:23 -08002462 if (isAppsViewVisible()) {
2463 showWorkspace(true);
2464 } else if (isWidgetsViewVisible()) {
2465 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002466 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002467 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002468 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002469 Folder openFolder = mWorkspace.getOpenFolder();
2470 if (openFolder.isEditingName()) {
2471 openFolder.dismissEditingName();
2472 } else {
2473 closeFolder();
2474 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002475 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002476 mWorkspace.exitWidgetResizeMode();
2477
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002478 // Back button is a no-op here, but give at least some feedback for the button press
2479 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002480 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002481 }
2482
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002483 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002484 * Re-listen when widgets are reset.
2485 */
Adam Cohen091440a2015-03-18 14:16:05 -07002486 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002487 if (mAppWidgetHost != null) {
2488 mAppWidgetHost.startListening();
2489 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002490 }
2491
2492 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 * Launches the intent referred by the clicked shortcut.
2494 *
2495 * @param v The view representing the clicked shortcut.
2496 */
2497 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002498 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2499 // view has detached (it's possible for this to happen if the view is removed mid touch).
2500 if (v.getWindowToken() == null) {
2501 return;
2502 }
2503
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002504 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002505 return;
2506 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002507
Adam Cohen1697b792013-09-17 19:08:21 -07002508 if (v instanceof Workspace) {
2509 if (mWorkspace.isInOverviewMode()) {
2510 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002511 }
2512 return;
2513 }
2514
2515 if (v instanceof CellLayout) {
2516 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002517 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002518 }
2519 }
2520
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002522 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002523 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002524 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002525 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002526 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002527 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002528 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002529 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002530 } else if (tag instanceof AppInfo) {
2531 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002532 } else if (tag instanceof LauncherAppWidgetInfo) {
2533 if (v instanceof PendingAppWidgetHostView) {
2534 onClickPendingWidget((PendingAppWidgetHostView) v);
2535 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536 }
2537 }
2538
Sunny Goyal508da152014-08-14 10:53:27 -07002539 public void onClickPagedViewIcon(View v) {
2540 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002541 if (mLauncherCallbacks != null) {
2542 mLauncherCallbacks.onClickPagedViewIcon(v);
2543 }
Sunny Goyal508da152014-08-14 10:53:27 -07002544 }
2545
Michael Jurka0e260592010-06-30 17:07:39 -07002546 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002547 return false;
2548 }
2549
Michael Jurkaaf442092010-06-10 17:01:57 -07002550 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002551 * Event handler for the app widget view which has not fully restored.
2552 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002553 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002554 if (mIsSafeModeEnabled) {
2555 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2556 return;
2557 }
2558
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002559 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002560 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002561 int widgetId = info.appWidgetId;
2562 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2563 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002564 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2565 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002566 mPendingAddInfo.copyFrom(info);
2567 mPendingAddWidgetId = widgetId;
2568
Sunny Goyalffe83f12014-08-14 17:39:34 -07002569 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2570 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002571 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002572 } else if (info.installProgress < 0) {
2573 // The install has not been queued
2574 final String packageName = info.providerName.getPackageName();
2575 showBrokenAppInstallDialog(packageName,
2576 new DialogInterface.OnClickListener() {
2577 public void onClick(DialogInterface dialog, int id) {
2578 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2579 }
2580 });
2581 } else {
2582 // Download has started.
2583 final String packageName = info.providerName.getPackageName();
2584 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002585 }
2586 }
2587
2588 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002589 * Event handler for the "grid" button that appears on the home screen, which
2590 * enters all apps mode.
2591 *
2592 * @param v The view that was clicked.
2593 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002594 protected void onClickAllAppsButton(View v) {
2595 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002596 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002597 showWorkspace(true);
2598 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002599 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002600 }
Adam Cohen9211d422014-10-07 18:14:53 -07002601 if (mLauncherCallbacks != null) {
2602 mLauncherCallbacks.onClickAllAppsButton(v);
2603 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002604 }
2605
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002606 private void showBrokenAppInstallDialog(final String packageName,
2607 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002608 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002609 .setTitle(R.string.abandoned_promises_title)
2610 .setMessage(R.string.abandoned_promise_explanation)
2611 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2612 .setNeutralButton(R.string.abandoned_clean_this,
2613 new DialogInterface.OnClickListener() {
2614 public void onClick(DialogInterface dialog, int id) {
2615 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2616 mWorkspace.removeAbandonedPromise(packageName, user);
2617 }
2618 })
2619 .create().show();
2620 return;
2621 }
2622
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002623 /**
2624 * Event handler for an app shortcut click.
2625 *
2626 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2627 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002628 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002629 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2630 Object tag = v.getTag();
2631 if (!(tag instanceof ShortcutInfo)) {
2632 throw new IllegalArgumentException("Input must be a Shortcut");
2633 }
2634
2635 // Open shortcut
2636 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002637
2638 if (shortcut.isDisabled != 0) {
2639 int error = R.string.activity_not_available;
2640 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2641 error = R.string.safemode_shortcut_error;
2642 }
2643 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2644 return;
2645 }
2646
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002647 final Intent intent = shortcut.intent;
2648
2649 // Check for special shortcuts
2650 if (intent.getComponent() != null) {
2651 final String shortcutClass = intent.getComponent().getClassName();
2652
2653 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2654 MemoryDumpActivity.startDump(this);
2655 return;
2656 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2657 toggleShowWeightWatcher();
2658 return;
2659 }
2660 }
2661
Chris Wren40c5ed32014-06-24 18:24:23 -04002662 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002663 if ((v instanceof BubbleTextView)
2664 && shortcut.isPromise()
2665 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002666 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002667 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002668 new DialogInterface.OnClickListener() {
2669 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002670 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002671 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002672 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002673 return;
2674 }
2675
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002676 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002677 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002678
2679 if (mLauncherCallbacks != null) {
2680 mLauncherCallbacks.onClickAppShortcut(v);
2681 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002682 }
2683
Adam Cohen091440a2015-03-18 14:16:05 -07002684 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002685 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002686 final ShortcutInfo shortcut;
2687 final Intent intent;
2688 if (tag instanceof ShortcutInfo) {
2689 shortcut = (ShortcutInfo) tag;
2690 intent = shortcut.intent;
2691 int[] pos = new int[2];
2692 v.getLocationOnScreen(pos);
2693 intent.setSourceBounds(new Rect(pos[0], pos[1],
2694 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002695
Sunny Goyal508da152014-08-14 10:53:27 -07002696 } else if (tag instanceof AppInfo) {
2697 shortcut = null;
2698 intent = ((AppInfo) tag).intent;
2699 } else {
2700 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2701 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002702
2703 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002704 mStats.recordLaunch(intent, shortcut);
2705
2706 if (success && v instanceof BubbleTextView) {
2707 mWaitingForResume = (BubbleTextView) v;
2708 mWaitingForResume.setStayPressed(true);
2709 }
2710 }
2711
2712 /**
2713 * Event handler for a folder icon click.
2714 *
2715 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2716 */
2717 protected void onClickFolderIcon(View v) {
2718 if (LOGD) Log.d(TAG, "onClickFolder");
2719 if (!(v instanceof FolderIcon)){
2720 throw new IllegalArgumentException("Input must be a FolderIcon");
2721 }
2722
2723 FolderIcon folderIcon = (FolderIcon) v;
2724 final FolderInfo info = folderIcon.getFolderInfo();
2725 Folder openFolder = mWorkspace.getFolderForTag(info);
2726
2727 // If the folder info reports that the associated folder is open, then verify that
2728 // it is actually opened. There have been a few instances where this gets out of sync.
2729 if (info.opened && openFolder == null) {
2730 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2731 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2732 info.opened = false;
2733 }
2734
2735 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2736 // Close any open folder
2737 closeFolder();
2738 // Open the requested folder
2739 openFolder(folderIcon);
2740 } else {
2741 // Find the open folder...
2742 int folderScreen;
2743 if (openFolder != null) {
2744 folderScreen = mWorkspace.getPageForView(openFolder);
2745 // .. and close it
2746 closeFolder(openFolder);
2747 if (folderScreen != mWorkspace.getCurrentPage()) {
2748 // Close any folder open on the current screen
2749 closeFolder();
2750 // Pull the folder onto this screen
2751 openFolder(folderIcon);
2752 }
2753 }
2754 }
Adam Cohen9211d422014-10-07 18:14:53 -07002755
2756 if (mLauncherCallbacks != null) {
2757 mLauncherCallbacks.onClickFolderIcon(v);
2758 }
Michael Jurka5130e402011-10-13 04:55:35 -07002759 }
2760
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002761 /**
2762 * Event handler for the (Add) Widgets button that appears after a long press
2763 * on the home screen.
2764 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002765 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002766 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002767 if (mIsSafeModeEnabled) {
2768 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2769 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002770 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002771 if (mLauncherCallbacks != null) {
2772 mLauncherCallbacks.onClickAddWidgetButton(view);
2773 }
Adam Cohen9211d422014-10-07 18:14:53 -07002774 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002775 }
2776
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002777 /**
2778 * Event handler for the wallpaper picker button that appears after a long press
2779 * on the home screen.
2780 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002781 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002782 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2783 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2784 pickWallpaper.setComponent(getWallpaperPickerComponent());
2785 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002786
2787 if (mLauncherCallbacks != null) {
2788 mLauncherCallbacks.onClickWallpaperPicker(v);
2789 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002790 }
2791
2792 /**
2793 * Event handler for a click on the settings button that appears after a long press
2794 * on the home screen.
2795 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002796 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002797 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002798 if (mLauncherCallbacks != null) {
2799 mLauncherCallbacks.onClickSettingsButton(v);
2800 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002801 }
2802
Michael Jurka5130e402011-10-13 04:55:35 -07002803 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002804 // Provide the same haptic feedback that the system offers for virtual keys.
2805 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002806 }
2807
Adam Cohen61f560d2013-09-30 15:58:20 -07002808 public void performHapticFeedbackOnTouchDown(View v) {
2809 // Provide the same haptic feedback that the system offers for virtual keys.
2810 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2811 }
2812
2813 public View.OnTouchListener getHapticFeedbackTouchListener() {
2814 if (mHapticFeedbackTouchListener == null) {
2815 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2816 @Override
2817 public boolean onTouch(View v, MotionEvent event) {
2818 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2819 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2820 }
2821 return false;
2822 }
2823 };
2824 }
2825 return mHapticFeedbackTouchListener;
2826 }
2827
Adam Cohen9211d422014-10-07 18:14:53 -07002828 public void onDragStarted(View view) {
2829 if (isOnCustomContent()) {
2830 // Custom content screen doesn't participate in drag and drop. If on custom
2831 // content screen, move to default.
2832 moveWorkspaceToDefaultScreen();
2833 }
2834
2835 if (mLauncherCallbacks != null) {
2836 mLauncherCallbacks.onDragStarted(view);
2837 }
2838 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002839
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002840 /**
2841 * Called when the user stops interacting with the launcher.
2842 * This implies that the user is now on the homescreen and is not doing housekeeping.
2843 */
Adam Cohen9211d422014-10-07 18:14:53 -07002844 protected void onInteractionEnd() {
2845 if (mLauncherCallbacks != null) {
2846 mLauncherCallbacks.onInteractionEnd();
2847 }
2848 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002849
2850 /**
2851 * Called when the user starts interacting with the launcher.
2852 * The possible interactions are:
2853 * - open all apps
2854 * - reorder an app shortcut, or a widget
2855 * - open the overview mode.
2856 * This is a good time to stop doing things that only make sense
2857 * when the user is on the homescreen and not doing housekeeping.
2858 */
Adam Cohen9211d422014-10-07 18:14:53 -07002859 protected void onInteractionBegin() {
2860 if (mLauncherCallbacks != null) {
2861 mLauncherCallbacks.onInteractionBegin();
2862 }
2863 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002864
Kenny Guyf07af7b2014-07-31 11:39:16 +01002865 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002866 try {
2867 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002868 launcherApps.showAppDetailsForProfile(componentName, user);
2869 } catch (SecurityException e) {
2870 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2871 Log.e(TAG, "Launcher does not have permission to launch settings");
2872 } catch (ActivityNotFoundException e) {
2873 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2874 Log.e(TAG, "Unable to launch settings");
2875 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002876 }
2877
Michael Jurka1e2f4652013-07-08 18:03:46 -07002878 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002879 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2880 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002881 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002882 // System applications cannot be installed. For now, show a toast explaining that.
2883 // We may give them the option of disabling apps this way.
2884 int messageId = R.string.uninstall_system_app_text;
2885 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002886 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002887 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002888 String packageName = componentName.getPackageName();
2889 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002890 Intent intent = new Intent(
2891 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002892 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2893 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002894 if (user != null) {
2895 user.addToIntent(intent, Intent.EXTRA_USER);
2896 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002897 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002898 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002899 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002900 }
2901
Michael Jurka86a720e2012-05-09 11:23:23 -07002902 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002903 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002904 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002905 // Only launch using the new animation if the shortcut has not opted out (this is a
2906 // private contract between launcher and may be ignored in the future).
2907 boolean useLaunchAnimation = (v != null) &&
2908 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002909 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2910 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002911
Kenny Guy1317e2d2014-05-08 18:52:50 +01002912 UserHandleCompat user = null;
2913 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2914 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2915 user = userManager.getUserForSerialNumber(serialNumber);
2916 }
2917
2918 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002919 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002920 ActivityOptions opts = null;
2921 if (sClipRevealMethod != null) {
2922 // TODO: call method directly when Launcher3 can depend on M APIs
2923 int left = 0, top = 0;
2924 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2925 if (v instanceof TextView) {
2926 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002927 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2928 if (icon != null) {
2929 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002930 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002931 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002932 width = bounds.width();
2933 height = bounds.height();
2934 }
2935 }
2936 try {
2937 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2938 left, top, width, height);
2939 } catch (IllegalAccessException e) {
2940 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2941 sClipRevealMethod = null;
2942 } catch (InvocationTargetException e) {
2943 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2944 sClipRevealMethod = null;
2945 }
2946 }
2947 if (opts == null) {
2948 opts = Utilities.isLmpOrAbove() ?
2949 ActivityOptions.makeCustomAnimation(this,
2950 R.anim.task_open_enter, R.anim.no_anim) :
2951 ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2952 v.getMeasuredWidth(), v.getMeasuredHeight());
2953 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002954 optsBundle = opts.toBundle();
2955 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002956
2957 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2958 // Could be launching some bookkeeping activity
2959 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002960 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002961 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002962 launcherApps.startActivityForProfile(intent.getComponent(), user,
2963 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002964 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002965 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 } catch (SecurityException e) {
2967 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002968 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002970 "or use the exported attribute for this activity. "
2971 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002972 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002973 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002975
Michael Jurka86a720e2012-05-09 11:23:23 -07002976 boolean startActivitySafely(View v, Intent intent, Object tag) {
2977 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002978 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2979 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2980 return false;
2981 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002982 try {
2983 success = startActivity(v, intent, tag);
2984 } catch (ActivityNotFoundException e) {
2985 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2986 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2987 }
2988 return success;
2989 }
2990
Adam Cohen268c4752012-06-06 17:47:33 -07002991 /**
2992 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2993 * in the DragLayer in the exact absolute location of the original FolderIcon.
2994 */
2995 private void copyFolderIconToImage(FolderIcon fi) {
2996 final int width = fi.getMeasuredWidth();
2997 final int height = fi.getMeasuredHeight();
2998
2999 // Lazy load ImageView, Bitmap and Canvas
3000 if (mFolderIconImageView == null) {
3001 mFolderIconImageView = new ImageView(this);
3002 }
3003 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3004 mFolderIconBitmap.getHeight() != height) {
3005 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3006 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3007 }
3008
3009 DragLayer.LayoutParams lp;
3010 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3011 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3012 } else {
3013 lp = new DragLayer.LayoutParams(width, height);
3014 }
3015
Adam Cohen307fe232012-08-16 17:55:58 -07003016 // The layout from which the folder is being opened may be scaled, adjust the starting
3017 // view size by this scale factor.
3018 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003019 lp.customPosition = true;
3020 lp.x = mRectForFolderAnimation.left;
3021 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003022 lp.width = (int) (scale * width);
3023 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003024
3025 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3026 fi.draw(mFolderIconCanvas);
3027 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003028 if (fi.getFolder() != null) {
3029 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3030 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003031 }
Adam Cohen268c4752012-06-06 17:47:33 -07003032 // Just in case this image view is still in the drag layer from a previous animation,
3033 // we remove it and re-add it.
3034 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3035 mDragLayer.removeView(mFolderIconImageView);
3036 }
3037 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003038 if (fi.getFolder() != null) {
3039 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003040 }
Adam Cohen268c4752012-06-06 17:47:33 -07003041 }
3042
Adam Cohen2801caf2011-05-13 20:57:39 -07003043 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003044 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003045 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3046 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3047 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3048
Adam Cohenc51934b2011-07-26 21:07:43 -07003049 FolderInfo info = (FolderInfo) fi.getTag();
3050 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3051 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003052 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3053 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003054 }
3055
Adam Cohen268c4752012-06-06 17:47:33 -07003056 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3057 copyFolderIconToImage(fi);
3058 fi.setVisibility(View.INVISIBLE);
3059
Michael Jurka2ecf9952012-06-18 12:52:28 -07003060 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003061 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003062 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003063 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3064 }
3065 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003066 oa.start();
3067 }
3068
Adam Cohen268c4752012-06-06 17:47:33 -07003069 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003070 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003071 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3072 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3073 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3074
Adam Cohen268c4752012-06-06 17:47:33 -07003075 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003076
Adam Cohen268c4752012-06-06 17:47:33 -07003077 // We remove and re-draw the FolderIcon in-case it has changed
3078 mDragLayer.removeView(mFolderIconImageView);
3079 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003080 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003081 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003082 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003083 oa.addListener(new AnimatorListenerAdapter() {
3084 @Override
3085 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003086 if (cl != null) {
3087 cl.clearFolderLeaveBehind();
3088 // Remove the ImageView copy of the FolderIcon and make the original visible.
3089 mDragLayer.removeView(mFolderIconImageView);
3090 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003091 }
3092 }
3093 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003094 oa.start();
3095 }
3096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003097 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003098 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099 * is animated relative to the specified View. If the View is null, no animation
3100 * is played.
3101 *
3102 * @param folderInfo The FolderInfo describing the folder to open.
3103 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003104 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003105 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003106 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003107
Adam Cohena9cf38f2011-05-02 15:36:58 -07003108 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003109
Adam Cohen4554ee12011-08-03 16:13:21 -07003110 // Just verify that the folder hasn't already been added to the DragLayer.
3111 // There was a one-off crash where the folder had a parent already.
3112 if (folder.getParent() == null) {
3113 mDragLayer.addView(folder);
3114 mDragController.addDropTarget((DropTarget) folder);
3115 } else {
3116 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3117 folder.getParent() + ").");
3118 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003119 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003120 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003121
3122 // Notify the accessibility manager that this folder "window" has appeared and occluded
3123 // the workspace items
3124 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3125 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003126 }
3127
3128 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003129 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003130 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003131 if (folder.isEditingName()) {
3132 folder.dismissEditingName();
3133 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003134 closeFolder(folder);
3135 }
3136 }
3137
3138 void closeFolder(Folder folder) {
3139 folder.getInfo().opened = false;
3140
3141 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3142 if (parent != null) {
3143 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3144 shrinkAndFadeInFolderIcon(fi);
3145 }
3146 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003147
3148 // Notify the accessibility manager that this folder "window" has disappeard and no
3149 // longer occludeds the workspace items
3150 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151 }
3152
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003154 if (!isDraggingEnabled()) return false;
3155 if (isWorkspaceLocked()) return false;
3156 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003157
Adam Cohen1697b792013-09-17 19:08:21 -07003158 if (v instanceof Workspace) {
3159 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003160 if (mWorkspace.enterOverviewMode()) {
3161 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3162 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3163 return true;
3164 } else {
3165 return false;
3166 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003167 } else {
3168 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003169 }
Adam Cohen1697b792013-09-17 19:08:21 -07003170 }
3171
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003172 CellLayout.CellInfo longClickCellInfo = null;
3173 View itemUnderLongClick = null;
3174 if (v.getTag() instanceof ItemInfo) {
3175 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003176 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003177 itemUnderLongClick = longClickCellInfo.cell;
3178 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003179 }
3180
Winson Chung3d503fb2011-07-13 17:25:49 -07003181 // The hotseat touch handling does not go through Workspace, and we always allow long press
3182 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003183 final boolean inHotseat = isHotseatLayout(v);
3184 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003185 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003186 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003187 // User long pressed on empty space
3188 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3189 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003190 if (mWorkspace.isInOverviewMode()) {
3191 mWorkspace.startReordering(v);
3192 } else {
3193 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003194 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003195 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003196 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3197 mHotseat.getOrderInHotseat(
3198 longClickCellInfo.cellX,
3199 longClickCellInfo.cellY));
3200 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003201 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003202 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203 }
3204 }
3205 }
3206 return true;
3207 }
3208
Winson Chung3d503fb2011-07-13 17:25:49 -07003209 boolean isHotseatLayout(View layout) {
3210 return mHotseat != null && layout != null &&
3211 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3212 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003213
Winson Chung3d503fb2011-07-13 17:25:49 -07003214 /**
3215 * Returns the CellLayout of the specified container at the specified screen.
3216 */
Sunny Goyal92820592015-03-02 11:31:35 -08003217 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003218 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3219 if (mHotseat != null) {
3220 return mHotseat.getLayout();
3221 } else {
3222 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003223 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003224 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003225 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003226 }
3227 }
3228
Winson Chungb745afb2015-03-02 11:51:23 -08003229 /**
3230 * For overridden classes.
3231 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003232 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003233 return isAppsViewVisible();
3234 }
3235
3236 public boolean isAppsViewVisible() {
3237 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3238 }
3239
3240 public boolean isWidgetsViewVisible() {
3241 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003242 }
3243
Craig Mautner360310b2012-10-26 15:13:08 -07003244 private void setWorkspaceBackground(boolean workspace) {
3245 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003246 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003247 }
3248
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003249 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003250 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3251 int curflags = getWindow().getAttributes().flags
3252 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3253 if (wpflags != curflags) {
3254 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3255 }
Craig Mautner360310b2012-10-26 15:13:08 -07003256 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003257 }
3258
Michael Jurkae326f182011-11-21 14:05:46 -08003259 @Override
3260 public void onTrimMemory(int level) {
3261 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003262 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3263 // The widget preview db can result in holding onto over
3264 // 3MB of memory for caching which isn't necessary.
3265 SQLiteDatabase.releaseMemory();
3266
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003267 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003268 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003269 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003270 }
Michael Jurkae326f182011-11-21 14:05:46 -08003271 }
3272 }
3273
Winson Chungb745afb2015-03-02 11:51:23 -08003274 @Override
3275 public void onStateTransitionHideSearchBar() {
3276 // Hide the search bar
3277 if (mSearchDropTargetBar != null) {
3278 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3279 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003280 }
3281
Winson Chungb745afb2015-03-02 11:51:23 -08003282 protected void showWorkspace(boolean animated) {
3283 showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003284 }
3285
Adam Cohened66b2b2012-01-23 17:28:51 -08003286 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003287 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003288 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003289 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003290 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
3291 animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003292
Winson Chungc7d2b602012-05-16 17:02:20 -07003293 // Show the search bar (only animate if we were showing the drop target bar in spring
3294 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003295 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003296 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003297 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003298
Michael Jurkab3e22d92011-10-31 15:58:33 -07003299 // Set focus to the AppsCustomize button
3300 if (mAllAppsButton != null) {
3301 mAllAppsButton.requestFocus();
3302 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003303 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003304
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003305 // Change the state *after* we've called all the transition code
3306 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003307
Winson Chung5fb63472011-02-02 17:03:37 -08003308 // Resume the auto-advance of widgets
3309 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003310 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003311
alanv1d4fde62012-10-17 13:15:47 -07003312 // Send an accessibility event to announce the context change
3313 getWindow().getDecorView()
3314 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003315
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003316 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003317 }
3318
Adam Cohened307df2013-10-02 09:37:31 -07003319 void showOverviewMode(boolean animated) {
3320 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003321 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
3322 animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003323 mState = State.WORKSPACE;
3324 onWorkspaceShown(animated);
3325 }
3326
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003327 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003328 }
3329
Winson Chungb745afb2015-03-02 11:51:23 -08003330 /**
3331 * Shows the apps view.
3332 */
3333 void showAppsView(boolean animated, boolean resetListToTop) {
3334 if (resetListToTop) {
3335 mAppsView.scrollToTop();
3336 }
3337 showAppsOrWidgets(animated, State.APPS);
3338 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003339
Winson Chungb745afb2015-03-02 11:51:23 -08003340 /**
3341 * Shows the widgets view.
3342 */
3343 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Winson Chung82963d52013-10-09 11:20:57 -07003344 if (resetPageToZero) {
3345 mAppsCustomizeTabHost.reset();
3346 }
Winson Chungb745afb2015-03-02 11:51:23 -08003347 showAppsOrWidgets(animated, State.WIDGETS);
Adam Cohendcc5e712014-06-23 15:38:55 -04003348 mAppsCustomizeTabHost.post(new Runnable() {
3349 @Override
3350 public void run() {
3351 // We post this in-case the all apps view isn't yet constructed.
3352 mAppsCustomizeTabHost.requestFocus();
3353 }
3354 });
Winson Chungb745afb2015-03-02 11:51:23 -08003355 }
3356
3357 /**
3358 * Sets up the transition to show the apps/widgets view.
3359 */
3360 private void showAppsOrWidgets(boolean animated, State toState) {
3361 if (mState != State.WORKSPACE) return;
3362 if (toState != State.APPS && toState != State.WIDGETS) return;
3363
3364 if (toState == State.APPS) {
3365 mStateTransitionAnimation.startAnimationToAllApps(animated);
3366 } else {
3367 mStateTransitionAnimation.startAnimationToWidgets(animated);
3368 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003369
Michael Jurkab3e22d92011-10-31 15:58:33 -07003370 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003371 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003372
3373 // Pause the auto-advance of widgets until we are out of AllApps
3374 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003375 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003376 closeFolder();
3377
3378 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003379 getWindow().getDecorView()
3380 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003381 }
3382
Adam Cohen7777d962011-08-18 18:58:38 -07003383 void enterSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003384 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3385 mState == State.WIDGETS_SPRING_LOADED) {
3386 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003387 }
Winson Chungb745afb2015-03-02 11:51:23 -08003388
3389 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
3390 true /* animated */, null /* onCompleteRunnable */);
3391 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003392 }
Adam Cohen7777d962011-08-18 18:58:38 -07003393
Adam Cohenad4e15c2013-10-17 16:21:35 -07003394 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003395 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003396 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003397
Winson Chunge7a03942011-08-05 15:05:12 -07003398 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003399 @Override
3400 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003401 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003402 // Before we show workspace, hide all apps again because
3403 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3404 // clean up our state transition functions
3405 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003406 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003407 } else {
3408 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003409 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003410 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003411 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003412 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003413
Michael Jurkad3ef3062010-11-23 16:23:58 -08003414 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003415 if (mState == State.APPS_SPRING_LOADED) {
3416 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3417 mState = State.APPS;
3418 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3419 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3420 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003421 }
3422 // Otherwise, we are not in spring loaded mode, so don't do anything.
3423 }
3424
Michael Jurkab3e22d92011-10-31 15:58:33 -07003425 void lockAllApps() {
3426 // TODO
3427 }
3428
3429 void unlockAllApps() {
3430 // TODO
3431 }
3432
Sunny Goyal594d76d2014-11-06 10:12:54 -08003433 protected void disableVoiceButtonProxy(boolean disable) {
3434 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003435 }
3436
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003437 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003438 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3439 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003440 }
3441
Adam Cohen24ce0b32014-01-14 16:18:14 -08003442 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003443 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3444 if (searchProvider == null) {
3445 return null;
3446 }
3447
3448 Bundle opts = new Bundle();
3449 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003450 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003451
3452 SharedPreferences sp = getSharedPreferences(
3453 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3454 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003455 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003456 if (!searchProvider.provider.flattenToString().equals(
3457 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003458 || (widgetInfo == null)
3459 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003460 // A valid widget is not already bound.
3461 if (widgetId > -1) {
3462 mAppWidgetHost.deleteAppWidgetId(widgetId);
3463 widgetId = -1;
3464 }
3465
3466 // Try to bind a new widget
3467 widgetId = mAppWidgetHost.allocateAppWidgetId();
3468
3469 if (!AppWidgetManagerCompat.getInstance(this)
3470 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3471 mAppWidgetHost.deleteAppWidgetId(widgetId);
3472 widgetId = -1;
3473 }
3474
3475 sp.edit()
3476 .putInt(QSB_WIDGET_ID, widgetId)
3477 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3478 .commit();
3479 }
3480
3481 if (widgetId != -1) {
3482 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3483 mQsb.updateAppWidgetOptions(opts);
3484 mQsb.setPadding(0, 0, 0, 0);
3485 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003486 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003487 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003488 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003489 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003490 }
3491
Michael Jurkad7c28052012-04-27 15:43:36 -07003492 @Override
3493 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003494 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003495 final List<CharSequence> text = event.getText();
3496 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003497 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003498 if (mState == State.APPS) {
3499 text.add("Apps");
3500 } else if (mState == State.WIDGETS) {
3501 text.add("Widgets");
alanv1d4fde62012-10-17 13:15:47 -07003502 } else {
3503 text.add(getString(R.string.all_apps_home_button_label));
3504 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003505 return result;
3506 }
3507
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003508 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003509 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003510 */
Adam Cohen091440a2015-03-18 14:16:05 -07003511 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003512 @Override
3513 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003514 closeSystemDialogs();
3515 }
3516 }
3517
3518 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003519 * Receives notifications whenever the appwidgets are reset.
3520 */
3521 private class AppWidgetResetObserver extends ContentObserver {
3522 public AppWidgetResetObserver() {
3523 super(new Handler());
3524 }
3525
3526 @Override
3527 public void onChange(boolean selfChange) {
3528 onAppWidgetReset();
3529 }
3530 }
3531
3532 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003533 * If the activity is currently paused, signal that we need to run the passed Runnable
3534 * in onResume.
3535 *
3536 * This needs to be called from incoming places where resources might have been loaded
3537 * while we are paused. That is becaues the Configuration might be wrong
3538 * when we're not running, and if it comes back to what it was when we
3539 * were paused, we are not restarted.
3540 *
3541 * Implementation of the method from LauncherModel.Callbacks.
3542 *
3543 * @return true if we are currently paused. The caller might be able to
3544 * skip some work in that case since we will come back again.
3545 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003546 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003547 if (mPaused) {
3548 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003549 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003550 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003551 }
3552 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003553 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003554 return true;
3555 } else {
3556 return false;
3557 }
3558 }
3559
Michael Jurkac402cd92013-05-20 15:49:32 +02003560 private boolean waitUntilResume(Runnable run) {
3561 return waitUntilResume(run, false);
3562 }
3563
Michael Jurka1e2f4652013-07-08 18:03:46 -07003564 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003565 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003566 }
3567
Michael Jurka7607c2f2013-04-03 14:33:19 -07003568 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003569 * If the activity is currently paused, signal that we need to re-run the loader
3570 * in onResume.
3571 *
3572 * This needs to be called from incoming places where resources might have been loaded
3573 * while we are paused. That is becaues the Configuration might be wrong
3574 * when we're not running, and if it comes back to what it was when we
3575 * were paused, we are not restarted.
3576 *
3577 * Implementation of the method from LauncherModel.Callbacks.
3578 *
3579 * @return true if we are currently paused. The caller might be able to
3580 * skip some work in that case since we will come back again.
3581 */
3582 public boolean setLoadOnResume() {
3583 if (mPaused) {
3584 Log.i(TAG, "setLoadOnResume");
3585 mOnResumeNeedsLoad = true;
3586 return true;
3587 } else {
3588 return false;
3589 }
3590 }
3591
3592 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003593 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003594 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003595 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003596 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003597 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003598 } else {
3599 return SCREEN_COUNT / 2;
3600 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003601 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003602
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 /**
3604 * Refreshes the shortcuts shown on the workspace.
3605 *
3606 * Implementation of the method from LauncherModel.Callbacks.
3607 */
3608 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003609 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003610
Michael Jurka7607c2f2013-04-03 14:33:19 -07003611 // If we're starting binding all over again, clear any bind calls we'd postponed in
3612 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3613 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003614 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003615
Winson Chungd64d1762013-08-20 14:37:16 -07003616 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003617 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003618 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003619
Michael Jurka05bf6442011-11-30 20:28:53 -08003620 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003621 if (mHotseat != null) {
3622 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003623 }
3624 }
3625
Adam Cohendcd297f2013-06-18 13:13:40 -07003626 @Override
3627 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003628 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003629
Adam Cohen5084cba2013-09-03 12:01:16 -07003630 // If there are no screens, we need to have an empty screen
3631 if (orderedScreenIds.size() == 0) {
3632 mWorkspace.addExtraEmptyScreen();
3633 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003634
3635 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003636 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003637 if (hasCustomContentToLeft()) {
3638 mWorkspace.createCustomContentContainer();
3639 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003640 }
Winson Chung64359a52013-07-08 17:17:08 -07003641 }
3642
3643 @Override
3644 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003645 // Log to disk
3646 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3647 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3648 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003649 int count = orderedScreenIds.size();
3650 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003651 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003652 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003653 }
3654
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003655 @Override
3656 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3657 final long screenId, final int[] cell, final int spanX, final int spanY) {
3658 showWorkspace(true, new Runnable() {
3659
3660 @Override
3661 public void run() {
3662 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3663 addPendingItem(info, container, screenId, cell, spanX, spanY);
3664 }
3665 });
3666 }
3667
Adam Cohen39a06042013-07-19 14:30:12 -07003668 private boolean shouldShowWeightWatcher() {
3669 String spKey = LauncherAppState.getSharedPreferencesKey();
3670 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003671 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003672
3673 return show;
3674 }
3675
3676 private void toggleShowWeightWatcher() {
3677 String spKey = LauncherAppState.getSharedPreferencesKey();
3678 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3679 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3680
3681 show = !show;
3682
3683 SharedPreferences.Editor editor = sp.edit();
3684 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3685 editor.commit();
3686
3687 if (mWeightWatcher != null) {
3688 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3689 }
3690 }
3691
Winson Chungd64d1762013-08-20 14:37:16 -07003692 public void bindAppsAdded(final ArrayList<Long> newScreens,
3693 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003694 final ArrayList<ItemInfo> addAnimated,
3695 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003696 Runnable r = new Runnable() {
3697 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003698 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003699 }
3700 };
3701 if (waitUntilResume(r)) {
3702 return;
3703 }
3704
Winson Chungd64d1762013-08-20 14:37:16 -07003705 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003706 if (newScreens != null) {
3707 bindAddScreens(newScreens);
3708 }
Winson Chungd64d1762013-08-20 14:37:16 -07003709
3710 // We add the items without animation on non-visible pages, and with
3711 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003712 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003713 bindItems(addNotAnimated, 0,
3714 addNotAnimated.size(), false);
3715 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003716 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003717 bindItems(addAnimated, 0,
3718 addAnimated.size(), true);
3719 }
Winson Chungc58497e2013-09-03 17:48:37 -07003720
Winson Chung87412982013-10-03 18:34:14 -07003721 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003722 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003723
Winson Chungb745afb2015-03-02 11:51:23 -08003724 if (addedApps != null && mAppsView != null) {
3725 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003726 }
Winson Chungd64d1762013-08-20 14:37:16 -07003727 }
3728
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003729 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003730 * Bind the items start-end from the list.
3731 *
3732 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003733 */
Winson Chung64359a52013-07-08 17:17:08 -07003734 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3735 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003736 Runnable r = new Runnable() {
3737 public void run() {
3738 bindItems(shortcuts, start, end, forceAnimateIcons);
3739 }
3740 };
3741 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003742 return;
3743 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003744
Winson Chungf0c6ae02012-03-21 16:10:31 -07003745 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003746 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3747 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003748 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003749 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003750 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003751 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003752 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003753
3754 // Short circuit if we are loading dock items for a configuration which has no dock
3755 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3756 mHotseat == null) {
3757 continue;
3758 }
3759
Joe Onorato9c1289c2009-08-17 11:03:03 -04003760 switch (item.itemType) {
3761 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3762 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003763 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003764 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003765
Winson Chung64359a52013-07-08 17:17:08 -07003766 /*
3767 * TODO: FIX collision case
3768 */
Winson Chungce376632013-07-11 16:12:41 -07003769 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3770 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3771 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003772 View v = cl.getChildAt(item.cellX, item.cellY);
3773 Object tag = v.getTag();
3774 String desc = "Collision while binding workspace item: " + item
3775 + ". Collides with " + tag;
3776 if (LauncherAppState.isDogfoodBuild()) {
3777 throw (new RuntimeException(desc));
3778 } else {
3779 Log.d(TAG, desc);
3780 }
Winson Chungce376632013-07-11 16:12:41 -07003781 }
Winson Chung64359a52013-07-08 17:17:08 -07003782 }
3783
Adam Cohendcd297f2013-06-18 13:13:40 -07003784 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3785 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003786 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003787 // Animate all the applications up now
3788 shortcut.setAlpha(0f);
3789 shortcut.setScaleX(0f);
3790 shortcut.setScaleY(0f);
3791 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003792 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003793 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003794 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003795 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003796 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003797 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003798 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003799 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3800 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003801 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003802 default:
3803 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003804 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003805 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003806
Winson Chung997a9232013-07-24 15:33:46 -07003807 if (animateIcons) {
3808 // Animate to the correct page
3809 if (newShortcutsScreenId > -1) {
3810 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003811 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003812 final Runnable startBounceAnimRunnable = new Runnable() {
3813 public void run() {
3814 anim.playTogether(bounceAnims);
3815 anim.start();
3816 }
3817 };
Winson Chung997a9232013-07-24 15:33:46 -07003818 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003819 // We post the animation slightly delayed to prevent slowdowns
3820 // when we are loading right after we return to launcher.
3821 mWorkspace.postDelayed(new Runnable() {
3822 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003823 if (mWorkspace != null) {
3824 mWorkspace.snapToPage(newScreenIndex);
3825 mWorkspace.postDelayed(startBounceAnimRunnable,
3826 NEW_APPS_ANIMATION_DELAY);
3827 }
Winson Chung94d67682013-09-25 16:29:40 -07003828 }
3829 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003830 } else {
3831 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003832 }
3833 }
Winson Chung64359a52013-07-08 17:17:08 -07003834 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003835 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003836 }
3837
Joe Onorato9c1289c2009-08-17 11:03:03 -04003838 /**
3839 * Implementation of the method from LauncherModel.Callbacks.
3840 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003841 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003842 Runnable r = new Runnable() {
3843 public void run() {
3844 bindFolders(folders);
3845 }
3846 };
3847 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003848 return;
3849 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003850 sFolders.clear();
3851 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003852 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003853
3854 /**
3855 * Add the views for a widget to the workspace.
3856 *
3857 * Implementation of the method from LauncherModel.Callbacks.
3858 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003859 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003860 Runnable r = new Runnable() {
3861 public void run() {
3862 bindAppWidget(item);
3863 }
3864 };
3865 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003866 return;
3867 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003868
Daniel Sandler843e8602010-06-07 14:59:01 -04003869 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3870 if (DEBUG_WIDGETS) {
3871 Log.d(TAG, "bindAppWidget: " + item);
3872 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003873 final Workspace workspace = mWorkspace;
3874
Adam Cohen59400422014-03-05 18:07:04 -08003875 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003876 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003877
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003878 if (!mIsSafeModeEnabled
3879 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3880 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003881 if (appWidgetInfo == null) {
3882 if (DEBUG_WIDGETS) {
3883 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3884 + " belongs to component " + item.providerName
3885 + ", as the povider is null");
3886 }
3887 LauncherModel.deleteItemFromDatabase(this, item);
3888 return;
3889 }
3890 // Note: This assumes that the id remap broadcast is received before this step.
3891 // If that is not the case, the id remap will be ignored and user may see the
3892 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003893 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003894 pendingInfo.spanX = item.spanX;
3895 pendingInfo.spanY = item.spanY;
3896 pendingInfo.minSpanX = item.minSpanX;
3897 pendingInfo.minSpanY = item.minSpanY;
3898 Bundle options =
3899 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
3900
Sunny Goyalff572272014-07-23 13:58:07 -07003901 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003902 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3903 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003904
3905 // TODO consider showing a permission dialog when the widget is clicked.
3906 if (!success) {
3907 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3908 if (DEBUG_WIDGETS) {
3909 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3910 + " belongs to component " + item.providerName
3911 + ", as the launcher is unable to bing a new widget id");
3912 }
3913 LauncherModel.deleteItemFromDatabase(this, item);
3914 return;
3915 }
3916
3917 item.appWidgetId = newWidgetId;
3918
3919 // If the widget has a configure activity, it is still needs to set it up, otherwise
3920 // the widget is ready to go.
3921 item.restoreStatus = (appWidgetInfo.configure == null)
3922 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3923 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3924
3925 LauncherModel.updateItemInDatabase(this, item);
3926 }
3927
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003928 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003929 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003930 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003931 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3932 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003933 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003934
Sunny Goyal651077b2014-06-30 14:15:31 -07003935 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3936 } else {
3937 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003938 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3939 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003940 view.updateIcon(mIconCache);
3941 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003942 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003943 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003944 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003945
Joe Onorato9c1289c2009-08-17 11:03:03 -04003946 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003947 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003948
Adam Cohendcd297f2013-06-18 13:13:40 -07003949 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003950 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003951 if (!item.isCustomWidget()) {
3952 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3953 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003954
Joe Onorato9c1289c2009-08-17 11:03:03 -04003955 workspace.requestLayout();
3956
Daniel Sandler843e8602010-06-07 14:59:01 -04003957 if (DEBUG_WIDGETS) {
3958 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3959 + (SystemClock.uptimeMillis()-start) + "ms");
3960 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003961 }
3962
Sunny Goyalff572272014-07-23 13:58:07 -07003963 /**
3964 * Restores a pending widget.
3965 *
3966 * @param appWidgetId The app widget id
3967 * @param cellInfo The position on screen where to create the widget.
3968 */
3969 private void completeRestoreAppWidget(final int appWidgetId) {
3970 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3971 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3972 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3973 return;
3974 }
3975
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003976 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003977 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3978
3979 mWorkspace.reinflateWidgetsIfNecessary();
3980 LauncherModel.updateItemInDatabase(this, info);
3981 }
3982
Adam Cohen1462de32012-07-24 22:34:36 -07003983 public void onPageBoundSynchronously(int page) {
3984 mSynchronouslyBoundPages.add(page);
3985 }
3986
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987 /**
3988 * Callback saying that there aren't any more items to bind.
3989 *
3990 * Implementation of the method from LauncherModel.Callbacks.
3991 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003992 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003993 Runnable r = new Runnable() {
3994 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003995 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003996 }
3997 };
3998 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003999 return;
4000 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004001 if (mSavedState != null) {
4002 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004003 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004004 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004005 mSavedState = null;
4006 }
4007
Adam Cohen1462de32012-07-24 22:34:36 -07004008 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004009
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004010 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004011 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004012
4013 // If we received the result of any pending adds while the loader was running (e.g. the
4014 // widget configuration forced an orientation change), process them now.
4015 if (sPendingAddItem != null) {
4016 final long screenId = completeAdd(sPendingAddItem);
4017
4018 // TODO: this moves the user to the page where the pending item was added. Ideally,
4019 // the screen would be guaranteed to exist after bind, and the page would be set through
4020 // the workspace restore process.
4021 mWorkspace.post(new Runnable() {
4022 @Override
4023 public void run() {
4024 mWorkspace.snapToScreenId(screenId);
4025 }
4026 });
4027 sPendingAddItem = null;
4028 }
4029
Sunny Goyale755d462014-07-22 13:48:29 -07004030 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004031
4032 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004033 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004034 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004035 }
4036
Adam Cohen3ed316a2014-07-23 18:21:20 -07004037 private void sendLoadingCompleteBroadcastIfNecessary() {
4038 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4039 String permission =
4040 getResources().getString(R.string.receive_first_load_broadcast_permission);
4041 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4042 sendBroadcast(intent, permission);
4043 SharedPreferences.Editor editor = mSharedPrefs.edit();
4044 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4045 editor.apply();
4046 }
4047 }
4048
Winson Chunga0b7e862013-09-05 16:03:15 -07004049 public boolean isAllAppsButtonRank(int rank) {
4050 if (mHotseat != null) {
4051 return mHotseat.isAllAppsButtonRank(rank);
4052 }
4053 return false;
4054 }
4055
Winson Chunga2413752012-04-03 14:22:34 -07004056 private boolean canRunNewAppsAnimation() {
4057 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4058 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4059 }
4060
Winson Chungc9168342013-06-26 14:54:55 -07004061 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4062 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4063 PropertyValuesHolder.ofFloat("alpha", 1f),
4064 PropertyValuesHolder.ofFloat("scaleX", 1f),
4065 PropertyValuesHolder.ofFloat("scaleY", 1f));
4066 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4067 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4068 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4069 return bounceAnim;
4070 }
4071
Adam Cohen27772732013-11-11 14:00:56 +00004072 public boolean useVerticalBarLayout() {
4073 return LauncherAppState.getInstance().getDynamicGrid().
4074 getDeviceProfile().isVerticalBarLayout();
4075 }
4076
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004077 protected Rect getSearchBarBounds() {
4078 return LauncherAppState.getInstance().getDynamicGrid().
4079 getDeviceProfile().getSearchBarBounds();
4080 }
4081
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004082 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004083 if (mSearchDropTargetBar == null) {
4084 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004085 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004086 if (mQsb != null) {
4087 mSearchDropTargetBar.removeView(mQsb);
4088 mQsb = null;
4089 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004090 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004091 }
4092
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004093 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004094 * Add the icons for all apps.
4095 *
4096 * Implementation of the method from LauncherModel.Callbacks.
4097 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004098 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004099 if (mAppsView != null) {
4100 mAppsView.setApps(apps);
4101 }
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004102 if (mAppsCustomizeContent != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004103 mAppsCustomizeContent.onPackagesUpdated(
Hyunyoung Song70a48be2015-03-11 16:36:52 -07004104 LauncherModel.getSortedWidgetsAndShortcuts(this, false /* refresh */));
Winson Chung785d2eb2011-04-14 16:08:02 -07004105 }
Adam Cohen9211d422014-10-07 18:14:53 -07004106 if (mLauncherCallbacks != null) {
4107 mLauncherCallbacks.bindAllApplications(apps);
4108 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004109 }
4110
4111 /**
4112 * A package was updated.
4113 *
4114 * Implementation of the method from LauncherModel.Callbacks.
4115 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004116 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004117 Runnable r = new Runnable() {
4118 public void run() {
4119 bindAppsUpdated(apps);
4120 }
4121 };
4122 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004123 return;
4124 }
4125
Winson Chungb745afb2015-03-02 11:51:23 -08004126 if (mAppsView != null) {
4127 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004128 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004129 }
4130
Sunny Goyal4390ace2014-10-13 11:33:11 -07004131 @Override
4132 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4133 Runnable r = new Runnable() {
4134 public void run() {
4135 bindWidgetsRestored(widgets);
4136 }
4137 };
4138 if (waitUntilResume(r)) {
4139 return;
4140 }
4141 mWorkspace.widgetsRestored(widgets);
4142 }
4143
Joe Onorato9c1289c2009-08-17 11:03:03 -04004144 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004145 * Some shortcuts were updated in the background.
4146 *
4147 * Implementation of the method from LauncherModel.Callbacks.
4148 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004149 @Override
4150 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4151 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004152 Runnable r = new Runnable() {
4153 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004154 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004155 }
4156 };
4157 if (waitUntilResume(r)) {
4158 return;
4159 }
4160
Sunny Goyal4390ace2014-10-13 11:33:11 -07004161 if (!updated.isEmpty()) {
4162 mWorkspace.updateShortcuts(updated);
4163 }
4164
4165 if (!removed.isEmpty()) {
4166 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4167 for (ShortcutInfo si : removed) {
4168 removedComponents.add(si.getTargetComponent());
4169 }
4170 mWorkspace.removeItemsByComponentName(removedComponents, user);
4171 // Notify the drag controller
4172 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004173 }
4174 }
4175
4176 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004177 * Update the state of a package, typically related to install state.
4178 *
4179 * Implementation of the method from LauncherModel.Callbacks.
4180 */
Sunny Goyale755d462014-07-22 13:48:29 -07004181 @Override
4182 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004183 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004184 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004185 }
4186 }
4187
4188 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004189 * Update the label and icon of all the icons in a package
4190 *
4191 * Implementation of the method from LauncherModel.Callbacks.
4192 */
4193 @Override
4194 public void updatePackageBadge(String packageName) {
4195 if (mWorkspace != null) {
4196 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4197 }
4198 }
4199
4200 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004201 * A package was uninstalled. We take both the super set of packageNames
4202 * in addition to specific applications to remove, the reason being that
4203 * this can be called when a package is updated as well. In that scenario,
4204 * we only remove specific components from the workspace, where as
4205 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004206 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004207 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004208 * Implementation of the method from LauncherModel.Callbacks.
4209 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004210 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004211 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004212 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004213 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004214 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004215 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004216 }
Winson Chungd64d1762013-08-20 14:37:16 -07004217 };
4218 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004219 return;
4220 }
4221
Sunny Goyal1a745e82014-10-02 15:58:31 -07004222 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004223 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4224 for (AppInfo info : appInfos) {
4225 removedComponents.add(info.componentName);
4226 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004227 if (!packageNames.isEmpty()) {
4228 mWorkspace.removeItemsByPackageName(packageNames, user);
4229 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004230 if (!removedComponents.isEmpty()) {
4231 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004232 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004233 // Notify the drag controller
4234 mDragController.onAppsRemoved(packageNames, removedComponents);
4235
Sunny Goyal1a745e82014-10-02 15:58:31 -07004236 } else {
4237 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004238 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004239
Winson Chungdf95eb12013-10-16 14:57:07 -07004240 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004241 if (mAppsView != null) {
4242 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004243 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004244 }
Joe Onoratobe386092009-11-17 17:32:16 -08004245
4246 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004247 * A number of packages were updated.
4248 */
Adam Cohen091440a2015-03-18 14:16:05 -07004249 @Thunk ArrayList<Object> mWidgetsAndShortcuts;
Michael Jurkac402cd92013-05-20 15:49:32 +02004250 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004251 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004252 bindPackagesUpdated(mWidgetsAndShortcuts);
4253 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004254 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004255 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004256 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4257 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4258 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004259 return;
4260 }
4261
Winson Chung64359a52013-07-08 17:17:08 -07004262 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004263 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004264 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004265 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004266 }
4267
Winson Chung400438b2011-01-16 17:53:48 -08004268 private int mapConfigurationOriActivityInfoOri(int configOri) {
4269 final Display d = getWindowManager().getDefaultDisplay();
4270 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4271 switch (d.getRotation()) {
4272 case Surface.ROTATION_0:
4273 case Surface.ROTATION_180:
4274 // We are currently in the same basic orientation as the natural orientation
4275 naturalOri = configOri;
4276 break;
4277 case Surface.ROTATION_90:
4278 case Surface.ROTATION_270:
4279 // We are currently in the other basic orientation to the natural orientation
4280 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4281 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4282 break;
4283 }
4284
4285 int[] oriMap = {
4286 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4287 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4288 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4289 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4290 };
4291 // Since the map starts at portrait, we need to offset if this device's natural orientation
4292 // is landscape.
4293 int indexOffset = 0;
4294 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4295 indexOffset = 1;
4296 }
4297 return oriMap[(d.getRotation() + indexOffset) % 4];
4298 }
Adam Cohen446e9402011-09-15 18:21:21 -07004299
Winson Chung4b919f82012-05-01 10:44:08 -07004300 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004301 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004302 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4303 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4304 .getConfiguration().orientation));
4305 } else {
4306 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4307 }
Winson Chung4b919f82012-05-01 10:44:08 -07004308 }
4309 }
4310 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004311 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004312 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004313 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004314 } else {
4315 mHandler.postDelayed(new Runnable() {
4316 public void run() {
4317 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4318 }
4319 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004320 }
Winson Chung4b919f82012-05-01 10:44:08 -07004321 }
Winson Chung400438b2011-01-16 17:53:48 -08004322 }
4323
Winson Chunge43a1e72014-01-15 10:33:02 -08004324 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004325 if (mLauncherCallbacks != null) {
4326 return mLauncherCallbacks.isLauncherPreinstalled();
4327 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004328 PackageManager pm = getPackageManager();
4329 try {
4330 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4331 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4332 return true;
4333 } else {
4334 return false;
4335 }
4336 } catch (NameNotFoundException e) {
4337 e.printStackTrace();
4338 return false;
4339 }
4340 }
4341
Adam Cohenea90f832014-05-21 15:03:34 -07004342 /**
4343 * This method indicates whether or not we should suggest default wallpaper dimensions
4344 * when our wallpaper cropper was not yet used to set a wallpaper.
4345 */
4346 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004347 if (mLauncherCallbacks != null) {
4348 return mLauncherCallbacks.overrideWallpaperDimensions();
4349 }
Adam Cohenea90f832014-05-21 15:03:34 -07004350 return true;
4351 }
4352
Adam Cohen432609a2014-03-13 17:03:22 -07004353 /**
4354 * To be overridden by subclasses to indicate that there is an activity to launch
4355 * before showing the standard launcher experience.
4356 */
4357 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004358 if (mLauncherCallbacks != null) {
4359 return mLauncherCallbacks.hasFirstRunActivity();
4360 }
Adam Cohen432609a2014-03-13 17:03:22 -07004361 return false;
4362 }
4363
4364 /**
4365 * To be overridden by subclasses to launch any first run activity
4366 */
4367 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004368 if (mLauncherCallbacks != null) {
4369 return mLauncherCallbacks.getFirstRunActivity();
4370 }
Adam Cohen432609a2014-03-13 17:03:22 -07004371 return null;
4372 }
4373
Winson Chunga6945242014-01-08 14:04:34 -08004374 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004375 return !ActivityManager.isRunningInTestHarness() &&
4376 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004377 }
4378
Adam Cohen4a9423d2014-03-28 14:22:31 -07004379 protected boolean hasRunFirstRunActivity() {
4380 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4381 }
4382
Adam Cohen432609a2014-03-13 17:03:22 -07004383 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004384 if (shouldRunFirstRunActivity() &&
4385 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004386 Intent firstRunIntent = getFirstRunActivity();
4387 if (firstRunIntent != null) {
4388 startActivity(firstRunIntent);
4389 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004390 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004391 }
4392 }
Adam Cohen432609a2014-03-13 17:03:22 -07004393 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004394 }
4395
4396 private void markFirstRunActivityShown() {
4397 SharedPreferences.Editor editor = mSharedPrefs.edit();
4398 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4399 editor.apply();
4400 }
4401
Adam Cohen432609a2014-03-13 17:03:22 -07004402 /**
4403 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4404 * screen that must be displayed and dismissed.
4405 */
4406 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004407 if (mLauncherCallbacks != null) {
4408 return mLauncherCallbacks.hasDismissableIntroScreen();
4409 }
Adam Cohen432609a2014-03-13 17:03:22 -07004410 return false;
4411 }
4412
4413 /**
4414 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4415 */
4416 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004417 if (mLauncherCallbacks != null) {
4418 return mLauncherCallbacks.getIntroScreen();
4419 }
Adam Cohen432609a2014-03-13 17:03:22 -07004420 return null;
4421 }
4422
4423 /**
4424 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4425 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4426 */
4427 private boolean shouldShowIntroScreen() {
4428 return hasDismissableIntroScreen() &&
4429 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4430 }
4431
4432 protected void showIntroScreen() {
4433 View introScreen = getIntroScreen();
4434 changeWallpaperVisiblity(false);
4435 if (introScreen != null) {
4436 mDragLayer.showOverlayView(introScreen);
4437 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004438 if (mLauncherOverlayContainer != null) {
4439 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4440 }
Adam Cohen432609a2014-03-13 17:03:22 -07004441 }
4442
4443 public void dismissIntroScreen() {
4444 markIntroScreenDismissed();
4445 if (showFirstRunActivity()) {
4446 // We delay hiding the intro view until the first run activity is showing. This
4447 // avoids a blip.
4448 mWorkspace.postDelayed(new Runnable() {
4449 @Override
4450 public void run() {
4451 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004452 if (mLauncherOverlayContainer != null) {
4453 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4454 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004455 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004456 }
4457 }, ACTIVITY_START_DELAY);
4458 } else {
4459 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004460 if (mLauncherOverlayContainer != null) {
4461 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4462 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004463 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004464 }
4465 changeWallpaperVisiblity(true);
4466 }
4467
4468 private void markIntroScreenDismissed() {
4469 SharedPreferences.Editor editor = mSharedPrefs.edit();
4470 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4471 editor.apply();
4472 }
4473
Adam Cohen091440a2015-03-18 14:16:05 -07004474 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004475 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4476 // on the device, then we always show the first run cling experience (or if there is no
4477 // launcher2). Otherwise, we prompt the user upon started for migration
4478 LauncherClings launcherClings = new LauncherClings(this);
4479 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4480 if (mModel.canMigrateFromOldLauncherDb(this)) {
4481 launcherClings.showMigrationCling();
4482 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004483 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004484 }
4485 }
4486 }
4487
Winson Chunga6945242014-01-08 14:04:34 -08004488 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004489 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4490 if (mHotseat != null) mHotseat.setAlpha(1f);
4491 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4492 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004493 }
4494
4495 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004496 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4497 if (mHotseat != null) mHotseat.setAlpha(0f);
4498 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4499 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004500 }
4501
Mathew Inwood72fbec12013-11-19 15:45:07 +00004502 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004503 // Called from search suggestion, not supported in other profiles.
4504 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4505 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4506 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4507 myUser);
4508 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004509 return null;
4510 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004511 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004512 }
4513
4514 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4515 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004516 // Called from search suggestion, not supported in other profiles.
4517 return createShortcutDragInfo(shortcutIntent, caption, icon,
4518 UserHandleCompat.myUserHandle());
4519 }
4520
4521 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4522 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004523 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004524 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004525 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004526 }
4527
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004528 protected void moveWorkspaceToDefaultScreen() {
4529 mWorkspace.moveToDefaultScreen(false);
4530 }
4531
Mathew Inwood72fbec12013-11-19 15:45:07 +00004532 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4533 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004534 mWorkspace.onExternalDragStartedWithItem(dragView);
4535 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004536 }
4537
Anjali Koppalf5d29132014-02-28 13:33:27 -08004538 @Override
4539 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004540 if (mLauncherCallbacks != null) {
4541 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4542 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004543 }
4544
Winson Chung80baf5a2010-08-09 16:03:15 -07004545 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004546 * Prints out out state for debugging.
4547 */
4548 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004549 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004550 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004551 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4552 Log.d(TAG, "mRestoring=" + mRestoring);
4553 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4554 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004555 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004556 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004557
Winson Chung785d2eb2011-04-14 16:08:02 -07004558 if (mAppsCustomizeContent != null) {
4559 mAppsCustomizeContent.dumpState();
4560 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004561 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004562 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004563
4564 @Override
4565 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4566 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004567 synchronized (sDumpLogs) {
4568 writer.println(" ");
4569 writer.println("Debug logs: ");
4570 for (int i = 0; i < sDumpLogs.size(); i++) {
4571 writer.println(" " + sDumpLogs.get(i));
4572 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004573 }
Adam Cohen9211d422014-10-07 18:14:53 -07004574 if (mLauncherCallbacks != null) {
4575 mLauncherCallbacks.dump(prefix, fd, writer, args);
4576 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004577 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004578
4579 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004580 if (DEBUG_DUMP_LOG) {
4581 synchronized (sDumpLogs) {
4582 Log.d(TAG, "");
4583 Log.d(TAG, "*********************");
4584 Log.d(TAG, "Launcher debug logs: ");
4585 for (int i = 0; i < sDumpLogs.size(); i++) {
4586 Log.d(TAG, " " + sDumpLogs.get(i));
4587 }
4588 Log.d(TAG, "*********************");
4589 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004590 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004591 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004592 }
4593
4594 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004595 addDumpLog(tag, log, null, debugLog);
4596 }
4597
4598 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004599 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004600 if (e != null) {
4601 Log.d(tag, log, e);
4602 } else {
4603 Log.d(tag, log);
4604 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004605 }
Winson Chungede41292013-09-19 16:27:36 -07004606 if (DEBUG_DUMP_LOG) {
4607 sDateStamp.setTime(System.currentTimeMillis());
4608 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004609 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4610 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004611 }
Winson Chungede41292013-09-19 16:27:36 -07004612 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004613 }
4614
Adam Cohen59400422014-03-05 18:07:04 -08004615 public static CustomAppWidget getCustomAppWidget(String name) {
4616 return sCustomAppWidgets.get(name);
4617 }
4618
4619 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4620 return sCustomAppWidgets;
4621 }
4622
Winson Chungede41292013-09-19 16:27:36 -07004623 public void dumpLogsToLocalData() {
4624 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004625 new AsyncTask<Void, Void, Void>() {
4626 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004627 boolean success = false;
4628 sDateStamp.setTime(sRunStart);
4629 String FILENAME = sDateStamp.getMonth() + "-"
4630 + sDateStamp.getDay() + "_"
4631 + sDateStamp.getHours() + "-"
4632 + sDateStamp.getMinutes() + "_"
4633 + sDateStamp.getSeconds() + ".txt";
4634
4635 FileOutputStream fos = null;
4636 File outFile = null;
4637 try {
4638 outFile = new File(getFilesDir(), FILENAME);
4639 outFile.createNewFile();
4640 fos = new FileOutputStream(outFile);
4641 } catch (Exception e) {
4642 e.printStackTrace();
4643 }
4644 if (fos != null) {
4645 PrintWriter writer = new PrintWriter(fos);
4646
4647 writer.println(" ");
4648 writer.println("Debug logs: ");
4649 synchronized (sDumpLogs) {
4650 for (int i = 0; i < sDumpLogs.size(); i++) {
4651 writer.println(" " + sDumpLogs.get(i));
4652 }
4653 }
4654 writer.close();
4655 }
4656 try {
4657 if (fos != null) {
4658 fos.close();
4659 success = true;
4660 }
4661 } catch (IOException e) {
4662 e.printStackTrace();
4663 }
Michael Jurka43467462013-11-14 12:03:58 +01004664 return null;
Winson Chungede41292013-09-19 16:27:36 -07004665 }
Michael Jurka43467462013-11-14 12:03:58 +01004666 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004667 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004669}
Michael Jurka2763be32011-02-24 11:19:57 -08004670
Michael Jurkaabded662011-03-04 12:06:57 -08004671interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004672 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004673 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004674 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004675 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004676 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004677}
Dan Sandlerd5024042014-01-09 15:01:33 -05004678
4679interface DebugIntents {
4680 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4681 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004682}