blob: cc63f3a4f8e5b84956eabcb9e5350924796403d4 [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 Cohen6c5891a2014-07-09 23:53:15 -0700104
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.DataInputStream;
106import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700107import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700108import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700110import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700112import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800113import java.lang.reflect.InvocationTargetException;
114import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700117import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700118import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700120import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700121import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000122import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124/**
125 * Default launcher application.
126 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100127public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700128 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800129 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
130 LauncherStateTransitionAnimation.Callbacks {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800131 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700132 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Daniel Sandlerf061f822013-06-27 13:59:36 -0400134 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400135 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700136 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400137 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700138 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700139
Dan Sandlerd5024042014-01-09 15:01:33 -0500140 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
141
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700143 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700144 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700145 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Michael Jurka8b805b12012-04-18 14:23:14 -0700147 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700148 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700149
Mathew Inwood876a8462013-06-14 14:12:41 +0100150 /**
151 * IntentStarter uses request codes starting with this. This must be greater than all activity
152 * request codes used internally.
153 */
154 protected static final int REQUEST_LAST = 100;
155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
157
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800158 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Michael Jurka0a457bf2012-11-19 14:05:05 -0800160 // To turn on these properties, type
161 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800162 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
Winson Chung2672ff92012-05-04 16:22:30 -0700164 // The Intent extra that defines whether to ignore the launch animation
165 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400166 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 // Type: int
169 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700170 // Type: int
171 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700173 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
174 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
176 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700177 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700179 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 // Type: boolean
181 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
182 // Type: long
183 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700184 // Type: int
185 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
186 // Type: int
187 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
188 // Type: parcelable
189 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000190 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800191 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000192 // Type: int[]
193 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Adam Cohen432609a2014-03-13 17:03:22 -0700195 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800196 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
197
Adam Cohen3ed316a2014-07-23 18:21:20 -0700198 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
199 static final String ACTION_FIRST_LOAD_COMPLETE =
200 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
201
Adam Cohen39a06042013-07-19 14:30:12 -0700202 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700203 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700204
Sunny Goyal594d76d2014-11-06 10:12:54 -0800205 private static final String QSB_WIDGET_ID = "qsb_widget_id";
206 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
207
Winson Chunga6945242014-01-08 14:04:34 -0800208 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
209
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700210 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800211 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700212 private State mState = State.WORKSPACE;
213 private AnimatorSet mStateAnimation;
Winson Chungb745afb2015-03-02 11:51:23 -0800214 private LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700215
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700216 private boolean mIsSafeModeEnabled;
217
Adam Cohenc2d6e892014-10-16 09:49:24 -0700218 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
219 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700220 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700221
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700223 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
224 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700225 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000227 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
228 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
229
Winson Chunga2413752012-04-03 14:22:34 -0700230 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700231 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
232 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700233 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700234
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800235 private final BroadcastReceiver mCloseSystemDialogsReceiver
236 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800237 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 private LayoutInflater mInflater;
240
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700242 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800243 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800244 private DragLayer mDragLayer;
245 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700246 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700247
Sunny Goyalffe83f12014-08-14 17:39:34 -0700248 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700249 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700250
Michael Jurkac9d95c52011-08-29 14:03:34 -0700251 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800252 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800253 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700254
Michael Jurka0280c3b2010-09-17 15:00:07 -0700255 private int[] mTmpAddItemCellCoordinates = new int[2];
256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private FolderInfo mFolderInfo;
258
Winson Chung3d503fb2011-07-13 17:25:49 -0700259 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800260 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700261
Michael Jurka838a4ca2011-02-07 13:33:06 -0800262 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700263
Winson Chungc51db6a2011-10-05 11:44:49 -0700264 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungb745afb2015-03-02 11:51:23 -0800265 private AppsContainerView mAppsView;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700266 private AppsCustomizeTabHost mAppsCustomizeTabHost;
267 private AppsCustomizePagedView mAppsCustomizeContent;
268 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800269 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700272 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
273 // scroll issues (because the workspace may not have been measured yet) and extra work.
274 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
275 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
277 private SpannableStringBuilder mDefaultKeySsb = null;
278
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279 private boolean mWorkspaceLoading = true;
280
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800281 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private boolean mRestoring;
283 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700284 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285
Michael Jurka1e2f4652013-07-08 18:03:46 -0700286 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700287 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
288
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 private Bundle mSavedInstanceState;
290
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800292 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800293 private boolean mUserPresent = true;
294 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700295 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800296 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400297
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700298 private static LocaleConfiguration sLocaleConfiguration = null;
299
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700300 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700301
Adam Cohen61f560d2013-09-30 15:58:20 -0700302 private View.OnTouchListener mHapticFeedbackTouchListener;
303
Adam Cohended9f8d2010-11-03 13:25:16 -0700304 // Related to the auto-advancing of widgets
305 private final int ADVANCE_MSG = 1;
306 private final int mAdvanceInterval = 20000;
307 private final int mAdvanceStagger = 250;
308 private long mAutoAdvanceSentTime;
309 private long mAutoAdvanceTimeLeft = -1;
310 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
311 new HashMap<View, AppWidgetProviderInfo>();
312
Winson Chung400438b2011-01-16 17:53:48 -0800313 // Determines how long to wait after a rotation before restoring the screen orientation to
314 // match the sensor state.
315 private final int mRestoreScreenOrientationDelay = 500;
316
Craig Mautner360310b2012-10-26 15:13:08 -0700317 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700318
Adam Cohen1462de32012-07-24 22:34:36 -0700319 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700320 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700321
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700322 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700323 static Date sDateStamp = new Date();
324 static DateFormat sDateFormat =
325 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
326 static long sRunStart = System.currentTimeMillis();
327 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700328
Winson Chung46353de2012-02-16 14:05:10 -0800329 // We only want to get the SharedPreferences once since it does an FS stat each time we get
330 // it from the context.
331 private SharedPreferences mSharedPrefs;
332
Winson Chungf0c6ae02012-03-21 16:10:31 -0700333 // Holds the page that we need to animate to, and the icon views that we need to animate up
334 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700335 private ImageView mFolderIconImageView;
336 private Bitmap mFolderIconBitmap;
337 private Canvas mFolderIconCanvas;
338 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700339
Michael Jurkaddd62e92011-02-16 17:49:14 -0800340 private BubbleTextView mWaitingForResume;
341
Adam Cohen59400422014-03-05 18:07:04 -0800342 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
343 new HashMap<String, CustomAppWidget>();
344
345 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
346 static {
347 if (ENABLE_CUSTOM_WIDGET_TEST) {
348 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
349 }
350 }
351
Chet Haasea8f996d2015-02-17 12:56:04 -0800352 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
353 private static Method sClipRevealMethod = null;
354 static {
355 Class<?> activityOptionsClass = ActivityOptions.class;
356 try {
357 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
358 View.class, int.class, int.class, int.class, int.class);
359 } catch (Exception e) {
360 // Earlier version
361 }
362 }
363
Michael Jurkac1f5d262011-09-30 19:32:27 -0700364 private Runnable mBuildLayersRunnable = new Runnable() {
365 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700366 if (mWorkspace != null) {
367 mWorkspace.buildPageHardwareLayers();
368 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700369 }
370 };
371
Adam Cohendb364c32014-05-20 14:23:40 -0700372 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800373
374 private static class PendingAddArguments {
375 int requestCode;
376 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700377 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700378 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800379 int cellX;
380 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700381 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800382 }
383
Daniel Sandlerff02d492013-08-05 02:12:05 -0400384 private Stats mStats;
385
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700386 FocusIndicatorView mFocusHandler;
387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 @Override
389 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700390 if (DEBUG_STRICT_MODE) {
391 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
392 .detectDiskReads()
393 .detectDiskWrites()
394 .detectNetwork() // or .detectAll() for all detectable problems
395 .penaltyLog()
396 .build());
397 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
398 .detectLeakedSqlLiteObjects()
399 .detectLeakedClosableObjects()
400 .penaltyLog()
401 .penaltyDeath()
402 .build());
403 }
404
Adam Cohen9211d422014-10-07 18:14:53 -0700405 if (mLauncherCallbacks != null) {
406 mLauncherCallbacks.preOnCreate();
407 }
408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400410
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400411 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400412 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700413 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700414
Winson Chung5f8afe62013-08-12 16:19:28 -0700415 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700416 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700417
418 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400419 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700420 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700421 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800422 mModel = app.setLauncher(this);
423 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700424 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400425 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800427 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700428
Daniel Sandlerff02d492013-08-05 02:12:05 -0400429 mStats = new Stats(this);
430
Sunny Goyalffe83f12014-08-14 17:39:34 -0700431 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700432
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
434 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700436 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
437 // this also ensures that any synchronous binding below doesn't re-trigger another
438 // LauncherModel load.
439 mPaused = false;
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200442 android.os.Debug.startMethodTracing(
443 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 }
445
446 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100450 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800452 registerContentObservers();
453
Joe Onorato7c312c12009-08-13 21:36:53 -0700454 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700455
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 mSavedState = savedInstanceState;
457 restoreState(mSavedState);
458
459 if (PROFILE_STARTUP) {
460 android.os.Debug.stopMethodTracing();
461 }
462
463 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700464 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 // If the user leaves launcher, then we should just load items asynchronously when
466 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500467 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700468 } else {
469 // We only load the page synchronously if the user rotates (or triggers a
470 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800471 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700472 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 }
474
475 // For handling default keys
476 mDefaultKeySsb = new SpannableStringBuilder();
477 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800478
479 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
480 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800481
Adam Cohenf9426d52012-06-04 17:26:21 -0700482 // On large interfaces, we want the screen to auto-rotate based on the current orientation
483 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700484
Adam Cohen9211d422014-10-07 18:14:53 -0700485 if (mLauncherCallbacks != null) {
486 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700487 if (mLauncherCallbacks.hasLauncherOverlay()) {
488 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700489 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
490 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
491 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700492 mWorkspace.setLauncherOverlay(mLauncherOverlay);
493 }
Adam Cohen9211d422014-10-07 18:14:53 -0700494 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700495
496 if (shouldShowIntroScreen()) {
497 showIntroScreen();
498 } else {
499 showFirstRunActivity();
500 showFirstRunClings();
501 }
Adam Cohen9211d422014-10-07 18:14:53 -0700502 }
503
504 private LauncherCallbacks mLauncherCallbacks;
505
506 public void onPostCreate(Bundle savedInstanceState) {
507 super.onPostCreate(savedInstanceState);
508 if (mLauncherCallbacks != null) {
509 mLauncherCallbacks.onPostCreate(savedInstanceState);
510 }
511 }
512
513 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
514 mLauncherCallbacks = callbacks;
515 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700516 }
517
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700518 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700519 public void onLauncherProviderChange() {
520 if (mLauncherCallbacks != null) {
521 mLauncherCallbacks.onLauncherProviderChange();
522 }
523 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700524
Adam Cohen9211d422014-10-07 18:14:53 -0700525 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700526 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700527 if (mLauncherCallbacks != null) {
528 return mLauncherCallbacks.hasCustomContentToLeft();
529 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700530 return false;
531 }
532
Dave Hawkeya8881582013-09-17 15:55:33 -0600533 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500534 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600535 * {@link #addToCustomContentPage}. This will only be invoked if
536 * {@link #hasCustomContentToLeft()} is {@code true}.
537 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500538 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700539 if (mLauncherCallbacks != null) {
540 mLauncherCallbacks.populateCustomContentContainer();
541 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600542 }
543
544 /**
545 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
546 * ensure the custom content page is added or removed if necessary.
547 */
548 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600549 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600550 // Not bound yet, wait for bindScreens to be called.
551 return;
552 }
553
554 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
555 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500556 mWorkspace.createCustomContentContainer();
557 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600558 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
559 mWorkspace.removeCustomContentPage();
560 }
561 }
562
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700564 if (sLocaleConfiguration == null) {
565 new AsyncTask<Void, Void, LocaleConfiguration>() {
566 @Override
567 protected LocaleConfiguration doInBackground(Void... unused) {
568 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
569 readConfiguration(Launcher.this, localeConfiguration);
570 return localeConfiguration;
571 }
572
573 @Override
574 protected void onPostExecute(LocaleConfiguration result) {
575 sLocaleConfiguration = result;
576 checkForLocaleChange(); // recursive, but now with a locale configuration
577 }
578 }.execute();
579 return;
580 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700581
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 final Configuration configuration = getResources().getConfiguration();
583
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700584 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 final String locale = configuration.locale.toString();
586
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700587 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 final int mcc = configuration.mcc;
589
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700590 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 final int mnc = configuration.mnc;
592
Romain Guy84f296c2009-11-04 15:00:44 -0800593 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594
Romain Guy84f296c2009-11-04 15:00:44 -0800595 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700596 sLocaleConfiguration.locale = locale;
597 sLocaleConfiguration.mcc = mcc;
598 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700599
Joe Onorato0589f0f2010-02-08 13:44:00 -0800600 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700601
602 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100603 new AsyncTask<Void, Void, Void>() {
604 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700605 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100606 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700607 }
Michael Jurka43467462013-11-14 12:03:58 +0100608 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700609 }
610 }
611
612 private static class LocaleConfiguration {
613 public String locale;
614 public int mcc = -1;
615 public int mnc = -1;
616 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700617
Romain Guy98d01652009-06-30 16:21:04 -0700618 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
619 DataInputStream in = null;
620 try {
Helena Josol28db2802014-10-09 17:04:09 +0100621 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700622 configuration.locale = in.readUTF();
623 configuration.mcc = in.readInt();
624 configuration.mnc = in.readInt();
625 } catch (FileNotFoundException e) {
626 // Ignore
627 } catch (IOException e) {
628 // Ignore
629 } finally {
630 if (in != null) {
631 try {
632 in.close();
633 } catch (IOException e) {
634 // Ignore
635 }
636 }
637 }
638 }
639
640 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
641 DataOutputStream out = null;
642 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100643 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100644 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700645 out.writeUTF(configuration.locale);
646 out.writeInt(configuration.mcc);
647 out.writeInt(configuration.mnc);
648 out.flush();
649 } catch (FileNotFoundException e) {
650 // Ignore
651 } catch (IOException e) {
652 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100653 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700654 } finally {
655 if (out != null) {
656 try {
657 out.close();
658 } catch (IOException e) {
659 // Ignore
660 }
661 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
663 }
664
Anton Hanssona2f665f2013-09-26 12:18:32 +0100665 public Stats getStats() {
666 return mStats;
667 }
668
Bjorn Bringertc459e522013-06-07 19:36:01 +0100669 public LayoutInflater getInflater() {
670 return mInflater;
671 }
672
Winson Chung36a62fe2012-05-06 18:04:42 -0700673 boolean isDraggingEnabled() {
674 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
675 // that is subsequently removed from the workspace in startBinding().
676 return !mModel.isLoadingWorkspace();
677 }
678
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800679 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000680 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100681 if (Build.VERSION.SDK_INT >= 17) {
682 return View.generateViewId();
683 } else {
684 // View.generateViewId() is not available. The following fallback logic is a copy
685 // of its implementation.
686 for (;;) {
687 final int result = sNextGeneratedId.get();
688 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
689 int newValue = result + 1;
690 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
691 if (sNextGeneratedId.compareAndSet(result, newValue)) {
692 return result;
693 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000694 }
695 }
696 }
697
698 public int getViewIdForItem(ItemInfo info) {
699 // This cast is safe given the > 2B range for int.
700 int itemId = (int) info.id;
701 if (mItemIdToViewId.containsKey(itemId)) {
702 return mItemIdToViewId.get(itemId);
703 }
704 int viewId = generateViewId();
705 mItemIdToViewId.put(itemId, viewId);
706 return viewId;
707 }
708
709 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700710 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
711 * a configuration step, this allows the proper animations to run after other transitions.
712 */
Adam Cohendb364c32014-05-20 14:23:40 -0700713 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700714 long screenId = args.screenId;
715 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
716 // When the screen id represents an actual screen (as opposed to a rank) we make sure
717 // that the drop page actually exists.
718 screenId = ensurePendingDropLayoutExists(args.screenId);
719 }
Adam Cohendb364c32014-05-20 14:23:40 -0700720
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800721 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800722 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700723 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700724 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700725 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800726 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700727 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700728 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700729 case REQUEST_RECONFIGURE_APPWIDGET:
730 completeRestoreAppWidget(args.appWidgetId);
731 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800732 }
Michael Jurka27614d22012-04-02 06:40:22 -0700733 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
734 // if you turned the screen off and then back while in All Apps, Launcher would not
735 // return to the workspace. Clearing mAddInfo.container here fixes this issue
736 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700737 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800738 }
739
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800740 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700742 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700743 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700744 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800745 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700746
Adam Cohenad4e15c2013-10-17 16:21:35 -0700747 Runnable exitSpringLoaded = new Runnable() {
748 @Override
749 public void run() {
750 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
751 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
752 }
753 };
754
Michael Jurka8b805b12012-04-18 14:23:14 -0700755 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700757 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
758 if (resultCode == RESULT_CANCELED) {
759 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700760 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700761 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700762 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800763 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 }
766 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200767 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
768 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
769 mWorkspace.exitOverviewMode(false);
770 }
771 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200773
Adam Cohened66b2b2012-01-23 17:28:51 -0800774 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
775 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700776
Adam Cohendb364c32014-05-20 14:23:40 -0700777 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800778 // We have special handling for widgets
779 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800780 final int appWidgetId;
781 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
782 : -1;
783 if (widgetId < 0) {
784 appWidgetId = pendingAddWidgetId;
785 } else {
786 appWidgetId = widgetId;
787 }
788
Adam Cohenad4e15c2013-10-17 16:21:35 -0700789 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800790 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700791 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
792 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700793 result = RESULT_CANCELED;
794 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700795 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700796 @Override
797 public void run() {
798 exitSpringLoadedDragModeDelayed(false, 0, null);
799 }
800 };
Adam Cohendb364c32014-05-20 14:23:40 -0700801 if (workspaceLocked) {
802 // No need to remove the empty screen if we're mid-binding, as the
803 // the bind will not add the empty screen.
804 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
805 } else {
806 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
807 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
808 }
Winson Chung5aaab772012-04-27 15:24:02 -0700809 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700810 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700811 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
812 // When the screen id represents an actual screen (as opposed to a rank)
813 // we make sure that the drop page actually exists.
814 mPendingAddInfo.screenId =
815 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
816 }
Adam Cohendb364c32014-05-20 14:23:40 -0700817 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
818
819 dropLayout.setDropPending(true);
820 final Runnable onComplete = new Runnable() {
821 @Override
822 public void run() {
823 completeTwoStageWidgetDrop(resultCode, appWidgetId);
824 dropLayout.setDropPending(false);
825 }
826 };
827 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
828 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
829 } else {
830 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
831 mPendingAddInfo);
832 sPendingAddItem = args;
833 }
Winson Chung5aaab772012-04-27 15:24:02 -0700834 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800835 return;
836 }
837
Sunny Goyalff572272014-07-23 13:58:07 -0700838 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
839 if (resultCode == RESULT_OK) {
840 // Update the widget view.
841 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
842 pendingAddWidgetId, mPendingAddInfo);
843 if (workspaceLocked) {
844 sPendingAddItem = args;
845 } else {
846 completeAdd(args);
847 }
848 }
849 // Leave the widget in the pending state if the user canceled the configure.
850 return;
851 }
852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 // The pattern used here is that a user PICKs a specific application,
854 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700855
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
857 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700858 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700859 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
860 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800861 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700862 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800863 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700864 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700865 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
866 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 }
Adam Cohen00074722013-10-11 17:46:09 -0700868 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700869 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700870 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800872 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800873
874 }
875
876 @Override
877 protected void onActivityResult(
878 final int requestCode, final int resultCode, final Intent data) {
879 handleActivityResult(requestCode, resultCode, data);
880 if (mLauncherCallbacks != null) {
881 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
882 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800883 }
884
Adam Cohendb364c32014-05-20 14:23:40 -0700885 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
886 appWidgetId, ItemInfo info) {
887 PendingAddArguments args = new PendingAddArguments();
888 args.requestCode = requestCode;
889 args.intent = data;
890 args.container = info.container;
891 args.screenId = info.screenId;
892 args.cellX = info.cellX;
893 args.cellY = info.cellY;
894 args.appWidgetId = appWidgetId;
895 return args;
896 }
897
898 /**
899 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
900 *
901 * @param screenId the screen id to check
902 * @return the new screen, or screenId if it exists
903 */
904 private long ensurePendingDropLayoutExists(long screenId) {
905 CellLayout dropLayout =
906 (CellLayout) mWorkspace.getScreenWithId(screenId);
907 if (dropLayout == null) {
908 // it's possible that the add screen was removed because it was
909 // empty and a re-bind occurred
910 mWorkspace.addExtraEmptyScreen();
911 return mWorkspace.commitExtraEmptyScreen();
912 } else {
913 return screenId;
914 }
915 }
916
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700918 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700919 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800920 Runnable onCompleteRunnable = null;
921 int animationType = 0;
922
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700923 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800924 if (resultCode == RESULT_OK) {
925 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
926 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700927 mPendingAddWidgetInfo);
928 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 onCompleteRunnable = new Runnable() {
930 @Override
931 public void run() {
932 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700933 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700934 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
935 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800936 }
937 };
938 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800939 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800940 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700942 if (mDragLayer.getAnimatedView() != null) {
943 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
944 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
945 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700946 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700947 // The animated view may be null in the case of a rotation during widget configuration
948 onCompleteRunnable.run();
949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 }
951
952 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700953 protected void onStop() {
954 super.onStop();
955 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700956
957 if (mLauncherCallbacks != null) {
958 mLauncherCallbacks.onStop();
959 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700960 }
961
962 @Override
963 protected void onStart() {
964 super.onStart();
965 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700966
967 if (mLauncherCallbacks != null) {
968 mLauncherCallbacks.onStart();
969 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700970 }
971
972 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200974 long startTime = 0;
975 if (DEBUG_RESUME_TIME) {
976 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400977 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200978 }
Adam Cohen9211d422014-10-07 18:14:53 -0700979
980 if (mLauncherCallbacks != null) {
981 mLauncherCallbacks.preOnResume();
982 }
983
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700985
Winson Chung4a2afa32012-07-19 14:53:05 -0700986 // Restore the previous launcher state
Winson Chungb745afb2015-03-02 11:51:23 -0800987 if (mOnResumeState == State.WORKSPACE || mOnResumeState == State.NONE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700988 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800989 } else if (mOnResumeState == State.APPS) {
990 showAppsView(false /* animated */, false /* resetListToTop */);
991 } else if (mOnResumeState == State.WIDGETS) {
992 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700993 }
994 mOnResumeState = State.NONE;
995
Craig Mautner360310b2012-10-26 15:13:08 -0700996 // Background was set to gradient in onPause(), restore to black if in all apps.
997 setWorkspaceBackground(mState == State.WORKSPACE);
998
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800999 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001000 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001001 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001002 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001004 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001006 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001007 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1008 // execute them here
1009 long startTimeCallbacks = 0;
1010 if (DEBUG_RESUME_TIME) {
1011 startTimeCallbacks = System.currentTimeMillis();
1012 }
1013
1014 if (mAppsCustomizeContent != null) {
1015 mAppsCustomizeContent.setBulkBind(true);
1016 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001017 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1018 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001019 }
1020 if (mAppsCustomizeContent != null) {
1021 mAppsCustomizeContent.setBulkBind(false);
1022 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001023 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001024 if (DEBUG_RESUME_TIME) {
1025 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1026 (System.currentTimeMillis() - startTimeCallbacks));
1027 }
Michael Jurka447bf842013-05-15 14:52:15 +02001028 }
Michael Jurka54554252013-08-01 12:52:23 +02001029 if (mOnResumeCallbacks.size() > 0) {
1030 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1031 mOnResumeCallbacks.get(i).run();
1032 }
1033 mOnResumeCallbacks.clear();
1034 }
Winson Chunge4e50662012-01-23 14:45:13 -08001035
1036 // Reset the pressed state of icons that were locked in the press state while activities
1037 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001038 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001039 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001040 mWaitingForResume.setStayPressed(false);
1041 }
Winson Chung82963d52013-10-09 11:20:57 -07001042
Adam Cohen06dff352012-06-01 17:17:08 -07001043 // It is possible that widgets can receive updates while launcher is not in the foreground.
1044 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1045 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1046 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001047 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001048
Winson Chung780fe592013-09-26 14:48:44 -07001049 // Process any items that were added while Launcher was away.
1050 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1051
Michael Jurka447bf842013-05-15 14:52:15 +02001052 if (DEBUG_RESUME_TIME) {
1053 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1054 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001055
1056 if (mWorkspace.getCustomContentCallbacks() != null) {
1057 // If we are resuming and the custom content is the current page, we call onShow().
1058 // It is also poassible that onShow will instead be called slightly after first layout
1059 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1060 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001061 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001062 }
1063 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001064 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001065 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001066
1067 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001068
1069 if (mLauncherCallbacks != null) {
1070 mLauncherCallbacks.onResume();
1071 }
Adam Cohen06dff352012-06-01 17:17:08 -07001072 }
1073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001075 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001076 // Ensure that items added to Launcher are queued until Launcher returns
1077 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001078 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001079
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001080 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001081 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001082 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001083 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001084
1085 // We call onHide() aggressively. The custom content callbacks should be able to
1086 // debounce excess onHide calls.
1087 if (mWorkspace.getCustomContentCallbacks() != null) {
1088 mWorkspace.getCustomContentCallbacks().onHide();
1089 }
Romain Guycbb89e42009-06-08 15:52:54 -07001090
Adam Cohen9211d422014-10-07 18:14:53 -07001091 if (mLauncherCallbacks != null) {
1092 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001093 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001094 }
1095
1096 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001097 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1098 // by a onResume or by scrolling otherwise.
1099 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001100
1101 // Custom content is completely hidden
1102 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001103
1104 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1105 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001106
1107 // Indicates whether the user is allowed to scroll away from the custom content.
1108 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001109 }
1110
Adam Cohenc2d6e892014-10-16 09:49:24 -07001111 public interface LauncherOverlay {
1112
1113 /**
1114 * Touch interaction leading to overscroll has begun
1115 */
1116 public void onScrollInteractionBegin();
1117
1118 /**
1119 * Touch interaction related to overscroll has ended
1120 */
1121 public void onScrollInteractionEnd();
1122
1123 /**
1124 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1125 * screen (or in the case of RTL, the rightmost screen).
1126 */
1127 public void onScrollChange(int progress, boolean rtl);
1128
1129 /**
1130 * Screen has stopped scrolling
1131 */
1132 public void onScrollSettled();
1133
1134 /**
1135 * This method can be called by the Launcher in order to force the LauncherOverlay
1136 * to exit fully immersive mode.
1137 */
1138 public void forceExitFullImmersion();
1139 }
1140
1141 public interface LauncherOverlayCallbacks {
1142 /**
1143 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1144 * however it doesn't modify any state within the launcher.
1145 */
1146 public boolean canEnterFullImmersion();
1147
1148 /**
1149 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1150 * eg. by occupying the full screen and handling all touch events.
1151 *
1152 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1153 * case, Launcher will modify any necessary state and assumes the overlay is
1154 * handling all interaction. If false, the LauncherOverlay should cancel any
1155 *
1156 */
1157 public boolean enterFullImmersion();
1158
1159 /**
1160 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1161 * full control over UI and state.
1162 */
1163 public void exitFullImmersion();
1164 }
1165
1166 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1167
1168 @Override
1169 public boolean canEnterFullImmersion() {
1170 return mState == State.WORKSPACE;
1171 }
1172
1173 @Override
1174 public boolean enterFullImmersion() {
1175 if (mState == State.WORKSPACE) {
1176 // When fully immersed, disregard any touches which fall through.
1177 mDragLayer.setBlockTouch(true);
1178 return true;
1179 }
1180 return false;
1181 }
1182
1183 @Override
1184 public void exitFullImmersion() {
1185 mDragLayer.setBlockTouch(false);
1186 }
1187 }
1188
Jorim Jaggid017f882014-01-14 17:08:48 -08001189 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001190 if (mLauncherCallbacks != null) {
1191 return mLauncherCallbacks.hasSettings();
1192 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001193 return false;
1194 }
1195
Adam Cohenbffe7452013-07-22 18:21:45 -07001196
Adam Cohen9211d422014-10-07 18:14:53 -07001197 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001198 CustomContentCallbacks callbacks, String description) {
1199 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001200 }
1201
Adam Cohenedb40762013-07-18 16:45:45 -07001202 // The custom content needs to offset its content to account for the QSB
1203 public int getTopOffsetForCustomContent() {
1204 return mWorkspace.getPaddingTop();
1205 }
1206
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001207 @Override
1208 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001209 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001210 if (mModel.isCurrentCallbacks(this)) {
1211 mModel.stopLoader();
1212 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001213 if (mAppsCustomizeContent != null) {
1214 mAppsCustomizeContent.surrender();
1215 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001216 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001217 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001218
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001219 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220 @Override
1221 public void onWindowFocusChanged(boolean hasFocus) {
1222 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001223 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001224
1225 if (mLauncherCallbacks != null) {
1226 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1227 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001228 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 private boolean acceptFilter() {
1231 final InputMethodManager inputManager = (InputMethodManager)
1232 getSystemService(Context.INPUT_METHOD_SERVICE);
1233 return !inputManager.isFullscreenMode();
1234 }
1235
1236 @Override
1237 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001238 final int uniChar = event.getUnicodeChar();
1239 final boolean handled = super.onKeyDown(keyCode, event);
1240 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1241 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1243 keyCode, event);
1244 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001245 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001246 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001247 // showSearchDialog()
1248 // If there are multiple keystrokes before the search dialog takes focus,
1249 // onSearchRequested() will be called for every keystroke,
1250 // but it is idempotent, so it's fine.
1251 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 }
1253 }
1254
Joe Onorato8a9625e2010-01-28 15:55:35 -08001255 // Eat the long press event so the keyboard doesn't come up.
1256 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1257 return true;
1258 }
1259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 return handled;
1261 }
1262
Karl Rosaen138a0412009-04-23 19:00:21 -07001263 private String getTypedText() {
1264 return mDefaultKeySsb.toString();
1265 }
1266
1267 private void clearTypedText() {
1268 mDefaultKeySsb.clear();
1269 mDefaultKeySsb.clearSpans();
1270 Selection.setSelection(mDefaultKeySsb, 0);
1271 }
1272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001274 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1275 * State
1276 */
1277 private static State intToState(int stateOrdinal) {
1278 State state = State.WORKSPACE;
1279 final State[] stateValues = State.values();
1280 for (int i = 0; i < stateValues.length; i++) {
1281 if (stateValues[i].ordinal() == stateOrdinal) {
1282 state = stateValues[i];
1283 break;
1284 }
1285 }
1286 return state;
1287 }
1288
1289 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 * Restores the previous state, if it exists.
1291 *
1292 * @param savedState The previous state.
1293 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001294 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 private void restoreState(Bundle savedState) {
1296 if (savedState == null) {
1297 return;
1298 }
1299
Michael Jurka883f55b2010-10-21 15:47:14 -07001300 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001301 if (state == State.APPS || state == State.WIDGETS) {
1302 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001303 }
1304
Adam Cohen21cd0022013-10-09 18:57:02 -07001305 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1306 PagedView.INVALID_RESTORE_PAGE);
1307 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001308 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 }
1310
Winson Chung3d503fb2011-07-13 17:25:49 -07001311 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001312 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001313
Winson Chung3d503fb2011-07-13 17:25:49 -07001314 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1315 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001316 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001317 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1318 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001319 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1320 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1321 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001322 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001323 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 mRestoring = true;
1325 }
1326
1327 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1328 if (renameFolder) {
1329 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001330 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 mRestoring = true;
1332 }
Winson Chunga12a2502010-12-20 14:41:35 -08001333
Winson Chung785d2eb2011-04-14 16:08:02 -07001334 // Restore the AppsCustomize tab
1335 if (mAppsCustomizeTabHost != null) {
1336 String curTab = savedState.getString("apps_customize_currentTab");
1337 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001338 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001339 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001340 mAppsCustomizeContent.loadAssociatedPages(
1341 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001342 }
1343
Winson Chung5afbf7b2011-07-25 11:53:08 -07001344 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1345 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001346 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001347 mItemIdToViewId = (HashMap<Integer, Integer>)
1348 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 }
1350
1351 /**
1352 * Finds all the views we need and configure them properly.
1353 */
1354 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001355 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001356
Craig Mautner360310b2012-10-26 15:13:08 -07001357 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001358 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001359 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1360 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001361 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001362 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001363
John Spurlock77e1f472013-09-11 10:09:51 -04001364 mLauncherView.setSystemUiVisibility(
1365 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001366 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367
Winson Chung4c98d922011-05-31 16:50:48 -07001368 // Setup the drag layer
1369 mDragLayer.setup(this, dragController);
1370
Winson Chung3d503fb2011-07-13 17:25:49 -07001371 // Setup the hotseat
1372 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1373 if (mHotseat != null) {
1374 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001375 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001376 }
1377
Jorim Jaggid017f882014-01-14 17:08:48 -08001378 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001379 View widgetButton = findViewById(R.id.widget_button);
1380 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001381 @Override
1382 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001383 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001384 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001385 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001386 }
1387 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001388 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1389
1390 View wallpaperButton = findViewById(R.id.wallpaper_button);
1391 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001392 @Override
1393 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001394 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001395 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001396 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001397 }
1398 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001399 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1400
1401 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001402 if (hasSettings()) {
1403 settingsButton.setOnClickListener(new OnClickListener() {
1404 @Override
1405 public void onClick(View arg0) {
1406 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001407 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001408 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001409 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001410 });
1411 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1412 } else {
1413 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001414 }
1415
Adam Cohendbdff6b2013-09-18 19:09:15 -07001416 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001417
Winson Chung4c98d922011-05-31 16:50:48 -07001418 // Setup the workspace
1419 mWorkspace.setHapticFeedbackEnabled(false);
1420 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001421 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001422 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001423
Winson Chungf0ea4d32011-06-06 14:27:16 -07001424 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001425 mSearchDropTargetBar = (SearchDropTargetBar)
1426 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001427
Winson Chungb745afb2015-03-02 11:51:23 -08001428 // Setup Apps
1429 mAppsView = (AppsContainerView) findViewById(R.id.apps_view);
1430
Winson Chungf0ea4d32011-06-06 14:27:16 -07001431 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001432 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001433 mAppsCustomizeContent = (AppsCustomizePagedView)
1434 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1435 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001436
Winson Chung3d503fb2011-07-13 17:25:49 -07001437 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001438 dragController.setDragScoller(mWorkspace);
1439 dragController.setScrollView(mDragLayer);
1440 dragController.setMoveTarget(mWorkspace);
1441 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001442 if (mSearchDropTargetBar != null) {
1443 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001444 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001445 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001446
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001447 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001448 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001449 mWeightWatcher = new WeightWatcher(this);
1450 mWeightWatcher.setAlpha(0.5f);
1451 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001452 new FrameLayout.LayoutParams(
1453 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001454 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001455 Gravity.BOTTOM)
1456 );
Adam Cohen39a06042013-07-19 14:30:12 -07001457
1458 boolean show = shouldShowWeightWatcher();
1459 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 }
1462
1463 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001464 * Sets the all apps button. This method is called from {@link Hotseat}.
1465 */
1466 public void setAllAppsButton(View allAppsButton) {
1467 mAllAppsButton = allAppsButton;
1468 }
1469
1470 public View getAllAppsButton() {
1471 return mAllAppsButton;
1472 }
1473
1474 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 * Creates a view representing a shortcut.
1476 *
1477 * @param info The data structure describing the shortcut.
1478 *
1479 * @return A View inflated from R.layout.application.
1480 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001481 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001483 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 }
1485
1486 /**
1487 * Creates a view representing a shortcut inflated from the specified resource.
1488 *
1489 * @param layoutResId The id of the XML layout used to create the shortcut.
1490 * @param parent The group the shortcut belongs to.
1491 * @param info The data structure describing the shortcut.
1492 *
1493 * @return A View inflated from layoutResId.
1494 */
Adam Cohen59400422014-03-05 18:07:04 -08001495 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001496 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001497 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001499 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 return favorite;
1501 }
1502
1503 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 * Add a shortcut to the workspace.
1505 *
1506 * @param data The intent describing the shortcut.
1507 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001509 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001510 int cellY) {
1511 int[] cellXY = mTmpAddItemCellCoordinates;
1512 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001513 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001514
Michael Jurkad3ef3062010-11-23 16:23:58 -08001515 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001516
Sunny Goyal2350bc92014-10-14 16:42:54 -07001517 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001518 if (info == null) {
1519 return;
1520 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001521 final View view = createShortcut(info);
1522
Adam Cohenfbba09b2011-07-18 21:43:05 -07001523 // First we check if we already know the exact location where we want to add this item.
1524 if (cellX >= 0 && cellY >= 0) {
1525 cellXY[0] = cellX;
1526 cellXY[1] = cellY;
1527 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001528
1529 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001530 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001531 true, null,null)) {
1532 return;
1533 }
1534 DragObject dragObject = new DragObject();
1535 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001536 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1537 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001538 return;
1539 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001540 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001541 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001542 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001543 foundCellSpan = (result != null);
1544 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001545 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001546 }
1547
1548 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001549 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001550 return;
1551 }
1552
Adam Cohendcd297f2013-06-18 13:13:40 -07001553 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554
1555 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001556 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001557 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 }
1559 }
1560
Adam Cohen2f093b62012-04-30 18:59:53 -07001561 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1562 int minHeight) {
1563 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001564 // We want to account for the extra amount of padding that we are adding to the widget
1565 // to ensure that it gets the full amount of space that it has requested
1566 int requiredWidth = minWidth + padding.left + padding.right;
1567 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001568 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001569 }
1570
Adam Cohen2f093b62012-04-30 18:59:53 -07001571 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1572 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001573 }
1574
Adam Cohen2f093b62012-04-30 18:59:53 -07001575 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1576 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001577 }
1578
Adam Cohen2f093b62012-04-30 18:59:53 -07001579 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1580 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001581 }
1582
Adam Cohen2f093b62012-04-30 18:59:53 -07001583 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1584 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001585 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001586 }
1587
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001589 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001591 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 */
Adam Cohen59400422014-03-05 18:07:04 -08001593 private void completeAddAppWidget(int appWidgetId, long container, long screenId,
1594 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1595
1596 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001597 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001598 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1599 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001600 }
Romain Guycbb89e42009-06-08 15:52:54 -07001601
Adam Cohen59400422014-03-05 18:07:04 -08001602 if (appWidgetInfo.isCustomWidget) {
1603 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001604 }
1605
Adam Cohen59400422014-03-05 18:07:04 -08001606 LauncherAppWidgetInfo launcherInfo;
1607 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1608 launcherInfo.spanX = info.spanX;
1609 launcherInfo.spanY = info.spanY;
1610 launcherInfo.minSpanX = info.minSpanX;
1611 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001612 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001613
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001615 container, screenId, info.cellX, info.cellY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616
1617 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001618 if (hostView == null) {
1619 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001620 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1621 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001622 } else {
1623 // The AppWidgetHostView has already been inflated and instantiated
1624 launcherInfo.hostView = hostView;
1625 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001627 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001628 launcherInfo.notifyWidgetSizeChanged(this);
1629
Adam Cohen59400422014-03-05 18:07:04 -08001630 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1631 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001632
1633 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001635 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 }
Romain Guycbb89e42009-06-08 15:52:54 -07001637
Adam Cohended9f8d2010-11-03 13:25:16 -07001638 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1639 @Override
1640 public void onReceive(Context context, Intent intent) {
1641 final String action = intent.getAction();
1642 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1643 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001644 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001645 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001646
Winson Chungc100e8e2011-08-09 16:02:43 -07001647 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001648 // processing a multi-step drop
Winson Chungb745afb2015-03-02 11:51:23 -08001649 if (mAppsView != null && mAppsCustomizeTabHost != null &&
1650 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001651 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001652 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001653 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1654 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001655 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001656 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1657 mModel.resetLoadedState(false, true);
1658 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1659 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1660 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1661 mModel.resetLoadedState(false, true);
1662 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1663 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1664 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001665 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1666 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1667 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001668 }
1669 }
1670 };
1671
1672 @Override
1673 public void onAttachedToWindow() {
1674 super.onAttachedToWindow();
1675
1676 // Listen for broadcasts related to user-presence
1677 final IntentFilter filter = new IntentFilter();
1678 filter.addAction(Intent.ACTION_SCREEN_OFF);
1679 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001680 // For handling managed profiles
1681 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1682 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001683 if (ENABLE_DEBUG_INTENTS) {
1684 filter.addAction(DebugIntents.DELETE_DATABASE);
1685 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1686 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001687 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001688 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001689 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001690 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001691 mVisible = true;
1692 }
1693
Adam Cohen0b395352014-06-09 22:54:36 +00001694 /**
1695 * Sets up transparent navigation and status bars in LMP.
1696 * This method is a no-op for other platform versions.
1697 */
Sunny Goyald0091012015-01-20 15:55:34 -08001698 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001699 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001700 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001701 Window window = getWindow();
1702 window.getAttributes().systemUiVisibility |=
1703 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1704 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1705 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1706 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1707 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1708 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1709 window.setStatusBarColor(Color.TRANSPARENT);
1710 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001711 }
1712 }
1713
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 @Override
1715 public void onDetachedFromWindow() {
1716 super.onDetachedFromWindow();
1717 mVisible = false;
1718
Adam Cohend113e0c2010-11-11 10:48:05 -08001719 if (mAttached) {
1720 unregisterReceiver(mReceiver);
1721 mAttached = false;
1722 }
Winson Chungb745afb2015-03-02 11:51:23 -08001723 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 }
1725
1726 public void onWindowVisibilityChanged(int visibility) {
1727 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001728 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001729 // The following code used to be in onResume, but it turns out onResume is called when
1730 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1731 // is a more appropriate event to handle
1732 if (mVisible) {
1733 mAppsCustomizeTabHost.onWindowVisible();
1734 if (!mWorkspaceLoading) {
1735 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001736 // We want to let Launcher draw itself at least once before we force it to build
1737 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001738 // apps is nice and speedy.
1739 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001740 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001741 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001742 if (mStarted) return;
1743 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001744 // We delay the layer building a bit in order to give
1745 // other message processing a time to run. In particular
1746 // this avoids a delay in hiding the IME if it was
1747 // currently shown, because doing that may involve
1748 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001749 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001750 final ViewTreeObserver.OnDrawListener listener = this;
1751 mWorkspace.post(new Runnable() {
1752 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001753 if (mWorkspace != null &&
1754 mWorkspace.getViewTreeObserver() != null) {
1755 mWorkspace.getViewTreeObserver().
1756 removeOnDrawListener(listener);
1757 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001758 }
1759 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001760 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001761 }
1762 });
1763 }
1764 clearTypedText();
1765 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001766 }
1767
1768 private void sendAdvanceMessage(long delay) {
1769 mHandler.removeMessages(ADVANCE_MSG);
1770 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1771 mHandler.sendMessageDelayed(msg, delay);
1772 mAutoAdvanceSentTime = System.currentTimeMillis();
1773 }
1774
Winson Chungb745afb2015-03-02 11:51:23 -08001775 private void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1777 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1778 mAutoAdvanceRunning = autoAdvanceRunning;
1779 if (autoAdvanceRunning) {
1780 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1781 sendAdvanceMessage(delay);
1782 } else {
1783 if (!mWidgetsToAdvance.isEmpty()) {
1784 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1785 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1786 }
1787 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001788 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001789 }
1790 }
1791 }
1792
1793 private final Handler mHandler = new Handler() {
1794 @Override
1795 public void handleMessage(Message msg) {
1796 if (msg.what == ADVANCE_MSG) {
1797 int i = 0;
1798 for (View key: mWidgetsToAdvance.keySet()) {
1799 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1800 final int delay = mAdvanceStagger * i;
1801 if (v instanceof Advanceable) {
1802 postDelayed(new Runnable() {
1803 public void run() {
1804 ((Advanceable) v).advance();
1805 }
1806 }, delay);
1807 }
1808 i++;
1809 }
1810 sendAdvanceMessage(mAdvanceInterval);
1811 }
1812 }
1813 };
1814
1815 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001816 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001817 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1818 if (v instanceof Advanceable) {
1819 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001820 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001821 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001822 }
1823 }
1824
1825 void removeWidgetToAutoAdvance(View hostView) {
1826 if (mWidgetsToAdvance.containsKey(hostView)) {
1827 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001828 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001829 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001830 }
1831
Joe Onorato9c1289c2009-08-17 11:03:03 -04001832 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001833 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001834 launcherInfo.hostView = null;
1835 }
1836
Winson Chung93eef082012-03-23 15:59:27 -07001837 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1838 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1839 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001840 }
1841
Winson Chunga6945242014-01-08 14:04:34 -08001842 public DragLayer getDragLayer() {
1843 return mDragLayer;
1844 }
1845
Winson Chungb745afb2015-03-02 11:51:23 -08001846 public AppsContainerView getAppsView() {
1847 return mAppsView;
1848 }
1849
1850 public AppsCustomizeTabHost getWidgetsView() {
1851 return mAppsCustomizeTabHost;
1852 }
1853
Winson Chunga6945242014-01-08 14:04:34 -08001854 public Workspace getWorkspace() {
1855 return mWorkspace;
1856 }
1857
1858 public Hotseat getHotseat() {
1859 return mHotseat;
1860 }
1861
Jorim Jaggid017f882014-01-14 17:08:48 -08001862 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001863 return mOverviewPanel;
1864 }
1865
1866 public SearchDropTargetBar getSearchBar() {
1867 return mSearchDropTargetBar;
1868 }
1869
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001870 public LauncherAppWidgetHost getAppWidgetHost() {
1871 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 }
Romain Guycbb89e42009-06-08 15:52:54 -07001873
Winson Chunga9abd0e2010-10-27 17:18:37 -07001874 public LauncherModel getModel() {
1875 return mModel;
1876 }
1877
Winson Chunga6945242014-01-08 14:04:34 -08001878 protected SharedPreferences getSharedPrefs() {
1879 return mSharedPrefs;
1880 }
1881
Bjorn Bringertc459e522013-06-07 19:36:01 +01001882 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001883 getWindow().closeAllPanels();
1884
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001885 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001886 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001887 }
1888
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 @Override
1890 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001891 long startTime = 0;
1892 if (DEBUG_RESUME_TIME) {
1893 startTime = System.currentTimeMillis();
1894 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 super.onNewIntent(intent);
1896
1897 // Close the menu
1898 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001899 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001900 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001901
Adam Cohened307df2013-10-02 09:37:31 -07001902 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1903 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1904 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001905
Adam Cohen6fecd412013-10-02 17:41:50 -07001906 if (mWorkspace == null) {
1907 // Can be cases where mWorkspace is null, this prevents a NPE
1908 return;
1909 }
1910 Folder openFolder = mWorkspace.getOpenFolder();
1911 // In all these cases, only animate if we're already on home
1912 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001913
1914 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1915 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001916 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001917 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001918 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001919 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001920
Adam Cohen6fecd412013-10-02 17:41:50 -07001921 closeFolder();
1922 exitSpringLoadedDragMode();
1923
1924 // If we are already on home, then just animate back to the workspace,
1925 // otherwise, just wait until onResume to set the state back to Workspace
1926 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001927 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001928 } else {
1929 mOnResumeState = State.WORKSPACE;
1930 }
1931
1932 final View v = getWindow().peekDecorView();
1933 if (v != null && v.getWindowToken() != null) {
1934 InputMethodManager imm = (InputMethodManager)getSystemService(
1935 INPUT_METHOD_SERVICE);
1936 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1937 }
1938
Winson Chungb745afb2015-03-02 11:51:23 -08001939 // Reset the apps view
1940 if (!alreadyOnHome && mAppsView != null) {
1941 mAppsView.scrollToTop();
1942 }
1943
Adam Cohen6fecd412013-10-02 17:41:50 -07001944 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001945 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001946 mAppsCustomizeTabHost.reset();
1947 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001948
Adam Cohen9211d422014-10-07 18:14:53 -07001949 if (mLauncherCallbacks != null) {
1950 mLauncherCallbacks.onHomeIntent();
1951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 }
Adam Cohened307df2013-10-02 09:37:31 -07001953
Michael Jurka447bf842013-05-15 14:52:15 +02001954 if (DEBUG_RESUME_TIME) {
1955 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1956 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957
Adam Cohen9211d422014-10-07 18:14:53 -07001958 if (mLauncherCallbacks != null) {
1959 mLauncherCallbacks.onNewIntent(intent);
1960 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001961 }
1962
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001964 public void onRestoreInstanceState(Bundle state) {
1965 super.onRestoreInstanceState(state);
1966 for (int page: mSynchronouslyBoundPages) {
1967 mWorkspace.restoreInstanceStateForChild(page);
1968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
1970
1971 @Override
1972 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001973 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001974 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1975 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001976 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001977 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978
Michael Jurka883f55b2010-10-21 15:47:14 -07001979 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001980 // We close any open folder since it will not be re-opened, and we need to make sure
1981 // this state is reflected.
1982 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983
Adam Cohendcd297f2013-06-18 13:13:40 -07001984 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001985 mWaitingForResult) {
1986 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001988 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1989 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001990 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1991 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1992 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001993 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 }
1995
1996 if (mFolderInfo != null && mWaitingForResult) {
1997 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1998 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1999 }
Michael Jurka946ad472010-07-09 18:05:18 -07002000
Winson Chung785d2eb2011-04-14 16:08:02 -07002001 // Save the current AppsCustomize tab
2002 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08002003 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2004 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002005 if (currentTabTag != null) {
2006 outState.putString("apps_customize_currentTab", currentTabTag);
2007 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002008 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2009 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002010 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002011 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002012
2013 if (mLauncherCallbacks != null) {
2014 mLauncherCallbacks.onSaveInstanceState(outState);
2015 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 }
2017
2018 @Override
2019 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002021
Winson Chunge7a03942011-08-05 15:05:12 -07002022 // Remove all pending runnables
2023 mHandler.removeMessages(ADVANCE_MSG);
2024 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002025 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002026
Winson Chungcd2b0142011-06-08 16:02:26 -07002027 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002028 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002029
2030 // It's possible to receive onDestroy after a new Launcher activity has
2031 // been created. In this case, don't interfere with the new Launcher.
2032 if (mModel.isCurrentCallbacks(this)) {
2033 mModel.stopLoader();
2034 app.setLauncher(null);
2035 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002038 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002040 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002042 mAppWidgetHost = null;
2043
2044 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045
2046 TextKeyListener.getInstance().release();
2047
Winson Chung81b52252012-08-27 15:34:29 -07002048 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2049 // to prevent leaking Launcher activities on orientation change.
2050 if (mModel != null) {
2051 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2052 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002053
2054 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002055 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002056
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002057 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002058 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002059 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002060 mWorkspace = null;
2061 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002062
Michael Jurka2ecf9952012-06-18 12:52:28 -07002063 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002064
2065 if (mLauncherCallbacks != null) {
2066 mLauncherCallbacks.onDestroy();
2067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 }
2069
Adam Cohena9cf38f2011-05-02 15:36:58 -07002070 public DragController getDragController() {
2071 return mDragController;
2072 }
2073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 @Override
2075 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002076 if (requestCode >= 0) {
2077 setWaitingForResult(true);
2078 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 super.startActivityForResult(intent, requestCode);
2080 }
2081
Winson Chung70d72102011-08-12 11:24:35 -07002082 /**
2083 * Indicates that we want global search for this activity by setting the globalSearch
2084 * argument for {@link #startSearch} to true.
2085 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002087 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002089
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002090 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002091
Karl Rosaen138a0412009-04-23 19:00:21 -07002092 if (initialQuery == null) {
2093 // Use any text typed in the launcher as the initial query
2094 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002095 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 if (appSearchData == null) {
2097 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002098 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 }
Winson Chungadf0c182012-08-23 14:59:07 -07002100 Rect sourceBounds = new Rect();
2101 if (mSearchDropTargetBar != null) {
2102 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2103 }
Romain Guycbb89e42009-06-08 15:52:54 -07002104
Ian Parkinson047036e2014-09-01 15:40:53 +01002105 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002106 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002107 if (clearTextImmediately) {
2108 clearTypedText();
2109 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002110 }
2111
Ian Parkinson047036e2014-09-01 15:40:53 +01002112 /**
2113 * Start a text search.
2114 *
2115 * @return {@code true} if the search will start immediately, so any further keypresses
2116 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2117 * to buffer keypresses.
2118 */
2119 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002120 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002121 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2122 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2123 sourceBounds);
2124 }
2125
Michael Jurkaa33411c2012-06-14 16:18:21 -07002126 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002127 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002128 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002129 }
2130
2131 /**
2132 * Starts the global search activity. This code is a copied from SearchManager
2133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002134 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002135 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002136 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002137 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2138 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2139 if (globalSearchActivity == null) {
2140 Log.w(TAG, "No global search activity found.");
2141 return;
2142 }
2143 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2144 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2145 intent.setComponent(globalSearchActivity);
2146 // Make sure that we have a Bundle to put source in
2147 if (appSearchData == null) {
2148 appSearchData = new Bundle();
2149 } else {
2150 appSearchData = new Bundle(appSearchData);
2151 }
Adam Cohen9211d422014-10-07 18:14:53 -07002152 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002153 if (!appSearchData.containsKey("source")) {
2154 appSearchData.putString("source", getPackageName());
2155 }
2156 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2157 if (!TextUtils.isEmpty(initialQuery)) {
2158 intent.putExtra(SearchManager.QUERY, initialQuery);
2159 }
2160 if (selectInitialQuery) {
2161 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2162 }
2163 intent.setSourceBounds(sourceBounds);
2164 try {
2165 startActivity(intent);
2166 } catch (ActivityNotFoundException ex) {
2167 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 }
2170
Yura4f93ec62014-02-04 14:15:21 +00002171 public boolean isOnCustomContent() {
2172 return mWorkspace.isOnOrMovingToCustomContent();
2173 }
2174
Winson Chung70d72102011-08-12 11:24:35 -07002175 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002176 public boolean onPrepareOptionsMenu(Menu menu) {
2177 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002178 if (!isOnCustomContent()) {
2179 // Close any open folders
2180 closeFolder();
2181 // Stop resizing any widgets
2182 mWorkspace.exitWidgetResizeMode();
2183 if (!mWorkspace.isInOverviewMode()) {
2184 // Show the overview mode
2185 showOverviewMode(true);
2186 } else {
2187 showWorkspace(true);
2188 }
Winson Chunge0298742014-01-17 12:03:00 -08002189 }
Adam Cohen9211d422014-10-07 18:14:53 -07002190 if (mLauncherCallbacks != null) {
2191 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2192 }
2193
Michael Jurkab94f3f82013-09-11 18:08:54 +02002194 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002195 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002197 @Override
2198 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002199 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002200 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002201 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002202 }
2203
Joe Onorato9c1289c2009-08-17 11:03:03 -04002204 public boolean isWorkspaceLocked() {
2205 return mWorkspaceLoading || mWaitingForResult;
2206 }
2207
Adam Cohen517a7f52014-03-01 12:12:59 -08002208 public boolean isWorkspaceLoading() {
2209 return mWorkspaceLoading;
2210 }
2211
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002212 private void setWorkspaceLoading(boolean value) {
2213 boolean isLocked = isWorkspaceLocked();
2214 mWorkspaceLoading = value;
2215 if (isLocked != isWorkspaceLocked()) {
2216 onWorkspaceLockedChanged();
2217 }
2218 }
2219
2220 private void setWaitingForResult(boolean value) {
2221 boolean isLocked = isWorkspaceLocked();
2222 mWaitingForResult = value;
2223 if (isLocked != isWorkspaceLocked()) {
2224 onWorkspaceLockedChanged();
2225 }
2226 }
2227
Adam Cohen9211d422014-10-07 18:14:53 -07002228 protected void onWorkspaceLockedChanged() {
2229 if (mLauncherCallbacks != null) {
2230 mLauncherCallbacks.onWorkspaceLockedChanged();
2231 }
2232 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002233
Michael Jurka0280c3b2010-09-17 15:00:07 -07002234 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002235 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002236 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002237 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2238 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002239 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002240 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002241 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002242
Sunny Goyale6b63a32015-01-16 16:14:29 -08002243 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002244 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002245 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2246 }
2247
Sunny Goyale6b63a32015-01-16 16:14:29 -08002248 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002249 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2250 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002251 if (appWidgetInfo.configure != null) {
2252 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002253 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002254
Bjorn Bringert7984c942009-12-09 15:38:25 +00002255 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002256 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2257 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2258
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002260 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002261 Runnable onComplete = new Runnable() {
2262 @Override
2263 public void run() {
2264 // Exit spring loaded mode if necessary after adding the widget
2265 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2266 null);
2267 }
2268 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002269 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002270 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002271 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 }
2273 }
Romain Guycbb89e42009-06-08 15:52:54 -07002274
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002275 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002276 // Close any folders that may be open.
2277 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002278 mWorkspace.moveToCustomContentScreen(animate);
2279 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002280
2281 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2282 int[] cell, int spanX, int spanY) {
2283 switch (info.itemType) {
2284 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2285 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2286 int span[] = new int[2];
2287 span[0] = spanX;
2288 span[1] = spanY;
2289 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2290 container, screenId, cell, span);
2291 break;
2292 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2293 processShortcutFromDrop(info.componentName, container, screenId, cell);
2294 break;
2295 default:
2296 throw new IllegalStateException("Unknown item type: " + info.itemType);
2297 }
2298 }
2299
Adam Cohenfbba09b2011-07-18 21:43:05 -07002300 /**
2301 * Process a shortcut drop.
2302 *
2303 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002304 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002305 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002306 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002307 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2308 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002309 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002310 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002311 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002312 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002313
2314 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002315 mPendingAddInfo.cellX = cell[0];
2316 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002317 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002318
2319 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2320 createShortcutIntent.setComponent(componentName);
2321 processShortcut(createShortcutIntent);
2322 }
2323
Adam Cohenfbba09b2011-07-18 21:43:05 -07002324 /**
2325 * Process a widget drop.
2326 *
2327 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002328 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002330 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002331 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2332 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002333 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002334 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002335 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002336 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002337 mPendingAddInfo.minSpanX = info.minSpanX;
2338 mPendingAddInfo.minSpanY = info.minSpanY;
2339
Adam Cohenfbba09b2011-07-18 21:43:05 -07002340 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002341 mPendingAddInfo.cellX = cell[0];
2342 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002343 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002344 if (span != null) {
2345 mPendingAddInfo.spanX = span[0];
2346 mPendingAddInfo.spanY = span[1];
2347 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002348
Adam Cohened66b2b2012-01-23 17:28:51 -08002349 AppWidgetHostView hostView = info.boundWidget;
2350 int appWidgetId;
2351 if (hostView != null) {
2352 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002353 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002354 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002355 // In this case, we either need to start an activity to get permission to bind
2356 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002357 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002358 Bundle options = info.bindOptions;
2359
Sunny Goyalffe83f12014-08-14 17:39:34 -07002360 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2361 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002362 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002363 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002364 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002365 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002366 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2367 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2368 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002369 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2370 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002371 // TODO: we need to make sure that this accounts for the options bundle.
2372 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002373 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2374 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002375 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002376 }
2377
Joe Onoratodeb98af2010-02-19 14:59:39 -08002378 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002379 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380 }
2381
Winson Chung24ab2f12010-09-16 14:10:47 -07002382 void processWallpaper(Intent intent) {
2383 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2384 }
2385
Adam Cohendcd297f2013-06-18 13:13:40 -07002386 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002387 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002388 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389 folderInfo.title = getText(R.string.folder_name);
2390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002392 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002393 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002394 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002395
2396 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002397 FolderIcon newFolder =
2398 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002399 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002400 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002401 // Force measure the new folder icon
2402 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2403 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002404 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 }
Romain Guycbb89e42009-06-08 15:52:54 -07002406
Joe Onorato9c1289c2009-08-17 11:03:03 -04002407 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002408 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002409 }
2410
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002411 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002412 if (mLauncherCallbacks != null) {
2413 return mLauncherCallbacks.getWallpaperPickerComponent();
2414 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002415 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002416 }
2417
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002418 /**
2419 * Registers various content observers. The current implementation registers
2420 * only a favorites observer to keep track of the favorites applications.
2421 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002422 private void registerContentObservers() {
2423 ContentResolver resolver = getContentResolver();
2424 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2425 true, mWidgetObserver);
2426 }
2427
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 @Override
2429 public boolean dispatchKeyEvent(KeyEvent event) {
2430 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2431 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002433 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002434 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002435 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002436 dumpState();
2437 return true;
2438 }
2439 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002440 }
2441 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2442 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002443 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 return true;
2445 }
2446 }
2447
2448 return super.dispatchKeyEvent(event);
2449 }
2450
Joe Onorato88ec0992009-11-19 13:16:06 -08002451 @Override
2452 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002453 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2454 return;
2455 }
2456
Adam Cohenc9735cf2015-01-23 16:11:55 -08002457 if (LauncherAppState.getInstance().getAccessibilityDelegate().onBackPressed()) {
2458 return;
2459 }
2460
Winson Chungb745afb2015-03-02 11:51:23 -08002461 if (isAppsViewVisible()) {
2462 showWorkspace(true);
2463 } else if (isWidgetsViewVisible()) {
2464 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002465 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002466 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002467 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002468 Folder openFolder = mWorkspace.getOpenFolder();
2469 if (openFolder.isEditingName()) {
2470 openFolder.dismissEditingName();
2471 } else {
2472 closeFolder();
2473 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002474 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002475 mWorkspace.exitWidgetResizeMode();
2476
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002477 // Back button is a no-op here, but give at least some feedback for the button press
2478 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002479 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002480 }
2481
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002483 * Re-listen when widgets are reset.
2484 */
2485 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002486 if (mAppWidgetHost != null) {
2487 mAppWidgetHost.startListening();
2488 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002489 }
2490
2491 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 * Launches the intent referred by the clicked shortcut.
2493 *
2494 * @param v The view representing the clicked shortcut.
2495 */
2496 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002497 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2498 // view has detached (it's possible for this to happen if the view is removed mid touch).
2499 if (v.getWindowToken() == null) {
2500 return;
2501 }
2502
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002503 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002504 return;
2505 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002506
Adam Cohen1697b792013-09-17 19:08:21 -07002507 if (v instanceof Workspace) {
2508 if (mWorkspace.isInOverviewMode()) {
2509 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002510 }
2511 return;
2512 }
2513
2514 if (v instanceof CellLayout) {
2515 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002516 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002517 }
2518 }
2519
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002521 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002522 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002524 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002525 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002526 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002527 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002528 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002529 } else if (tag instanceof AppInfo) {
2530 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002531 } else if (tag instanceof LauncherAppWidgetInfo) {
2532 if (v instanceof PendingAppWidgetHostView) {
2533 onClickPendingWidget((PendingAppWidgetHostView) v);
2534 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 }
2536 }
2537
Sunny Goyal508da152014-08-14 10:53:27 -07002538 public void onClickPagedViewIcon(View v) {
2539 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002540 if (mLauncherCallbacks != null) {
2541 mLauncherCallbacks.onClickPagedViewIcon(v);
2542 }
Sunny Goyal508da152014-08-14 10:53:27 -07002543 }
2544
Michael Jurka0e260592010-06-30 17:07:39 -07002545 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002546 return false;
2547 }
2548
Michael Jurkaaf442092010-06-10 17:01:57 -07002549 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002550 * Event handler for the app widget view which has not fully restored.
2551 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002552 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002553 if (mIsSafeModeEnabled) {
2554 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2555 return;
2556 }
2557
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002558 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002559 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002560 int widgetId = info.appWidgetId;
2561 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2562 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002563 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2564 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002565 mPendingAddInfo.copyFrom(info);
2566 mPendingAddWidgetId = widgetId;
2567
Sunny Goyalffe83f12014-08-14 17:39:34 -07002568 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2569 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002570 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002571 } else if (info.installProgress < 0) {
2572 // The install has not been queued
2573 final String packageName = info.providerName.getPackageName();
2574 showBrokenAppInstallDialog(packageName,
2575 new DialogInterface.OnClickListener() {
2576 public void onClick(DialogInterface dialog, int id) {
2577 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2578 }
2579 });
2580 } else {
2581 // Download has started.
2582 final String packageName = info.providerName.getPackageName();
2583 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002584 }
2585 }
2586
2587 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002588 * Event handler for the "grid" button that appears on the home screen, which
2589 * enters all apps mode.
2590 *
2591 * @param v The view that was clicked.
2592 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002593 protected void onClickAllAppsButton(View v) {
2594 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002595 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002596 showWorkspace(true);
2597 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002598 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002599 }
Adam Cohen9211d422014-10-07 18:14:53 -07002600 if (mLauncherCallbacks != null) {
2601 mLauncherCallbacks.onClickAllAppsButton(v);
2602 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002603 }
2604
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002605 private void showBrokenAppInstallDialog(final String packageName,
2606 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002607 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002608 .setTitle(R.string.abandoned_promises_title)
2609 .setMessage(R.string.abandoned_promise_explanation)
2610 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2611 .setNeutralButton(R.string.abandoned_clean_this,
2612 new DialogInterface.OnClickListener() {
2613 public void onClick(DialogInterface dialog, int id) {
2614 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2615 mWorkspace.removeAbandonedPromise(packageName, user);
2616 }
2617 })
2618 .create().show();
2619 return;
2620 }
2621
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002622 /**
2623 * Event handler for an app shortcut click.
2624 *
2625 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2626 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002627 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002628 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2629 Object tag = v.getTag();
2630 if (!(tag instanceof ShortcutInfo)) {
2631 throw new IllegalArgumentException("Input must be a Shortcut");
2632 }
2633
2634 // Open shortcut
2635 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002636
2637 if (shortcut.isDisabled != 0) {
2638 int error = R.string.activity_not_available;
2639 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2640 error = R.string.safemode_shortcut_error;
2641 }
2642 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2643 return;
2644 }
2645
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002646 final Intent intent = shortcut.intent;
2647
2648 // Check for special shortcuts
2649 if (intent.getComponent() != null) {
2650 final String shortcutClass = intent.getComponent().getClassName();
2651
2652 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2653 MemoryDumpActivity.startDump(this);
2654 return;
2655 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2656 toggleShowWeightWatcher();
2657 return;
2658 }
2659 }
2660
Chris Wren40c5ed32014-06-24 18:24:23 -04002661 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002662 if ((v instanceof BubbleTextView)
2663 && shortcut.isPromise()
2664 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002665 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002666 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002667 new DialogInterface.OnClickListener() {
2668 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002669 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002670 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002671 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002672 return;
2673 }
2674
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002675 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002676 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002677
2678 if (mLauncherCallbacks != null) {
2679 mLauncherCallbacks.onClickAppShortcut(v);
2680 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002681 }
2682
Sunny Goyal508da152014-08-14 10:53:27 -07002683 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002685 final ShortcutInfo shortcut;
2686 final Intent intent;
2687 if (tag instanceof ShortcutInfo) {
2688 shortcut = (ShortcutInfo) tag;
2689 intent = shortcut.intent;
2690 int[] pos = new int[2];
2691 v.getLocationOnScreen(pos);
2692 intent.setSourceBounds(new Rect(pos[0], pos[1],
2693 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002694
Sunny Goyal508da152014-08-14 10:53:27 -07002695 } else if (tag instanceof AppInfo) {
2696 shortcut = null;
2697 intent = ((AppInfo) tag).intent;
2698 } else {
2699 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2700 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002701
2702 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002703 mStats.recordLaunch(intent, shortcut);
2704
2705 if (success && v instanceof BubbleTextView) {
2706 mWaitingForResume = (BubbleTextView) v;
2707 mWaitingForResume.setStayPressed(true);
2708 }
2709 }
2710
2711 /**
2712 * Event handler for a folder icon click.
2713 *
2714 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2715 */
2716 protected void onClickFolderIcon(View v) {
2717 if (LOGD) Log.d(TAG, "onClickFolder");
2718 if (!(v instanceof FolderIcon)){
2719 throw new IllegalArgumentException("Input must be a FolderIcon");
2720 }
2721
2722 FolderIcon folderIcon = (FolderIcon) v;
2723 final FolderInfo info = folderIcon.getFolderInfo();
2724 Folder openFolder = mWorkspace.getFolderForTag(info);
2725
2726 // If the folder info reports that the associated folder is open, then verify that
2727 // it is actually opened. There have been a few instances where this gets out of sync.
2728 if (info.opened && openFolder == null) {
2729 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2730 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2731 info.opened = false;
2732 }
2733
2734 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2735 // Close any open folder
2736 closeFolder();
2737 // Open the requested folder
2738 openFolder(folderIcon);
2739 } else {
2740 // Find the open folder...
2741 int folderScreen;
2742 if (openFolder != null) {
2743 folderScreen = mWorkspace.getPageForView(openFolder);
2744 // .. and close it
2745 closeFolder(openFolder);
2746 if (folderScreen != mWorkspace.getCurrentPage()) {
2747 // Close any folder open on the current screen
2748 closeFolder();
2749 // Pull the folder onto this screen
2750 openFolder(folderIcon);
2751 }
2752 }
2753 }
Adam Cohen9211d422014-10-07 18:14:53 -07002754
2755 if (mLauncherCallbacks != null) {
2756 mLauncherCallbacks.onClickFolderIcon(v);
2757 }
Michael Jurka5130e402011-10-13 04:55:35 -07002758 }
2759
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002760 /**
2761 * Event handler for the (Add) Widgets button that appears after a long press
2762 * on the home screen.
2763 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002764 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002765 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002766 if (mIsSafeModeEnabled) {
2767 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2768 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002769 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002770 if (mLauncherCallbacks != null) {
2771 mLauncherCallbacks.onClickAddWidgetButton(view);
2772 }
Adam Cohen9211d422014-10-07 18:14:53 -07002773 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002774 }
2775
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002776 /**
2777 * Event handler for the wallpaper picker button that appears after a long press
2778 * on the home screen.
2779 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002780 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002781 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2782 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2783 pickWallpaper.setComponent(getWallpaperPickerComponent());
2784 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002785
2786 if (mLauncherCallbacks != null) {
2787 mLauncherCallbacks.onClickWallpaperPicker(v);
2788 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002789 }
2790
2791 /**
2792 * Event handler for a click on the settings button that appears after a long press
2793 * on the home screen.
2794 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002795 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002796 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002797 if (mLauncherCallbacks != null) {
2798 mLauncherCallbacks.onClickSettingsButton(v);
2799 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002800 }
2801
Michael Jurka5130e402011-10-13 04:55:35 -07002802 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002803 // Provide the same haptic feedback that the system offers for virtual keys.
2804 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002805 }
2806
Adam Cohen61f560d2013-09-30 15:58:20 -07002807 public void performHapticFeedbackOnTouchDown(View v) {
2808 // Provide the same haptic feedback that the system offers for virtual keys.
2809 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2810 }
2811
2812 public View.OnTouchListener getHapticFeedbackTouchListener() {
2813 if (mHapticFeedbackTouchListener == null) {
2814 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2815 @Override
2816 public boolean onTouch(View v, MotionEvent event) {
2817 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2818 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2819 }
2820 return false;
2821 }
2822 };
2823 }
2824 return mHapticFeedbackTouchListener;
2825 }
2826
Adam Cohen9211d422014-10-07 18:14:53 -07002827 public void onDragStarted(View view) {
2828 if (isOnCustomContent()) {
2829 // Custom content screen doesn't participate in drag and drop. If on custom
2830 // content screen, move to default.
2831 moveWorkspaceToDefaultScreen();
2832 }
2833
2834 if (mLauncherCallbacks != null) {
2835 mLauncherCallbacks.onDragStarted(view);
2836 }
2837 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002838
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002839 /**
2840 * Called when the user stops interacting with the launcher.
2841 * This implies that the user is now on the homescreen and is not doing housekeeping.
2842 */
Adam Cohen9211d422014-10-07 18:14:53 -07002843 protected void onInteractionEnd() {
2844 if (mLauncherCallbacks != null) {
2845 mLauncherCallbacks.onInteractionEnd();
2846 }
2847 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002848
2849 /**
2850 * Called when the user starts interacting with the launcher.
2851 * The possible interactions are:
2852 * - open all apps
2853 * - reorder an app shortcut, or a widget
2854 * - open the overview mode.
2855 * This is a good time to stop doing things that only make sense
2856 * when the user is on the homescreen and not doing housekeeping.
2857 */
Adam Cohen9211d422014-10-07 18:14:53 -07002858 protected void onInteractionBegin() {
2859 if (mLauncherCallbacks != null) {
2860 mLauncherCallbacks.onInteractionBegin();
2861 }
2862 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002863
Kenny Guyf07af7b2014-07-31 11:39:16 +01002864 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002865 try {
2866 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002867 launcherApps.showAppDetailsForProfile(componentName, user);
2868 } catch (SecurityException e) {
2869 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2870 Log.e(TAG, "Launcher does not have permission to launch settings");
2871 } catch (ActivityNotFoundException e) {
2872 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2873 Log.e(TAG, "Unable to launch settings");
2874 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002875 }
2876
Michael Jurka1e2f4652013-07-08 18:03:46 -07002877 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002878 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2879 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002880 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002881 // System applications cannot be installed. For now, show a toast explaining that.
2882 // We may give them the option of disabling apps this way.
2883 int messageId = R.string.uninstall_system_app_text;
2884 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002885 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002886 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002887 String packageName = componentName.getPackageName();
2888 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002889 Intent intent = new Intent(
2890 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002891 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2892 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002893 if (user != null) {
2894 user.addToIntent(intent, Intent.EXTRA_USER);
2895 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002896 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002897 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002898 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002899 }
2900
Michael Jurka86a720e2012-05-09 11:23:23 -07002901 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002902 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002903 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002904 // Only launch using the new animation if the shortcut has not opted out (this is a
2905 // private contract between launcher and may be ignored in the future).
2906 boolean useLaunchAnimation = (v != null) &&
2907 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002908 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2909 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002910
Kenny Guy1317e2d2014-05-08 18:52:50 +01002911 UserHandleCompat user = null;
2912 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2913 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2914 user = userManager.getUserForSerialNumber(serialNumber);
2915 }
2916
2917 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002918 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002919 ActivityOptions opts = null;
2920 if (sClipRevealMethod != null) {
2921 // TODO: call method directly when Launcher3 can depend on M APIs
2922 int left = 0, top = 0;
2923 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2924 if (v instanceof TextView) {
2925 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002926 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2927 if (icon != null) {
2928 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002929 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002930 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002931 width = bounds.width();
2932 height = bounds.height();
2933 }
2934 }
2935 try {
2936 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2937 left, top, width, height);
2938 } catch (IllegalAccessException e) {
2939 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2940 sClipRevealMethod = null;
2941 } catch (InvocationTargetException e) {
2942 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2943 sClipRevealMethod = null;
2944 }
2945 }
2946 if (opts == null) {
2947 opts = Utilities.isLmpOrAbove() ?
2948 ActivityOptions.makeCustomAnimation(this,
2949 R.anim.task_open_enter, R.anim.no_anim) :
2950 ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2951 v.getMeasuredWidth(), v.getMeasuredHeight());
2952 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002953 optsBundle = opts.toBundle();
2954 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002955
2956 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2957 // Could be launching some bookkeeping activity
2958 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002959 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002960 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002961 launcherApps.startActivityForProfile(intent.getComponent(), user,
2962 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002963 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002964 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 } catch (SecurityException e) {
2966 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002967 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002969 "or use the exported attribute for this activity. "
2970 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002972 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002974
Michael Jurka86a720e2012-05-09 11:23:23 -07002975 boolean startActivitySafely(View v, Intent intent, Object tag) {
2976 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002977 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2978 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2979 return false;
2980 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002981 try {
2982 success = startActivity(v, intent, tag);
2983 } catch (ActivityNotFoundException e) {
2984 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2985 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2986 }
2987 return success;
2988 }
2989
Adam Cohen268c4752012-06-06 17:47:33 -07002990 /**
2991 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2992 * in the DragLayer in the exact absolute location of the original FolderIcon.
2993 */
2994 private void copyFolderIconToImage(FolderIcon fi) {
2995 final int width = fi.getMeasuredWidth();
2996 final int height = fi.getMeasuredHeight();
2997
2998 // Lazy load ImageView, Bitmap and Canvas
2999 if (mFolderIconImageView == null) {
3000 mFolderIconImageView = new ImageView(this);
3001 }
3002 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3003 mFolderIconBitmap.getHeight() != height) {
3004 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3005 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3006 }
3007
3008 DragLayer.LayoutParams lp;
3009 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3010 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3011 } else {
3012 lp = new DragLayer.LayoutParams(width, height);
3013 }
3014
Adam Cohen307fe232012-08-16 17:55:58 -07003015 // The layout from which the folder is being opened may be scaled, adjust the starting
3016 // view size by this scale factor.
3017 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003018 lp.customPosition = true;
3019 lp.x = mRectForFolderAnimation.left;
3020 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003021 lp.width = (int) (scale * width);
3022 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003023
3024 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3025 fi.draw(mFolderIconCanvas);
3026 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003027 if (fi.getFolder() != null) {
3028 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3029 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003030 }
Adam Cohen268c4752012-06-06 17:47:33 -07003031 // Just in case this image view is still in the drag layer from a previous animation,
3032 // we remove it and re-add it.
3033 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3034 mDragLayer.removeView(mFolderIconImageView);
3035 }
3036 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003037 if (fi.getFolder() != null) {
3038 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003039 }
Adam Cohen268c4752012-06-06 17:47:33 -07003040 }
3041
Adam Cohen2801caf2011-05-13 20:57:39 -07003042 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003043 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003044 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3045 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3046 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3047
Adam Cohenc51934b2011-07-26 21:07:43 -07003048 FolderInfo info = (FolderInfo) fi.getTag();
3049 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3050 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003051 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3052 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003053 }
3054
Adam Cohen268c4752012-06-06 17:47:33 -07003055 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3056 copyFolderIconToImage(fi);
3057 fi.setVisibility(View.INVISIBLE);
3058
Michael Jurka2ecf9952012-06-18 12:52:28 -07003059 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003060 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003061 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003062 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3063 }
3064 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003065 oa.start();
3066 }
3067
Adam Cohen268c4752012-06-06 17:47:33 -07003068 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003069 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003070 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3071 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3072 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3073
Adam Cohen268c4752012-06-06 17:47:33 -07003074 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003075
Adam Cohen268c4752012-06-06 17:47:33 -07003076 // We remove and re-draw the FolderIcon in-case it has changed
3077 mDragLayer.removeView(mFolderIconImageView);
3078 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003079 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003080 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003081 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003082 oa.addListener(new AnimatorListenerAdapter() {
3083 @Override
3084 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003085 if (cl != null) {
3086 cl.clearFolderLeaveBehind();
3087 // Remove the ImageView copy of the FolderIcon and make the original visible.
3088 mDragLayer.removeView(mFolderIconImageView);
3089 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003090 }
3091 }
3092 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003093 oa.start();
3094 }
3095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003096 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003097 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003098 * is animated relative to the specified View. If the View is null, no animation
3099 * is played.
3100 *
3101 * @param folderInfo The FolderInfo describing the folder to open.
3102 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003103 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003104 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003105 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106
Adam Cohena9cf38f2011-05-02 15:36:58 -07003107 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108
Adam Cohen4554ee12011-08-03 16:13:21 -07003109 // Just verify that the folder hasn't already been added to the DragLayer.
3110 // There was a one-off crash where the folder had a parent already.
3111 if (folder.getParent() == null) {
3112 mDragLayer.addView(folder);
3113 mDragController.addDropTarget((DropTarget) folder);
3114 } else {
3115 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3116 folder.getParent() + ").");
3117 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003118 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003119 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003120
3121 // Notify the accessibility manager that this folder "window" has appeared and occluded
3122 // the workspace items
3123 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3124 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003125 }
3126
3127 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003128 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003129 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003130 if (folder.isEditingName()) {
3131 folder.dismissEditingName();
3132 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003133 closeFolder(folder);
3134 }
3135 }
3136
3137 void closeFolder(Folder folder) {
3138 folder.getInfo().opened = false;
3139
3140 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3141 if (parent != null) {
3142 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3143 shrinkAndFadeInFolderIcon(fi);
3144 }
3145 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003146
3147 // Notify the accessibility manager that this folder "window" has disappeard and no
3148 // longer occludeds the workspace items
3149 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003150 }
3151
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003152 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003153 if (!isDraggingEnabled()) return false;
3154 if (isWorkspaceLocked()) return false;
3155 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156
Adam Cohen1697b792013-09-17 19:08:21 -07003157 if (v instanceof Workspace) {
3158 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003159 if (mWorkspace.enterOverviewMode()) {
3160 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3161 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3162 return true;
3163 } else {
3164 return false;
3165 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003166 } else {
3167 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003168 }
Adam Cohen1697b792013-09-17 19:08:21 -07003169 }
3170
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003171 CellLayout.CellInfo longClickCellInfo = null;
3172 View itemUnderLongClick = null;
3173 if (v.getTag() instanceof ItemInfo) {
3174 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003175 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003176 itemUnderLongClick = longClickCellInfo.cell;
3177 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178 }
3179
Winson Chung3d503fb2011-07-13 17:25:49 -07003180 // The hotseat touch handling does not go through Workspace, and we always allow long press
3181 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003182 final boolean inHotseat = isHotseatLayout(v);
3183 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003184 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003185 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003186 // User long pressed on empty space
3187 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3188 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003189 if (mWorkspace.isInOverviewMode()) {
3190 mWorkspace.startReordering(v);
3191 } else {
3192 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003193 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003194 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003195 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3196 mHotseat.getOrderInHotseat(
3197 longClickCellInfo.cellX,
3198 longClickCellInfo.cellY));
3199 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003200 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003201 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003202 }
3203 }
3204 }
3205 return true;
3206 }
3207
Winson Chung3d503fb2011-07-13 17:25:49 -07003208 boolean isHotseatLayout(View layout) {
3209 return mHotseat != null && layout != null &&
3210 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3211 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003212
Winson Chung3d503fb2011-07-13 17:25:49 -07003213 /**
3214 * Returns the CellLayout of the specified container at the specified screen.
3215 */
Sunny Goyal92820592015-03-02 11:31:35 -08003216 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003217 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3218 if (mHotseat != null) {
3219 return mHotseat.getLayout();
3220 } else {
3221 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003222 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003223 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003224 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003225 }
3226 }
3227
Winson Chungb745afb2015-03-02 11:51:23 -08003228 /**
3229 * For overridden classes.
3230 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003231 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003232 return isAppsViewVisible();
3233 }
3234
3235 public boolean isAppsViewVisible() {
3236 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3237 }
3238
3239 public boolean isWidgetsViewVisible() {
3240 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003241 }
3242
Craig Mautner360310b2012-10-26 15:13:08 -07003243 private void setWorkspaceBackground(boolean workspace) {
3244 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003245 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003246 }
3247
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003248 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003249 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3250 int curflags = getWindow().getAttributes().flags
3251 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3252 if (wpflags != curflags) {
3253 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3254 }
Craig Mautner360310b2012-10-26 15:13:08 -07003255 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003256 }
3257
Michael Jurkae326f182011-11-21 14:05:46 -08003258 @Override
3259 public void onTrimMemory(int level) {
3260 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003261 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3262 // The widget preview db can result in holding onto over
3263 // 3MB of memory for caching which isn't necessary.
3264 SQLiteDatabase.releaseMemory();
3265
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003266 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003267 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003268 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003269 }
Michael Jurkae326f182011-11-21 14:05:46 -08003270 }
3271 }
3272
Winson Chungb745afb2015-03-02 11:51:23 -08003273 @Override
3274 public void onStateTransitionHideSearchBar() {
3275 // Hide the search bar
3276 if (mSearchDropTargetBar != null) {
3277 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3278 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003279 }
3280
Winson Chungb745afb2015-03-02 11:51:23 -08003281 protected void showWorkspace(boolean animated) {
3282 showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003283 }
3284
Adam Cohened66b2b2012-01-23 17:28:51 -08003285 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003286 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003287 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003288 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003289 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
3290 animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003291
Winson Chungc7d2b602012-05-16 17:02:20 -07003292 // Show the search bar (only animate if we were showing the drop target bar in spring
3293 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003294 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003295 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003296 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003297
Michael Jurkab3e22d92011-10-31 15:58:33 -07003298 // Set focus to the AppsCustomize button
3299 if (mAllAppsButton != null) {
3300 mAllAppsButton.requestFocus();
3301 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003302 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003303
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003304 // Change the state *after* we've called all the transition code
3305 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003306
Winson Chung5fb63472011-02-02 17:03:37 -08003307 // Resume the auto-advance of widgets
3308 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003309 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003310
alanv1d4fde62012-10-17 13:15:47 -07003311 // Send an accessibility event to announce the context change
3312 getWindow().getDecorView()
3313 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003314
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003315 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003316 }
3317
Adam Cohened307df2013-10-02 09:37:31 -07003318 void showOverviewMode(boolean animated) {
3319 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003320 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
3321 animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003322 mState = State.WORKSPACE;
3323 onWorkspaceShown(animated);
3324 }
3325
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003326 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003327 }
3328
Winson Chungb745afb2015-03-02 11:51:23 -08003329 /**
3330 * Shows the apps view.
3331 */
3332 void showAppsView(boolean animated, boolean resetListToTop) {
3333 if (resetListToTop) {
3334 mAppsView.scrollToTop();
3335 }
3336 showAppsOrWidgets(animated, State.APPS);
3337 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003338
Winson Chungb745afb2015-03-02 11:51:23 -08003339 /**
3340 * Shows the widgets view.
3341 */
3342 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Winson Chung82963d52013-10-09 11:20:57 -07003343 if (resetPageToZero) {
3344 mAppsCustomizeTabHost.reset();
3345 }
Winson Chungb745afb2015-03-02 11:51:23 -08003346 showAppsOrWidgets(animated, State.WIDGETS);
Adam Cohendcc5e712014-06-23 15:38:55 -04003347 mAppsCustomizeTabHost.post(new Runnable() {
3348 @Override
3349 public void run() {
3350 // We post this in-case the all apps view isn't yet constructed.
3351 mAppsCustomizeTabHost.requestFocus();
3352 }
3353 });
Winson Chungb745afb2015-03-02 11:51:23 -08003354 }
3355
3356 /**
3357 * Sets up the transition to show the apps/widgets view.
3358 */
3359 private void showAppsOrWidgets(boolean animated, State toState) {
3360 if (mState != State.WORKSPACE) return;
3361 if (toState != State.APPS && toState != State.WIDGETS) return;
3362
3363 if (toState == State.APPS) {
3364 mStateTransitionAnimation.startAnimationToAllApps(animated);
3365 } else {
3366 mStateTransitionAnimation.startAnimationToWidgets(animated);
3367 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003368
Michael Jurkab3e22d92011-10-31 15:58:33 -07003369 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003370 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003371
3372 // Pause the auto-advance of widgets until we are out of AllApps
3373 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003374 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003375 closeFolder();
3376
3377 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003378 getWindow().getDecorView()
3379 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003380 }
3381
Adam Cohen7777d962011-08-18 18:58:38 -07003382 void enterSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003383 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3384 mState == State.WIDGETS_SPRING_LOADED) {
3385 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003386 }
Winson Chungb745afb2015-03-02 11:51:23 -08003387
3388 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
3389 true /* animated */, null /* onCompleteRunnable */);
3390 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003391 }
Adam Cohen7777d962011-08-18 18:58:38 -07003392
Adam Cohenad4e15c2013-10-17 16:21:35 -07003393 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003394 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003395 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003396
Winson Chunge7a03942011-08-05 15:05:12 -07003397 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003398 @Override
3399 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003400 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003401 // Before we show workspace, hide all apps again because
3402 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3403 // clean up our state transition functions
3404 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003405 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003406 } else {
3407 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003408 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003409 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003410 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003411 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003412
Michael Jurkad3ef3062010-11-23 16:23:58 -08003413 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003414 if (mState == State.APPS_SPRING_LOADED) {
3415 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3416 mState = State.APPS;
3417 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3418 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3419 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003420 }
3421 // Otherwise, we are not in spring loaded mode, so don't do anything.
3422 }
3423
Michael Jurkab3e22d92011-10-31 15:58:33 -07003424 void lockAllApps() {
3425 // TODO
3426 }
3427
3428 void unlockAllApps() {
3429 // TODO
3430 }
3431
Sunny Goyal594d76d2014-11-06 10:12:54 -08003432 protected void disableVoiceButtonProxy(boolean disable) {
3433 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003434 }
3435
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003436 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003437 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3438 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003439 }
3440
Adam Cohen24ce0b32014-01-14 16:18:14 -08003441 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003442 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3443 if (searchProvider == null) {
3444 return null;
3445 }
3446
3447 Bundle opts = new Bundle();
3448 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003449 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003450
3451 SharedPreferences sp = getSharedPreferences(
3452 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3453 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003454 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003455 if (!searchProvider.provider.flattenToString().equals(
3456 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003457 || (widgetInfo == null)
3458 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003459 // A valid widget is not already bound.
3460 if (widgetId > -1) {
3461 mAppWidgetHost.deleteAppWidgetId(widgetId);
3462 widgetId = -1;
3463 }
3464
3465 // Try to bind a new widget
3466 widgetId = mAppWidgetHost.allocateAppWidgetId();
3467
3468 if (!AppWidgetManagerCompat.getInstance(this)
3469 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3470 mAppWidgetHost.deleteAppWidgetId(widgetId);
3471 widgetId = -1;
3472 }
3473
3474 sp.edit()
3475 .putInt(QSB_WIDGET_ID, widgetId)
3476 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3477 .commit();
3478 }
3479
3480 if (widgetId != -1) {
3481 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3482 mQsb.updateAppWidgetOptions(opts);
3483 mQsb.setPadding(0, 0, 0, 0);
3484 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003485 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003486 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003487 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003488 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003489 }
3490
Michael Jurkad7c28052012-04-27 15:43:36 -07003491 @Override
3492 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003493 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003494 final List<CharSequence> text = event.getText();
3495 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003496 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003497 if (mState == State.APPS) {
3498 text.add("Apps");
3499 } else if (mState == State.WIDGETS) {
3500 text.add("Widgets");
alanv1d4fde62012-10-17 13:15:47 -07003501 } else {
3502 text.add(getString(R.string.all_apps_home_button_label));
3503 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003504 return result;
3505 }
3506
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003507 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003508 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003509 */
3510 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3511 @Override
3512 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003513 closeSystemDialogs();
3514 }
3515 }
3516
3517 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003518 * Receives notifications whenever the appwidgets are reset.
3519 */
3520 private class AppWidgetResetObserver extends ContentObserver {
3521 public AppWidgetResetObserver() {
3522 super(new Handler());
3523 }
3524
3525 @Override
3526 public void onChange(boolean selfChange) {
3527 onAppWidgetReset();
3528 }
3529 }
3530
3531 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003532 * If the activity is currently paused, signal that we need to run the passed Runnable
3533 * in onResume.
3534 *
3535 * This needs to be called from incoming places where resources might have been loaded
3536 * while we are paused. That is becaues the Configuration might be wrong
3537 * when we're not running, and if it comes back to what it was when we
3538 * were paused, we are not restarted.
3539 *
3540 * Implementation of the method from LauncherModel.Callbacks.
3541 *
3542 * @return true if we are currently paused. The caller might be able to
3543 * skip some work in that case since we will come back again.
3544 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003545 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003546 if (mPaused) {
3547 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003548 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003549 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003550 }
3551 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003552 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003553 return true;
3554 } else {
3555 return false;
3556 }
3557 }
3558
Michael Jurkac402cd92013-05-20 15:49:32 +02003559 private boolean waitUntilResume(Runnable run) {
3560 return waitUntilResume(run, false);
3561 }
3562
Michael Jurka1e2f4652013-07-08 18:03:46 -07003563 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003564 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003565 }
3566
Michael Jurka7607c2f2013-04-03 14:33:19 -07003567 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003568 * If the activity is currently paused, signal that we need to re-run the loader
3569 * in onResume.
3570 *
3571 * This needs to be called from incoming places where resources might have been loaded
3572 * while we are paused. That is becaues the Configuration might be wrong
3573 * when we're not running, and if it comes back to what it was when we
3574 * were paused, we are not restarted.
3575 *
3576 * Implementation of the method from LauncherModel.Callbacks.
3577 *
3578 * @return true if we are currently paused. The caller might be able to
3579 * skip some work in that case since we will come back again.
3580 */
3581 public boolean setLoadOnResume() {
3582 if (mPaused) {
3583 Log.i(TAG, "setLoadOnResume");
3584 mOnResumeNeedsLoad = true;
3585 return true;
3586 } else {
3587 return false;
3588 }
3589 }
3590
3591 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003592 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003593 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003594 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003595 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003596 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003597 } else {
3598 return SCREEN_COUNT / 2;
3599 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003601
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 /**
3603 * Refreshes the shortcuts shown on the workspace.
3604 *
3605 * Implementation of the method from LauncherModel.Callbacks.
3606 */
3607 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003608 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003609
Michael Jurka7607c2f2013-04-03 14:33:19 -07003610 // If we're starting binding all over again, clear any bind calls we'd postponed in
3611 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3612 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003613 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003614
Winson Chungd64d1762013-08-20 14:37:16 -07003615 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003616 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003617 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003618
Michael Jurka05bf6442011-11-30 20:28:53 -08003619 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003620 if (mHotseat != null) {
3621 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003622 }
3623 }
3624
Adam Cohendcd297f2013-06-18 13:13:40 -07003625 @Override
3626 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003627 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003628
Adam Cohen5084cba2013-09-03 12:01:16 -07003629 // If there are no screens, we need to have an empty screen
3630 if (orderedScreenIds.size() == 0) {
3631 mWorkspace.addExtraEmptyScreen();
3632 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003633
3634 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003635 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003636 if (hasCustomContentToLeft()) {
3637 mWorkspace.createCustomContentContainer();
3638 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003639 }
Winson Chung64359a52013-07-08 17:17:08 -07003640 }
3641
3642 @Override
3643 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003644 // Log to disk
3645 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3646 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3647 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003648 int count = orderedScreenIds.size();
3649 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003650 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003651 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003652 }
3653
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003654 @Override
3655 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3656 final long screenId, final int[] cell, final int spanX, final int spanY) {
3657 showWorkspace(true, new Runnable() {
3658
3659 @Override
3660 public void run() {
3661 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3662 addPendingItem(info, container, screenId, cell, spanX, spanY);
3663 }
3664 });
3665 }
3666
Adam Cohen39a06042013-07-19 14:30:12 -07003667 private boolean shouldShowWeightWatcher() {
3668 String spKey = LauncherAppState.getSharedPreferencesKey();
3669 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003670 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003671
3672 return show;
3673 }
3674
3675 private void toggleShowWeightWatcher() {
3676 String spKey = LauncherAppState.getSharedPreferencesKey();
3677 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3678 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3679
3680 show = !show;
3681
3682 SharedPreferences.Editor editor = sp.edit();
3683 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3684 editor.commit();
3685
3686 if (mWeightWatcher != null) {
3687 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3688 }
3689 }
3690
Winson Chungd64d1762013-08-20 14:37:16 -07003691 public void bindAppsAdded(final ArrayList<Long> newScreens,
3692 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003693 final ArrayList<ItemInfo> addAnimated,
3694 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003695 Runnable r = new Runnable() {
3696 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003697 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003698 }
3699 };
3700 if (waitUntilResume(r)) {
3701 return;
3702 }
3703
Winson Chungd64d1762013-08-20 14:37:16 -07003704 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003705 if (newScreens != null) {
3706 bindAddScreens(newScreens);
3707 }
Winson Chungd64d1762013-08-20 14:37:16 -07003708
3709 // We add the items without animation on non-visible pages, and with
3710 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003711 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003712 bindItems(addNotAnimated, 0,
3713 addNotAnimated.size(), false);
3714 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003715 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003716 bindItems(addAnimated, 0,
3717 addAnimated.size(), true);
3718 }
Winson Chungc58497e2013-09-03 17:48:37 -07003719
Winson Chung87412982013-10-03 18:34:14 -07003720 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003721 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003722
Winson Chungb745afb2015-03-02 11:51:23 -08003723 if (addedApps != null && mAppsView != null) {
3724 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003725 }
Winson Chungd64d1762013-08-20 14:37:16 -07003726 }
3727
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003728 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003729 * Bind the items start-end from the list.
3730 *
3731 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003732 */
Winson Chung64359a52013-07-08 17:17:08 -07003733 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3734 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003735 Runnable r = new Runnable() {
3736 public void run() {
3737 bindItems(shortcuts, start, end, forceAnimateIcons);
3738 }
3739 };
3740 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003741 return;
3742 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003743
Winson Chungf0c6ae02012-03-21 16:10:31 -07003744 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003745 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3746 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003747 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003748 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003749 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003750 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003751 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003752
3753 // Short circuit if we are loading dock items for a configuration which has no dock
3754 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3755 mHotseat == null) {
3756 continue;
3757 }
3758
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 switch (item.itemType) {
3760 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3761 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003762 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003763 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003764
Winson Chung64359a52013-07-08 17:17:08 -07003765 /*
3766 * TODO: FIX collision case
3767 */
Winson Chungce376632013-07-11 16:12:41 -07003768 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3769 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3770 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003771 View v = cl.getChildAt(item.cellX, item.cellY);
3772 Object tag = v.getTag();
3773 String desc = "Collision while binding workspace item: " + item
3774 + ". Collides with " + tag;
3775 if (LauncherAppState.isDogfoodBuild()) {
3776 throw (new RuntimeException(desc));
3777 } else {
3778 Log.d(TAG, desc);
3779 }
Winson Chungce376632013-07-11 16:12:41 -07003780 }
Winson Chung64359a52013-07-08 17:17:08 -07003781 }
3782
Adam Cohendcd297f2013-06-18 13:13:40 -07003783 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3784 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003785 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003786 // Animate all the applications up now
3787 shortcut.setAlpha(0f);
3788 shortcut.setScaleX(0f);
3789 shortcut.setScaleY(0f);
3790 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003791 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003792 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003793 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003794 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003795 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003796 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003797 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003798 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3799 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003801 default:
3802 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003803 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003804 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003805
Winson Chung997a9232013-07-24 15:33:46 -07003806 if (animateIcons) {
3807 // Animate to the correct page
3808 if (newShortcutsScreenId > -1) {
3809 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003810 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003811 final Runnable startBounceAnimRunnable = new Runnable() {
3812 public void run() {
3813 anim.playTogether(bounceAnims);
3814 anim.start();
3815 }
3816 };
Winson Chung997a9232013-07-24 15:33:46 -07003817 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003818 // We post the animation slightly delayed to prevent slowdowns
3819 // when we are loading right after we return to launcher.
3820 mWorkspace.postDelayed(new Runnable() {
3821 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003822 if (mWorkspace != null) {
3823 mWorkspace.snapToPage(newScreenIndex);
3824 mWorkspace.postDelayed(startBounceAnimRunnable,
3825 NEW_APPS_ANIMATION_DELAY);
3826 }
Winson Chung94d67682013-09-25 16:29:40 -07003827 }
3828 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003829 } else {
3830 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003831 }
3832 }
Winson Chung64359a52013-07-08 17:17:08 -07003833 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003834 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003835 }
3836
Joe Onorato9c1289c2009-08-17 11:03:03 -04003837 /**
3838 * Implementation of the method from LauncherModel.Callbacks.
3839 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003840 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003841 Runnable r = new Runnable() {
3842 public void run() {
3843 bindFolders(folders);
3844 }
3845 };
3846 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003847 return;
3848 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003849 sFolders.clear();
3850 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003851 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852
3853 /**
3854 * Add the views for a widget to the workspace.
3855 *
3856 * Implementation of the method from LauncherModel.Callbacks.
3857 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003858 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003859 Runnable r = new Runnable() {
3860 public void run() {
3861 bindAppWidget(item);
3862 }
3863 };
3864 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003865 return;
3866 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003867
Daniel Sandler843e8602010-06-07 14:59:01 -04003868 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3869 if (DEBUG_WIDGETS) {
3870 Log.d(TAG, "bindAppWidget: " + item);
3871 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003872 final Workspace workspace = mWorkspace;
3873
Adam Cohen59400422014-03-05 18:07:04 -08003874 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003875 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003876
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003877 if (!mIsSafeModeEnabled
3878 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3879 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003880 if (appWidgetInfo == null) {
3881 if (DEBUG_WIDGETS) {
3882 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3883 + " belongs to component " + item.providerName
3884 + ", as the povider is null");
3885 }
3886 LauncherModel.deleteItemFromDatabase(this, item);
3887 return;
3888 }
3889 // Note: This assumes that the id remap broadcast is received before this step.
3890 // If that is not the case, the id remap will be ignored and user may see the
3891 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003892 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003893 pendingInfo.spanX = item.spanX;
3894 pendingInfo.spanY = item.spanY;
3895 pendingInfo.minSpanX = item.minSpanX;
3896 pendingInfo.minSpanY = item.minSpanY;
3897 Bundle options =
3898 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
3899
Sunny Goyalff572272014-07-23 13:58:07 -07003900 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003901 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3902 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003903
3904 // TODO consider showing a permission dialog when the widget is clicked.
3905 if (!success) {
3906 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3907 if (DEBUG_WIDGETS) {
3908 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3909 + " belongs to component " + item.providerName
3910 + ", as the launcher is unable to bing a new widget id");
3911 }
3912 LauncherModel.deleteItemFromDatabase(this, item);
3913 return;
3914 }
3915
3916 item.appWidgetId = newWidgetId;
3917
3918 // If the widget has a configure activity, it is still needs to set it up, otherwise
3919 // the widget is ready to go.
3920 item.restoreStatus = (appWidgetInfo.configure == null)
3921 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3922 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3923
3924 LauncherModel.updateItemInDatabase(this, item);
3925 }
3926
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003927 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003928 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003929 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003930 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3931 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003932 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003933
Sunny Goyal651077b2014-06-30 14:15:31 -07003934 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3935 } else {
3936 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003937 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3938 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003939 view.updateIcon(mIconCache);
3940 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003941 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003942 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003943 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003944
Joe Onorato9c1289c2009-08-17 11:03:03 -04003945 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003946 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003947
Adam Cohendcd297f2013-06-18 13:13:40 -07003948 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003949 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003950 if (!item.isCustomWidget()) {
3951 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3952 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003953
Joe Onorato9c1289c2009-08-17 11:03:03 -04003954 workspace.requestLayout();
3955
Daniel Sandler843e8602010-06-07 14:59:01 -04003956 if (DEBUG_WIDGETS) {
3957 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3958 + (SystemClock.uptimeMillis()-start) + "ms");
3959 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003960 }
3961
Sunny Goyalff572272014-07-23 13:58:07 -07003962 /**
3963 * Restores a pending widget.
3964 *
3965 * @param appWidgetId The app widget id
3966 * @param cellInfo The position on screen where to create the widget.
3967 */
3968 private void completeRestoreAppWidget(final int appWidgetId) {
3969 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3970 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3971 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3972 return;
3973 }
3974
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003975 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003976 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3977
3978 mWorkspace.reinflateWidgetsIfNecessary();
3979 LauncherModel.updateItemInDatabase(this, info);
3980 }
3981
Adam Cohen1462de32012-07-24 22:34:36 -07003982 public void onPageBoundSynchronously(int page) {
3983 mSynchronouslyBoundPages.add(page);
3984 }
3985
Joe Onorato9c1289c2009-08-17 11:03:03 -04003986 /**
3987 * Callback saying that there aren't any more items to bind.
3988 *
3989 * Implementation of the method from LauncherModel.Callbacks.
3990 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003991 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003992 Runnable r = new Runnable() {
3993 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003994 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003995 }
3996 };
3997 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003998 return;
3999 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004000 if (mSavedState != null) {
4001 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004002 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004003 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004004 mSavedState = null;
4005 }
4006
Adam Cohen1462de32012-07-24 22:34:36 -07004007 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004008
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004009 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004010 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004011
4012 // If we received the result of any pending adds while the loader was running (e.g. the
4013 // widget configuration forced an orientation change), process them now.
4014 if (sPendingAddItem != null) {
4015 final long screenId = completeAdd(sPendingAddItem);
4016
4017 // TODO: this moves the user to the page where the pending item was added. Ideally,
4018 // the screen would be guaranteed to exist after bind, and the page would be set through
4019 // the workspace restore process.
4020 mWorkspace.post(new Runnable() {
4021 @Override
4022 public void run() {
4023 mWorkspace.snapToScreenId(screenId);
4024 }
4025 });
4026 sPendingAddItem = null;
4027 }
4028
Sunny Goyale755d462014-07-22 13:48:29 -07004029 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004030
4031 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004032 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004033 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004034 }
4035
Adam Cohen3ed316a2014-07-23 18:21:20 -07004036 private void sendLoadingCompleteBroadcastIfNecessary() {
4037 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4038 String permission =
4039 getResources().getString(R.string.receive_first_load_broadcast_permission);
4040 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4041 sendBroadcast(intent, permission);
4042 SharedPreferences.Editor editor = mSharedPrefs.edit();
4043 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4044 editor.apply();
4045 }
4046 }
4047
Winson Chunga0b7e862013-09-05 16:03:15 -07004048 public boolean isAllAppsButtonRank(int rank) {
4049 if (mHotseat != null) {
4050 return mHotseat.isAllAppsButtonRank(rank);
4051 }
4052 return false;
4053 }
4054
Winson Chunga2413752012-04-03 14:22:34 -07004055 private boolean canRunNewAppsAnimation() {
4056 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4057 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4058 }
4059
Winson Chungc9168342013-06-26 14:54:55 -07004060 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4061 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4062 PropertyValuesHolder.ofFloat("alpha", 1f),
4063 PropertyValuesHolder.ofFloat("scaleX", 1f),
4064 PropertyValuesHolder.ofFloat("scaleY", 1f));
4065 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4066 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4067 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4068 return bounceAnim;
4069 }
4070
Adam Cohen27772732013-11-11 14:00:56 +00004071 public boolean useVerticalBarLayout() {
4072 return LauncherAppState.getInstance().getDynamicGrid().
4073 getDeviceProfile().isVerticalBarLayout();
4074 }
4075
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004076 protected Rect getSearchBarBounds() {
4077 return LauncherAppState.getInstance().getDynamicGrid().
4078 getDeviceProfile().getSearchBarBounds();
4079 }
4080
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004081 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004082 if (mSearchDropTargetBar == null) {
4083 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004084 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004085 if (mQsb != null) {
4086 mSearchDropTargetBar.removeView(mQsb);
4087 mQsb = null;
4088 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004089 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004090 }
4091
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004092 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004093 * Add the icons for all apps.
4094 *
4095 * Implementation of the method from LauncherModel.Callbacks.
4096 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004097 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004098 if (mAppsView != null) {
4099 mAppsView.setApps(apps);
4100 }
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004101 if (mAppsCustomizeContent != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004102 mAppsCustomizeContent.onPackagesUpdated(
Hyunyoung Song70a48be2015-03-11 16:36:52 -07004103 LauncherModel.getSortedWidgetsAndShortcuts(this, false /* refresh */));
Winson Chung785d2eb2011-04-14 16:08:02 -07004104 }
Adam Cohen9211d422014-10-07 18:14:53 -07004105 if (mLauncherCallbacks != null) {
4106 mLauncherCallbacks.bindAllApplications(apps);
4107 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004108 }
4109
4110 /**
4111 * A package was updated.
4112 *
4113 * Implementation of the method from LauncherModel.Callbacks.
4114 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004115 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004116 Runnable r = new Runnable() {
4117 public void run() {
4118 bindAppsUpdated(apps);
4119 }
4120 };
4121 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004122 return;
4123 }
4124
Winson Chungb745afb2015-03-02 11:51:23 -08004125 if (mAppsView != null) {
4126 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004127 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004128 }
4129
Sunny Goyal4390ace2014-10-13 11:33:11 -07004130 @Override
4131 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4132 Runnable r = new Runnable() {
4133 public void run() {
4134 bindWidgetsRestored(widgets);
4135 }
4136 };
4137 if (waitUntilResume(r)) {
4138 return;
4139 }
4140 mWorkspace.widgetsRestored(widgets);
4141 }
4142
Joe Onorato9c1289c2009-08-17 11:03:03 -04004143 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004144 * Some shortcuts were updated in the background.
4145 *
4146 * Implementation of the method from LauncherModel.Callbacks.
4147 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004148 @Override
4149 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4150 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004151 Runnable r = new Runnable() {
4152 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004153 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004154 }
4155 };
4156 if (waitUntilResume(r)) {
4157 return;
4158 }
4159
Sunny Goyal4390ace2014-10-13 11:33:11 -07004160 if (!updated.isEmpty()) {
4161 mWorkspace.updateShortcuts(updated);
4162 }
4163
4164 if (!removed.isEmpty()) {
4165 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4166 for (ShortcutInfo si : removed) {
4167 removedComponents.add(si.getTargetComponent());
4168 }
4169 mWorkspace.removeItemsByComponentName(removedComponents, user);
4170 // Notify the drag controller
4171 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004172 }
4173 }
4174
4175 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004176 * Update the state of a package, typically related to install state.
4177 *
4178 * Implementation of the method from LauncherModel.Callbacks.
4179 */
Sunny Goyale755d462014-07-22 13:48:29 -07004180 @Override
4181 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004182 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004183 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004184 }
4185 }
4186
4187 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004188 * Update the label and icon of all the icons in a package
4189 *
4190 * Implementation of the method from LauncherModel.Callbacks.
4191 */
4192 @Override
4193 public void updatePackageBadge(String packageName) {
4194 if (mWorkspace != null) {
4195 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4196 }
4197 }
4198
4199 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004200 * A package was uninstalled. We take both the super set of packageNames
4201 * in addition to specific applications to remove, the reason being that
4202 * this can be called when a package is updated as well. In that scenario,
4203 * we only remove specific components from the workspace, where as
4204 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004205 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004206 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004207 * Implementation of the method from LauncherModel.Callbacks.
4208 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004209 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004210 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004211 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004212 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004213 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004214 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004215 }
Winson Chungd64d1762013-08-20 14:37:16 -07004216 };
4217 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004218 return;
4219 }
4220
Sunny Goyal1a745e82014-10-02 15:58:31 -07004221 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004222 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4223 for (AppInfo info : appInfos) {
4224 removedComponents.add(info.componentName);
4225 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004226 if (!packageNames.isEmpty()) {
4227 mWorkspace.removeItemsByPackageName(packageNames, user);
4228 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004229 if (!removedComponents.isEmpty()) {
4230 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004231 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004232 // Notify the drag controller
4233 mDragController.onAppsRemoved(packageNames, removedComponents);
4234
Sunny Goyal1a745e82014-10-02 15:58:31 -07004235 } else {
4236 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004237 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004238
Winson Chungdf95eb12013-10-16 14:57:07 -07004239 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004240 if (mAppsView != null) {
4241 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004242 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004243 }
Joe Onoratobe386092009-11-17 17:32:16 -08004244
4245 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004246 * A number of packages were updated.
4247 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004248 private ArrayList<Object> mWidgetsAndShortcuts;
4249 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004250 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004251 bindPackagesUpdated(mWidgetsAndShortcuts);
4252 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004253 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004254 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004255 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4256 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4257 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004258 return;
4259 }
4260
Winson Chung64359a52013-07-08 17:17:08 -07004261 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004262 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004263 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004264 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004265 }
4266
Winson Chung400438b2011-01-16 17:53:48 -08004267 private int mapConfigurationOriActivityInfoOri(int configOri) {
4268 final Display d = getWindowManager().getDefaultDisplay();
4269 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4270 switch (d.getRotation()) {
4271 case Surface.ROTATION_0:
4272 case Surface.ROTATION_180:
4273 // We are currently in the same basic orientation as the natural orientation
4274 naturalOri = configOri;
4275 break;
4276 case Surface.ROTATION_90:
4277 case Surface.ROTATION_270:
4278 // We are currently in the other basic orientation to the natural orientation
4279 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4280 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4281 break;
4282 }
4283
4284 int[] oriMap = {
4285 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4286 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4287 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4288 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4289 };
4290 // Since the map starts at portrait, we need to offset if this device's natural orientation
4291 // is landscape.
4292 int indexOffset = 0;
4293 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4294 indexOffset = 1;
4295 }
4296 return oriMap[(d.getRotation() + indexOffset) % 4];
4297 }
Adam Cohen446e9402011-09-15 18:21:21 -07004298
Winson Chung4b919f82012-05-01 10:44:08 -07004299 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004300 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004301 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4302 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4303 .getConfiguration().orientation));
4304 } else {
4305 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4306 }
Winson Chung4b919f82012-05-01 10:44:08 -07004307 }
4308 }
4309 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004310 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004311 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004312 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004313 } else {
4314 mHandler.postDelayed(new Runnable() {
4315 public void run() {
4316 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4317 }
4318 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004319 }
Winson Chung4b919f82012-05-01 10:44:08 -07004320 }
Winson Chung400438b2011-01-16 17:53:48 -08004321 }
4322
Winson Chunge43a1e72014-01-15 10:33:02 -08004323 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004324 if (mLauncherCallbacks != null) {
4325 return mLauncherCallbacks.isLauncherPreinstalled();
4326 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004327 PackageManager pm = getPackageManager();
4328 try {
4329 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4330 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4331 return true;
4332 } else {
4333 return false;
4334 }
4335 } catch (NameNotFoundException e) {
4336 e.printStackTrace();
4337 return false;
4338 }
4339 }
4340
Adam Cohenea90f832014-05-21 15:03:34 -07004341 /**
4342 * This method indicates whether or not we should suggest default wallpaper dimensions
4343 * when our wallpaper cropper was not yet used to set a wallpaper.
4344 */
4345 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004346 if (mLauncherCallbacks != null) {
4347 return mLauncherCallbacks.overrideWallpaperDimensions();
4348 }
Adam Cohenea90f832014-05-21 15:03:34 -07004349 return true;
4350 }
4351
Adam Cohen432609a2014-03-13 17:03:22 -07004352 /**
4353 * To be overridden by subclasses to indicate that there is an activity to launch
4354 * before showing the standard launcher experience.
4355 */
4356 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004357 if (mLauncherCallbacks != null) {
4358 return mLauncherCallbacks.hasFirstRunActivity();
4359 }
Adam Cohen432609a2014-03-13 17:03:22 -07004360 return false;
4361 }
4362
4363 /**
4364 * To be overridden by subclasses to launch any first run activity
4365 */
4366 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004367 if (mLauncherCallbacks != null) {
4368 return mLauncherCallbacks.getFirstRunActivity();
4369 }
Adam Cohen432609a2014-03-13 17:03:22 -07004370 return null;
4371 }
4372
Winson Chunga6945242014-01-08 14:04:34 -08004373 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004374 return !ActivityManager.isRunningInTestHarness() &&
4375 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004376 }
4377
Adam Cohen4a9423d2014-03-28 14:22:31 -07004378 protected boolean hasRunFirstRunActivity() {
4379 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4380 }
4381
Adam Cohen432609a2014-03-13 17:03:22 -07004382 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004383 if (shouldRunFirstRunActivity() &&
4384 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004385 Intent firstRunIntent = getFirstRunActivity();
4386 if (firstRunIntent != null) {
4387 startActivity(firstRunIntent);
4388 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004389 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004390 }
4391 }
Adam Cohen432609a2014-03-13 17:03:22 -07004392 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004393 }
4394
4395 private void markFirstRunActivityShown() {
4396 SharedPreferences.Editor editor = mSharedPrefs.edit();
4397 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4398 editor.apply();
4399 }
4400
Adam Cohen432609a2014-03-13 17:03:22 -07004401 /**
4402 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4403 * screen that must be displayed and dismissed.
4404 */
4405 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004406 if (mLauncherCallbacks != null) {
4407 return mLauncherCallbacks.hasDismissableIntroScreen();
4408 }
Adam Cohen432609a2014-03-13 17:03:22 -07004409 return false;
4410 }
4411
4412 /**
4413 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4414 */
4415 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004416 if (mLauncherCallbacks != null) {
4417 return mLauncherCallbacks.getIntroScreen();
4418 }
Adam Cohen432609a2014-03-13 17:03:22 -07004419 return null;
4420 }
4421
4422 /**
4423 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4424 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4425 */
4426 private boolean shouldShowIntroScreen() {
4427 return hasDismissableIntroScreen() &&
4428 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4429 }
4430
4431 protected void showIntroScreen() {
4432 View introScreen = getIntroScreen();
4433 changeWallpaperVisiblity(false);
4434 if (introScreen != null) {
4435 mDragLayer.showOverlayView(introScreen);
4436 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004437 if (mLauncherOverlayContainer != null) {
4438 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4439 }
Adam Cohen432609a2014-03-13 17:03:22 -07004440 }
4441
4442 public void dismissIntroScreen() {
4443 markIntroScreenDismissed();
4444 if (showFirstRunActivity()) {
4445 // We delay hiding the intro view until the first run activity is showing. This
4446 // avoids a blip.
4447 mWorkspace.postDelayed(new Runnable() {
4448 @Override
4449 public void run() {
4450 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004451 if (mLauncherOverlayContainer != null) {
4452 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4453 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004454 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004455 }
4456 }, ACTIVITY_START_DELAY);
4457 } else {
4458 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004459 if (mLauncherOverlayContainer != null) {
4460 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4461 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004462 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004463 }
4464 changeWallpaperVisiblity(true);
4465 }
4466
4467 private void markIntroScreenDismissed() {
4468 SharedPreferences.Editor editor = mSharedPrefs.edit();
4469 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4470 editor.apply();
4471 }
4472
Sunny Goyal88d60f12014-09-08 03:47:29 -07004473 private void showFirstRunClings() {
4474 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4475 // on the device, then we always show the first run cling experience (or if there is no
4476 // launcher2). Otherwise, we prompt the user upon started for migration
4477 LauncherClings launcherClings = new LauncherClings(this);
4478 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4479 if (mModel.canMigrateFromOldLauncherDb(this)) {
4480 launcherClings.showMigrationCling();
4481 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004482 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004483 }
4484 }
4485 }
4486
Winson Chunga6945242014-01-08 14:04:34 -08004487 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004488 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4489 if (mHotseat != null) mHotseat.setAlpha(1f);
4490 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4491 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004492 }
4493
4494 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004495 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4496 if (mHotseat != null) mHotseat.setAlpha(0f);
4497 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4498 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004499 }
4500
Mathew Inwood72fbec12013-11-19 15:45:07 +00004501 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004502 // Called from search suggestion, not supported in other profiles.
4503 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4504 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4505 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4506 myUser);
4507 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004508 return null;
4509 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004510 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004511 }
4512
4513 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4514 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004515 // Called from search suggestion, not supported in other profiles.
4516 return createShortcutDragInfo(shortcutIntent, caption, icon,
4517 UserHandleCompat.myUserHandle());
4518 }
4519
4520 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4521 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004522 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004523 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004524 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004525 }
4526
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004527 protected void moveWorkspaceToDefaultScreen() {
4528 mWorkspace.moveToDefaultScreen(false);
4529 }
4530
Mathew Inwood72fbec12013-11-19 15:45:07 +00004531 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4532 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004533 mWorkspace.onExternalDragStartedWithItem(dragView);
4534 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004535 }
4536
Anjali Koppalf5d29132014-02-28 13:33:27 -08004537 @Override
4538 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004539 if (mLauncherCallbacks != null) {
4540 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4541 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004542 }
4543
Winson Chung80baf5a2010-08-09 16:03:15 -07004544 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004545 * Prints out out state for debugging.
4546 */
4547 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004548 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004549 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004550 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4551 Log.d(TAG, "mRestoring=" + mRestoring);
4552 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4553 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004554 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004555 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004556
Winson Chung785d2eb2011-04-14 16:08:02 -07004557 if (mAppsCustomizeContent != null) {
4558 mAppsCustomizeContent.dumpState();
4559 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004560 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004561 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004562
4563 @Override
4564 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4565 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004566 synchronized (sDumpLogs) {
4567 writer.println(" ");
4568 writer.println("Debug logs: ");
4569 for (int i = 0; i < sDumpLogs.size(); i++) {
4570 writer.println(" " + sDumpLogs.get(i));
4571 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004572 }
Adam Cohen9211d422014-10-07 18:14:53 -07004573 if (mLauncherCallbacks != null) {
4574 mLauncherCallbacks.dump(prefix, fd, writer, args);
4575 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004576 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004577
4578 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004579 if (DEBUG_DUMP_LOG) {
4580 synchronized (sDumpLogs) {
4581 Log.d(TAG, "");
4582 Log.d(TAG, "*********************");
4583 Log.d(TAG, "Launcher debug logs: ");
4584 for (int i = 0; i < sDumpLogs.size(); i++) {
4585 Log.d(TAG, " " + sDumpLogs.get(i));
4586 }
4587 Log.d(TAG, "*********************");
4588 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004589 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004590 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004591 }
4592
4593 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004594 addDumpLog(tag, log, null, debugLog);
4595 }
4596
4597 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004598 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004599 if (e != null) {
4600 Log.d(tag, log, e);
4601 } else {
4602 Log.d(tag, log);
4603 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004604 }
Winson Chungede41292013-09-19 16:27:36 -07004605 if (DEBUG_DUMP_LOG) {
4606 sDateStamp.setTime(System.currentTimeMillis());
4607 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004608 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4609 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004610 }
Winson Chungede41292013-09-19 16:27:36 -07004611 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004612 }
4613
Adam Cohen59400422014-03-05 18:07:04 -08004614 public static CustomAppWidget getCustomAppWidget(String name) {
4615 return sCustomAppWidgets.get(name);
4616 }
4617
4618 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4619 return sCustomAppWidgets;
4620 }
4621
Winson Chungede41292013-09-19 16:27:36 -07004622 public void dumpLogsToLocalData() {
4623 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004624 new AsyncTask<Void, Void, Void>() {
4625 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004626 boolean success = false;
4627 sDateStamp.setTime(sRunStart);
4628 String FILENAME = sDateStamp.getMonth() + "-"
4629 + sDateStamp.getDay() + "_"
4630 + sDateStamp.getHours() + "-"
4631 + sDateStamp.getMinutes() + "_"
4632 + sDateStamp.getSeconds() + ".txt";
4633
4634 FileOutputStream fos = null;
4635 File outFile = null;
4636 try {
4637 outFile = new File(getFilesDir(), FILENAME);
4638 outFile.createNewFile();
4639 fos = new FileOutputStream(outFile);
4640 } catch (Exception e) {
4641 e.printStackTrace();
4642 }
4643 if (fos != null) {
4644 PrintWriter writer = new PrintWriter(fos);
4645
4646 writer.println(" ");
4647 writer.println("Debug logs: ");
4648 synchronized (sDumpLogs) {
4649 for (int i = 0; i < sDumpLogs.size(); i++) {
4650 writer.println(" " + sDumpLogs.get(i));
4651 }
4652 }
4653 writer.close();
4654 }
4655 try {
4656 if (fos != null) {
4657 fos.close();
4658 success = true;
4659 }
4660 } catch (IOException e) {
4661 e.printStackTrace();
4662 }
Michael Jurka43467462013-11-14 12:03:58 +01004663 return null;
Winson Chungede41292013-09-19 16:27:36 -07004664 }
Michael Jurka43467462013-11-14 12:03:58 +01004665 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004666 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004668}
Michael Jurka2763be32011-02-24 11:19:57 -08004669
Michael Jurkaabded662011-03-04 12:06:57 -08004670interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004671 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004672 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004673 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004674 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004675 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004676}
Dan Sandlerd5024042014-01-09 15:01:33 -05004677
4678interface DebugIntents {
4679 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4680 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004681}