blob: 9c4632cc85052b23034c0bb2aab72aeb2f46c22e [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);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08001444 if (getOrCreateQsbBar() == null) {
1445 // Explicitly set it to null during initialization.
1446 mSearchDropTargetBar.setQsbSearchBar(null);
1447 }
Michael Jurkae0f5a612011-02-07 16:45:41 -08001448 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001449
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001450 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001451 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001452 mWeightWatcher = new WeightWatcher(this);
1453 mWeightWatcher.setAlpha(0.5f);
1454 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001455 new FrameLayout.LayoutParams(
1456 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001457 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001458 Gravity.BOTTOM)
1459 );
Adam Cohen39a06042013-07-19 14:30:12 -07001460
1461 boolean show = shouldShowWeightWatcher();
1462 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 }
1465
1466 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001467 * Sets the all apps button. This method is called from {@link Hotseat}.
1468 */
1469 public void setAllAppsButton(View allAppsButton) {
1470 mAllAppsButton = allAppsButton;
1471 }
1472
1473 public View getAllAppsButton() {
1474 return mAllAppsButton;
1475 }
1476
1477 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 * Creates a view representing a shortcut.
1479 *
1480 * @param info The data structure describing the shortcut.
1481 *
1482 * @return A View inflated from R.layout.application.
1483 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001484 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001486 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 }
1488
1489 /**
1490 * Creates a view representing a shortcut inflated from the specified resource.
1491 *
1492 * @param layoutResId The id of the XML layout used to create the shortcut.
1493 * @param parent The group the shortcut belongs to.
1494 * @param info The data structure describing the shortcut.
1495 *
1496 * @return A View inflated from layoutResId.
1497 */
Adam Cohen59400422014-03-05 18:07:04 -08001498 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001499 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001500 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001502 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 return favorite;
1504 }
1505
1506 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 * Add a shortcut to the workspace.
1508 *
1509 * @param data The intent describing the shortcut.
1510 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001512 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001513 int cellY) {
1514 int[] cellXY = mTmpAddItemCellCoordinates;
1515 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001516 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001517
Michael Jurkad3ef3062010-11-23 16:23:58 -08001518 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001519
Sunny Goyal2350bc92014-10-14 16:42:54 -07001520 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001521 if (info == null) {
1522 return;
1523 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001524 final View view = createShortcut(info);
1525
Adam Cohenfbba09b2011-07-18 21:43:05 -07001526 // First we check if we already know the exact location where we want to add this item.
1527 if (cellX >= 0 && cellY >= 0) {
1528 cellXY[0] = cellX;
1529 cellXY[1] = cellY;
1530 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001531
1532 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001533 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001534 true, null,null)) {
1535 return;
1536 }
1537 DragObject dragObject = new DragObject();
1538 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001539 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1540 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001541 return;
1542 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001543 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001544 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001545 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001546 foundCellSpan = (result != null);
1547 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001548 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001549 }
1550
1551 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001552 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001553 return;
1554 }
1555
Adam Cohendcd297f2013-06-18 13:13:40 -07001556 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557
1558 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001559 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001560 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
1562 }
1563
Adam Cohen2f093b62012-04-30 18:59:53 -07001564 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1565 int minHeight) {
1566 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001567 // We want to account for the extra amount of padding that we are adding to the widget
1568 // to ensure that it gets the full amount of space that it has requested
1569 int requiredWidth = minWidth + padding.left + padding.right;
1570 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001571 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001572 }
1573
Adam Cohen2f093b62012-04-30 18:59:53 -07001574 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1575 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001576 }
1577
Adam Cohen2f093b62012-04-30 18:59:53 -07001578 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1579 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001580 }
1581
Adam Cohen2f093b62012-04-30 18:59:53 -07001582 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1583 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001584 }
1585
Adam Cohen2f093b62012-04-30 18:59:53 -07001586 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1587 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001588 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001589 }
1590
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001592 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001594 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 */
Adam Cohen59400422014-03-05 18:07:04 -08001596 private void completeAddAppWidget(int appWidgetId, long container, long screenId,
1597 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1598
1599 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001600 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001601 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1602 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001603 }
Romain Guycbb89e42009-06-08 15:52:54 -07001604
Adam Cohen59400422014-03-05 18:07:04 -08001605 if (appWidgetInfo.isCustomWidget) {
1606 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001607 }
1608
Adam Cohen59400422014-03-05 18:07:04 -08001609 LauncherAppWidgetInfo launcherInfo;
1610 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1611 launcherInfo.spanX = info.spanX;
1612 launcherInfo.spanY = info.spanY;
1613 launcherInfo.minSpanX = info.minSpanX;
1614 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001615 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001616
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001618 container, screenId, info.cellX, info.cellY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619
1620 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001621 if (hostView == null) {
1622 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001623 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1624 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001625 } else {
1626 // The AppWidgetHostView has already been inflated and instantiated
1627 launcherInfo.hostView = hostView;
1628 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001630 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001631 launcherInfo.notifyWidgetSizeChanged(this);
1632
Adam Cohen59400422014-03-05 18:07:04 -08001633 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1634 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001635
1636 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001638 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 }
Romain Guycbb89e42009-06-08 15:52:54 -07001640
Adam Cohended9f8d2010-11-03 13:25:16 -07001641 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1642 @Override
1643 public void onReceive(Context context, Intent intent) {
1644 final String action = intent.getAction();
1645 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1646 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001647 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001648 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001649
Winson Chungc100e8e2011-08-09 16:02:43 -07001650 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001651 // processing a multi-step drop
Winson Chungb745afb2015-03-02 11:51:23 -08001652 if (mAppsView != null && mAppsCustomizeTabHost != null &&
1653 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001654 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001655 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001656 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1657 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001658 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001659 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1660 mModel.resetLoadedState(false, true);
1661 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1662 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1663 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1664 mModel.resetLoadedState(false, true);
1665 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1666 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1667 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001668 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1669 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1670 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001671 }
1672 }
1673 };
1674
1675 @Override
1676 public void onAttachedToWindow() {
1677 super.onAttachedToWindow();
1678
1679 // Listen for broadcasts related to user-presence
1680 final IntentFilter filter = new IntentFilter();
1681 filter.addAction(Intent.ACTION_SCREEN_OFF);
1682 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001683 // For handling managed profiles
1684 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1685 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001686 if (ENABLE_DEBUG_INTENTS) {
1687 filter.addAction(DebugIntents.DELETE_DATABASE);
1688 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1689 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001690 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001691 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001692 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001693 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001694 mVisible = true;
1695 }
1696
Adam Cohen0b395352014-06-09 22:54:36 +00001697 /**
1698 * Sets up transparent navigation and status bars in LMP.
1699 * This method is a no-op for other platform versions.
1700 */
Sunny Goyald0091012015-01-20 15:55:34 -08001701 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001702 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001703 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001704 Window window = getWindow();
1705 window.getAttributes().systemUiVisibility |=
1706 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1707 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1708 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1709 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1710 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1711 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1712 window.setStatusBarColor(Color.TRANSPARENT);
1713 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001714 }
1715 }
1716
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 @Override
1718 public void onDetachedFromWindow() {
1719 super.onDetachedFromWindow();
1720 mVisible = false;
1721
Adam Cohend113e0c2010-11-11 10:48:05 -08001722 if (mAttached) {
1723 unregisterReceiver(mReceiver);
1724 mAttached = false;
1725 }
Winson Chungb745afb2015-03-02 11:51:23 -08001726 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 }
1728
1729 public void onWindowVisibilityChanged(int visibility) {
1730 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001731 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001732 // The following code used to be in onResume, but it turns out onResume is called when
1733 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1734 // is a more appropriate event to handle
1735 if (mVisible) {
1736 mAppsCustomizeTabHost.onWindowVisible();
1737 if (!mWorkspaceLoading) {
1738 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001739 // We want to let Launcher draw itself at least once before we force it to build
1740 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001741 // apps is nice and speedy.
1742 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001743 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001744 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001745 if (mStarted) return;
1746 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001747 // We delay the layer building a bit in order to give
1748 // other message processing a time to run. In particular
1749 // this avoids a delay in hiding the IME if it was
1750 // currently shown, because doing that may involve
1751 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001752 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001753 final ViewTreeObserver.OnDrawListener listener = this;
1754 mWorkspace.post(new Runnable() {
1755 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001756 if (mWorkspace != null &&
1757 mWorkspace.getViewTreeObserver() != null) {
1758 mWorkspace.getViewTreeObserver().
1759 removeOnDrawListener(listener);
1760 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001761 }
1762 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001763 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001764 }
1765 });
1766 }
1767 clearTypedText();
1768 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001769 }
1770
1771 private void sendAdvanceMessage(long delay) {
1772 mHandler.removeMessages(ADVANCE_MSG);
1773 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1774 mHandler.sendMessageDelayed(msg, delay);
1775 mAutoAdvanceSentTime = System.currentTimeMillis();
1776 }
1777
Winson Chungb745afb2015-03-02 11:51:23 -08001778 private void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001779 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1780 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1781 mAutoAdvanceRunning = autoAdvanceRunning;
1782 if (autoAdvanceRunning) {
1783 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1784 sendAdvanceMessage(delay);
1785 } else {
1786 if (!mWidgetsToAdvance.isEmpty()) {
1787 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1788 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1789 }
1790 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001791 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001792 }
1793 }
1794 }
1795
1796 private final Handler mHandler = new Handler() {
1797 @Override
1798 public void handleMessage(Message msg) {
1799 if (msg.what == ADVANCE_MSG) {
1800 int i = 0;
1801 for (View key: mWidgetsToAdvance.keySet()) {
1802 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1803 final int delay = mAdvanceStagger * i;
1804 if (v instanceof Advanceable) {
1805 postDelayed(new Runnable() {
1806 public void run() {
1807 ((Advanceable) v).advance();
1808 }
1809 }, delay);
1810 }
1811 i++;
1812 }
1813 sendAdvanceMessage(mAdvanceInterval);
1814 }
1815 }
1816 };
1817
1818 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001819 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001820 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1821 if (v instanceof Advanceable) {
1822 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001823 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001824 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001825 }
1826 }
1827
1828 void removeWidgetToAutoAdvance(View hostView) {
1829 if (mWidgetsToAdvance.containsKey(hostView)) {
1830 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001831 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001832 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001833 }
1834
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001836 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001837 launcherInfo.hostView = null;
1838 }
1839
Winson Chung93eef082012-03-23 15:59:27 -07001840 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1841 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1842 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001843 }
1844
Winson Chunga6945242014-01-08 14:04:34 -08001845 public DragLayer getDragLayer() {
1846 return mDragLayer;
1847 }
1848
Winson Chungb745afb2015-03-02 11:51:23 -08001849 public AppsContainerView getAppsView() {
1850 return mAppsView;
1851 }
1852
1853 public AppsCustomizeTabHost getWidgetsView() {
1854 return mAppsCustomizeTabHost;
1855 }
1856
Winson Chunga6945242014-01-08 14:04:34 -08001857 public Workspace getWorkspace() {
1858 return mWorkspace;
1859 }
1860
1861 public Hotseat getHotseat() {
1862 return mHotseat;
1863 }
1864
Jorim Jaggid017f882014-01-14 17:08:48 -08001865 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001866 return mOverviewPanel;
1867 }
1868
1869 public SearchDropTargetBar getSearchBar() {
1870 return mSearchDropTargetBar;
1871 }
1872
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001873 public LauncherAppWidgetHost getAppWidgetHost() {
1874 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 }
Romain Guycbb89e42009-06-08 15:52:54 -07001876
Winson Chunga9abd0e2010-10-27 17:18:37 -07001877 public LauncherModel getModel() {
1878 return mModel;
1879 }
1880
Winson Chunga6945242014-01-08 14:04:34 -08001881 protected SharedPreferences getSharedPrefs() {
1882 return mSharedPrefs;
1883 }
1884
Bjorn Bringertc459e522013-06-07 19:36:01 +01001885 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001886 getWindow().closeAllPanels();
1887
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001888 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001889 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001890 }
1891
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 @Override
1893 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001894 long startTime = 0;
1895 if (DEBUG_RESUME_TIME) {
1896 startTime = System.currentTimeMillis();
1897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 super.onNewIntent(intent);
1899
1900 // Close the menu
1901 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001902 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001903 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001904
Adam Cohened307df2013-10-02 09:37:31 -07001905 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1906 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1907 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001908
Adam Cohen6fecd412013-10-02 17:41:50 -07001909 if (mWorkspace == null) {
1910 // Can be cases where mWorkspace is null, this prevents a NPE
1911 return;
1912 }
1913 Folder openFolder = mWorkspace.getOpenFolder();
1914 // In all these cases, only animate if we're already on home
1915 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001916
1917 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1918 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001919 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001920 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001921 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001922 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001923
Adam Cohen6fecd412013-10-02 17:41:50 -07001924 closeFolder();
1925 exitSpringLoadedDragMode();
1926
1927 // If we are already on home, then just animate back to the workspace,
1928 // otherwise, just wait until onResume to set the state back to Workspace
1929 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001930 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001931 } else {
1932 mOnResumeState = State.WORKSPACE;
1933 }
1934
1935 final View v = getWindow().peekDecorView();
1936 if (v != null && v.getWindowToken() != null) {
1937 InputMethodManager imm = (InputMethodManager)getSystemService(
1938 INPUT_METHOD_SERVICE);
1939 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1940 }
1941
Winson Chungb745afb2015-03-02 11:51:23 -08001942 // Reset the apps view
1943 if (!alreadyOnHome && mAppsView != null) {
1944 mAppsView.scrollToTop();
1945 }
1946
Adam Cohen6fecd412013-10-02 17:41:50 -07001947 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001948 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001949 mAppsCustomizeTabHost.reset();
1950 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001951
Adam Cohen9211d422014-10-07 18:14:53 -07001952 if (mLauncherCallbacks != null) {
1953 mLauncherCallbacks.onHomeIntent();
1954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 }
Adam Cohened307df2013-10-02 09:37:31 -07001956
Michael Jurka447bf842013-05-15 14:52:15 +02001957 if (DEBUG_RESUME_TIME) {
1958 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960
Adam Cohen9211d422014-10-07 18:14:53 -07001961 if (mLauncherCallbacks != null) {
1962 mLauncherCallbacks.onNewIntent(intent);
1963 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001964 }
1965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001967 public void onRestoreInstanceState(Bundle state) {
1968 super.onRestoreInstanceState(state);
1969 for (int page: mSynchronouslyBoundPages) {
1970 mWorkspace.restoreInstanceStateForChild(page);
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
1974 @Override
1975 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001976 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001977 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1978 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001979 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001980 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981
Michael Jurka883f55b2010-10-21 15:47:14 -07001982 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001983 // We close any open folder since it will not be re-opened, and we need to make sure
1984 // this state is reflected.
1985 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001988 mWaitingForResult) {
1989 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001990 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1992 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001993 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1994 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1995 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001996 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
1998
1999 if (mFolderInfo != null && mWaitingForResult) {
2000 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2001 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2002 }
Michael Jurka946ad472010-07-09 18:05:18 -07002003
Winson Chung785d2eb2011-04-14 16:08:02 -07002004 // Save the current AppsCustomize tab
2005 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08002006 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2007 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002008 if (currentTabTag != null) {
2009 outState.putString("apps_customize_currentTab", currentTabTag);
2010 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002011 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2012 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002013 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002014 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002015
2016 if (mLauncherCallbacks != null) {
2017 mLauncherCallbacks.onSaveInstanceState(outState);
2018 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 }
2020
2021 @Override
2022 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002024
Winson Chunge7a03942011-08-05 15:05:12 -07002025 // Remove all pending runnables
2026 mHandler.removeMessages(ADVANCE_MSG);
2027 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002028 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002029
Winson Chungcd2b0142011-06-08 16:02:26 -07002030 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002031 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002032
2033 // It's possible to receive onDestroy after a new Launcher activity has
2034 // been created. In this case, don't interfere with the new Launcher.
2035 if (mModel.isCurrentCallbacks(this)) {
2036 mModel.stopLoader();
2037 app.setLauncher(null);
2038 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002041 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002043 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002045 mAppWidgetHost = null;
2046
2047 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048
2049 TextKeyListener.getInstance().release();
2050
Winson Chung81b52252012-08-27 15:34:29 -07002051 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2052 // to prevent leaking Launcher activities on orientation change.
2053 if (mModel != null) {
2054 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2055 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002056
2057 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002058 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002059
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002060 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002061 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002062 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002063 mWorkspace = null;
2064 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002065
Michael Jurka2ecf9952012-06-18 12:52:28 -07002066 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002067
2068 if (mLauncherCallbacks != null) {
2069 mLauncherCallbacks.onDestroy();
2070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 }
2072
Adam Cohena9cf38f2011-05-02 15:36:58 -07002073 public DragController getDragController() {
2074 return mDragController;
2075 }
2076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 @Override
2078 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002079 if (requestCode >= 0) {
2080 setWaitingForResult(true);
2081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 super.startActivityForResult(intent, requestCode);
2083 }
2084
Winson Chung70d72102011-08-12 11:24:35 -07002085 /**
2086 * Indicates that we want global search for this activity by setting the globalSearch
2087 * argument for {@link #startSearch} to true.
2088 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002090 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002092
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002093 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002094
Karl Rosaen138a0412009-04-23 19:00:21 -07002095 if (initialQuery == null) {
2096 // Use any text typed in the launcher as the initial query
2097 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002098 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 if (appSearchData == null) {
2100 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002101 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 }
Winson Chungadf0c182012-08-23 14:59:07 -07002103 Rect sourceBounds = new Rect();
2104 if (mSearchDropTargetBar != null) {
2105 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2106 }
Romain Guycbb89e42009-06-08 15:52:54 -07002107
Ian Parkinson047036e2014-09-01 15:40:53 +01002108 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002109 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002110 if (clearTextImmediately) {
2111 clearTypedText();
2112 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002113 }
2114
Ian Parkinson047036e2014-09-01 15:40:53 +01002115 /**
2116 * Start a text search.
2117 *
2118 * @return {@code true} if the search will start immediately, so any further keypresses
2119 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2120 * to buffer keypresses.
2121 */
2122 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002123 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002124 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2125 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2126 sourceBounds);
2127 }
2128
Michael Jurkaa33411c2012-06-14 16:18:21 -07002129 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002130 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002131 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002132 }
2133
2134 /**
2135 * Starts the global search activity. This code is a copied from SearchManager
2136 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002137 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002138 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002139 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002140 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2141 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2142 if (globalSearchActivity == null) {
2143 Log.w(TAG, "No global search activity found.");
2144 return;
2145 }
2146 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2147 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2148 intent.setComponent(globalSearchActivity);
2149 // Make sure that we have a Bundle to put source in
2150 if (appSearchData == null) {
2151 appSearchData = new Bundle();
2152 } else {
2153 appSearchData = new Bundle(appSearchData);
2154 }
Adam Cohen9211d422014-10-07 18:14:53 -07002155 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002156 if (!appSearchData.containsKey("source")) {
2157 appSearchData.putString("source", getPackageName());
2158 }
2159 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2160 if (!TextUtils.isEmpty(initialQuery)) {
2161 intent.putExtra(SearchManager.QUERY, initialQuery);
2162 }
2163 if (selectInitialQuery) {
2164 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2165 }
2166 intent.setSourceBounds(sourceBounds);
2167 try {
2168 startActivity(intent);
2169 } catch (ActivityNotFoundException ex) {
2170 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 }
2173
Yura4f93ec62014-02-04 14:15:21 +00002174 public boolean isOnCustomContent() {
2175 return mWorkspace.isOnOrMovingToCustomContent();
2176 }
2177
Winson Chung70d72102011-08-12 11:24:35 -07002178 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002179 public boolean onPrepareOptionsMenu(Menu menu) {
2180 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002181 if (!isOnCustomContent()) {
2182 // Close any open folders
2183 closeFolder();
2184 // Stop resizing any widgets
2185 mWorkspace.exitWidgetResizeMode();
2186 if (!mWorkspace.isInOverviewMode()) {
2187 // Show the overview mode
2188 showOverviewMode(true);
2189 } else {
2190 showWorkspace(true);
2191 }
Winson Chunge0298742014-01-17 12:03:00 -08002192 }
Adam Cohen9211d422014-10-07 18:14:53 -07002193 if (mLauncherCallbacks != null) {
2194 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2195 }
2196
Michael Jurkab94f3f82013-09-11 18:08:54 +02002197 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002198 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002200 @Override
2201 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002202 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002203 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002204 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002205 }
2206
Joe Onorato9c1289c2009-08-17 11:03:03 -04002207 public boolean isWorkspaceLocked() {
2208 return mWorkspaceLoading || mWaitingForResult;
2209 }
2210
Adam Cohen517a7f52014-03-01 12:12:59 -08002211 public boolean isWorkspaceLoading() {
2212 return mWorkspaceLoading;
2213 }
2214
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002215 private void setWorkspaceLoading(boolean value) {
2216 boolean isLocked = isWorkspaceLocked();
2217 mWorkspaceLoading = value;
2218 if (isLocked != isWorkspaceLocked()) {
2219 onWorkspaceLockedChanged();
2220 }
2221 }
2222
2223 private void setWaitingForResult(boolean value) {
2224 boolean isLocked = isWorkspaceLocked();
2225 mWaitingForResult = value;
2226 if (isLocked != isWorkspaceLocked()) {
2227 onWorkspaceLockedChanged();
2228 }
2229 }
2230
Adam Cohen9211d422014-10-07 18:14:53 -07002231 protected void onWorkspaceLockedChanged() {
2232 if (mLauncherCallbacks != null) {
2233 mLauncherCallbacks.onWorkspaceLockedChanged();
2234 }
2235 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002236
Michael Jurka0280c3b2010-09-17 15:00:07 -07002237 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002238 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002239 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002240 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2241 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002242 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002243 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002244 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002245
Sunny Goyale6b63a32015-01-16 16:14:29 -08002246 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002247 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002248 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2249 }
2250
Sunny Goyale6b63a32015-01-16 16:14:29 -08002251 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002252 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2253 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002254 if (appWidgetInfo.configure != null) {
2255 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002256 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002257
Bjorn Bringert7984c942009-12-09 15:38:25 +00002258 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002259 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2260 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002263 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002264 Runnable onComplete = new Runnable() {
2265 @Override
2266 public void run() {
2267 // Exit spring loaded mode if necessary after adding the widget
2268 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2269 null);
2270 }
2271 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002272 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002273 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002274 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275 }
2276 }
Romain Guycbb89e42009-06-08 15:52:54 -07002277
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002278 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002279 // Close any folders that may be open.
2280 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002281 mWorkspace.moveToCustomContentScreen(animate);
2282 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002283
2284 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2285 int[] cell, int spanX, int spanY) {
2286 switch (info.itemType) {
2287 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2288 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2289 int span[] = new int[2];
2290 span[0] = spanX;
2291 span[1] = spanY;
2292 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2293 container, screenId, cell, span);
2294 break;
2295 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2296 processShortcutFromDrop(info.componentName, container, screenId, cell);
2297 break;
2298 default:
2299 throw new IllegalStateException("Unknown item type: " + info.itemType);
2300 }
2301 }
2302
Adam Cohenfbba09b2011-07-18 21:43:05 -07002303 /**
2304 * Process a shortcut drop.
2305 *
2306 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002307 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002308 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002309 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002310 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2311 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002312 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002313 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002314 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002315 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002316
2317 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002318 mPendingAddInfo.cellX = cell[0];
2319 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002320 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002321
2322 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2323 createShortcutIntent.setComponent(componentName);
2324 processShortcut(createShortcutIntent);
2325 }
2326
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 /**
2328 * Process a widget drop.
2329 *
2330 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002332 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002333 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002334 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2335 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002336 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002338 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002339 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002340 mPendingAddInfo.minSpanX = info.minSpanX;
2341 mPendingAddInfo.minSpanY = info.minSpanY;
2342
Adam Cohenfbba09b2011-07-18 21:43:05 -07002343 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002344 mPendingAddInfo.cellX = cell[0];
2345 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002346 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002347 if (span != null) {
2348 mPendingAddInfo.spanX = span[0];
2349 mPendingAddInfo.spanY = span[1];
2350 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351
Adam Cohened66b2b2012-01-23 17:28:51 -08002352 AppWidgetHostView hostView = info.boundWidget;
2353 int appWidgetId;
2354 if (hostView != null) {
2355 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002356 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002357 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002358 // In this case, we either need to start an activity to get permission to bind
2359 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002360 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002361 Bundle options = info.bindOptions;
2362
Sunny Goyalffe83f12014-08-14 17:39:34 -07002363 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2364 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002365 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002366 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002367 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002368 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002369 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2370 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2371 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002372 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2373 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002374 // TODO: we need to make sure that this accounts for the options bundle.
2375 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002376 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2377 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002378 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002379 }
2380
Joe Onoratodeb98af2010-02-19 14:59:39 -08002381 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002382 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 }
2384
Winson Chung24ab2f12010-09-16 14:10:47 -07002385 void processWallpaper(Intent intent) {
2386 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2387 }
2388
Adam Cohendcd297f2013-06-18 13:13:40 -07002389 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002390 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002391 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002392 folderInfo.title = getText(R.string.folder_name);
2393
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002395 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002396 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002397 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398
2399 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002400 FolderIcon newFolder =
2401 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002402 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002403 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002404 // Force measure the new folder icon
2405 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2406 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002407 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 }
Romain Guycbb89e42009-06-08 15:52:54 -07002409
Joe Onorato9c1289c2009-08-17 11:03:03 -04002410 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002411 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002412 }
2413
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002414 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002415 if (mLauncherCallbacks != null) {
2416 return mLauncherCallbacks.getWallpaperPickerComponent();
2417 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002418 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002419 }
2420
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002421 /**
2422 * Registers various content observers. The current implementation registers
2423 * only a favorites observer to keep track of the favorites applications.
2424 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002425 private void registerContentObservers() {
2426 ContentResolver resolver = getContentResolver();
2427 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2428 true, mWidgetObserver);
2429 }
2430
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 @Override
2432 public boolean dispatchKeyEvent(KeyEvent event) {
2433 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2434 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002436 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002437 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002438 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002439 dumpState();
2440 return true;
2441 }
2442 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002443 }
2444 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2445 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002446 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447 return true;
2448 }
2449 }
2450
2451 return super.dispatchKeyEvent(event);
2452 }
2453
Joe Onorato88ec0992009-11-19 13:16:06 -08002454 @Override
2455 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002456 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2457 return;
2458 }
2459
Adam Cohenc9735cf2015-01-23 16:11:55 -08002460 if (LauncherAppState.getInstance().getAccessibilityDelegate().onBackPressed()) {
2461 return;
2462 }
2463
Winson Chungb745afb2015-03-02 11:51:23 -08002464 if (isAppsViewVisible()) {
2465 showWorkspace(true);
2466 } else if (isWidgetsViewVisible()) {
2467 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002468 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002469 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002470 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002471 Folder openFolder = mWorkspace.getOpenFolder();
2472 if (openFolder.isEditingName()) {
2473 openFolder.dismissEditingName();
2474 } else {
2475 closeFolder();
2476 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002477 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002478 mWorkspace.exitWidgetResizeMode();
2479
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002480 // Back button is a no-op here, but give at least some feedback for the button press
2481 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002482 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002483 }
2484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002486 * Re-listen when widgets are reset.
2487 */
2488 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002489 if (mAppWidgetHost != null) {
2490 mAppWidgetHost.startListening();
2491 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002492 }
2493
2494 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 * Launches the intent referred by the clicked shortcut.
2496 *
2497 * @param v The view representing the clicked shortcut.
2498 */
2499 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002500 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2501 // view has detached (it's possible for this to happen if the view is removed mid touch).
2502 if (v.getWindowToken() == null) {
2503 return;
2504 }
2505
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002506 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002507 return;
2508 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002509
Adam Cohen1697b792013-09-17 19:08:21 -07002510 if (v instanceof Workspace) {
2511 if (mWorkspace.isInOverviewMode()) {
2512 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002513 }
2514 return;
2515 }
2516
2517 if (v instanceof CellLayout) {
2518 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002519 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002520 }
2521 }
2522
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002524 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002525 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002527 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002528 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002529 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002530 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002531 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002532 } else if (tag instanceof AppInfo) {
2533 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002534 } else if (tag instanceof LauncherAppWidgetInfo) {
2535 if (v instanceof PendingAppWidgetHostView) {
2536 onClickPendingWidget((PendingAppWidgetHostView) v);
2537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002538 }
2539 }
2540
Sunny Goyal508da152014-08-14 10:53:27 -07002541 public void onClickPagedViewIcon(View v) {
2542 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002543 if (mLauncherCallbacks != null) {
2544 mLauncherCallbacks.onClickPagedViewIcon(v);
2545 }
Sunny Goyal508da152014-08-14 10:53:27 -07002546 }
2547
Michael Jurka0e260592010-06-30 17:07:39 -07002548 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002549 return false;
2550 }
2551
Michael Jurkaaf442092010-06-10 17:01:57 -07002552 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002553 * Event handler for the app widget view which has not fully restored.
2554 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002555 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002556 if (mIsSafeModeEnabled) {
2557 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2558 return;
2559 }
2560
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002561 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002562 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002563 int widgetId = info.appWidgetId;
2564 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2565 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002566 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2567 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002568 mPendingAddInfo.copyFrom(info);
2569 mPendingAddWidgetId = widgetId;
2570
Sunny Goyalffe83f12014-08-14 17:39:34 -07002571 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2572 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002573 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002574 } else if (info.installProgress < 0) {
2575 // The install has not been queued
2576 final String packageName = info.providerName.getPackageName();
2577 showBrokenAppInstallDialog(packageName,
2578 new DialogInterface.OnClickListener() {
2579 public void onClick(DialogInterface dialog, int id) {
2580 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2581 }
2582 });
2583 } else {
2584 // Download has started.
2585 final String packageName = info.providerName.getPackageName();
2586 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002587 }
2588 }
2589
2590 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002591 * Event handler for the "grid" button that appears on the home screen, which
2592 * enters all apps mode.
2593 *
2594 * @param v The view that was clicked.
2595 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002596 protected void onClickAllAppsButton(View v) {
2597 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002598 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002599 showWorkspace(true);
2600 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002601 showAppsView(true /* animated */, false /* resetListToTop */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002602 }
Adam Cohen9211d422014-10-07 18:14:53 -07002603 if (mLauncherCallbacks != null) {
2604 mLauncherCallbacks.onClickAllAppsButton(v);
2605 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002606 }
2607
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002608 private void showBrokenAppInstallDialog(final String packageName,
2609 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002610 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002611 .setTitle(R.string.abandoned_promises_title)
2612 .setMessage(R.string.abandoned_promise_explanation)
2613 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2614 .setNeutralButton(R.string.abandoned_clean_this,
2615 new DialogInterface.OnClickListener() {
2616 public void onClick(DialogInterface dialog, int id) {
2617 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2618 mWorkspace.removeAbandonedPromise(packageName, user);
2619 }
2620 })
2621 .create().show();
2622 return;
2623 }
2624
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 /**
2626 * Event handler for an app shortcut click.
2627 *
2628 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2629 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002630 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002631 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2632 Object tag = v.getTag();
2633 if (!(tag instanceof ShortcutInfo)) {
2634 throw new IllegalArgumentException("Input must be a Shortcut");
2635 }
2636
2637 // Open shortcut
2638 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002639
2640 if (shortcut.isDisabled != 0) {
2641 int error = R.string.activity_not_available;
2642 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2643 error = R.string.safemode_shortcut_error;
2644 }
2645 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2646 return;
2647 }
2648
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002649 final Intent intent = shortcut.intent;
2650
2651 // Check for special shortcuts
2652 if (intent.getComponent() != null) {
2653 final String shortcutClass = intent.getComponent().getClassName();
2654
2655 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2656 MemoryDumpActivity.startDump(this);
2657 return;
2658 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2659 toggleShowWeightWatcher();
2660 return;
2661 }
2662 }
2663
Chris Wren40c5ed32014-06-24 18:24:23 -04002664 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002665 if ((v instanceof BubbleTextView)
2666 && shortcut.isPromise()
2667 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002668 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002669 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002670 new DialogInterface.OnClickListener() {
2671 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002672 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002673 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002675 return;
2676 }
2677
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002678 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002679 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002680
2681 if (mLauncherCallbacks != null) {
2682 mLauncherCallbacks.onClickAppShortcut(v);
2683 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 }
2685
Sunny Goyal508da152014-08-14 10:53:27 -07002686 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002687 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002688 final ShortcutInfo shortcut;
2689 final Intent intent;
2690 if (tag instanceof ShortcutInfo) {
2691 shortcut = (ShortcutInfo) tag;
2692 intent = shortcut.intent;
2693 int[] pos = new int[2];
2694 v.getLocationOnScreen(pos);
2695 intent.setSourceBounds(new Rect(pos[0], pos[1],
2696 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002697
Sunny Goyal508da152014-08-14 10:53:27 -07002698 } else if (tag instanceof AppInfo) {
2699 shortcut = null;
2700 intent = ((AppInfo) tag).intent;
2701 } else {
2702 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2703 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002704
2705 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002706 mStats.recordLaunch(intent, shortcut);
2707
2708 if (success && v instanceof BubbleTextView) {
2709 mWaitingForResume = (BubbleTextView) v;
2710 mWaitingForResume.setStayPressed(true);
2711 }
2712 }
2713
2714 /**
2715 * Event handler for a folder icon click.
2716 *
2717 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2718 */
2719 protected void onClickFolderIcon(View v) {
2720 if (LOGD) Log.d(TAG, "onClickFolder");
2721 if (!(v instanceof FolderIcon)){
2722 throw new IllegalArgumentException("Input must be a FolderIcon");
2723 }
2724
2725 FolderIcon folderIcon = (FolderIcon) v;
2726 final FolderInfo info = folderIcon.getFolderInfo();
2727 Folder openFolder = mWorkspace.getFolderForTag(info);
2728
2729 // If the folder info reports that the associated folder is open, then verify that
2730 // it is actually opened. There have been a few instances where this gets out of sync.
2731 if (info.opened && openFolder == null) {
2732 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2733 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2734 info.opened = false;
2735 }
2736
2737 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2738 // Close any open folder
2739 closeFolder();
2740 // Open the requested folder
2741 openFolder(folderIcon);
2742 } else {
2743 // Find the open folder...
2744 int folderScreen;
2745 if (openFolder != null) {
2746 folderScreen = mWorkspace.getPageForView(openFolder);
2747 // .. and close it
2748 closeFolder(openFolder);
2749 if (folderScreen != mWorkspace.getCurrentPage()) {
2750 // Close any folder open on the current screen
2751 closeFolder();
2752 // Pull the folder onto this screen
2753 openFolder(folderIcon);
2754 }
2755 }
2756 }
Adam Cohen9211d422014-10-07 18:14:53 -07002757
2758 if (mLauncherCallbacks != null) {
2759 mLauncherCallbacks.onClickFolderIcon(v);
2760 }
Michael Jurka5130e402011-10-13 04:55:35 -07002761 }
2762
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002763 /**
2764 * Event handler for the (Add) Widgets button that appears after a long press
2765 * on the home screen.
2766 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002767 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002768 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002769 if (mIsSafeModeEnabled) {
2770 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2771 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002772 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002773 if (mLauncherCallbacks != null) {
2774 mLauncherCallbacks.onClickAddWidgetButton(view);
2775 }
Adam Cohen9211d422014-10-07 18:14:53 -07002776 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002777 }
2778
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002779 /**
2780 * Event handler for the wallpaper picker button that appears after a long press
2781 * on the home screen.
2782 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002783 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002784 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2785 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2786 pickWallpaper.setComponent(getWallpaperPickerComponent());
2787 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002788
2789 if (mLauncherCallbacks != null) {
2790 mLauncherCallbacks.onClickWallpaperPicker(v);
2791 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002792 }
2793
2794 /**
2795 * Event handler for a click on the settings button that appears after a long press
2796 * on the home screen.
2797 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002798 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002799 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002800 if (mLauncherCallbacks != null) {
2801 mLauncherCallbacks.onClickSettingsButton(v);
2802 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002803 }
2804
Michael Jurka5130e402011-10-13 04:55:35 -07002805 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002806 // Provide the same haptic feedback that the system offers for virtual keys.
2807 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002808 }
2809
Adam Cohen61f560d2013-09-30 15:58:20 -07002810 public void performHapticFeedbackOnTouchDown(View v) {
2811 // Provide the same haptic feedback that the system offers for virtual keys.
2812 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2813 }
2814
2815 public View.OnTouchListener getHapticFeedbackTouchListener() {
2816 if (mHapticFeedbackTouchListener == null) {
2817 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2818 @Override
2819 public boolean onTouch(View v, MotionEvent event) {
2820 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2821 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2822 }
2823 return false;
2824 }
2825 };
2826 }
2827 return mHapticFeedbackTouchListener;
2828 }
2829
Adam Cohen9211d422014-10-07 18:14:53 -07002830 public void onDragStarted(View view) {
2831 if (isOnCustomContent()) {
2832 // Custom content screen doesn't participate in drag and drop. If on custom
2833 // content screen, move to default.
2834 moveWorkspaceToDefaultScreen();
2835 }
2836
2837 if (mLauncherCallbacks != null) {
2838 mLauncherCallbacks.onDragStarted(view);
2839 }
2840 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002841
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002842 /**
2843 * Called when the user stops interacting with the launcher.
2844 * This implies that the user is now on the homescreen and is not doing housekeeping.
2845 */
Adam Cohen9211d422014-10-07 18:14:53 -07002846 protected void onInteractionEnd() {
2847 if (mLauncherCallbacks != null) {
2848 mLauncherCallbacks.onInteractionEnd();
2849 }
2850 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002851
2852 /**
2853 * Called when the user starts interacting with the launcher.
2854 * The possible interactions are:
2855 * - open all apps
2856 * - reorder an app shortcut, or a widget
2857 * - open the overview mode.
2858 * This is a good time to stop doing things that only make sense
2859 * when the user is on the homescreen and not doing housekeeping.
2860 */
Adam Cohen9211d422014-10-07 18:14:53 -07002861 protected void onInteractionBegin() {
2862 if (mLauncherCallbacks != null) {
2863 mLauncherCallbacks.onInteractionBegin();
2864 }
2865 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002866
Kenny Guyf07af7b2014-07-31 11:39:16 +01002867 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002868 try {
2869 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002870 launcherApps.showAppDetailsForProfile(componentName, user);
2871 } catch (SecurityException e) {
2872 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2873 Log.e(TAG, "Launcher does not have permission to launch settings");
2874 } catch (ActivityNotFoundException e) {
2875 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2876 Log.e(TAG, "Unable to launch settings");
2877 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002878 }
2879
Michael Jurka1e2f4652013-07-08 18:03:46 -07002880 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002881 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2882 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002883 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002884 // System applications cannot be installed. For now, show a toast explaining that.
2885 // We may give them the option of disabling apps this way.
2886 int messageId = R.string.uninstall_system_app_text;
2887 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002888 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002889 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002890 String packageName = componentName.getPackageName();
2891 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002892 Intent intent = new Intent(
2893 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002894 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2895 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002896 if (user != null) {
2897 user.addToIntent(intent, Intent.EXTRA_USER);
2898 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002899 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002900 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002901 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002902 }
2903
Michael Jurka86a720e2012-05-09 11:23:23 -07002904 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002905 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002906 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002907 // Only launch using the new animation if the shortcut has not opted out (this is a
2908 // private contract between launcher and may be ignored in the future).
2909 boolean useLaunchAnimation = (v != null) &&
2910 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002911 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2912 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002913
Kenny Guy1317e2d2014-05-08 18:52:50 +01002914 UserHandleCompat user = null;
2915 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2916 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2917 user = userManager.getUserForSerialNumber(serialNumber);
2918 }
2919
2920 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002921 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002922 ActivityOptions opts = null;
2923 if (sClipRevealMethod != null) {
2924 // TODO: call method directly when Launcher3 can depend on M APIs
2925 int left = 0, top = 0;
2926 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2927 if (v instanceof TextView) {
2928 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002929 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2930 if (icon != null) {
2931 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002932 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002933 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002934 width = bounds.width();
2935 height = bounds.height();
2936 }
2937 }
2938 try {
2939 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2940 left, top, width, height);
2941 } catch (IllegalAccessException e) {
2942 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2943 sClipRevealMethod = null;
2944 } catch (InvocationTargetException e) {
2945 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2946 sClipRevealMethod = null;
2947 }
2948 }
2949 if (opts == null) {
2950 opts = Utilities.isLmpOrAbove() ?
2951 ActivityOptions.makeCustomAnimation(this,
2952 R.anim.task_open_enter, R.anim.no_anim) :
2953 ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2954 v.getMeasuredWidth(), v.getMeasuredHeight());
2955 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002956 optsBundle = opts.toBundle();
2957 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002958
2959 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2960 // Could be launching some bookkeeping activity
2961 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002962 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002963 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002964 launcherApps.startActivityForProfile(intent.getComponent(), user,
2965 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002966 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002967 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 } catch (SecurityException e) {
2969 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002970 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002972 "or use the exported attribute for this activity. "
2973 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002975 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002976 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002977
Michael Jurka86a720e2012-05-09 11:23:23 -07002978 boolean startActivitySafely(View v, Intent intent, Object tag) {
2979 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002980 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2981 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2982 return false;
2983 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002984 try {
2985 success = startActivity(v, intent, tag);
2986 } catch (ActivityNotFoundException e) {
2987 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2988 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2989 }
2990 return success;
2991 }
2992
Adam Cohen268c4752012-06-06 17:47:33 -07002993 /**
2994 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2995 * in the DragLayer in the exact absolute location of the original FolderIcon.
2996 */
2997 private void copyFolderIconToImage(FolderIcon fi) {
2998 final int width = fi.getMeasuredWidth();
2999 final int height = fi.getMeasuredHeight();
3000
3001 // Lazy load ImageView, Bitmap and Canvas
3002 if (mFolderIconImageView == null) {
3003 mFolderIconImageView = new ImageView(this);
3004 }
3005 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3006 mFolderIconBitmap.getHeight() != height) {
3007 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3008 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3009 }
3010
3011 DragLayer.LayoutParams lp;
3012 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3013 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3014 } else {
3015 lp = new DragLayer.LayoutParams(width, height);
3016 }
3017
Adam Cohen307fe232012-08-16 17:55:58 -07003018 // The layout from which the folder is being opened may be scaled, adjust the starting
3019 // view size by this scale factor.
3020 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003021 lp.customPosition = true;
3022 lp.x = mRectForFolderAnimation.left;
3023 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003024 lp.width = (int) (scale * width);
3025 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003026
3027 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3028 fi.draw(mFolderIconCanvas);
3029 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003030 if (fi.getFolder() != null) {
3031 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3032 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003033 }
Adam Cohen268c4752012-06-06 17:47:33 -07003034 // Just in case this image view is still in the drag layer from a previous animation,
3035 // we remove it and re-add it.
3036 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3037 mDragLayer.removeView(mFolderIconImageView);
3038 }
3039 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003040 if (fi.getFolder() != null) {
3041 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003042 }
Adam Cohen268c4752012-06-06 17:47:33 -07003043 }
3044
Adam Cohen2801caf2011-05-13 20:57:39 -07003045 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003046 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003047 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3048 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3049 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3050
Adam Cohenc51934b2011-07-26 21:07:43 -07003051 FolderInfo info = (FolderInfo) fi.getTag();
3052 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3053 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003054 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3055 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003056 }
3057
Adam Cohen268c4752012-06-06 17:47:33 -07003058 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3059 copyFolderIconToImage(fi);
3060 fi.setVisibility(View.INVISIBLE);
3061
Michael Jurka2ecf9952012-06-18 12:52:28 -07003062 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003063 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003064 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003065 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3066 }
3067 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003068 oa.start();
3069 }
3070
Adam Cohen268c4752012-06-06 17:47:33 -07003071 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003072 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003073 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3074 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3075 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3076
Adam Cohen268c4752012-06-06 17:47:33 -07003077 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003078
Adam Cohen268c4752012-06-06 17:47:33 -07003079 // We remove and re-draw the FolderIcon in-case it has changed
3080 mDragLayer.removeView(mFolderIconImageView);
3081 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003082 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003083 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003084 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003085 oa.addListener(new AnimatorListenerAdapter() {
3086 @Override
3087 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003088 if (cl != null) {
3089 cl.clearFolderLeaveBehind();
3090 // Remove the ImageView copy of the FolderIcon and make the original visible.
3091 mDragLayer.removeView(mFolderIconImageView);
3092 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003093 }
3094 }
3095 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003096 oa.start();
3097 }
3098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003100 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003101 * is animated relative to the specified View. If the View is null, no animation
3102 * is played.
3103 *
3104 * @param folderInfo The FolderInfo describing the folder to open.
3105 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003106 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003107 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003108 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003109
Adam Cohena9cf38f2011-05-02 15:36:58 -07003110 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003111
Adam Cohen4554ee12011-08-03 16:13:21 -07003112 // Just verify that the folder hasn't already been added to the DragLayer.
3113 // There was a one-off crash where the folder had a parent already.
3114 if (folder.getParent() == null) {
3115 mDragLayer.addView(folder);
3116 mDragController.addDropTarget((DropTarget) folder);
3117 } else {
3118 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3119 folder.getParent() + ").");
3120 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003121 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003122 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003123
3124 // Notify the accessibility manager that this folder "window" has appeared and occluded
3125 // the workspace items
3126 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3127 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003128 }
3129
3130 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003131 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003132 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003133 if (folder.isEditingName()) {
3134 folder.dismissEditingName();
3135 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003136 closeFolder(folder);
3137 }
3138 }
3139
3140 void closeFolder(Folder folder) {
3141 folder.getInfo().opened = false;
3142
3143 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3144 if (parent != null) {
3145 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3146 shrinkAndFadeInFolderIcon(fi);
3147 }
3148 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003149
3150 // Notify the accessibility manager that this folder "window" has disappeard and no
3151 // longer occludeds the workspace items
3152 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153 }
3154
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003156 if (!isDraggingEnabled()) return false;
3157 if (isWorkspaceLocked()) return false;
3158 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003159
Adam Cohen1697b792013-09-17 19:08:21 -07003160 if (v instanceof Workspace) {
3161 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003162 if (mWorkspace.enterOverviewMode()) {
3163 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3164 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3165 return true;
3166 } else {
3167 return false;
3168 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003169 } else {
3170 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003171 }
Adam Cohen1697b792013-09-17 19:08:21 -07003172 }
3173
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003174 CellLayout.CellInfo longClickCellInfo = null;
3175 View itemUnderLongClick = null;
3176 if (v.getTag() instanceof ItemInfo) {
3177 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003178 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003179 itemUnderLongClick = longClickCellInfo.cell;
3180 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181 }
3182
Winson Chung3d503fb2011-07-13 17:25:49 -07003183 // The hotseat touch handling does not go through Workspace, and we always allow long press
3184 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003185 final boolean inHotseat = isHotseatLayout(v);
3186 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003187 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003188 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003189 // User long pressed on empty space
3190 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3191 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003192 if (mWorkspace.isInOverviewMode()) {
3193 mWorkspace.startReordering(v);
3194 } else {
3195 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003197 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003198 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3199 mHotseat.getOrderInHotseat(
3200 longClickCellInfo.cellX,
3201 longClickCellInfo.cellY));
3202 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003204 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003205 }
3206 }
3207 }
3208 return true;
3209 }
3210
Winson Chung3d503fb2011-07-13 17:25:49 -07003211 boolean isHotseatLayout(View layout) {
3212 return mHotseat != null && layout != null &&
3213 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3214 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003215
Winson Chung3d503fb2011-07-13 17:25:49 -07003216 /**
3217 * Returns the CellLayout of the specified container at the specified screen.
3218 */
Sunny Goyal92820592015-03-02 11:31:35 -08003219 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003220 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3221 if (mHotseat != null) {
3222 return mHotseat.getLayout();
3223 } else {
3224 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003225 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003226 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003227 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003228 }
3229 }
3230
Winson Chungb745afb2015-03-02 11:51:23 -08003231 /**
3232 * For overridden classes.
3233 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003234 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003235 return isAppsViewVisible();
3236 }
3237
3238 public boolean isAppsViewVisible() {
3239 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3240 }
3241
3242 public boolean isWidgetsViewVisible() {
3243 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003244 }
3245
Craig Mautner360310b2012-10-26 15:13:08 -07003246 private void setWorkspaceBackground(boolean workspace) {
3247 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003248 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003249 }
3250
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003251 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003252 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3253 int curflags = getWindow().getAttributes().flags
3254 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3255 if (wpflags != curflags) {
3256 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3257 }
Craig Mautner360310b2012-10-26 15:13:08 -07003258 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003259 }
3260
Michael Jurkae326f182011-11-21 14:05:46 -08003261 @Override
3262 public void onTrimMemory(int level) {
3263 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003264 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3265 // The widget preview db can result in holding onto over
3266 // 3MB of memory for caching which isn't necessary.
3267 SQLiteDatabase.releaseMemory();
3268
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003269 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003270 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003271 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003272 }
Michael Jurkae326f182011-11-21 14:05:46 -08003273 }
3274 }
3275
Winson Chungb745afb2015-03-02 11:51:23 -08003276 @Override
3277 public void onStateTransitionHideSearchBar() {
3278 // Hide the search bar
3279 if (mSearchDropTargetBar != null) {
3280 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3281 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003282 }
3283
Winson Chungb745afb2015-03-02 11:51:23 -08003284 protected void showWorkspace(boolean animated) {
3285 showWorkspace(animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003286 }
3287
Adam Cohened66b2b2012-01-23 17:28:51 -08003288 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003289 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003290 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003291 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003292 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
3293 animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003294
Winson Chungc7d2b602012-05-16 17:02:20 -07003295 // Show the search bar (only animate if we were showing the drop target bar in spring
3296 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003297 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003298 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003299 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003300
Michael Jurkab3e22d92011-10-31 15:58:33 -07003301 // Set focus to the AppsCustomize button
3302 if (mAllAppsButton != null) {
3303 mAllAppsButton.requestFocus();
3304 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003305 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003306
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003307 // Change the state *after* we've called all the transition code
3308 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003309
Winson Chung5fb63472011-02-02 17:03:37 -08003310 // Resume the auto-advance of widgets
3311 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003312 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003313
alanv1d4fde62012-10-17 13:15:47 -07003314 // Send an accessibility event to announce the context change
3315 getWindow().getDecorView()
3316 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003317
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003318 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003319 }
3320
Adam Cohened307df2013-10-02 09:37:31 -07003321 void showOverviewMode(boolean animated) {
3322 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003323 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
3324 animated, null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003325 mState = State.WORKSPACE;
3326 onWorkspaceShown(animated);
3327 }
3328
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003329 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003330 }
3331
Winson Chungb745afb2015-03-02 11:51:23 -08003332 /**
3333 * Shows the apps view.
3334 */
3335 void showAppsView(boolean animated, boolean resetListToTop) {
3336 if (resetListToTop) {
3337 mAppsView.scrollToTop();
3338 }
3339 showAppsOrWidgets(animated, State.APPS);
3340 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003341
Winson Chungb745afb2015-03-02 11:51:23 -08003342 /**
3343 * Shows the widgets view.
3344 */
3345 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Winson Chung82963d52013-10-09 11:20:57 -07003346 if (resetPageToZero) {
3347 mAppsCustomizeTabHost.reset();
3348 }
Winson Chungb745afb2015-03-02 11:51:23 -08003349 showAppsOrWidgets(animated, State.WIDGETS);
Adam Cohendcc5e712014-06-23 15:38:55 -04003350 mAppsCustomizeTabHost.post(new Runnable() {
3351 @Override
3352 public void run() {
3353 // We post this in-case the all apps view isn't yet constructed.
3354 mAppsCustomizeTabHost.requestFocus();
3355 }
3356 });
Winson Chungb745afb2015-03-02 11:51:23 -08003357 }
3358
3359 /**
3360 * Sets up the transition to show the apps/widgets view.
3361 */
3362 private void showAppsOrWidgets(boolean animated, State toState) {
3363 if (mState != State.WORKSPACE) return;
3364 if (toState != State.APPS && toState != State.WIDGETS) return;
3365
3366 if (toState == State.APPS) {
3367 mStateTransitionAnimation.startAnimationToAllApps(animated);
3368 } else {
3369 mStateTransitionAnimation.startAnimationToWidgets(animated);
3370 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003371
Michael Jurkab3e22d92011-10-31 15:58:33 -07003372 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003373 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003374
3375 // Pause the auto-advance of widgets until we are out of AllApps
3376 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003377 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003378 closeFolder();
3379
3380 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003381 getWindow().getDecorView()
3382 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003383 }
3384
Adam Cohen7777d962011-08-18 18:58:38 -07003385 void enterSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003386 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3387 mState == State.WIDGETS_SPRING_LOADED) {
3388 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003389 }
Winson Chungb745afb2015-03-02 11:51:23 -08003390
3391 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
3392 true /* animated */, null /* onCompleteRunnable */);
3393 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003394 }
Adam Cohen7777d962011-08-18 18:58:38 -07003395
Adam Cohenad4e15c2013-10-17 16:21:35 -07003396 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003397 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003398 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003399
Winson Chunge7a03942011-08-05 15:05:12 -07003400 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003401 @Override
3402 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003403 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003404 // Before we show workspace, hide all apps again because
3405 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3406 // clean up our state transition functions
3407 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003408 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003409 } else {
3410 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003411 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003412 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003413 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003414 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003415
Michael Jurkad3ef3062010-11-23 16:23:58 -08003416 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003417 if (mState == State.APPS_SPRING_LOADED) {
3418 mStateTransitionAnimation.startAnimationToAllApps(true /* animated */);
3419 mState = State.APPS;
3420 } else if (mState == State.WIDGETS_SPRING_LOADED) {
3421 mStateTransitionAnimation.startAnimationToWidgets(true /* animated */);
3422 mState = State.WIDGETS;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003423 }
3424 // Otherwise, we are not in spring loaded mode, so don't do anything.
3425 }
3426
Michael Jurkab3e22d92011-10-31 15:58:33 -07003427 void lockAllApps() {
3428 // TODO
3429 }
3430
3431 void unlockAllApps() {
3432 // TODO
3433 }
3434
Sunny Goyal594d76d2014-11-06 10:12:54 -08003435 protected void disableVoiceButtonProxy(boolean disable) {
3436 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003437 }
3438
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003439 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003440 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3441 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003442 }
3443
Adam Cohen24ce0b32014-01-14 16:18:14 -08003444 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003445 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3446 if (searchProvider == null) {
3447 return null;
3448 }
3449
3450 Bundle opts = new Bundle();
3451 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003452 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003453
3454 SharedPreferences sp = getSharedPreferences(
3455 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3456 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003457 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003458 if (!searchProvider.provider.flattenToString().equals(
3459 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003460 || (widgetInfo == null)
3461 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003462 // A valid widget is not already bound.
3463 if (widgetId > -1) {
3464 mAppWidgetHost.deleteAppWidgetId(widgetId);
3465 widgetId = -1;
3466 }
3467
3468 // Try to bind a new widget
3469 widgetId = mAppWidgetHost.allocateAppWidgetId();
3470
3471 if (!AppWidgetManagerCompat.getInstance(this)
3472 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3473 mAppWidgetHost.deleteAppWidgetId(widgetId);
3474 widgetId = -1;
3475 }
3476
3477 sp.edit()
3478 .putInt(QSB_WIDGET_ID, widgetId)
3479 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3480 .commit();
3481 }
3482
3483 if (widgetId != -1) {
3484 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3485 mQsb.updateAppWidgetOptions(opts);
3486 mQsb.setPadding(0, 0, 0, 0);
3487 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003488 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003489 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003490 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003491 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003492 }
3493
Michael Jurkad7c28052012-04-27 15:43:36 -07003494 @Override
3495 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003496 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003497 final List<CharSequence> text = event.getText();
3498 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003499 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003500 if (mState == State.APPS) {
3501 text.add("Apps");
3502 } else if (mState == State.WIDGETS) {
3503 text.add("Widgets");
alanv1d4fde62012-10-17 13:15:47 -07003504 } else {
3505 text.add(getString(R.string.all_apps_home_button_label));
3506 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003507 return result;
3508 }
3509
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003510 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003511 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003512 */
3513 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3514 @Override
3515 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003516 closeSystemDialogs();
3517 }
3518 }
3519
3520 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003521 * Receives notifications whenever the appwidgets are reset.
3522 */
3523 private class AppWidgetResetObserver extends ContentObserver {
3524 public AppWidgetResetObserver() {
3525 super(new Handler());
3526 }
3527
3528 @Override
3529 public void onChange(boolean selfChange) {
3530 onAppWidgetReset();
3531 }
3532 }
3533
3534 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003535 * If the activity is currently paused, signal that we need to run the passed Runnable
3536 * in onResume.
3537 *
3538 * This needs to be called from incoming places where resources might have been loaded
3539 * while we are paused. That is becaues the Configuration might be wrong
3540 * when we're not running, and if it comes back to what it was when we
3541 * were paused, we are not restarted.
3542 *
3543 * Implementation of the method from LauncherModel.Callbacks.
3544 *
3545 * @return true if we are currently paused. The caller might be able to
3546 * skip some work in that case since we will come back again.
3547 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003548 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003549 if (mPaused) {
3550 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003551 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003552 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003553 }
3554 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003555 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003556 return true;
3557 } else {
3558 return false;
3559 }
3560 }
3561
Michael Jurkac402cd92013-05-20 15:49:32 +02003562 private boolean waitUntilResume(Runnable run) {
3563 return waitUntilResume(run, false);
3564 }
3565
Michael Jurka1e2f4652013-07-08 18:03:46 -07003566 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003567 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003568 }
3569
Michael Jurka7607c2f2013-04-03 14:33:19 -07003570 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003571 * If the activity is currently paused, signal that we need to re-run the loader
3572 * in onResume.
3573 *
3574 * This needs to be called from incoming places where resources might have been loaded
3575 * while we are paused. That is becaues the Configuration might be wrong
3576 * when we're not running, and if it comes back to what it was when we
3577 * were paused, we are not restarted.
3578 *
3579 * Implementation of the method from LauncherModel.Callbacks.
3580 *
3581 * @return true if we are currently paused. The caller might be able to
3582 * skip some work in that case since we will come back again.
3583 */
3584 public boolean setLoadOnResume() {
3585 if (mPaused) {
3586 Log.i(TAG, "setLoadOnResume");
3587 mOnResumeNeedsLoad = true;
3588 return true;
3589 } else {
3590 return false;
3591 }
3592 }
3593
3594 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003595 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003596 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003597 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003598 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003599 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003600 } else {
3601 return SCREEN_COUNT / 2;
3602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003604
Joe Onorato9c1289c2009-08-17 11:03:03 -04003605 /**
3606 * Refreshes the shortcuts shown on the workspace.
3607 *
3608 * Implementation of the method from LauncherModel.Callbacks.
3609 */
3610 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003611 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003612
Michael Jurka7607c2f2013-04-03 14:33:19 -07003613 // If we're starting binding all over again, clear any bind calls we'd postponed in
3614 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3615 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003616 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003617
Winson Chungd64d1762013-08-20 14:37:16 -07003618 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003619 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003620 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003621
Michael Jurka05bf6442011-11-30 20:28:53 -08003622 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003623 if (mHotseat != null) {
3624 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003625 }
3626 }
3627
Adam Cohendcd297f2013-06-18 13:13:40 -07003628 @Override
3629 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003630 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003631
Adam Cohen5084cba2013-09-03 12:01:16 -07003632 // If there are no screens, we need to have an empty screen
3633 if (orderedScreenIds.size() == 0) {
3634 mWorkspace.addExtraEmptyScreen();
3635 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003636
3637 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003638 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003639 if (hasCustomContentToLeft()) {
3640 mWorkspace.createCustomContentContainer();
3641 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003642 }
Winson Chung64359a52013-07-08 17:17:08 -07003643 }
3644
3645 @Override
3646 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003647 // Log to disk
3648 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3649 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3650 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003651 int count = orderedScreenIds.size();
3652 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003653 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003654 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003655 }
3656
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003657 @Override
3658 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
3659 final long screenId, final int[] cell, final int spanX, final int spanY) {
3660 showWorkspace(true, new Runnable() {
3661
3662 @Override
3663 public void run() {
3664 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
3665 addPendingItem(info, container, screenId, cell, spanX, spanY);
3666 }
3667 });
3668 }
3669
Adam Cohen39a06042013-07-19 14:30:12 -07003670 private boolean shouldShowWeightWatcher() {
3671 String spKey = LauncherAppState.getSharedPreferencesKey();
3672 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003673 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003674
3675 return show;
3676 }
3677
3678 private void toggleShowWeightWatcher() {
3679 String spKey = LauncherAppState.getSharedPreferencesKey();
3680 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3681 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3682
3683 show = !show;
3684
3685 SharedPreferences.Editor editor = sp.edit();
3686 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3687 editor.commit();
3688
3689 if (mWeightWatcher != null) {
3690 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3691 }
3692 }
3693
Winson Chungd64d1762013-08-20 14:37:16 -07003694 public void bindAppsAdded(final ArrayList<Long> newScreens,
3695 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003696 final ArrayList<ItemInfo> addAnimated,
3697 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003698 Runnable r = new Runnable() {
3699 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003700 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003701 }
3702 };
3703 if (waitUntilResume(r)) {
3704 return;
3705 }
3706
Winson Chungd64d1762013-08-20 14:37:16 -07003707 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003708 if (newScreens != null) {
3709 bindAddScreens(newScreens);
3710 }
Winson Chungd64d1762013-08-20 14:37:16 -07003711
3712 // We add the items without animation on non-visible pages, and with
3713 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003714 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003715 bindItems(addNotAnimated, 0,
3716 addNotAnimated.size(), false);
3717 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003718 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003719 bindItems(addAnimated, 0,
3720 addAnimated.size(), true);
3721 }
Winson Chungc58497e2013-09-03 17:48:37 -07003722
Winson Chung87412982013-10-03 18:34:14 -07003723 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003724 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003725
Winson Chungb745afb2015-03-02 11:51:23 -08003726 if (addedApps != null && mAppsView != null) {
3727 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003728 }
Winson Chungd64d1762013-08-20 14:37:16 -07003729 }
3730
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003731 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 * Bind the items start-end from the list.
3733 *
3734 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003735 */
Winson Chung64359a52013-07-08 17:17:08 -07003736 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3737 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003738 Runnable r = new Runnable() {
3739 public void run() {
3740 bindItems(shortcuts, start, end, forceAnimateIcons);
3741 }
3742 };
3743 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003744 return;
3745 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003746
Winson Chungf0c6ae02012-03-21 16:10:31 -07003747 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003748 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3749 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003750 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003751 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003752 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003753 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003754 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003755
3756 // Short circuit if we are loading dock items for a configuration which has no dock
3757 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3758 mHotseat == null) {
3759 continue;
3760 }
3761
Joe Onorato9c1289c2009-08-17 11:03:03 -04003762 switch (item.itemType) {
3763 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3764 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003765 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003766 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003767
Winson Chung64359a52013-07-08 17:17:08 -07003768 /*
3769 * TODO: FIX collision case
3770 */
Winson Chungce376632013-07-11 16:12:41 -07003771 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3772 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3773 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003774 View v = cl.getChildAt(item.cellX, item.cellY);
3775 Object tag = v.getTag();
3776 String desc = "Collision while binding workspace item: " + item
3777 + ". Collides with " + tag;
3778 if (LauncherAppState.isDogfoodBuild()) {
3779 throw (new RuntimeException(desc));
3780 } else {
3781 Log.d(TAG, desc);
3782 }
Winson Chungce376632013-07-11 16:12:41 -07003783 }
Winson Chung64359a52013-07-08 17:17:08 -07003784 }
3785
Adam Cohendcd297f2013-06-18 13:13:40 -07003786 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3787 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003788 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003789 // Animate all the applications up now
3790 shortcut.setAlpha(0f);
3791 shortcut.setScaleX(0f);
3792 shortcut.setScaleY(0f);
3793 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003794 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003795 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003796 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003797 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003798 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003799 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003800 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003801 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3802 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003803 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003804 default:
3805 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003806 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003807 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003808
Winson Chung997a9232013-07-24 15:33:46 -07003809 if (animateIcons) {
3810 // Animate to the correct page
3811 if (newShortcutsScreenId > -1) {
3812 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003813 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003814 final Runnable startBounceAnimRunnable = new Runnable() {
3815 public void run() {
3816 anim.playTogether(bounceAnims);
3817 anim.start();
3818 }
3819 };
Winson Chung997a9232013-07-24 15:33:46 -07003820 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003821 // We post the animation slightly delayed to prevent slowdowns
3822 // when we are loading right after we return to launcher.
3823 mWorkspace.postDelayed(new Runnable() {
3824 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003825 if (mWorkspace != null) {
3826 mWorkspace.snapToPage(newScreenIndex);
3827 mWorkspace.postDelayed(startBounceAnimRunnable,
3828 NEW_APPS_ANIMATION_DELAY);
3829 }
Winson Chung94d67682013-09-25 16:29:40 -07003830 }
3831 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003832 } else {
3833 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003834 }
3835 }
Winson Chung64359a52013-07-08 17:17:08 -07003836 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003837 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003838 }
3839
Joe Onorato9c1289c2009-08-17 11:03:03 -04003840 /**
3841 * Implementation of the method from LauncherModel.Callbacks.
3842 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003843 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003844 Runnable r = new Runnable() {
3845 public void run() {
3846 bindFolders(folders);
3847 }
3848 };
3849 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003850 return;
3851 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003852 sFolders.clear();
3853 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003854 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003855
3856 /**
3857 * Add the views for a widget to the workspace.
3858 *
3859 * Implementation of the method from LauncherModel.Callbacks.
3860 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003861 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003862 Runnable r = new Runnable() {
3863 public void run() {
3864 bindAppWidget(item);
3865 }
3866 };
3867 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003868 return;
3869 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003870
Daniel Sandler843e8602010-06-07 14:59:01 -04003871 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3872 if (DEBUG_WIDGETS) {
3873 Log.d(TAG, "bindAppWidget: " + item);
3874 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003875 final Workspace workspace = mWorkspace;
3876
Adam Cohen59400422014-03-05 18:07:04 -08003877 LauncherAppWidgetProviderInfo appWidgetInfo =
3878 LauncherModel.getProviderInfo(this, item.providerName);
3879
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003880 if (!mIsSafeModeEnabled
3881 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3882 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003883 if (appWidgetInfo == null) {
3884 if (DEBUG_WIDGETS) {
3885 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3886 + " belongs to component " + item.providerName
3887 + ", as the povider is null");
3888 }
3889 LauncherModel.deleteItemFromDatabase(this, item);
3890 return;
3891 }
3892 // Note: This assumes that the id remap broadcast is received before this step.
3893 // If that is not the case, the id remap will be ignored and user may see the
3894 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08003895 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003896 pendingInfo.spanX = item.spanX;
3897 pendingInfo.spanY = item.spanY;
3898 pendingInfo.minSpanX = item.minSpanX;
3899 pendingInfo.minSpanY = item.minSpanY;
3900 Bundle options =
3901 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
3902
Sunny Goyalff572272014-07-23 13:58:07 -07003903 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003904 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3905 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003906
3907 // TODO consider showing a permission dialog when the widget is clicked.
3908 if (!success) {
3909 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3910 if (DEBUG_WIDGETS) {
3911 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3912 + " belongs to component " + item.providerName
3913 + ", as the launcher is unable to bing a new widget id");
3914 }
3915 LauncherModel.deleteItemFromDatabase(this, item);
3916 return;
3917 }
3918
3919 item.appWidgetId = newWidgetId;
3920
3921 // If the widget has a configure activity, it is still needs to set it up, otherwise
3922 // the widget is ready to go.
3923 item.restoreStatus = (appWidgetInfo.configure == null)
3924 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
3925 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
3926
3927 LauncherModel.updateItemInDatabase(this, item);
3928 }
3929
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003930 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07003931 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07003932 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08003933 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
3934 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07003935 }
Daniel Sandler843e8602010-06-07 14:59:01 -04003936
Sunny Goyal651077b2014-06-30 14:15:31 -07003937 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3938 } else {
3939 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003940 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
3941 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003942 view.updateIcon(mIconCache);
3943 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07003944 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07003945 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07003946 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003947
Joe Onorato9c1289c2009-08-17 11:03:03 -04003948 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003949 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003950
Adam Cohendcd297f2013-06-18 13:13:40 -07003951 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003952 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08003953 if (!item.isCustomWidget()) {
3954 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3955 }
Adam Cohended9f8d2010-11-03 13:25:16 -07003956
Joe Onorato9c1289c2009-08-17 11:03:03 -04003957 workspace.requestLayout();
3958
Daniel Sandler843e8602010-06-07 14:59:01 -04003959 if (DEBUG_WIDGETS) {
3960 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3961 + (SystemClock.uptimeMillis()-start) + "ms");
3962 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003963 }
3964
Sunny Goyalff572272014-07-23 13:58:07 -07003965 /**
3966 * Restores a pending widget.
3967 *
3968 * @param appWidgetId The app widget id
3969 * @param cellInfo The position on screen where to create the widget.
3970 */
3971 private void completeRestoreAppWidget(final int appWidgetId) {
3972 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
3973 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
3974 Log.e(TAG, "Widget update called, when the widget no longer exists.");
3975 return;
3976 }
3977
Sunny Goyal0fc1be12014-08-11 17:05:23 -07003978 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07003979 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
3980
3981 mWorkspace.reinflateWidgetsIfNecessary();
3982 LauncherModel.updateItemInDatabase(this, info);
3983 }
3984
Adam Cohen1462de32012-07-24 22:34:36 -07003985 public void onPageBoundSynchronously(int page) {
3986 mSynchronouslyBoundPages.add(page);
3987 }
3988
Joe Onorato9c1289c2009-08-17 11:03:03 -04003989 /**
3990 * Callback saying that there aren't any more items to bind.
3991 *
3992 * Implementation of the method from LauncherModel.Callbacks.
3993 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003994 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07003995 Runnable r = new Runnable() {
3996 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08003997 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07003998 }
3999 };
4000 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004001 return;
4002 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004003 if (mSavedState != null) {
4004 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004005 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004006 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004007 mSavedState = null;
4008 }
4009
Adam Cohen1462de32012-07-24 22:34:36 -07004010 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004011
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004012 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004013 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004014
4015 // If we received the result of any pending adds while the loader was running (e.g. the
4016 // widget configuration forced an orientation change), process them now.
4017 if (sPendingAddItem != null) {
4018 final long screenId = completeAdd(sPendingAddItem);
4019
4020 // TODO: this moves the user to the page where the pending item was added. Ideally,
4021 // the screen would be guaranteed to exist after bind, and the page would be set through
4022 // the workspace restore process.
4023 mWorkspace.post(new Runnable() {
4024 @Override
4025 public void run() {
4026 mWorkspace.snapToScreenId(screenId);
4027 }
4028 });
4029 sPendingAddItem = null;
4030 }
4031
Sunny Goyale755d462014-07-22 13:48:29 -07004032 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004033
4034 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004035 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004036 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004037 }
4038
Adam Cohen3ed316a2014-07-23 18:21:20 -07004039 private void sendLoadingCompleteBroadcastIfNecessary() {
4040 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4041 String permission =
4042 getResources().getString(R.string.receive_first_load_broadcast_permission);
4043 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4044 sendBroadcast(intent, permission);
4045 SharedPreferences.Editor editor = mSharedPrefs.edit();
4046 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4047 editor.apply();
4048 }
4049 }
4050
Winson Chunga0b7e862013-09-05 16:03:15 -07004051 public boolean isAllAppsButtonRank(int rank) {
4052 if (mHotseat != null) {
4053 return mHotseat.isAllAppsButtonRank(rank);
4054 }
4055 return false;
4056 }
4057
Winson Chunga2413752012-04-03 14:22:34 -07004058 private boolean canRunNewAppsAnimation() {
4059 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4060 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4061 }
4062
Winson Chungc9168342013-06-26 14:54:55 -07004063 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4064 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4065 PropertyValuesHolder.ofFloat("alpha", 1f),
4066 PropertyValuesHolder.ofFloat("scaleX", 1f),
4067 PropertyValuesHolder.ofFloat("scaleY", 1f));
4068 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4069 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4070 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4071 return bounceAnim;
4072 }
4073
Adam Cohen27772732013-11-11 14:00:56 +00004074 public boolean useVerticalBarLayout() {
4075 return LauncherAppState.getInstance().getDynamicGrid().
4076 getDeviceProfile().isVerticalBarLayout();
4077 }
4078
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004079 protected Rect getSearchBarBounds() {
4080 return LauncherAppState.getInstance().getDynamicGrid().
4081 getDeviceProfile().getSearchBarBounds();
4082 }
4083
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004084 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004085 if (mSearchDropTargetBar == null) {
4086 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004087 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004088 if (mQsb != null) {
4089 mSearchDropTargetBar.removeView(mQsb);
4090 mQsb = null;
4091 }
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08004092 getOrCreateQsbBar();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004093 }
4094
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004095 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004096 * Add the icons for all apps.
4097 *
4098 * Implementation of the method from LauncherModel.Callbacks.
4099 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004100 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungb745afb2015-03-02 11:51:23 -08004101 if (mAppsView != null) {
4102 mAppsView.setApps(apps);
4103 }
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004104 if (mAppsCustomizeContent != null) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004105 mAppsCustomizeContent.onPackagesUpdated(
4106 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -07004107 }
Adam Cohen9211d422014-10-07 18:14:53 -07004108 if (mLauncherCallbacks != null) {
4109 mLauncherCallbacks.bindAllApplications(apps);
4110 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004111 }
4112
4113 /**
4114 * A package was updated.
4115 *
4116 * Implementation of the method from LauncherModel.Callbacks.
4117 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004118 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004119 Runnable r = new Runnable() {
4120 public void run() {
4121 bindAppsUpdated(apps);
4122 }
4123 };
4124 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004125 return;
4126 }
4127
Winson Chungb745afb2015-03-02 11:51:23 -08004128 if (mAppsView != null) {
4129 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004130 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004131 }
4132
Sunny Goyal4390ace2014-10-13 11:33:11 -07004133 @Override
4134 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4135 Runnable r = new Runnable() {
4136 public void run() {
4137 bindWidgetsRestored(widgets);
4138 }
4139 };
4140 if (waitUntilResume(r)) {
4141 return;
4142 }
4143 mWorkspace.widgetsRestored(widgets);
4144 }
4145
Joe Onorato9c1289c2009-08-17 11:03:03 -04004146 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004147 * Some shortcuts were updated in the background.
4148 *
4149 * Implementation of the method from LauncherModel.Callbacks.
4150 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004151 @Override
4152 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4153 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004154 Runnable r = new Runnable() {
4155 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004156 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004157 }
4158 };
4159 if (waitUntilResume(r)) {
4160 return;
4161 }
4162
Sunny Goyal4390ace2014-10-13 11:33:11 -07004163 if (!updated.isEmpty()) {
4164 mWorkspace.updateShortcuts(updated);
4165 }
4166
4167 if (!removed.isEmpty()) {
4168 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4169 for (ShortcutInfo si : removed) {
4170 removedComponents.add(si.getTargetComponent());
4171 }
4172 mWorkspace.removeItemsByComponentName(removedComponents, user);
4173 // Notify the drag controller
4174 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004175 }
4176 }
4177
4178 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004179 * Update the state of a package, typically related to install state.
4180 *
4181 * Implementation of the method from LauncherModel.Callbacks.
4182 */
Sunny Goyale755d462014-07-22 13:48:29 -07004183 @Override
4184 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004185 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004186 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004187 }
4188 }
4189
4190 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004191 * Update the label and icon of all the icons in a package
4192 *
4193 * Implementation of the method from LauncherModel.Callbacks.
4194 */
4195 @Override
4196 public void updatePackageBadge(String packageName) {
4197 if (mWorkspace != null) {
4198 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4199 }
4200 }
4201
4202 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004203 * A package was uninstalled. We take both the super set of packageNames
4204 * in addition to specific applications to remove, the reason being that
4205 * this can be called when a package is updated as well. In that scenario,
4206 * we only remove specific components from the workspace, where as
4207 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004208 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004209 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004210 * Implementation of the method from LauncherModel.Callbacks.
4211 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004212 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004213 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004214 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004215 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004216 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004217 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004218 }
Winson Chungd64d1762013-08-20 14:37:16 -07004219 };
4220 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004221 return;
4222 }
4223
Sunny Goyal1a745e82014-10-02 15:58:31 -07004224 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004225 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4226 for (AppInfo info : appInfos) {
4227 removedComponents.add(info.componentName);
4228 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004229 if (!packageNames.isEmpty()) {
4230 mWorkspace.removeItemsByPackageName(packageNames, user);
4231 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004232 if (!removedComponents.isEmpty()) {
4233 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004234 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004235 // Notify the drag controller
4236 mDragController.onAppsRemoved(packageNames, removedComponents);
4237
Sunny Goyal1a745e82014-10-02 15:58:31 -07004238 } else {
4239 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004240 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004241
Winson Chungdf95eb12013-10-16 14:57:07 -07004242 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004243 if (mAppsView != null) {
4244 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004245 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004246 }
Joe Onoratobe386092009-11-17 17:32:16 -08004247
4248 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004249 * A number of packages were updated.
4250 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004251 private ArrayList<Object> mWidgetsAndShortcuts;
4252 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004253 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004254 bindPackagesUpdated(mWidgetsAndShortcuts);
4255 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004256 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004257 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004258 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4259 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4260 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004261 return;
4262 }
4263
Winson Chung64359a52013-07-08 17:17:08 -07004264 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004265 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004266 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004267 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004268 }
4269
Winson Chung400438b2011-01-16 17:53:48 -08004270 private int mapConfigurationOriActivityInfoOri(int configOri) {
4271 final Display d = getWindowManager().getDefaultDisplay();
4272 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4273 switch (d.getRotation()) {
4274 case Surface.ROTATION_0:
4275 case Surface.ROTATION_180:
4276 // We are currently in the same basic orientation as the natural orientation
4277 naturalOri = configOri;
4278 break;
4279 case Surface.ROTATION_90:
4280 case Surface.ROTATION_270:
4281 // We are currently in the other basic orientation to the natural orientation
4282 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4283 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4284 break;
4285 }
4286
4287 int[] oriMap = {
4288 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4289 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4290 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4291 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4292 };
4293 // Since the map starts at portrait, we need to offset if this device's natural orientation
4294 // is landscape.
4295 int indexOffset = 0;
4296 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4297 indexOffset = 1;
4298 }
4299 return oriMap[(d.getRotation() + indexOffset) % 4];
4300 }
Adam Cohen446e9402011-09-15 18:21:21 -07004301
Winson Chung4b919f82012-05-01 10:44:08 -07004302 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004303 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004304 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4305 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4306 .getConfiguration().orientation));
4307 } else {
4308 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4309 }
Winson Chung4b919f82012-05-01 10:44:08 -07004310 }
4311 }
4312 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004313 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004314 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004315 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004316 } else {
4317 mHandler.postDelayed(new Runnable() {
4318 public void run() {
4319 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4320 }
4321 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004322 }
Winson Chung4b919f82012-05-01 10:44:08 -07004323 }
Winson Chung400438b2011-01-16 17:53:48 -08004324 }
4325
Winson Chunge43a1e72014-01-15 10:33:02 -08004326 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004327 if (mLauncherCallbacks != null) {
4328 return mLauncherCallbacks.isLauncherPreinstalled();
4329 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004330 PackageManager pm = getPackageManager();
4331 try {
4332 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4333 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4334 return true;
4335 } else {
4336 return false;
4337 }
4338 } catch (NameNotFoundException e) {
4339 e.printStackTrace();
4340 return false;
4341 }
4342 }
4343
Adam Cohenea90f832014-05-21 15:03:34 -07004344 /**
4345 * This method indicates whether or not we should suggest default wallpaper dimensions
4346 * when our wallpaper cropper was not yet used to set a wallpaper.
4347 */
4348 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004349 if (mLauncherCallbacks != null) {
4350 return mLauncherCallbacks.overrideWallpaperDimensions();
4351 }
Adam Cohenea90f832014-05-21 15:03:34 -07004352 return true;
4353 }
4354
Adam Cohen432609a2014-03-13 17:03:22 -07004355 /**
4356 * To be overridden by subclasses to indicate that there is an activity to launch
4357 * before showing the standard launcher experience.
4358 */
4359 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004360 if (mLauncherCallbacks != null) {
4361 return mLauncherCallbacks.hasFirstRunActivity();
4362 }
Adam Cohen432609a2014-03-13 17:03:22 -07004363 return false;
4364 }
4365
4366 /**
4367 * To be overridden by subclasses to launch any first run activity
4368 */
4369 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004370 if (mLauncherCallbacks != null) {
4371 return mLauncherCallbacks.getFirstRunActivity();
4372 }
Adam Cohen432609a2014-03-13 17:03:22 -07004373 return null;
4374 }
4375
Winson Chunga6945242014-01-08 14:04:34 -08004376 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004377 return !ActivityManager.isRunningInTestHarness() &&
4378 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004379 }
4380
Adam Cohen4a9423d2014-03-28 14:22:31 -07004381 protected boolean hasRunFirstRunActivity() {
4382 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4383 }
4384
Adam Cohen432609a2014-03-13 17:03:22 -07004385 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004386 if (shouldRunFirstRunActivity() &&
4387 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004388 Intent firstRunIntent = getFirstRunActivity();
4389 if (firstRunIntent != null) {
4390 startActivity(firstRunIntent);
4391 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004392 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004393 }
4394 }
Adam Cohen432609a2014-03-13 17:03:22 -07004395 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004396 }
4397
4398 private void markFirstRunActivityShown() {
4399 SharedPreferences.Editor editor = mSharedPrefs.edit();
4400 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4401 editor.apply();
4402 }
4403
Adam Cohen432609a2014-03-13 17:03:22 -07004404 /**
4405 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4406 * screen that must be displayed and dismissed.
4407 */
4408 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004409 if (mLauncherCallbacks != null) {
4410 return mLauncherCallbacks.hasDismissableIntroScreen();
4411 }
Adam Cohen432609a2014-03-13 17:03:22 -07004412 return false;
4413 }
4414
4415 /**
4416 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4417 */
4418 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004419 if (mLauncherCallbacks != null) {
4420 return mLauncherCallbacks.getIntroScreen();
4421 }
Adam Cohen432609a2014-03-13 17:03:22 -07004422 return null;
4423 }
4424
4425 /**
4426 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4427 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4428 */
4429 private boolean shouldShowIntroScreen() {
4430 return hasDismissableIntroScreen() &&
4431 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4432 }
4433
4434 protected void showIntroScreen() {
4435 View introScreen = getIntroScreen();
4436 changeWallpaperVisiblity(false);
4437 if (introScreen != null) {
4438 mDragLayer.showOverlayView(introScreen);
4439 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004440 if (mLauncherOverlayContainer != null) {
4441 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4442 }
Adam Cohen432609a2014-03-13 17:03:22 -07004443 }
4444
4445 public void dismissIntroScreen() {
4446 markIntroScreenDismissed();
4447 if (showFirstRunActivity()) {
4448 // We delay hiding the intro view until the first run activity is showing. This
4449 // avoids a blip.
4450 mWorkspace.postDelayed(new Runnable() {
4451 @Override
4452 public void run() {
4453 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004454 if (mLauncherOverlayContainer != null) {
4455 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4456 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004457 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004458 }
4459 }, ACTIVITY_START_DELAY);
4460 } else {
4461 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004462 if (mLauncherOverlayContainer != null) {
4463 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4464 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004465 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004466 }
4467 changeWallpaperVisiblity(true);
4468 }
4469
4470 private void markIntroScreenDismissed() {
4471 SharedPreferences.Editor editor = mSharedPrefs.edit();
4472 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4473 editor.apply();
4474 }
4475
Sunny Goyal88d60f12014-09-08 03:47:29 -07004476 private void showFirstRunClings() {
4477 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4478 // on the device, then we always show the first run cling experience (or if there is no
4479 // launcher2). Otherwise, we prompt the user upon started for migration
4480 LauncherClings launcherClings = new LauncherClings(this);
4481 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4482 if (mModel.canMigrateFromOldLauncherDb(this)) {
4483 launcherClings.showMigrationCling();
4484 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004485 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004486 }
4487 }
4488 }
4489
Winson Chunga6945242014-01-08 14:04:34 -08004490 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004491 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4492 if (mHotseat != null) mHotseat.setAlpha(1f);
4493 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4494 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004495 }
4496
4497 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004498 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4499 if (mHotseat != null) mHotseat.setAlpha(0f);
4500 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4501 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004502 }
4503
Mathew Inwood72fbec12013-11-19 15:45:07 +00004504 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004505 // Called from search suggestion, not supported in other profiles.
4506 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4507 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4508 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4509 myUser);
4510 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004511 return null;
4512 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004513 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004514 }
4515
4516 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4517 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004518 // Called from search suggestion, not supported in other profiles.
4519 return createShortcutDragInfo(shortcutIntent, caption, icon,
4520 UserHandleCompat.myUserHandle());
4521 }
4522
4523 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4524 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004525 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004526 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004527 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004528 }
4529
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004530 protected void moveWorkspaceToDefaultScreen() {
4531 mWorkspace.moveToDefaultScreen(false);
4532 }
4533
Mathew Inwood72fbec12013-11-19 15:45:07 +00004534 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4535 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004536 mWorkspace.onExternalDragStartedWithItem(dragView);
4537 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004538 }
4539
Anjali Koppalf5d29132014-02-28 13:33:27 -08004540 @Override
4541 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004542 if (mLauncherCallbacks != null) {
4543 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4544 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004545 }
4546
Winson Chung80baf5a2010-08-09 16:03:15 -07004547 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004548 * Prints out out state for debugging.
4549 */
4550 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004551 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004552 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004553 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4554 Log.d(TAG, "mRestoring=" + mRestoring);
4555 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4556 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004557 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004558 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004559
Winson Chung785d2eb2011-04-14 16:08:02 -07004560 if (mAppsCustomizeContent != null) {
4561 mAppsCustomizeContent.dumpState();
4562 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004563 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004564 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004565
4566 @Override
4567 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4568 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004569 synchronized (sDumpLogs) {
4570 writer.println(" ");
4571 writer.println("Debug logs: ");
4572 for (int i = 0; i < sDumpLogs.size(); i++) {
4573 writer.println(" " + sDumpLogs.get(i));
4574 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004575 }
Adam Cohen9211d422014-10-07 18:14:53 -07004576 if (mLauncherCallbacks != null) {
4577 mLauncherCallbacks.dump(prefix, fd, writer, args);
4578 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004579 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004580
4581 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004582 if (DEBUG_DUMP_LOG) {
4583 synchronized (sDumpLogs) {
4584 Log.d(TAG, "");
4585 Log.d(TAG, "*********************");
4586 Log.d(TAG, "Launcher debug logs: ");
4587 for (int i = 0; i < sDumpLogs.size(); i++) {
4588 Log.d(TAG, " " + sDumpLogs.get(i));
4589 }
4590 Log.d(TAG, "*********************");
4591 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004592 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004593 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004594 }
4595
4596 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004597 addDumpLog(tag, log, null, debugLog);
4598 }
4599
4600 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004601 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004602 if (e != null) {
4603 Log.d(tag, log, e);
4604 } else {
4605 Log.d(tag, log);
4606 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004607 }
Winson Chungede41292013-09-19 16:27:36 -07004608 if (DEBUG_DUMP_LOG) {
4609 sDateStamp.setTime(System.currentTimeMillis());
4610 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004611 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4612 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004613 }
Winson Chungede41292013-09-19 16:27:36 -07004614 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004615 }
4616
Adam Cohen59400422014-03-05 18:07:04 -08004617 public static CustomAppWidget getCustomAppWidget(String name) {
4618 return sCustomAppWidgets.get(name);
4619 }
4620
4621 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4622 return sCustomAppWidgets;
4623 }
4624
Winson Chungede41292013-09-19 16:27:36 -07004625 public void dumpLogsToLocalData() {
4626 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004627 new AsyncTask<Void, Void, Void>() {
4628 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004629 boolean success = false;
4630 sDateStamp.setTime(sRunStart);
4631 String FILENAME = sDateStamp.getMonth() + "-"
4632 + sDateStamp.getDay() + "_"
4633 + sDateStamp.getHours() + "-"
4634 + sDateStamp.getMinutes() + "_"
4635 + sDateStamp.getSeconds() + ".txt";
4636
4637 FileOutputStream fos = null;
4638 File outFile = null;
4639 try {
4640 outFile = new File(getFilesDir(), FILENAME);
4641 outFile.createNewFile();
4642 fos = new FileOutputStream(outFile);
4643 } catch (Exception e) {
4644 e.printStackTrace();
4645 }
4646 if (fos != null) {
4647 PrintWriter writer = new PrintWriter(fos);
4648
4649 writer.println(" ");
4650 writer.println("Debug logs: ");
4651 synchronized (sDumpLogs) {
4652 for (int i = 0; i < sDumpLogs.size(); i++) {
4653 writer.println(" " + sDumpLogs.get(i));
4654 }
4655 }
4656 writer.close();
4657 }
4658 try {
4659 if (fos != null) {
4660 fos.close();
4661 success = true;
4662 }
4663 } catch (IOException e) {
4664 e.printStackTrace();
4665 }
Michael Jurka43467462013-11-14 12:03:58 +01004666 return null;
Winson Chungede41292013-09-19 16:27:36 -07004667 }
Michael Jurka43467462013-11-14 12:03:58 +01004668 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004669 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004671}
Michael Jurka2763be32011-02-24 11:19:57 -08004672
Michael Jurkaabded662011-03-04 12:06:57 -08004673interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004674 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004675 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004676 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004677 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004678 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004679}
Dan Sandlerd5024042014-01-09 15:01:33 -05004680
4681interface DebugIntents {
4682 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4683 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004684}