blob: 61915b7552f00b64cc133bc20e287662b4c105ea [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Adam Cohen2854d252014-08-27 16:04:07 -070025import android.animation.TimeInterpolator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000027import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070029import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070053import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.Bitmap;
55import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070056import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen2854d252014-08-27 16:04:07 -070085import android.view.ViewAnimationUtils;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070087import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070089import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080091import android.view.accessibility.AccessibilityEvent;
Adam Cohen9bfdb762014-07-21 17:44:06 -070092import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Advanceable;
96import android.widget.FrameLayout;
97import android.widget.ImageView;
Adam Cohen96d30a12013-07-16 18:13:21 -070098import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070099
Sunny Goyal651077b2014-06-30 14:15:31 -0700100import com.android.launcher3.DropTarget.DragObject;
101import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherActivityInfoCompat;
104import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700105import com.android.launcher3.compat.PackageInstallerCompat;
106import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100107import com.android.launcher3.compat.UserHandleCompat;
108import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700109
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.io.DataInputStream;
111import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700113import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700118import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700120import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700121import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700122import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700123import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700124import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000125import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127/**
128 * Default launcher application.
129 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100130public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700131 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700132 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800133 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700134 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Daniel Sandlerf061f822013-06-27 13:59:36 -0400136 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400137 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700138 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700140 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700141
Dan Sandlerd5024042014-01-09 15:01:33 -0500142 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700148 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Michael Jurka8b805b12012-04-18 14:23:14 -0700150 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700151 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700152
Mathew Inwood876a8462013-06-14 14:12:41 +0100153 /**
154 * IntentStarter uses request codes starting with this. This must be greater than all activity
155 * request codes used internally.
156 */
157 protected static final int REQUEST_LAST = 100;
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
160
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800161 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Michael Jurka0a457bf2012-11-19 14:05:05 -0800163 // To turn on these properties, type
164 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800165 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800166 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Winson Chung2672ff92012-05-04 16:22:30 -0700168 // The Intent extra that defines whether to ignore the launch animation
169 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400170 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
173 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700174 // Type: int
175 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700177 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
178 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
180 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: boolean
185 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
186 // Type: long
187 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700188 // Type: int
189 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: int[]
197 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
Adam Cohen432609a2014-03-13 17:03:22 -0700199 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800200 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
201
Adam Cohen3ed316a2014-07-23 18:21:20 -0700202 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
203 static final String ACTION_FIRST_LOAD_COMPLETE =
204 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
205
Adam Cohen39a06042013-07-19 14:30:12 -0700206 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700207 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700208
Sunny Goyal594d76d2014-11-06 10:12:54 -0800209 private static final String QSB_WIDGET_ID = "qsb_widget_id";
210 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
211
Winson Chunga6945242014-01-08 14:04:34 -0800212 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
213
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700214 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700215 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700216 private State mState = State.WORKSPACE;
217 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700218
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700219 private boolean mIsSafeModeEnabled;
220
Adam Cohenc2d6e892014-10-16 09:49:24 -0700221 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
222 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700223 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700224
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700226 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
227 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700228 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 private static final Object sLock = new Object();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000232 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
233 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
234
Winson Chunga2413752012-04-03 14:22:34 -0700235 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700236 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
237 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700238 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700239 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700240
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800241 private final BroadcastReceiver mCloseSystemDialogsReceiver
242 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800243 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 private LayoutInflater mInflater;
246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700248 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800249 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800250 private DragLayer mDragLayer;
251 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700252 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700253
Sunny Goyalffe83f12014-08-14 17:39:34 -0700254 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700255 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700256
Michael Jurkac9d95c52011-08-29 14:03:34 -0700257 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800258 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800259 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700260
Michael Jurka0280c3b2010-09-17 15:00:07 -0700261 private int[] mTmpAddItemCellCoordinates = new int[2];
262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 private FolderInfo mFolderInfo;
264
Winson Chung3d503fb2011-07-13 17:25:49 -0700265 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800266 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700267
Michael Jurka838a4ca2011-02-07 13:33:06 -0800268 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700269
Winson Chungc51db6a2011-10-05 11:44:49 -0700270 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700271 private AppsCustomizeTabHost mAppsCustomizeTabHost;
272 private AppsCustomizePagedView mAppsCustomizeContent;
273 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800274 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700277 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
278 // scroll issues (because the workspace may not have been measured yet) and extra work.
279 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
280 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
282 private SpannableStringBuilder mDefaultKeySsb = null;
283
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284 private boolean mWorkspaceLoading = true;
285
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800286 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 private boolean mRestoring;
288 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700289 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290
Michael Jurka1e2f4652013-07-08 18:03:46 -0700291 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700292 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 private Bundle mSavedInstanceState;
295
Joe Onorato9c1289c2009-08-17 11:03:03 -0400296 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800297 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800298 private boolean mUserPresent = true;
299 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700300 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800301 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700303 private static LocaleConfiguration sLocaleConfiguration = null;
304
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700305 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700306
Adam Cohen61f560d2013-09-30 15:58:20 -0700307 private View.OnTouchListener mHapticFeedbackTouchListener;
308
Adam Cohended9f8d2010-11-03 13:25:16 -0700309 // Related to the auto-advancing of widgets
310 private final int ADVANCE_MSG = 1;
311 private final int mAdvanceInterval = 20000;
312 private final int mAdvanceStagger = 250;
313 private long mAutoAdvanceSentTime;
314 private long mAutoAdvanceTimeLeft = -1;
315 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
316 new HashMap<View, AppWidgetProviderInfo>();
317
Winson Chung400438b2011-01-16 17:53:48 -0800318 // Determines how long to wait after a rotation before restoring the screen orientation to
319 // match the sensor state.
320 private final int mRestoreScreenOrientationDelay = 500;
321
Craig Mautner360310b2012-10-26 15:13:08 -0700322 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700323
Adam Cohen1462de32012-07-24 22:34:36 -0700324 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700325 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700326
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700327 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700328 static Date sDateStamp = new Date();
329 static DateFormat sDateFormat =
330 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
331 static long sRunStart = System.currentTimeMillis();
332 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700333
Winson Chung46353de2012-02-16 14:05:10 -0800334 // We only want to get the SharedPreferences once since it does an FS stat each time we get
335 // it from the context.
336 private SharedPreferences mSharedPrefs;
337
Adam Cohene25af792013-06-06 23:08:25 -0700338 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
339
Winson Chungf0c6ae02012-03-21 16:10:31 -0700340 // Holds the page that we need to animate to, and the icon views that we need to animate up
341 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700342 private ImageView mFolderIconImageView;
343 private Bitmap mFolderIconBitmap;
344 private Canvas mFolderIconCanvas;
345 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700346
Michael Jurkaddd62e92011-02-16 17:49:14 -0800347 private BubbleTextView mWaitingForResume;
348
Adam Cohen59400422014-03-05 18:07:04 -0800349 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
350 new HashMap<String, CustomAppWidget>();
351
352 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
353 static {
354 if (ENABLE_CUSTOM_WIDGET_TEST) {
355 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
356 }
357 }
358
Michael Jurkac1f5d262011-09-30 19:32:27 -0700359 private Runnable mBuildLayersRunnable = new Runnable() {
360 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700361 if (mWorkspace != null) {
362 mWorkspace.buildPageHardwareLayers();
363 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700364 }
365 };
366
Adam Cohendb364c32014-05-20 14:23:40 -0700367 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800368
369 private static class PendingAddArguments {
370 int requestCode;
371 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700372 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700373 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800374 int cellX;
375 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700376 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800377 }
378
Daniel Sandlerff02d492013-08-05 02:12:05 -0400379 private Stats mStats;
380
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700381 FocusIndicatorView mFocusHandler;
382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 @Override
384 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700385 if (DEBUG_STRICT_MODE) {
386 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
387 .detectDiskReads()
388 .detectDiskWrites()
389 .detectNetwork() // or .detectAll() for all detectable problems
390 .penaltyLog()
391 .build());
392 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
393 .detectLeakedSqlLiteObjects()
394 .detectLeakedClosableObjects()
395 .penaltyLog()
396 .penaltyDeath()
397 .build());
398 }
399
Adam Cohen9211d422014-10-07 18:14:53 -0700400 if (mLauncherCallbacks != null) {
401 mLauncherCallbacks.preOnCreate();
402 }
403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400405
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400406 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400407 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700408 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700409
Winson Chung5f8afe62013-08-12 16:19:28 -0700410 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700411 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700412
413 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400414 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700415 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700416 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800417 mModel = app.setLauncher(this);
418 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700419 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400420 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700422
Daniel Sandlerff02d492013-08-05 02:12:05 -0400423 mStats = new Stats(this);
424
Sunny Goyalffe83f12014-08-14 17:39:34 -0700425 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700426
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
428 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700430 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
431 // this also ensures that any synchronous binding below doesn't re-trigger another
432 // LauncherModel load.
433 mPaused = false;
434
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200436 android.os.Debug.startMethodTracing(
437 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 }
439
440 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100444 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800446 registerContentObservers();
447
Joe Onorato7c312c12009-08-13 21:36:53 -0700448 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 mSavedState = savedInstanceState;
451 restoreState(mSavedState);
452
453 if (PROFILE_STARTUP) {
454 android.os.Debug.stopMethodTracing();
455 }
456
457 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700458 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700459 // If the user leaves launcher, then we should just load items asynchronously when
460 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500461 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 } else {
463 // We only load the page synchronously if the user rotates (or triggers a
464 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800465 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 }
468
469 // For handling default keys
470 mDefaultKeySsb = new SpannableStringBuilder();
471 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800472
473 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
474 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800475
Adam Cohenf9426d52012-06-04 17:26:21 -0700476 // On large interfaces, we want the screen to auto-rotate based on the current orientation
477 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700478
Adam Cohen9211d422014-10-07 18:14:53 -0700479 if (mLauncherCallbacks != null) {
480 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700481 if (mLauncherCallbacks.hasLauncherOverlay()) {
482 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700483 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
484 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
485 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700486 mWorkspace.setLauncherOverlay(mLauncherOverlay);
487 }
Adam Cohen9211d422014-10-07 18:14:53 -0700488 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700489
490 if (shouldShowIntroScreen()) {
491 showIntroScreen();
492 } else {
493 showFirstRunActivity();
494 showFirstRunClings();
495 }
Adam Cohen9211d422014-10-07 18:14:53 -0700496 }
497
498 private LauncherCallbacks mLauncherCallbacks;
499
500 public void onPostCreate(Bundle savedInstanceState) {
501 super.onPostCreate(savedInstanceState);
502 if (mLauncherCallbacks != null) {
503 mLauncherCallbacks.onPostCreate(savedInstanceState);
504 }
505 }
506
507 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
508 mLauncherCallbacks = callbacks;
509 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700510 }
511
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700512 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700513 public void onLauncherProviderChange() {
514 if (mLauncherCallbacks != null) {
515 mLauncherCallbacks.onLauncherProviderChange();
516 }
517 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700518
Adam Cohen9211d422014-10-07 18:14:53 -0700519 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700520 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700521 if (mLauncherCallbacks != null) {
522 return mLauncherCallbacks.hasCustomContentToLeft();
523 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700524 return false;
525 }
526
Dave Hawkeya8881582013-09-17 15:55:33 -0600527 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500528 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600529 * {@link #addToCustomContentPage}. This will only be invoked if
530 * {@link #hasCustomContentToLeft()} is {@code true}.
531 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500532 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700533 if (mLauncherCallbacks != null) {
534 mLauncherCallbacks.populateCustomContentContainer();
535 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600536 }
537
538 /**
539 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
540 * ensure the custom content page is added or removed if necessary.
541 */
542 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600543 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600544 // Not bound yet, wait for bindScreens to be called.
545 return;
546 }
547
548 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
549 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500550 mWorkspace.createCustomContentContainer();
551 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600552 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
553 mWorkspace.removeCustomContentPage();
554 }
555 }
556
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700558 if (sLocaleConfiguration == null) {
559 new AsyncTask<Void, Void, LocaleConfiguration>() {
560 @Override
561 protected LocaleConfiguration doInBackground(Void... unused) {
562 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
563 readConfiguration(Launcher.this, localeConfiguration);
564 return localeConfiguration;
565 }
566
567 @Override
568 protected void onPostExecute(LocaleConfiguration result) {
569 sLocaleConfiguration = result;
570 checkForLocaleChange(); // recursive, but now with a locale configuration
571 }
572 }.execute();
573 return;
574 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700575
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 final Configuration configuration = getResources().getConfiguration();
577
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700578 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 final String locale = configuration.locale.toString();
580
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700581 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 final int mcc = configuration.mcc;
583
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700584 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 final int mnc = configuration.mnc;
586
Romain Guy84f296c2009-11-04 15:00:44 -0800587 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588
Romain Guy84f296c2009-11-04 15:00:44 -0800589 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700590 sLocaleConfiguration.locale = locale;
591 sLocaleConfiguration.mcc = mcc;
592 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700593
Joe Onorato0589f0f2010-02-08 13:44:00 -0800594 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700595
596 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100597 new AsyncTask<Void, Void, Void>() {
598 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700599 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100600 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700601 }
Michael Jurka43467462013-11-14 12:03:58 +0100602 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700603 }
604 }
605
606 private static class LocaleConfiguration {
607 public String locale;
608 public int mcc = -1;
609 public int mnc = -1;
610 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700611
Romain Guy98d01652009-06-30 16:21:04 -0700612 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
613 DataInputStream in = null;
614 try {
Helena Josol28db2802014-10-09 17:04:09 +0100615 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700616 configuration.locale = in.readUTF();
617 configuration.mcc = in.readInt();
618 configuration.mnc = in.readInt();
619 } catch (FileNotFoundException e) {
620 // Ignore
621 } catch (IOException e) {
622 // Ignore
623 } finally {
624 if (in != null) {
625 try {
626 in.close();
627 } catch (IOException e) {
628 // Ignore
629 }
630 }
631 }
632 }
633
634 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
635 DataOutputStream out = null;
636 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100637 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100638 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700639 out.writeUTF(configuration.locale);
640 out.writeInt(configuration.mcc);
641 out.writeInt(configuration.mnc);
642 out.flush();
643 } catch (FileNotFoundException e) {
644 // Ignore
645 } catch (IOException e) {
646 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100647 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700648 } finally {
649 if (out != null) {
650 try {
651 out.close();
652 } catch (IOException e) {
653 // Ignore
654 }
655 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 }
657 }
658
Anton Hanssona2f665f2013-09-26 12:18:32 +0100659 public Stats getStats() {
660 return mStats;
661 }
662
Bjorn Bringertc459e522013-06-07 19:36:01 +0100663 public LayoutInflater getInflater() {
664 return mInflater;
665 }
666
Winson Chung36a62fe2012-05-06 18:04:42 -0700667 boolean isDraggingEnabled() {
668 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
669 // that is subsequently removed from the workspace in startBinding().
670 return !mModel.isLoadingWorkspace();
671 }
672
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800673 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000674 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100675 if (Build.VERSION.SDK_INT >= 17) {
676 return View.generateViewId();
677 } else {
678 // View.generateViewId() is not available. The following fallback logic is a copy
679 // of its implementation.
680 for (;;) {
681 final int result = sNextGeneratedId.get();
682 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
683 int newValue = result + 1;
684 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
685 if (sNextGeneratedId.compareAndSet(result, newValue)) {
686 return result;
687 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000688 }
689 }
690 }
691
692 public int getViewIdForItem(ItemInfo info) {
693 // This cast is safe given the > 2B range for int.
694 int itemId = (int) info.id;
695 if (mItemIdToViewId.containsKey(itemId)) {
696 return mItemIdToViewId.get(itemId);
697 }
698 int viewId = generateViewId();
699 mItemIdToViewId.put(itemId, viewId);
700 return viewId;
701 }
702
703 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700704 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
705 * a configuration step, this allows the proper animations to run after other transitions.
706 */
Adam Cohendb364c32014-05-20 14:23:40 -0700707 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700708 long screenId = args.screenId;
709 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
710 // When the screen id represents an actual screen (as opposed to a rank) we make sure
711 // that the drop page actually exists.
712 screenId = ensurePendingDropLayoutExists(args.screenId);
713 }
Adam Cohendb364c32014-05-20 14:23:40 -0700714
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800715 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800716 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700717 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700718 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700719 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800720 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700721 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700722 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700723 case REQUEST_RECONFIGURE_APPWIDGET:
724 completeRestoreAppWidget(args.appWidgetId);
725 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800726 }
Michael Jurka27614d22012-04-02 06:40:22 -0700727 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
728 // if you turned the screen off and then back while in All Apps, Launcher would not
729 // return to the workspace. Clearing mAddInfo.container here fixes this issue
730 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700731 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800732 }
733
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800734 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700735 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700736 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700737 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700738 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800739 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700740
Adam Cohenad4e15c2013-10-17 16:21:35 -0700741 Runnable exitSpringLoaded = new Runnable() {
742 @Override
743 public void run() {
744 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
745 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
746 }
747 };
748
Michael Jurka8b805b12012-04-18 14:23:14 -0700749 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700750 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700751 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
752 if (resultCode == RESULT_CANCELED) {
753 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700754 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700756 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800757 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700759 }
760 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200761 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
762 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
763 mWorkspace.exitOverviewMode(false);
764 }
765 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700766 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200767
Adam Cohened66b2b2012-01-23 17:28:51 -0800768 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
769 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700770
Adam Cohendb364c32014-05-20 14:23:40 -0700771 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800772 // We have special handling for widgets
773 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800774 final int appWidgetId;
775 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
776 : -1;
777 if (widgetId < 0) {
778 appWidgetId = pendingAddWidgetId;
779 } else {
780 appWidgetId = widgetId;
781 }
782
Adam Cohenad4e15c2013-10-17 16:21:35 -0700783 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800784 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700785 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
786 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700787 result = RESULT_CANCELED;
788 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700789 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700790 @Override
791 public void run() {
792 exitSpringLoadedDragModeDelayed(false, 0, null);
793 }
794 };
Adam Cohendb364c32014-05-20 14:23:40 -0700795 if (workspaceLocked) {
796 // No need to remove the empty screen if we're mid-binding, as the
797 // the bind will not add the empty screen.
798 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
799 } else {
800 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
801 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
802 }
Winson Chung5aaab772012-04-27 15:24:02 -0700803 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700804 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700805 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
806 // When the screen id represents an actual screen (as opposed to a rank)
807 // we make sure that the drop page actually exists.
808 mPendingAddInfo.screenId =
809 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
810 }
Adam Cohendb364c32014-05-20 14:23:40 -0700811 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
812
813 dropLayout.setDropPending(true);
814 final Runnable onComplete = new Runnable() {
815 @Override
816 public void run() {
817 completeTwoStageWidgetDrop(resultCode, appWidgetId);
818 dropLayout.setDropPending(false);
819 }
820 };
821 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
822 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
823 } else {
824 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
825 mPendingAddInfo);
826 sPendingAddItem = args;
827 }
Winson Chung5aaab772012-04-27 15:24:02 -0700828 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800829 return;
830 }
831
Sunny Goyalff572272014-07-23 13:58:07 -0700832 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
833 if (resultCode == RESULT_OK) {
834 // Update the widget view.
835 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
836 pendingAddWidgetId, mPendingAddInfo);
837 if (workspaceLocked) {
838 sPendingAddItem = args;
839 } else {
840 completeAdd(args);
841 }
842 }
843 // Leave the widget in the pending state if the user canceled the configure.
844 return;
845 }
846
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 // The pattern used here is that a user PICKs a specific application,
848 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700849
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
851 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700852 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700853 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
854 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800855 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700856 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800857 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700858 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700859 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
860 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 }
Adam Cohen00074722013-10-11 17:46:09 -0700862 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700863 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700864 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800866 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800867
868 }
869
870 @Override
871 protected void onActivityResult(
872 final int requestCode, final int resultCode, final Intent data) {
873 handleActivityResult(requestCode, resultCode, data);
874 if (mLauncherCallbacks != null) {
875 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
876 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800877 }
878
Adam Cohendb364c32014-05-20 14:23:40 -0700879 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
880 appWidgetId, ItemInfo info) {
881 PendingAddArguments args = new PendingAddArguments();
882 args.requestCode = requestCode;
883 args.intent = data;
884 args.container = info.container;
885 args.screenId = info.screenId;
886 args.cellX = info.cellX;
887 args.cellY = info.cellY;
888 args.appWidgetId = appWidgetId;
889 return args;
890 }
891
892 /**
893 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
894 *
895 * @param screenId the screen id to check
896 * @return the new screen, or screenId if it exists
897 */
898 private long ensurePendingDropLayoutExists(long screenId) {
899 CellLayout dropLayout =
900 (CellLayout) mWorkspace.getScreenWithId(screenId);
901 if (dropLayout == null) {
902 // it's possible that the add screen was removed because it was
903 // empty and a re-bind occurred
904 mWorkspace.addExtraEmptyScreen();
905 return mWorkspace.commitExtraEmptyScreen();
906 } else {
907 return screenId;
908 }
909 }
910
Adam Cohened66b2b2012-01-23 17:28:51 -0800911 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700912 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700913 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 Runnable onCompleteRunnable = null;
915 int animationType = 0;
916
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700917 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800918 if (resultCode == RESULT_OK) {
919 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
920 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700921 mPendingAddWidgetInfo);
922 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 onCompleteRunnable = new Runnable() {
924 @Override
925 public void run() {
926 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700927 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700928 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
929 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 }
931 };
932 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800933 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800935 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700936 if (mDragLayer.getAnimatedView() != null) {
937 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
938 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
939 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700940 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700941 // The animated view may be null in the case of a rotation during widget configuration
942 onCompleteRunnable.run();
943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945
946 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700947 protected void onStop() {
948 super.onStop();
949 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700950
951 if (mLauncherCallbacks != null) {
952 mLauncherCallbacks.onStop();
953 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700954 }
955
956 @Override
957 protected void onStart() {
958 super.onStart();
959 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700960
961 if (mLauncherCallbacks != null) {
962 mLauncherCallbacks.onStart();
963 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700964 }
965
966 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200968 long startTime = 0;
969 if (DEBUG_RESUME_TIME) {
970 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400971 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200972 }
Adam Cohen9211d422014-10-07 18:14:53 -0700973
974 if (mLauncherCallbacks != null) {
975 mLauncherCallbacks.preOnResume();
976 }
977
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700979
Winson Chung4a2afa32012-07-19 14:53:05 -0700980 // Restore the previous launcher state
981 if (mOnResumeState == State.WORKSPACE) {
982 showWorkspace(false);
983 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c62013-11-06 15:49:51 -0800984 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700985 }
986 mOnResumeState = State.NONE;
987
Craig Mautner360310b2012-10-26 15:13:08 -0700988 // Background was set to gradient in onPause(), restore to black if in all apps.
989 setWorkspaceBackground(mState == State.WORKSPACE);
990
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800991 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700992 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700993 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500994 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400995 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700996 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700998 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200999 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1000 // execute them here
1001 long startTimeCallbacks = 0;
1002 if (DEBUG_RESUME_TIME) {
1003 startTimeCallbacks = System.currentTimeMillis();
1004 }
1005
1006 if (mAppsCustomizeContent != null) {
1007 mAppsCustomizeContent.setBulkBind(true);
1008 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001009 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1010 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001011 }
1012 if (mAppsCustomizeContent != null) {
1013 mAppsCustomizeContent.setBulkBind(false);
1014 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001015 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001016 if (DEBUG_RESUME_TIME) {
1017 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1018 (System.currentTimeMillis() - startTimeCallbacks));
1019 }
Michael Jurka447bf842013-05-15 14:52:15 +02001020 }
Michael Jurka54554252013-08-01 12:52:23 +02001021 if (mOnResumeCallbacks.size() > 0) {
1022 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1023 mOnResumeCallbacks.get(i).run();
1024 }
1025 mOnResumeCallbacks.clear();
1026 }
Winson Chunge4e50662012-01-23 14:45:13 -08001027
1028 // Reset the pressed state of icons that were locked in the press state while activities
1029 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001030 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001031 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001032 mWaitingForResume.setStayPressed(false);
1033 }
Winson Chung82963d52013-10-09 11:20:57 -07001034
Adam Cohen06dff352012-06-01 17:17:08 -07001035 // It is possible that widgets can receive updates while launcher is not in the foreground.
1036 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1037 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1038 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001039 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001040
Winson Chung780fe592013-09-26 14:48:44 -07001041 // Process any items that were added while Launcher was away.
1042 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1043
Michael Jurka447bf842013-05-15 14:52:15 +02001044 if (DEBUG_RESUME_TIME) {
1045 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1046 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001047
1048 if (mWorkspace.getCustomContentCallbacks() != null) {
1049 // If we are resuming and the custom content is the current page, we call onShow().
1050 // It is also poassible that onShow will instead be called slightly after first layout
1051 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1052 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001053 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001054 }
1055 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001056 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001057 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001058
1059 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001060
1061 if (mLauncherCallbacks != null) {
1062 mLauncherCallbacks.onResume();
1063 }
Adam Cohen06dff352012-06-01 17:17:08 -07001064 }
1065
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001067 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001068 // Ensure that items added to Launcher are queued until Launcher returns
1069 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001070 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001071
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001072 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001073 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001074 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001075 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001076
1077 // We call onHide() aggressively. The custom content callbacks should be able to
1078 // debounce excess onHide calls.
1079 if (mWorkspace.getCustomContentCallbacks() != null) {
1080 mWorkspace.getCustomContentCallbacks().onHide();
1081 }
Romain Guycbb89e42009-06-08 15:52:54 -07001082
Adam Cohen9211d422014-10-07 18:14:53 -07001083 if (mLauncherCallbacks != null) {
1084 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001085 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001086 }
1087
1088 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001089 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1090 // by a onResume or by scrolling otherwise.
1091 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001092
1093 // Custom content is completely hidden
1094 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001095
1096 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1097 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001098
1099 // Indicates whether the user is allowed to scroll away from the custom content.
1100 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001101 }
1102
Adam Cohenc2d6e892014-10-16 09:49:24 -07001103 public interface LauncherOverlay {
1104
1105 /**
1106 * Touch interaction leading to overscroll has begun
1107 */
1108 public void onScrollInteractionBegin();
1109
1110 /**
1111 * Touch interaction related to overscroll has ended
1112 */
1113 public void onScrollInteractionEnd();
1114
1115 /**
1116 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1117 * screen (or in the case of RTL, the rightmost screen).
1118 */
1119 public void onScrollChange(int progress, boolean rtl);
1120
1121 /**
1122 * Screen has stopped scrolling
1123 */
1124 public void onScrollSettled();
1125
1126 /**
1127 * This method can be called by the Launcher in order to force the LauncherOverlay
1128 * to exit fully immersive mode.
1129 */
1130 public void forceExitFullImmersion();
1131 }
1132
1133 public interface LauncherOverlayCallbacks {
1134 /**
1135 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1136 * however it doesn't modify any state within the launcher.
1137 */
1138 public boolean canEnterFullImmersion();
1139
1140 /**
1141 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1142 * eg. by occupying the full screen and handling all touch events.
1143 *
1144 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1145 * case, Launcher will modify any necessary state and assumes the overlay is
1146 * handling all interaction. If false, the LauncherOverlay should cancel any
1147 *
1148 */
1149 public boolean enterFullImmersion();
1150
1151 /**
1152 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1153 * full control over UI and state.
1154 */
1155 public void exitFullImmersion();
1156 }
1157
1158 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1159
1160 @Override
1161 public boolean canEnterFullImmersion() {
1162 return mState == State.WORKSPACE;
1163 }
1164
1165 @Override
1166 public boolean enterFullImmersion() {
1167 if (mState == State.WORKSPACE) {
1168 // When fully immersed, disregard any touches which fall through.
1169 mDragLayer.setBlockTouch(true);
1170 return true;
1171 }
1172 return false;
1173 }
1174
1175 @Override
1176 public void exitFullImmersion() {
1177 mDragLayer.setBlockTouch(false);
1178 }
1179 }
1180
Jorim Jaggid017f882014-01-14 17:08:48 -08001181 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001182 if (mLauncherCallbacks != null) {
1183 return mLauncherCallbacks.hasSettings();
1184 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001185 return false;
1186 }
1187
Adam Cohenbffe7452013-07-22 18:21:45 -07001188
Adam Cohen9211d422014-10-07 18:14:53 -07001189 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001190 CustomContentCallbacks callbacks, String description) {
1191 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001192 }
1193
Adam Cohenedb40762013-07-18 16:45:45 -07001194 // The custom content needs to offset its content to account for the QSB
1195 public int getTopOffsetForCustomContent() {
1196 return mWorkspace.getPaddingTop();
1197 }
1198
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001199 @Override
1200 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001202 if (mModel.isCurrentCallbacks(this)) {
1203 mModel.stopLoader();
1204 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001205 if (mAppsCustomizeContent != null) {
1206 mAppsCustomizeContent.surrender();
1207 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001208 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001209 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001210
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 @Override
1213 public void onWindowFocusChanged(boolean hasFocus) {
1214 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001215 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001216
1217 if (mLauncherCallbacks != null) {
1218 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1219 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 private boolean acceptFilter() {
1223 final InputMethodManager inputManager = (InputMethodManager)
1224 getSystemService(Context.INPUT_METHOD_SERVICE);
1225 return !inputManager.isFullscreenMode();
1226 }
1227
1228 @Override
1229 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001230 final int uniChar = event.getUnicodeChar();
1231 final boolean handled = super.onKeyDown(keyCode, event);
1232 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1233 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1235 keyCode, event);
1236 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001237 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001238 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001239 // showSearchDialog()
1240 // If there are multiple keystrokes before the search dialog takes focus,
1241 // onSearchRequested() will be called for every keystroke,
1242 // but it is idempotent, so it's fine.
1243 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245 }
1246
Joe Onorato8a9625e2010-01-28 15:55:35 -08001247 // Eat the long press event so the keyboard doesn't come up.
1248 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1249 return true;
1250 }
1251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 return handled;
1253 }
1254
Karl Rosaen138a0412009-04-23 19:00:21 -07001255 private String getTypedText() {
1256 return mDefaultKeySsb.toString();
1257 }
1258
1259 private void clearTypedText() {
1260 mDefaultKeySsb.clear();
1261 mDefaultKeySsb.clearSpans();
1262 Selection.setSelection(mDefaultKeySsb, 0);
1263 }
1264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001266 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1267 * State
1268 */
1269 private static State intToState(int stateOrdinal) {
1270 State state = State.WORKSPACE;
1271 final State[] stateValues = State.values();
1272 for (int i = 0; i < stateValues.length; i++) {
1273 if (stateValues[i].ordinal() == stateOrdinal) {
1274 state = stateValues[i];
1275 break;
1276 }
1277 }
1278 return state;
1279 }
1280
1281 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 * Restores the previous state, if it exists.
1283 *
1284 * @param savedState The previous state.
1285 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001286 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 private void restoreState(Bundle savedState) {
1288 if (savedState == null) {
1289 return;
1290 }
1291
Michael Jurka883f55b2010-10-21 15:47:14 -07001292 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001293 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001294 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001295 }
1296
Adam Cohen21cd0022013-10-09 18:57:02 -07001297 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1298 PagedView.INVALID_RESTORE_PAGE);
1299 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001300 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302
Winson Chung3d503fb2011-07-13 17:25:49 -07001303 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001304 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001305
Winson Chung3d503fb2011-07-13 17:25:49 -07001306 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1307 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001308 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001309 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1310 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001311 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1312 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1313 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001314 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001315 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 mRestoring = true;
1317 }
1318
1319 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1320 if (renameFolder) {
1321 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001322 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 mRestoring = true;
1324 }
Winson Chunga12a2502010-12-20 14:41:35 -08001325
Winson Chung785d2eb2011-04-14 16:08:02 -07001326 // Restore the AppsCustomize tab
1327 if (mAppsCustomizeTabHost != null) {
1328 String curTab = savedState.getString("apps_customize_currentTab");
1329 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001330 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001331 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001332 mAppsCustomizeContent.loadAssociatedPages(
1333 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001334 }
1335
Winson Chung5afbf7b2011-07-25 11:53:08 -07001336 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1337 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001338 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001339 mItemIdToViewId = (HashMap<Integer, Integer>)
1340 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 }
1342
1343 /**
1344 * Finds all the views we need and configure them properly.
1345 */
1346 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001347 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001348
Craig Mautner360310b2012-10-26 15:13:08 -07001349 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001350 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001351 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1352 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001353 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001354 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001355
John Spurlock77e1f472013-09-11 10:09:51 -04001356 mLauncherView.setSystemUiVisibility(
1357 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001358 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359
Winson Chung4c98d922011-05-31 16:50:48 -07001360 // Setup the drag layer
1361 mDragLayer.setup(this, dragController);
1362
Winson Chung3d503fb2011-07-13 17:25:49 -07001363 // Setup the hotseat
1364 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1365 if (mHotseat != null) {
1366 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001367 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001368 }
1369
Jorim Jaggid017f882014-01-14 17:08:48 -08001370 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001371 View widgetButton = findViewById(R.id.widget_button);
1372 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001373 @Override
1374 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001375 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001376 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001377 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001378 }
1379 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001380 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1381
1382 View wallpaperButton = findViewById(R.id.wallpaper_button);
1383 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001384 @Override
1385 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001386 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001387 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001388 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001389 }
1390 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001391 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1392
1393 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001394 if (hasSettings()) {
1395 settingsButton.setOnClickListener(new OnClickListener() {
1396 @Override
1397 public void onClick(View arg0) {
1398 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001399 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001400 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001401 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001402 });
1403 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1404 } else {
1405 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001406 }
1407
Adam Cohendbdff6b2013-09-18 19:09:15 -07001408 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001409
Winson Chung4c98d922011-05-31 16:50:48 -07001410 // Setup the workspace
1411 mWorkspace.setHapticFeedbackEnabled(false);
1412 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001413 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001414 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001415
Winson Chungf0ea4d32011-06-06 14:27:16 -07001416 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001417 mSearchDropTargetBar = (SearchDropTargetBar)
1418 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001419
Winson Chungf0ea4d32011-06-06 14:27:16 -07001420 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001421 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001422 mAppsCustomizeContent = (AppsCustomizePagedView)
1423 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1424 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001425
Winson Chung3d503fb2011-07-13 17:25:49 -07001426 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001427 dragController.setDragScoller(mWorkspace);
1428 dragController.setScrollView(mDragLayer);
1429 dragController.setMoveTarget(mWorkspace);
1430 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001431 if (mSearchDropTargetBar != null) {
1432 mSearchDropTargetBar.setup(this, dragController);
Sunny Goyal594d76d2014-11-06 10:12:54 -08001433 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001434 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001435
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001436 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001437 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001438 mWeightWatcher = new WeightWatcher(this);
1439 mWeightWatcher.setAlpha(0.5f);
1440 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001441 new FrameLayout.LayoutParams(
1442 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001443 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001444 Gravity.BOTTOM)
1445 );
Adam Cohen39a06042013-07-19 14:30:12 -07001446
1447 boolean show = shouldShowWeightWatcher();
1448 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001449 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 }
1451
1452 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001453 * Sets the all apps button. This method is called from {@link Hotseat}.
1454 */
1455 public void setAllAppsButton(View allAppsButton) {
1456 mAllAppsButton = allAppsButton;
1457 }
1458
1459 public View getAllAppsButton() {
1460 return mAllAppsButton;
1461 }
1462
1463 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 * Creates a view representing a shortcut.
1465 *
1466 * @param info The data structure describing the shortcut.
1467 *
1468 * @return A View inflated from R.layout.application.
1469 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001470 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001472 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 }
1474
1475 /**
1476 * Creates a view representing a shortcut inflated from the specified resource.
1477 *
1478 * @param layoutResId The id of the XML layout used to create the shortcut.
1479 * @param parent The group the shortcut belongs to.
1480 * @param info The data structure describing the shortcut.
1481 *
1482 * @return A View inflated from layoutResId.
1483 */
Adam Cohen59400422014-03-05 18:07:04 -08001484 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001485 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001486 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001488 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 return favorite;
1490 }
1491
1492 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 * Add a shortcut to the workspace.
1494 *
1495 * @param data The intent describing the shortcut.
1496 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001498 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001499 int cellY) {
1500 int[] cellXY = mTmpAddItemCellCoordinates;
1501 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001502 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001503
Michael Jurkad3ef3062010-11-23 16:23:58 -08001504 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001505
Sunny Goyal2350bc92014-10-14 16:42:54 -07001506 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001507 if (info == null) {
1508 return;
1509 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001510 final View view = createShortcut(info);
1511
Adam Cohenfbba09b2011-07-18 21:43:05 -07001512 // First we check if we already know the exact location where we want to add this item.
1513 if (cellX >= 0 && cellY >= 0) {
1514 cellXY[0] = cellX;
1515 cellXY[1] = cellY;
1516 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001517
1518 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001519 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001520 true, null,null)) {
1521 return;
1522 }
1523 DragObject dragObject = new DragObject();
1524 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001525 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1526 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001527 return;
1528 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001529 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001530 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001531 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001532 foundCellSpan = (result != null);
1533 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001534 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001535 }
1536
1537 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001538 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001539 return;
1540 }
1541
Adam Cohendcd297f2013-06-18 13:13:40 -07001542 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543
1544 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001545 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001546 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
1548 }
1549
Adam Cohen2f093b62012-04-30 18:59:53 -07001550 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1551 int minHeight) {
1552 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001553 // We want to account for the extra amount of padding that we are adding to the widget
1554 // to ensure that it gets the full amount of space that it has requested
1555 int requiredWidth = minWidth + padding.left + padding.right;
1556 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001557 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001558 }
1559
Adam Cohen2f093b62012-04-30 18:59:53 -07001560 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1561 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001562 }
1563
Adam Cohen2f093b62012-04-30 18:59:53 -07001564 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1565 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001566 }
1567
Adam Cohen2f093b62012-04-30 18:59:53 -07001568 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1569 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001570 }
1571
Adam Cohen2f093b62012-04-30 18:59:53 -07001572 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1573 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001574 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001575 }
1576
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001578 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001580 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 */
Adam Cohen59400422014-03-05 18:07:04 -08001582 private void completeAddAppWidget(int appWidgetId, long container, long screenId,
1583 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1584
1585 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001586 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001587 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1588 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001589 }
Romain Guycbb89e42009-06-08 15:52:54 -07001590
Adam Cohen59400422014-03-05 18:07:04 -08001591 if (appWidgetInfo.isCustomWidget) {
1592 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001593 }
1594
Adam Cohen59400422014-03-05 18:07:04 -08001595 LauncherAppWidgetInfo launcherInfo;
1596 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1597 launcherInfo.spanX = info.spanX;
1598 launcherInfo.spanY = info.spanY;
1599 launcherInfo.minSpanX = info.minSpanX;
1600 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001601 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001602
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001604 container, screenId, info.cellX, info.cellY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605
1606 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001607 if (hostView == null) {
1608 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001609 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1610 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001611 } else {
1612 // The AppWidgetHostView has already been inflated and instantiated
1613 launcherInfo.hostView = hostView;
1614 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001616 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001617 launcherInfo.notifyWidgetSizeChanged(this);
1618
Adam Cohen59400422014-03-05 18:07:04 -08001619 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1620 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001621
1622 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001624 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 }
Romain Guycbb89e42009-06-08 15:52:54 -07001626
Adam Cohended9f8d2010-11-03 13:25:16 -07001627 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1628 @Override
1629 public void onReceive(Context context, Intent intent) {
1630 final String action = intent.getAction();
1631 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1632 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001633 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001634 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001635
Winson Chungc100e8e2011-08-09 16:02:43 -07001636 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001637 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001638 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001639 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001640 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001641 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1642 mUserPresent = true;
1643 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001644 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1645 mModel.resetLoadedState(false, true);
1646 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1647 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1648 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1649 mModel.resetLoadedState(false, true);
1650 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1651 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1652 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001653 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1654 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1655 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001656 }
1657 }
1658 };
1659
1660 @Override
1661 public void onAttachedToWindow() {
1662 super.onAttachedToWindow();
1663
1664 // Listen for broadcasts related to user-presence
1665 final IntentFilter filter = new IntentFilter();
1666 filter.addAction(Intent.ACTION_SCREEN_OFF);
1667 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001668 // For handling managed profiles
1669 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1670 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001671 if (ENABLE_DEBUG_INTENTS) {
1672 filter.addAction(DebugIntents.DELETE_DATABASE);
1673 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1674 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001675 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001676 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001677 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001678 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001679 mVisible = true;
1680 }
1681
Adam Cohen0b395352014-06-09 22:54:36 +00001682 /**
1683 * Sets up transparent navigation and status bars in LMP.
1684 * This method is a no-op for other platform versions.
1685 */
Sunny Goyald0091012015-01-20 15:55:34 -08001686 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001687 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001688 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001689 Window window = getWindow();
1690 window.getAttributes().systemUiVisibility |=
1691 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1692 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1693 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1694 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1695 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1696 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1697 window.setStatusBarColor(Color.TRANSPARENT);
1698 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001699 }
1700 }
1701
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 @Override
1703 public void onDetachedFromWindow() {
1704 super.onDetachedFromWindow();
1705 mVisible = false;
1706
Adam Cohend113e0c2010-11-11 10:48:05 -08001707 if (mAttached) {
1708 unregisterReceiver(mReceiver);
1709 mAttached = false;
1710 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 updateRunning();
1712 }
1713
1714 public void onWindowVisibilityChanged(int visibility) {
1715 mVisible = visibility == View.VISIBLE;
1716 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001717 // The following code used to be in onResume, but it turns out onResume is called when
1718 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1719 // is a more appropriate event to handle
1720 if (mVisible) {
1721 mAppsCustomizeTabHost.onWindowVisible();
1722 if (!mWorkspaceLoading) {
1723 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001724 // We want to let Launcher draw itself at least once before we force it to build
1725 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001726 // apps is nice and speedy.
1727 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001728 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001729 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001730 if (mStarted) return;
1731 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001732 // We delay the layer building a bit in order to give
1733 // other message processing a time to run. In particular
1734 // this avoids a delay in hiding the IME if it was
1735 // currently shown, because doing that may involve
1736 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001737 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001738 final ViewTreeObserver.OnDrawListener listener = this;
1739 mWorkspace.post(new Runnable() {
1740 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001741 if (mWorkspace != null &&
1742 mWorkspace.getViewTreeObserver() != null) {
1743 mWorkspace.getViewTreeObserver().
1744 removeOnDrawListener(listener);
1745 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001746 }
1747 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001748 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001749 }
1750 });
1751 }
1752 clearTypedText();
1753 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001754 }
1755
1756 private void sendAdvanceMessage(long delay) {
1757 mHandler.removeMessages(ADVANCE_MSG);
1758 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1759 mHandler.sendMessageDelayed(msg, delay);
1760 mAutoAdvanceSentTime = System.currentTimeMillis();
1761 }
1762
1763 private void updateRunning() {
1764 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1765 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1766 mAutoAdvanceRunning = autoAdvanceRunning;
1767 if (autoAdvanceRunning) {
1768 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1769 sendAdvanceMessage(delay);
1770 } else {
1771 if (!mWidgetsToAdvance.isEmpty()) {
1772 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1773 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1774 }
1775 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001776 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001777 }
1778 }
1779 }
1780
1781 private final Handler mHandler = new Handler() {
1782 @Override
1783 public void handleMessage(Message msg) {
1784 if (msg.what == ADVANCE_MSG) {
1785 int i = 0;
1786 for (View key: mWidgetsToAdvance.keySet()) {
1787 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1788 final int delay = mAdvanceStagger * i;
1789 if (v instanceof Advanceable) {
1790 postDelayed(new Runnable() {
1791 public void run() {
1792 ((Advanceable) v).advance();
1793 }
1794 }, delay);
1795 }
1796 i++;
1797 }
1798 sendAdvanceMessage(mAdvanceInterval);
1799 }
1800 }
1801 };
1802
1803 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001804 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001805 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1806 if (v instanceof Advanceable) {
1807 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001808 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001809 updateRunning();
1810 }
1811 }
1812
1813 void removeWidgetToAutoAdvance(View hostView) {
1814 if (mWidgetsToAdvance.containsKey(hostView)) {
1815 mWidgetsToAdvance.remove(hostView);
1816 updateRunning();
1817 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001818 }
1819
Joe Onorato9c1289c2009-08-17 11:03:03 -04001820 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001821 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001822 launcherInfo.hostView = null;
1823 }
1824
Winson Chung93eef082012-03-23 15:59:27 -07001825 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1826 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1827 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001828 }
1829
Adam Cohen59400422014-03-05 18:07:04 -08001830 public ArrayList<AppInfo> getAllAppsList() {
1831 return mAppsCustomizeContent.getApps();
1832 }
1833
Winson Chunga6945242014-01-08 14:04:34 -08001834 public DragLayer getDragLayer() {
1835 return mDragLayer;
1836 }
1837
1838 public Workspace getWorkspace() {
1839 return mWorkspace;
1840 }
1841
1842 public Hotseat getHotseat() {
1843 return mHotseat;
1844 }
1845
Jorim Jaggid017f882014-01-14 17:08:48 -08001846 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001847 return mOverviewPanel;
1848 }
1849
1850 public SearchDropTargetBar getSearchBar() {
1851 return mSearchDropTargetBar;
1852 }
1853
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001854 public LauncherAppWidgetHost getAppWidgetHost() {
1855 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 }
Romain Guycbb89e42009-06-08 15:52:54 -07001857
Winson Chunga9abd0e2010-10-27 17:18:37 -07001858 public LauncherModel getModel() {
1859 return mModel;
1860 }
1861
Winson Chunga6945242014-01-08 14:04:34 -08001862 protected SharedPreferences getSharedPrefs() {
1863 return mSharedPrefs;
1864 }
1865
Bjorn Bringertc459e522013-06-07 19:36:01 +01001866 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001867 getWindow().closeAllPanels();
1868
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001869 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001870 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001871 }
1872
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 @Override
1874 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001875 long startTime = 0;
1876 if (DEBUG_RESUME_TIME) {
1877 startTime = System.currentTimeMillis();
1878 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 super.onNewIntent(intent);
1880
1881 // Close the menu
1882 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001883 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001884 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001885
Adam Cohened307df2013-10-02 09:37:31 -07001886 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1887 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1888 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001889
Adam Cohen6fecd412013-10-02 17:41:50 -07001890 if (mWorkspace == null) {
1891 // Can be cases where mWorkspace is null, this prevents a NPE
1892 return;
1893 }
1894 Folder openFolder = mWorkspace.getOpenFolder();
1895 // In all these cases, only animate if we're already on home
1896 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001897
1898 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1899 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001900 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001901 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001902 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001903 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001904
Adam Cohen6fecd412013-10-02 17:41:50 -07001905 closeFolder();
1906 exitSpringLoadedDragMode();
1907
1908 // If we are already on home, then just animate back to the workspace,
1909 // otherwise, just wait until onResume to set the state back to Workspace
1910 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001911 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001912 } else {
1913 mOnResumeState = State.WORKSPACE;
1914 }
1915
1916 final View v = getWindow().peekDecorView();
1917 if (v != null && v.getWindowToken() != null) {
1918 InputMethodManager imm = (InputMethodManager)getSystemService(
1919 INPUT_METHOD_SERVICE);
1920 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1921 }
1922
1923 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001924 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001925 mAppsCustomizeTabHost.reset();
1926 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001927
Adam Cohen9211d422014-10-07 18:14:53 -07001928 if (mLauncherCallbacks != null) {
1929 mLauncherCallbacks.onHomeIntent();
1930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
Adam Cohened307df2013-10-02 09:37:31 -07001932
Michael Jurka447bf842013-05-15 14:52:15 +02001933 if (DEBUG_RESUME_TIME) {
1934 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936
Adam Cohen9211d422014-10-07 18:14:53 -07001937 if (mLauncherCallbacks != null) {
1938 mLauncherCallbacks.onNewIntent(intent);
1939 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001940 }
1941
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001943 public void onRestoreInstanceState(Bundle state) {
1944 super.onRestoreInstanceState(state);
1945 for (int page: mSynchronouslyBoundPages) {
1946 mWorkspace.restoreInstanceStateForChild(page);
1947 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949
1950 @Override
1951 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001952 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001953 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1954 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001955 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001956 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957
Michael Jurka883f55b2010-10-21 15:47:14 -07001958 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001959 // We close any open folder since it will not be re-opened, and we need to make sure
1960 // this state is reflected.
1961 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962
Adam Cohendcd297f2013-06-18 13:13:40 -07001963 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001964 mWaitingForResult) {
1965 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001966 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001967 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1968 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001969 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1970 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1971 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001972 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
1974
1975 if (mFolderInfo != null && mWaitingForResult) {
1976 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1977 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1978 }
Michael Jurka946ad472010-07-09 18:05:18 -07001979
Winson Chung785d2eb2011-04-14 16:08:02 -07001980 // Save the current AppsCustomize tab
1981 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08001982 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1983 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001984 if (currentTabTag != null) {
1985 outState.putString("apps_customize_currentTab", currentTabTag);
1986 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001987 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1988 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001989 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001990 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07001991
1992 if (mLauncherCallbacks != null) {
1993 mLauncherCallbacks.onSaveInstanceState(outState);
1994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 }
1996
1997 @Override
1998 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002000
Winson Chunge7a03942011-08-05 15:05:12 -07002001 // Remove all pending runnables
2002 mHandler.removeMessages(ADVANCE_MSG);
2003 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002004 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002005
Winson Chungcd2b0142011-06-08 16:02:26 -07002006 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002007 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002008
2009 // It's possible to receive onDestroy after a new Launcher activity has
2010 // been created. In this case, don't interfere with the new Launcher.
2011 if (mModel.isCurrentCallbacks(this)) {
2012 mModel.stopLoader();
2013 app.setLauncher(null);
2014 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002017 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002019 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002021 mAppWidgetHost = null;
2022
2023 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024
2025 TextKeyListener.getInstance().release();
2026
Winson Chung81b52252012-08-27 15:34:29 -07002027 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2028 // to prevent leaking Launcher activities on orientation change.
2029 if (mModel != null) {
2030 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2031 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002032
2033 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002034 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002035
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002036 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002037 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002038 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002039 mWorkspace = null;
2040 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002041
Michael Jurka2ecf9952012-06-18 12:52:28 -07002042 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002043
2044 if (mLauncherCallbacks != null) {
2045 mLauncherCallbacks.onDestroy();
2046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 }
2048
Adam Cohena9cf38f2011-05-02 15:36:58 -07002049 public DragController getDragController() {
2050 return mDragController;
2051 }
2052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 @Override
2054 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002055 if (requestCode >= 0) {
2056 setWaitingForResult(true);
2057 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 super.startActivityForResult(intent, requestCode);
2059 }
2060
Winson Chung70d72102011-08-12 11:24:35 -07002061 /**
2062 * Indicates that we want global search for this activity by setting the globalSearch
2063 * argument for {@link #startSearch} to true.
2064 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002066 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002068
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002069 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002070
Karl Rosaen138a0412009-04-23 19:00:21 -07002071 if (initialQuery == null) {
2072 // Use any text typed in the launcher as the initial query
2073 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 if (appSearchData == null) {
2076 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002077 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 }
Winson Chungadf0c182012-08-23 14:59:07 -07002079 Rect sourceBounds = new Rect();
2080 if (mSearchDropTargetBar != null) {
2081 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2082 }
Romain Guycbb89e42009-06-08 15:52:54 -07002083
Ian Parkinson047036e2014-09-01 15:40:53 +01002084 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002085 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002086 if (clearTextImmediately) {
2087 clearTypedText();
2088 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002089 }
2090
Ian Parkinson047036e2014-09-01 15:40:53 +01002091 /**
2092 * Start a text search.
2093 *
2094 * @return {@code true} if the search will start immediately, so any further keypresses
2095 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2096 * to buffer keypresses.
2097 */
2098 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002099 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002100 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2101 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2102 sourceBounds);
2103 }
2104
Michael Jurkaa33411c2012-06-14 16:18:21 -07002105 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002106 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002107 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002108 }
2109
2110 /**
2111 * Starts the global search activity. This code is a copied from SearchManager
2112 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002113 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002114 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002115 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002116 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2117 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2118 if (globalSearchActivity == null) {
2119 Log.w(TAG, "No global search activity found.");
2120 return;
2121 }
2122 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2123 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2124 intent.setComponent(globalSearchActivity);
2125 // Make sure that we have a Bundle to put source in
2126 if (appSearchData == null) {
2127 appSearchData = new Bundle();
2128 } else {
2129 appSearchData = new Bundle(appSearchData);
2130 }
Adam Cohen9211d422014-10-07 18:14:53 -07002131 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002132 if (!appSearchData.containsKey("source")) {
2133 appSearchData.putString("source", getPackageName());
2134 }
2135 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2136 if (!TextUtils.isEmpty(initialQuery)) {
2137 intent.putExtra(SearchManager.QUERY, initialQuery);
2138 }
2139 if (selectInitialQuery) {
2140 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2141 }
2142 intent.setSourceBounds(sourceBounds);
2143 try {
2144 startActivity(intent);
2145 } catch (ActivityNotFoundException ex) {
2146 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2147 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 }
2149
Yura4f93ec62014-02-04 14:15:21 +00002150 public boolean isOnCustomContent() {
2151 return mWorkspace.isOnOrMovingToCustomContent();
2152 }
2153
Winson Chung70d72102011-08-12 11:24:35 -07002154 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002155 public boolean onPrepareOptionsMenu(Menu menu) {
2156 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002157 if (!isOnCustomContent()) {
2158 // Close any open folders
2159 closeFolder();
2160 // Stop resizing any widgets
2161 mWorkspace.exitWidgetResizeMode();
2162 if (!mWorkspace.isInOverviewMode()) {
2163 // Show the overview mode
2164 showOverviewMode(true);
2165 } else {
2166 showWorkspace(true);
2167 }
Winson Chunge0298742014-01-17 12:03:00 -08002168 }
Adam Cohen9211d422014-10-07 18:14:53 -07002169 if (mLauncherCallbacks != null) {
2170 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2171 }
2172
Michael Jurkab94f3f82013-09-11 18:08:54 +02002173 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002176 @Override
2177 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002178 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002179 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002180 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002181 }
2182
Joe Onorato9c1289c2009-08-17 11:03:03 -04002183 public boolean isWorkspaceLocked() {
2184 return mWorkspaceLoading || mWaitingForResult;
2185 }
2186
Adam Cohen517a7f52014-03-01 12:12:59 -08002187 public boolean isWorkspaceLoading() {
2188 return mWorkspaceLoading;
2189 }
2190
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002191 private void setWorkspaceLoading(boolean value) {
2192 boolean isLocked = isWorkspaceLocked();
2193 mWorkspaceLoading = value;
2194 if (isLocked != isWorkspaceLocked()) {
2195 onWorkspaceLockedChanged();
2196 }
2197 }
2198
2199 private void setWaitingForResult(boolean value) {
2200 boolean isLocked = isWorkspaceLocked();
2201 mWaitingForResult = value;
2202 if (isLocked != isWorkspaceLocked()) {
2203 onWorkspaceLockedChanged();
2204 }
2205 }
2206
Adam Cohen9211d422014-10-07 18:14:53 -07002207 protected void onWorkspaceLockedChanged() {
2208 if (mLauncherCallbacks != null) {
2209 mLauncherCallbacks.onWorkspaceLockedChanged();
2210 }
2211 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002212
Michael Jurka0280c3b2010-09-17 15:00:07 -07002213 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002214 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002215 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002216 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2217 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002218 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002219 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002220 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002221
Sunny Goyale6b63a32015-01-16 16:14:29 -08002222 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002223 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002224 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2225 }
2226
Sunny Goyale6b63a32015-01-16 16:14:29 -08002227 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002228 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2229 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002230 if (appWidgetInfo.configure != null) {
2231 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002232 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002233
Bjorn Bringert7984c942009-12-09 15:38:25 +00002234 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002235 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2236 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002239 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002240 Runnable onComplete = new Runnable() {
2241 @Override
2242 public void run() {
2243 // Exit spring loaded mode if necessary after adding the widget
2244 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2245 null);
2246 }
2247 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002249 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002250 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 }
2252 }
Romain Guycbb89e42009-06-08 15:52:54 -07002253
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002254 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002255 // Close any folders that may be open.
2256 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002257 mWorkspace.moveToCustomContentScreen(animate);
2258 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002259
2260 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2261 int[] cell, int spanX, int spanY) {
2262 switch (info.itemType) {
2263 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2264 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2265 int span[] = new int[2];
2266 span[0] = spanX;
2267 span[1] = spanY;
2268 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2269 container, screenId, cell, span);
2270 break;
2271 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2272 processShortcutFromDrop(info.componentName, container, screenId, cell);
2273 break;
2274 default:
2275 throw new IllegalStateException("Unknown item type: " + info.itemType);
2276 }
2277 }
2278
Adam Cohenfbba09b2011-07-18 21:43:05 -07002279 /**
2280 * Process a shortcut drop.
2281 *
2282 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002283 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002285 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002286 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2287 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002288 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002289 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002290 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002291 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002292
2293 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002294 mPendingAddInfo.cellX = cell[0];
2295 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002296 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002297
2298 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2299 createShortcutIntent.setComponent(componentName);
2300 processShortcut(createShortcutIntent);
2301 }
2302
Adam Cohenfbba09b2011-07-18 21:43:05 -07002303 /**
2304 * Process a widget drop.
2305 *
2306 * @param info The PendingAppWidgetInfo of the widget being added.
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 addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2311 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002312 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002313 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002314 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002315 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002316 mPendingAddInfo.minSpanX = info.minSpanX;
2317 mPendingAddInfo.minSpanY = info.minSpanY;
2318
Adam Cohenfbba09b2011-07-18 21:43:05 -07002319 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002320 mPendingAddInfo.cellX = cell[0];
2321 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002322 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002323 if (span != null) {
2324 mPendingAddInfo.spanX = span[0];
2325 mPendingAddInfo.spanY = span[1];
2326 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327
Adam Cohened66b2b2012-01-23 17:28:51 -08002328 AppWidgetHostView hostView = info.boundWidget;
2329 int appWidgetId;
2330 if (hostView != null) {
2331 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002332 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002333 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002334 // In this case, we either need to start an activity to get permission to bind
2335 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002336 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002337 Bundle options = info.bindOptions;
2338
Sunny Goyalffe83f12014-08-14 17:39:34 -07002339 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2340 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002341 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002342 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002343 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002344 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002345 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2346 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2347 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002348 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2349 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002350 // TODO: we need to make sure that this accounts for the options bundle.
2351 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002352 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2353 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002354 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002355 }
2356
Joe Onoratodeb98af2010-02-19 14:59:39 -08002357 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002358 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002359 }
2360
Winson Chung24ab2f12010-09-16 14:10:47 -07002361 void processWallpaper(Intent intent) {
2362 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2363 }
2364
Adam Cohendcd297f2013-06-18 13:13:40 -07002365 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002366 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002367 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368 folderInfo.title = getText(R.string.folder_name);
2369
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002371 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002372 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002373 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374
2375 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002376 FolderIcon newFolder =
2377 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002378 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002379 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002380 // Force measure the new folder icon
2381 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2382 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002383 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 }
Romain Guycbb89e42009-06-08 15:52:54 -07002385
Joe Onorato9c1289c2009-08-17 11:03:03 -04002386 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002387 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002388 }
2389
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002390 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002391 if (mLauncherCallbacks != null) {
2392 return mLauncherCallbacks.getWallpaperPickerComponent();
2393 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002394 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002395 }
2396
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002397 /**
2398 * Registers various content observers. The current implementation registers
2399 * only a favorites observer to keep track of the favorites applications.
2400 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002401 private void registerContentObservers() {
2402 ContentResolver resolver = getContentResolver();
2403 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2404 true, mWidgetObserver);
2405 }
2406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 @Override
2408 public boolean dispatchKeyEvent(KeyEvent event) {
2409 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2410 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002412 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002413 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002414 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002415 dumpState();
2416 return true;
2417 }
2418 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002419 }
2420 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2421 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002422 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 return true;
2424 }
2425 }
2426
2427 return super.dispatchKeyEvent(event);
2428 }
2429
Joe Onorato88ec0992009-11-19 13:16:06 -08002430 @Override
2431 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002432 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2433 return;
2434 }
2435
Winson Chungc93e5ae2012-07-23 20:48:26 -07002436 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002437 if (mAppsCustomizeContent.getContentType() ==
2438 AppsCustomizePagedView.ContentType.Applications) {
2439 showWorkspace(true);
2440 } else {
2441 showOverviewMode(true);
2442 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002443 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002444 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002445 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002446 Folder openFolder = mWorkspace.getOpenFolder();
2447 if (openFolder.isEditingName()) {
2448 openFolder.dismissEditingName();
2449 } else {
2450 closeFolder();
2451 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002452 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002453 mWorkspace.exitWidgetResizeMode();
2454
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002455 // Back button is a no-op here, but give at least some feedback for the button press
2456 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002457 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002458 }
2459
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002460 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002461 * Re-listen when widgets are reset.
2462 */
2463 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002464 if (mAppWidgetHost != null) {
2465 mAppWidgetHost.startListening();
2466 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002467 }
2468
2469 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 * Launches the intent referred by the clicked shortcut.
2471 *
2472 * @param v The view representing the clicked shortcut.
2473 */
2474 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002475 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2476 // view has detached (it's possible for this to happen if the view is removed mid touch).
2477 if (v.getWindowToken() == null) {
2478 return;
2479 }
2480
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002481 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002482 return;
2483 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002484
Adam Cohen1697b792013-09-17 19:08:21 -07002485 if (v instanceof Workspace) {
2486 if (mWorkspace.isInOverviewMode()) {
2487 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002488 }
2489 return;
2490 }
2491
2492 if (v instanceof CellLayout) {
2493 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002494 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002495 }
2496 }
2497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002499 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002500 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002501 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002502 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002503 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002504 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002505 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002506 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002507 } else if (tag instanceof AppInfo) {
2508 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002509 } else if (tag instanceof LauncherAppWidgetInfo) {
2510 if (v instanceof PendingAppWidgetHostView) {
2511 onClickPendingWidget((PendingAppWidgetHostView) v);
2512 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 }
2514 }
2515
Sunny Goyal508da152014-08-14 10:53:27 -07002516 public void onClickPagedViewIcon(View v) {
2517 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002518 if (mLauncherCallbacks != null) {
2519 mLauncherCallbacks.onClickPagedViewIcon(v);
2520 }
Sunny Goyal508da152014-08-14 10:53:27 -07002521 }
2522
Michael Jurka0e260592010-06-30 17:07:39 -07002523 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002524 return false;
2525 }
2526
Michael Jurkaaf442092010-06-10 17:01:57 -07002527 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002528 * Event handler for the app widget view which has not fully restored.
2529 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002530 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002531 if (mIsSafeModeEnabled) {
2532 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2533 return;
2534 }
2535
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002536 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002537 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002538 int widgetId = info.appWidgetId;
2539 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2540 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002541 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2542 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002543 mPendingAddInfo.copyFrom(info);
2544 mPendingAddWidgetId = widgetId;
2545
Sunny Goyalffe83f12014-08-14 17:39:34 -07002546 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2547 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002548 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002549 } else if (info.installProgress < 0) {
2550 // The install has not been queued
2551 final String packageName = info.providerName.getPackageName();
2552 showBrokenAppInstallDialog(packageName,
2553 new DialogInterface.OnClickListener() {
2554 public void onClick(DialogInterface dialog, int id) {
2555 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2556 }
2557 });
2558 } else {
2559 // Download has started.
2560 final String packageName = info.providerName.getPackageName();
2561 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002562 }
2563 }
2564
2565 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002566 * Event handler for the "grid" button that appears on the home screen, which
2567 * enters all apps mode.
2568 *
2569 * @param v The view that was clicked.
2570 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002571 protected void onClickAllAppsButton(View v) {
2572 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2573 if (isAllAppsVisible()) {
2574 showWorkspace(true);
2575 } else {
2576 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2577 }
Adam Cohen9211d422014-10-07 18:14:53 -07002578 if (mLauncherCallbacks != null) {
2579 mLauncherCallbacks.onClickAllAppsButton(v);
2580 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002581 }
2582
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002583 private void showBrokenAppInstallDialog(final String packageName,
2584 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002585 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002586 .setTitle(R.string.abandoned_promises_title)
2587 .setMessage(R.string.abandoned_promise_explanation)
2588 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2589 .setNeutralButton(R.string.abandoned_clean_this,
2590 new DialogInterface.OnClickListener() {
2591 public void onClick(DialogInterface dialog, int id) {
2592 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2593 mWorkspace.removeAbandonedPromise(packageName, user);
2594 }
2595 })
2596 .create().show();
2597 return;
2598 }
2599
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002600 /**
2601 * Event handler for an app shortcut click.
2602 *
2603 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2604 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002605 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002606 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2607 Object tag = v.getTag();
2608 if (!(tag instanceof ShortcutInfo)) {
2609 throw new IllegalArgumentException("Input must be a Shortcut");
2610 }
2611
2612 // Open shortcut
2613 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002614
2615 if (shortcut.isDisabled != 0) {
2616 int error = R.string.activity_not_available;
2617 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2618 error = R.string.safemode_shortcut_error;
2619 }
2620 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2621 return;
2622 }
2623
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624 final Intent intent = shortcut.intent;
2625
2626 // Check for special shortcuts
2627 if (intent.getComponent() != null) {
2628 final String shortcutClass = intent.getComponent().getClassName();
2629
2630 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2631 MemoryDumpActivity.startDump(this);
2632 return;
2633 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2634 toggleShowWeightWatcher();
2635 return;
2636 }
2637 }
2638
Chris Wren40c5ed32014-06-24 18:24:23 -04002639 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002640 if ((v instanceof BubbleTextView)
2641 && shortcut.isPromise()
2642 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002643 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002644 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002645 new DialogInterface.OnClickListener() {
2646 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002647 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002648 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002650 return;
2651 }
2652
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002653 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002654 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002655
2656 if (mLauncherCallbacks != null) {
2657 mLauncherCallbacks.onClickAppShortcut(v);
2658 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002659 }
2660
Sunny Goyal508da152014-08-14 10:53:27 -07002661 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002662 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002663 final ShortcutInfo shortcut;
2664 final Intent intent;
2665 if (tag instanceof ShortcutInfo) {
2666 shortcut = (ShortcutInfo) tag;
2667 intent = shortcut.intent;
2668 int[] pos = new int[2];
2669 v.getLocationOnScreen(pos);
2670 intent.setSourceBounds(new Rect(pos[0], pos[1],
2671 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002672
Sunny Goyal508da152014-08-14 10:53:27 -07002673 } else if (tag instanceof AppInfo) {
2674 shortcut = null;
2675 intent = ((AppInfo) tag).intent;
2676 } else {
2677 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2678 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002679
2680 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002681 mStats.recordLaunch(intent, shortcut);
2682
2683 if (success && v instanceof BubbleTextView) {
2684 mWaitingForResume = (BubbleTextView) v;
2685 mWaitingForResume.setStayPressed(true);
2686 }
2687 }
2688
2689 /**
2690 * Event handler for a folder icon click.
2691 *
2692 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2693 */
2694 protected void onClickFolderIcon(View v) {
2695 if (LOGD) Log.d(TAG, "onClickFolder");
2696 if (!(v instanceof FolderIcon)){
2697 throw new IllegalArgumentException("Input must be a FolderIcon");
2698 }
2699
2700 FolderIcon folderIcon = (FolderIcon) v;
2701 final FolderInfo info = folderIcon.getFolderInfo();
2702 Folder openFolder = mWorkspace.getFolderForTag(info);
2703
2704 // If the folder info reports that the associated folder is open, then verify that
2705 // it is actually opened. There have been a few instances where this gets out of sync.
2706 if (info.opened && openFolder == null) {
2707 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2708 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2709 info.opened = false;
2710 }
2711
2712 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2713 // Close any open folder
2714 closeFolder();
2715 // Open the requested folder
2716 openFolder(folderIcon);
2717 } else {
2718 // Find the open folder...
2719 int folderScreen;
2720 if (openFolder != null) {
2721 folderScreen = mWorkspace.getPageForView(openFolder);
2722 // .. and close it
2723 closeFolder(openFolder);
2724 if (folderScreen != mWorkspace.getCurrentPage()) {
2725 // Close any folder open on the current screen
2726 closeFolder();
2727 // Pull the folder onto this screen
2728 openFolder(folderIcon);
2729 }
2730 }
2731 }
Adam Cohen9211d422014-10-07 18:14:53 -07002732
2733 if (mLauncherCallbacks != null) {
2734 mLauncherCallbacks.onClickFolderIcon(v);
2735 }
Michael Jurka5130e402011-10-13 04:55:35 -07002736 }
2737
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002738 /**
2739 * Event handler for the (Add) Widgets button that appears after a long press
2740 * on the home screen.
2741 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002742 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002743 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002744 if (mIsSafeModeEnabled) {
2745 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2746 } else {
2747 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2748 if (mLauncherCallbacks != null) {
2749 mLauncherCallbacks.onClickAddWidgetButton(view);
2750 }
Adam Cohen9211d422014-10-07 18:14:53 -07002751 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002752 }
2753
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002754 /**
2755 * Event handler for the wallpaper picker button that appears after a long press
2756 * on the home screen.
2757 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002758 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002759 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2760 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2761 pickWallpaper.setComponent(getWallpaperPickerComponent());
2762 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002763
2764 if (mLauncherCallbacks != null) {
2765 mLauncherCallbacks.onClickWallpaperPicker(v);
2766 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002767 }
2768
2769 /**
2770 * Event handler for a click on the settings button that appears after a long press
2771 * on the home screen.
2772 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002773 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002774 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002775 if (mLauncherCallbacks != null) {
2776 mLauncherCallbacks.onClickSettingsButton(v);
2777 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002778 }
2779
Michael Jurka5130e402011-10-13 04:55:35 -07002780 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002781 // Provide the same haptic feedback that the system offers for virtual keys.
2782 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002783 }
2784
Adam Cohen61f560d2013-09-30 15:58:20 -07002785 public void performHapticFeedbackOnTouchDown(View v) {
2786 // Provide the same haptic feedback that the system offers for virtual keys.
2787 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2788 }
2789
2790 public View.OnTouchListener getHapticFeedbackTouchListener() {
2791 if (mHapticFeedbackTouchListener == null) {
2792 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2793 @Override
2794 public boolean onTouch(View v, MotionEvent event) {
2795 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2796 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2797 }
2798 return false;
2799 }
2800 };
2801 }
2802 return mHapticFeedbackTouchListener;
2803 }
2804
Adam Cohen9211d422014-10-07 18:14:53 -07002805 public void onDragStarted(View view) {
2806 if (isOnCustomContent()) {
2807 // Custom content screen doesn't participate in drag and drop. If on custom
2808 // content screen, move to default.
2809 moveWorkspaceToDefaultScreen();
2810 }
2811
2812 if (mLauncherCallbacks != null) {
2813 mLauncherCallbacks.onDragStarted(view);
2814 }
2815 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002816
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002817 /**
2818 * Called when the user stops interacting with the launcher.
2819 * This implies that the user is now on the homescreen and is not doing housekeeping.
2820 */
Adam Cohen9211d422014-10-07 18:14:53 -07002821 protected void onInteractionEnd() {
2822 if (mLauncherCallbacks != null) {
2823 mLauncherCallbacks.onInteractionEnd();
2824 }
2825 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002826
2827 /**
2828 * Called when the user starts interacting with the launcher.
2829 * The possible interactions are:
2830 * - open all apps
2831 * - reorder an app shortcut, or a widget
2832 * - open the overview mode.
2833 * This is a good time to stop doing things that only make sense
2834 * when the user is on the homescreen and not doing housekeeping.
2835 */
Adam Cohen9211d422014-10-07 18:14:53 -07002836 protected void onInteractionBegin() {
2837 if (mLauncherCallbacks != null) {
2838 mLauncherCallbacks.onInteractionBegin();
2839 }
2840 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002841
Kenny Guyf07af7b2014-07-31 11:39:16 +01002842 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002843 try {
2844 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002845 launcherApps.showAppDetailsForProfile(componentName, user);
2846 } catch (SecurityException e) {
2847 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2848 Log.e(TAG, "Launcher does not have permission to launch settings");
2849 } catch (ActivityNotFoundException e) {
2850 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2851 Log.e(TAG, "Unable to launch settings");
2852 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002853 }
2854
Michael Jurka1e2f4652013-07-08 18:03:46 -07002855 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002856 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2857 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002858 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002859 // System applications cannot be installed. For now, show a toast explaining that.
2860 // We may give them the option of disabling apps this way.
2861 int messageId = R.string.uninstall_system_app_text;
2862 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002863 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002864 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002865 String packageName = componentName.getPackageName();
2866 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002867 Intent intent = new Intent(
2868 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002869 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2870 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002871 if (user != null) {
2872 user.addToIntent(intent, Intent.EXTRA_USER);
2873 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002874 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002875 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002876 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002877 }
2878
Michael Jurka86a720e2012-05-09 11:23:23 -07002879 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002880 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002881 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002882 // Only launch using the new animation if the shortcut has not opted out (this is a
2883 // private contract between launcher and may be ignored in the future).
2884 boolean useLaunchAnimation = (v != null) &&
2885 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002886 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2887 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002888
Kenny Guy1317e2d2014-05-08 18:52:50 +01002889 UserHandleCompat user = null;
2890 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2891 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2892 user = userManager.getUserForSerialNumber(serialNumber);
2893 }
2894
2895 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002896 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002897 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002898 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2899 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002900 optsBundle = opts.toBundle();
2901 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002902
2903 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2904 // Could be launching some bookkeeping activity
2905 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002906 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002907 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002908 launcherApps.startActivityForProfile(intent.getComponent(), user,
2909 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002910 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002911 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002912 } catch (SecurityException e) {
2913 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002914 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002915 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002916 "or use the exported attribute for this activity. "
2917 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002918 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002919 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002921
Michael Jurka86a720e2012-05-09 11:23:23 -07002922 boolean startActivitySafely(View v, Intent intent, Object tag) {
2923 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002924 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2925 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2926 return false;
2927 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002928 try {
2929 success = startActivity(v, intent, tag);
2930 } catch (ActivityNotFoundException e) {
2931 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2932 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2933 }
2934 return success;
2935 }
2936
Adam Cohen268c4752012-06-06 17:47:33 -07002937 /**
2938 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2939 * in the DragLayer in the exact absolute location of the original FolderIcon.
2940 */
2941 private void copyFolderIconToImage(FolderIcon fi) {
2942 final int width = fi.getMeasuredWidth();
2943 final int height = fi.getMeasuredHeight();
2944
2945 // Lazy load ImageView, Bitmap and Canvas
2946 if (mFolderIconImageView == null) {
2947 mFolderIconImageView = new ImageView(this);
2948 }
2949 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2950 mFolderIconBitmap.getHeight() != height) {
2951 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2952 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2953 }
2954
2955 DragLayer.LayoutParams lp;
2956 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2957 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2958 } else {
2959 lp = new DragLayer.LayoutParams(width, height);
2960 }
2961
Adam Cohen307fe232012-08-16 17:55:58 -07002962 // The layout from which the folder is being opened may be scaled, adjust the starting
2963 // view size by this scale factor.
2964 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002965 lp.customPosition = true;
2966 lp.x = mRectForFolderAnimation.left;
2967 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002968 lp.width = (int) (scale * width);
2969 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002970
2971 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2972 fi.draw(mFolderIconCanvas);
2973 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002974 if (fi.getFolder() != null) {
2975 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2976 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002977 }
Adam Cohen268c4752012-06-06 17:47:33 -07002978 // Just in case this image view is still in the drag layer from a previous animation,
2979 // we remove it and re-add it.
2980 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2981 mDragLayer.removeView(mFolderIconImageView);
2982 }
2983 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002984 if (fi.getFolder() != null) {
2985 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002986 }
Adam Cohen268c4752012-06-06 17:47:33 -07002987 }
2988
Adam Cohen2801caf2011-05-13 20:57:39 -07002989 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002990 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002991 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2992 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2993 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2994
Adam Cohenc51934b2011-07-26 21:07:43 -07002995 FolderInfo info = (FolderInfo) fi.getTag();
2996 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2997 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002998 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2999 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003000 }
3001
Adam Cohen268c4752012-06-06 17:47:33 -07003002 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3003 copyFolderIconToImage(fi);
3004 fi.setVisibility(View.INVISIBLE);
3005
Michael Jurka2ecf9952012-06-18 12:52:28 -07003006 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003007 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003008 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003009 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3010 }
3011 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003012 oa.start();
3013 }
3014
Adam Cohen268c4752012-06-06 17:47:33 -07003015 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003016 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003017 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3018 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3019 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3020
Adam Cohen268c4752012-06-06 17:47:33 -07003021 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003022
Adam Cohen268c4752012-06-06 17:47:33 -07003023 // We remove and re-draw the FolderIcon in-case it has changed
3024 mDragLayer.removeView(mFolderIconImageView);
3025 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003026 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003027 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003028 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003029 oa.addListener(new AnimatorListenerAdapter() {
3030 @Override
3031 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003032 if (cl != null) {
3033 cl.clearFolderLeaveBehind();
3034 // Remove the ImageView copy of the FolderIcon and make the original visible.
3035 mDragLayer.removeView(mFolderIconImageView);
3036 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003037 }
3038 }
3039 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003040 oa.start();
3041 }
3042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003043 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003044 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003045 * is animated relative to the specified View. If the View is null, no animation
3046 * is played.
3047 *
3048 * @param folderInfo The FolderInfo describing the folder to open.
3049 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003050 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003051 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003052 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003053
Adam Cohena9cf38f2011-05-02 15:36:58 -07003054 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003055
Adam Cohen4554ee12011-08-03 16:13:21 -07003056 // Just verify that the folder hasn't already been added to the DragLayer.
3057 // There was a one-off crash where the folder had a parent already.
3058 if (folder.getParent() == null) {
3059 mDragLayer.addView(folder);
3060 mDragController.addDropTarget((DropTarget) folder);
3061 } else {
3062 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3063 folder.getParent() + ").");
3064 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003065 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003066 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003067
3068 // Notify the accessibility manager that this folder "window" has appeared and occluded
3069 // the workspace items
3070 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3071 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003072 }
3073
3074 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003075 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003076 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003077 if (folder.isEditingName()) {
3078 folder.dismissEditingName();
3079 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003080 closeFolder(folder);
3081 }
3082 }
3083
3084 void closeFolder(Folder folder) {
3085 folder.getInfo().opened = false;
3086
3087 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3088 if (parent != null) {
3089 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3090 shrinkAndFadeInFolderIcon(fi);
3091 }
3092 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003093
3094 // Notify the accessibility manager that this folder "window" has disappeard and no
3095 // longer occludeds the workspace items
3096 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003097 }
3098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003100 if (!isDraggingEnabled()) return false;
3101 if (isWorkspaceLocked()) return false;
3102 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003103
Adam Cohen1697b792013-09-17 19:08:21 -07003104 if (v instanceof Workspace) {
3105 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003106 if (mWorkspace.enterOverviewMode()) {
3107 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3108 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3109 return true;
3110 } else {
3111 return false;
3112 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003113 } else {
3114 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003115 }
Adam Cohen1697b792013-09-17 19:08:21 -07003116 }
3117
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003118 CellLayout.CellInfo longClickCellInfo = null;
3119 View itemUnderLongClick = null;
3120 if (v.getTag() instanceof ItemInfo) {
3121 ItemInfo info = (ItemInfo) v.getTag();
3122 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3123 itemUnderLongClick = longClickCellInfo.cell;
3124 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003125 }
3126
Winson Chung3d503fb2011-07-13 17:25:49 -07003127 // The hotseat touch handling does not go through Workspace, and we always allow long press
3128 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003129 final boolean inHotseat = isHotseatLayout(v);
3130 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003131 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003132 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003133 // User long pressed on empty space
3134 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3135 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003136 if (mWorkspace.isInOverviewMode()) {
3137 mWorkspace.startReordering(v);
3138 } else {
3139 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003141 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003142 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3143 mHotseat.getOrderInHotseat(
3144 longClickCellInfo.cellX,
3145 longClickCellInfo.cellY));
3146 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003148 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003149 }
3150 }
3151 }
3152 return true;
3153 }
3154
Winson Chung3d503fb2011-07-13 17:25:49 -07003155 boolean isHotseatLayout(View layout) {
3156 return mHotseat != null && layout != null &&
3157 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3158 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003159
Winson Chung3d503fb2011-07-13 17:25:49 -07003160 /**
3161 * Returns the CellLayout of the specified container at the specified screen.
3162 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003163 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003164 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3165 if (mHotseat != null) {
3166 return mHotseat.getLayout();
3167 } else {
3168 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003169 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003170 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003171 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003172 }
3173 }
3174
Daniel Sandler843e8602010-06-07 14:59:01 -04003175 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003176 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003177 }
3178
Craig Mautner360310b2012-10-26 15:13:08 -07003179 private void setWorkspaceBackground(boolean workspace) {
3180 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003181 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003182 }
3183
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003184 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003185 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3186 int curflags = getWindow().getAttributes().flags
3187 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3188 if (wpflags != curflags) {
3189 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3190 }
Craig Mautner360310b2012-10-26 15:13:08 -07003191 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003192 }
3193
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003194 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3195 if (v instanceof LauncherTransitionable) {
3196 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3197 }
3198 }
3199
Michael Jurkabed61d22012-02-14 22:51:29 -08003200 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3201 if (v instanceof LauncherTransitionable) {
3202 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3203 }
Winson Chung70442722012-02-10 15:43:22 -08003204
3205 // Update the workspace transition step as well
3206 dispatchOnLauncherTransitionStep(v, 0f);
3207 }
3208
3209 private void dispatchOnLauncherTransitionStep(View v, float t) {
3210 if (v instanceof LauncherTransitionable) {
3211 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3212 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003213 }
3214
3215 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3216 if (v instanceof LauncherTransitionable) {
3217 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3218 }
Winson Chung70442722012-02-10 15:43:22 -08003219
3220 // Update the workspace transition step as well
3221 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003222 }
3223
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003224 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003225 * Things to test when changing the following seven functions.
3226 * - Home from workspace
3227 * - from center screen
3228 * - from other screens
3229 * - Home from all apps
3230 * - from center screen
3231 * - from other screens
3232 * - Back from all apps
3233 * - from center screen
3234 * - from other screens
3235 * - Launch app from workspace and quit
3236 * - with back
3237 * - with home
3238 * - Launch app from all apps and quit
3239 * - with back
3240 * - with home
3241 * - Go to a screen that's not the default, then all
3242 * apps, and launch and app, and go back
3243 * - with back
3244 * -with home
3245 * - On workspace, long press power and go back
3246 * - with back
3247 * - with home
3248 * - On all apps, long press power and go back
3249 * - with back
3250 * - with home
3251 * - On workspace, power off
3252 * - On all apps, power off
3253 * - Launch an app and turn off the screen while in that app
3254 * - Go back with home key
3255 * - Go back with back key TODO: make this not go to workspace
3256 * - From all apps
3257 * - From workspace
3258 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3259 * - From all apps
3260 * - From the center workspace
3261 * - From another workspace
3262 */
3263
3264 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003265 * Zoom the camera out from the workspace to reveal 'toView'.
3266 * Assumes that the view to show is anchored at either the very top or very bottom
3267 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003268 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003269 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003270 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3271 showAppsCustomizeHelper(animated, springLoaded, contentType);
3272 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003273
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003274 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chungc58497e2013-09-03 17:48:37 -07003275 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3276 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003277 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003278 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003279 mStateAnimation.cancel();
3280 mStateAnimation = null;
3281 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003282
Kenny Guyd794a3f2014-09-16 15:17:58 +01003283 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003284
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003285 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003286
Adam Cohen6c5891a2014-07-09 23:53:15 -07003287 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003288 final int itemsAlphaStagger =
3289 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003290
Michael Jurkabed61d22012-02-14 22:51:29 -08003291 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003292 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003293
Adam Cohen2854d252014-08-27 16:04:07 -07003294 final ArrayList<View> layerViews = new ArrayList<View>();
3295
Adam Cohen6c5891a2014-07-09 23:53:15 -07003296 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3297 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003298 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003299 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003300 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003301 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3302 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003303 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3304 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003305
Adam Cohena38dc902014-09-07 17:48:55 +02003306 // If for some reason our views aren't initialized, don't animate
3307 boolean initialized = getAllAppsButton() != null;
3308
3309 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003310 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003311 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3312 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003313
Adam Cohen9bfdb762014-07-21 17:44:06 -07003314 final View page = content.getPageAt(content.getCurrentPage());
3315 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003316
Adam Cohen63f1ec02014-08-12 09:23:13 -07003317 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3318 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003319 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3320 } else {
3321 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3322 }
3323
Adam Cohen9bfdb762014-07-21 17:44:06 -07003324 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003325 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003326 revealView.setVisibility(View.VISIBLE);
3327 // We need to hide this view as the animation start will be posted.
3328 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003329
Adam Cohen9bfdb762014-07-21 17:44:06 -07003330 int width = revealView.getMeasuredWidth();
3331 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003332 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003333
Adam Cohen63f1ec02014-08-12 09:23:13 -07003334 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003335 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003336
Adam Cohen63f1ec02014-08-12 09:23:13 -07003337 // Get the y delta between the center of the page and the center of the all apps button
3338 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3339 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003340
Adam Cohen2854d252014-08-27 16:04:07 -07003341 float alpha = 0;
3342 float xDrift = 0;
3343 float yDrift = 0;
3344 if (material) {
3345 alpha = isWidgetTray ? 0.3f : 1f;
3346 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3347 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3348 } else {
3349 yDrift = 2 * height / 3;
3350 xDrift = 0;
3351 }
3352 final float initAlpha = alpha;
3353
Adam Cohen9bfdb762014-07-21 17:44:06 -07003354 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003355 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003356 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003357 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003358 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003359 PropertyValuesHolder panelDriftX =
3360 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003361
Adam Cohen2854d252014-08-27 16:04:07 -07003362 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3363 panelAlpha, panelDriftY, panelDriftX);
3364
Adam Cohen9bfdb762014-07-21 17:44:06 -07003365 panelAlphaAndDrift.setDuration(revealDuration);
3366 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003367
Adam Cohen9bfdb762014-07-21 17:44:06 -07003368 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003369
Adam Cohen4e243a22014-08-10 18:30:55 -07003370 if (page != null) {
3371 page.setVisibility(View.VISIBLE);
3372 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003373 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003374
Adam Cohen2854d252014-08-27 16:04:07 -07003375 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3376 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003377 pageDrift.setDuration(revealDuration);
3378 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003379 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003380 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003381
Adam Cohen63f1ec02014-08-12 09:23:13 -07003382 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003383 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003384 itemsAlpha.setDuration(revealDuration);
3385 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3386 itemsAlpha.setStartDelay(itemsAlphaStagger);
3387 mStateAnimation.play(itemsAlpha);
3388 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003389
Adam Cohen4e243a22014-08-10 18:30:55 -07003390 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3391 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003392 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003393 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003394 indicatorsAlpha.setDuration(revealDuration);
3395 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003396
Adam Cohen9bfdb762014-07-21 17:44:06 -07003397 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003398 final View allApps = getAllAppsButton();
3399 int allAppsButtonSize = LauncherAppState.getInstance().
3400 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3401 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003402 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003403 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003404 reveal.setDuration(revealDuration);
3405 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003406
3407 reveal.addListener(new AnimatorListenerAdapter() {
3408 public void onAnimationStart(Animator animation) {
3409 if (!isWidgetTray) {
3410 allApps.setVisibility(View.INVISIBLE);
3411 }
3412 }
3413 public void onAnimationEnd(Animator animation) {
3414 if (!isWidgetTray) {
3415 allApps.setVisibility(View.VISIBLE);
3416 }
3417 }
3418 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003419 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003420 }
Michael Jurka1899a362011-11-03 13:50:45 -07003421
Adam Cohen9bfdb762014-07-21 17:44:06 -07003422 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3423 @Override
3424 public void onAnimationEnd(Animator animation) {
3425 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3426 dispatchOnLauncherTransitionEnd(toView, animated, false);
3427
3428 revealView.setVisibility(View.INVISIBLE);
3429 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003430 if (page != null) {
3431 page.setLayerType(View.LAYER_TYPE_NONE, null);
3432 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003433 content.setPageBackgroundsVisible(true);
3434
3435 // Hide the search bar
3436 if (mSearchDropTargetBar != null) {
3437 mSearchDropTargetBar.hideSearchBar(false);
3438 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003439
3440 // This can hold unnecessary references to views.
3441 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003442 }
3443
Adam Cohen9bfdb762014-07-21 17:44:06 -07003444 });
3445
Adam Cohen6c5891a2014-07-09 23:53:15 -07003446 if (workspaceAnim != null) {
3447 mStateAnimation.play(workspaceAnim);
3448 }
Adam Cohen2854d252014-08-27 16:04:07 -07003449
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003450 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3451 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003452 final AnimatorSet stateAnimation = mStateAnimation;
3453 final Runnable startAnimRunnable = new Runnable() {
3454 public void run() {
3455 // Check that mStateAnimation hasn't changed while
3456 // we waited for a layout/draw pass
3457 if (mStateAnimation != stateAnimation)
3458 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003459 dispatchOnLauncherTransitionStart(fromView, animated, false);
3460 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003461
3462 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003463 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003464 for (int i = 0; i < layerViews.size(); i++) {
3465 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003466 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003467 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003468 }
Adam Cohen2854d252014-08-27 16:04:07 -07003469 }
3470 }
3471 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003472 }
3473 };
Adam Cohen2854d252014-08-27 16:04:07 -07003474 toView.bringToFront();
3475 toView.setVisibility(View.VISIBLE);
3476 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003477 } else {
3478 toView.setTranslationX(0.0f);
3479 toView.setTranslationY(0.0f);
3480 toView.setScaleX(1.0f);
3481 toView.setScaleY(1.0f);
3482 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003483 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003484
Daniel Sandlere4f98912013-06-25 15:13:26 -04003485 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003486 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003487 if (mSearchDropTargetBar != null) {
3488 mSearchDropTargetBar.hideSearchBar(false);
3489 }
Michael Jurkaabded662011-03-04 12:06:57 -08003490 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003491 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003492 dispatchOnLauncherTransitionStart(fromView, animated, false);
3493 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003494 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003495 dispatchOnLauncherTransitionStart(toView, animated, false);
3496 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003497 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003498 }
3499
3500 /**
3501 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003502 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003503 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003504 */
Adam Cohened307df2013-10-02 09:37:31 -07003505 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003506 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003507
Michael Jurkab3e22d92011-10-31 15:58:33 -07003508 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003509 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003510 mStateAnimation.cancel();
3511 mStateAnimation = null;
3512 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003513
Kenny Guyd794a3f2014-09-16 15:17:58 +01003514 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003515 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003516
Adam Cohen63f1ec02014-08-12 09:23:13 -07003517 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003518 final int itemsAlphaStagger =
3519 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003520
Winson Chungf0ea4d32011-06-06 14:27:16 -07003521 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003522 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003523 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003524 final ArrayList<View> layerViews = new ArrayList<View>();
3525
Adam Cohened307df2013-10-02 09:37:31 -07003526 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003527 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003528 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003529 } else if (toState == Workspace.State.SPRING_LOADED ||
3530 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003531 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003532 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003533 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003534
Adam Cohena38dc902014-09-07 17:48:55 +02003535 // If for some reason our views aren't initialized, don't animate
3536 boolean initialized = getAllAppsButton() != null;
3537
3538 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003539 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003540 if (workspaceAnim != null) {
3541 mStateAnimation.play(workspaceAnim);
3542 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003543
Adam Cohen9bfdb762014-07-21 17:44:06 -07003544 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3545 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003546
Adam Cohen9bfdb762014-07-21 17:44:06 -07003547 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003548
3549 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3550 int count = content.getChildCount();
3551 for (int i = 0; i < count; i++) {
3552 View child = content.getChildAt(i);
3553 if (child != page) {
3554 child.setVisibility(View.INVISIBLE);
3555 }
3556 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003557 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003558
Adam Cohen2854d252014-08-27 16:04:07 -07003559 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3560 // don't perform all these no-op animations. In particularly, this was causing
3561 // the all-apps button to pop in and out.
3562 if (fromView.getVisibility() == View.VISIBLE) {
3563 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3564 final boolean isWidgetTray =
3565 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003566
Adam Cohen2854d252014-08-27 16:04:07 -07003567 if (isWidgetTray) {
3568 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3569 } else {
3570 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003571 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003572
Adam Cohen2854d252014-08-27 16:04:07 -07003573 int width = revealView.getMeasuredWidth();
3574 int height = revealView.getMeasuredHeight();
3575 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3576
3577 // Hide the real page background, and swap in the fake one
3578 revealView.setVisibility(View.VISIBLE);
3579 content.setPageBackgroundsVisible(false);
3580
3581 final View allAppsButton = getAllAppsButton();
3582 revealView.setTranslationY(0);
3583 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3584 allAppsButton, null);
3585
3586 float xDrift = 0;
3587 float yDrift = 0;
3588 if (material) {
3589 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3590 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3591 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003592 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003593 xDrift = 0;
3594 }
3595
3596 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3597 TimeInterpolator decelerateInterpolator = material ?
3598 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003599 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003600
3601 // The vertical motion of the apps panel should be delayed by one frame
3602 // from the conceal animation in order to give the right feel. We correpsondingly
3603 // shorten the duration so that the slide and conceal end at the same time.
3604 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3605 0, yDrift);
3606 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3607 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3608 panelDriftY.setInterpolator(decelerateInterpolator);
3609 mStateAnimation.play(panelDriftY);
3610
3611 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3612 0, xDrift);
3613 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3614 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3615 panelDriftX.setInterpolator(decelerateInterpolator);
3616 mStateAnimation.play(panelDriftX);
3617
3618 if (isWidgetTray || !material) {
3619 float finalAlpha = material ? 0.4f : 0f;
3620 revealView.setAlpha(1f);
3621 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3622 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003623 panelAlpha.setDuration(material ? revealDuration : 150);
3624 panelAlpha.setInterpolator(decelerateInterpolator);
3625 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003626 mStateAnimation.play(panelAlpha);
3627 }
3628
3629 if (page != null) {
3630 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3631
3632 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3633 0, yDrift);
3634 page.setTranslationY(0);
3635 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3636 pageDrift.setInterpolator(decelerateInterpolator);
3637 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3638 mStateAnimation.play(pageDrift);
3639
3640 page.setAlpha(1f);
3641 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3642 itemsAlpha.setDuration(100);
3643 itemsAlpha.setInterpolator(decelerateInterpolator);
3644 mStateAnimation.play(itemsAlpha);
3645 }
3646
3647 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3648 pageIndicators.setAlpha(1f);
3649 ObjectAnimator indicatorsAlpha =
3650 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3651 indicatorsAlpha.setDuration(revealDuration);
3652 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3653 mStateAnimation.play(indicatorsAlpha);
3654
3655 width = revealView.getMeasuredWidth();
3656
3657 if (material) {
3658 if (!isWidgetTray) {
3659 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003660 }
Adam Cohen2854d252014-08-27 16:04:07 -07003661 int allAppsButtonSize = LauncherAppState.getInstance().
3662 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3663 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3664 Animator reveal =
3665 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3666 height / 2, revealRadius, finalRadius);
3667 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3668 reveal.setDuration(revealDuration);
3669 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003670
Adam Cohen2854d252014-08-27 16:04:07 -07003671 reveal.addListener(new AnimatorListenerAdapter() {
3672 public void onAnimationEnd(Animator animation) {
3673 revealView.setVisibility(View.INVISIBLE);
3674 if (!isWidgetTray) {
3675 allAppsButton.setVisibility(View.VISIBLE);
3676 }
3677 }
3678 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003679
Adam Cohen2854d252014-08-27 16:04:07 -07003680 mStateAnimation.play(reveal);
3681 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003682
Adam Cohen2854d252014-08-27 16:04:07 -07003683 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3684 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3685 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003686 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003687
3688 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003689 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003690 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003691 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003692 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3693 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003694 if (onCompleteRunnable != null) {
3695 onCompleteRunnable.run();
3696 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003697
3698 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003699 if (page != null) {
3700 page.setLayerType(View.LAYER_TYPE_NONE, null);
3701 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003702 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003703 // Unhide side pages
3704 int count = content.getChildCount();
3705 for (int i = 0; i < count; i++) {
3706 View child = content.getChildAt(i);
3707 child.setVisibility(View.VISIBLE);
3708 }
3709
3710 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003711 if (page != null) {
3712 page.setTranslationX(0);
3713 page.setTranslationY(0);
3714 page.setAlpha(1);
3715 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003716 content.setCurrentPage(content.getNextPage());
3717
Chet Haasebc2f0822012-10-26 17:59:53 -07003718 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003719
3720 // This can hold unnecessary references to views.
3721 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003722 }
3723 });
3724
Adam Cohen2854d252014-08-27 16:04:07 -07003725 final AnimatorSet stateAnimation = mStateAnimation;
3726 final Runnable startAnimRunnable = new Runnable() {
3727 public void run() {
3728 // Check that mStateAnimation hasn't changed while
3729 // we waited for a layout/draw pass
3730 if (mStateAnimation != stateAnimation)
3731 return;
3732 dispatchOnLauncherTransitionStart(fromView, animated, false);
3733 dispatchOnLauncherTransitionStart(toView, animated, false);
3734
Kenny Guyd794a3f2014-09-16 15:17:58 +01003735 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003736 for (int i = 0; i < layerViews.size(); i++) {
3737 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003738 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003739 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003740 }
Adam Cohen2854d252014-08-27 16:04:07 -07003741 }
3742 }
3743 mStateAnimation.start();
3744 }
3745 };
3746 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003747 } else {
3748 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003749 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003750 dispatchOnLauncherTransitionStart(fromView, animated, true);
3751 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003752 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003753 dispatchOnLauncherTransitionStart(toView, animated, true);
3754 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003755 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003756 }
3757
Michael Jurkae326f182011-11-21 14:05:46 -08003758 @Override
3759 public void onTrimMemory(int level) {
3760 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003761 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3762 // The widget preview db can result in holding onto over
3763 // 3MB of memory for caching which isn't necessary.
3764 SQLiteDatabase.releaseMemory();
3765
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003766 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003767 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003768 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003769 }
Michael Jurkae326f182011-11-21 14:05:46 -08003770 }
3771 }
3772
Adam Cohened307df2013-10-02 09:37:31 -07003773 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003774 showWorkspace(animated, null);
3775 }
3776
Adam Cohened307df2013-10-02 09:37:31 -07003777 protected void showWorkspace() {
3778 showWorkspace(true);
3779 }
3780
Adam Cohened66b2b2012-01-23 17:28:51 -08003781 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003782 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003783 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003784 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003785 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003786
Winson Chungc7d2b602012-05-16 17:02:20 -07003787 // Show the search bar (only animate if we were showing the drop target bar in spring
3788 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003789 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003790 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003791 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003792
Michael Jurkab3e22d92011-10-31 15:58:33 -07003793 // Set focus to the AppsCustomize button
3794 if (mAllAppsButton != null) {
3795 mAllAppsButton.requestFocus();
3796 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003797 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003798
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003799 // Change the state *after* we've called all the transition code
3800 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003801
Winson Chung5fb63472011-02-02 17:03:37 -08003802 // Resume the auto-advance of widgets
3803 mUserPresent = true;
3804 updateRunning();
3805
alanv1d4fde62012-10-17 13:15:47 -07003806 // Send an accessibility event to announce the context change
3807 getWindow().getDecorView()
3808 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003809
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003810 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003811 }
3812
Adam Cohened307df2013-10-02 09:37:31 -07003813 void showOverviewMode(boolean animated) {
3814 mWorkspace.setVisibility(View.VISIBLE);
3815 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3816 mState = State.WORKSPACE;
3817 onWorkspaceShown(animated);
3818 }
3819
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003820 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003821 }
3822
Winson Chung82963d52013-10-09 11:20:57 -07003823 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3824 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003825 if (mState != State.WORKSPACE) return;
3826
Winson Chung82963d52013-10-09 11:20:57 -07003827 if (resetPageToZero) {
3828 mAppsCustomizeTabHost.reset();
3829 }
Winson Chungc58497e2013-09-03 17:48:37 -07003830 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003831 mAppsCustomizeTabHost.post(new Runnable() {
3832 @Override
3833 public void run() {
3834 // We post this in-case the all apps view isn't yet constructed.
3835 mAppsCustomizeTabHost.requestFocus();
3836 }
3837 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003838
Michael Jurkab3e22d92011-10-31 15:58:33 -07003839 // Change the state *after* we've called all the transition code
3840 mState = State.APPS_CUSTOMIZE;
3841
3842 // Pause the auto-advance of widgets until we are out of AllApps
3843 mUserPresent = false;
3844 updateRunning();
3845 closeFolder();
3846
3847 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003848 getWindow().getDecorView()
3849 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003850 }
3851
Adam Cohen7777d962011-08-18 18:58:38 -07003852 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003853 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003854 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003855 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003856 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003857 }
Adam Cohen7777d962011-08-18 18:58:38 -07003858
Adam Cohenad4e15c2013-10-17 16:21:35 -07003859 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003860 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003861 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3862
Winson Chunge7a03942011-08-05 15:05:12 -07003863 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003864 @Override
3865 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003866 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003867 // Before we show workspace, hide all apps again because
3868 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3869 // clean up our state transition functions
3870 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003871 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003872 } else {
3873 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003874 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003875 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003876 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003877 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003878
Michael Jurkad3ef3062010-11-23 16:23:58 -08003879 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003880 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003881 final boolean animated = true;
3882 final boolean springLoaded = true;
3883 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003884 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003885 }
3886 // Otherwise, we are not in spring loaded mode, so don't do anything.
3887 }
3888
Michael Jurkab3e22d92011-10-31 15:58:33 -07003889 void lockAllApps() {
3890 // TODO
3891 }
3892
3893 void unlockAllApps() {
3894 // TODO
3895 }
3896
Sunny Goyal594d76d2014-11-06 10:12:54 -08003897 protected void disableVoiceButtonProxy(boolean disable) {
3898 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003899 }
3900
Cristina Stancu476493b2013-08-07 17:20:14 +01003901 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003902 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3903 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003904 }
3905
Adam Cohen24ce0b32014-01-14 16:18:14 -08003906 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003907 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3908 if (searchProvider == null) {
3909 return null;
3910 }
3911
3912 Bundle opts = new Bundle();
3913 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
3914 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
3915
3916 SharedPreferences sp = getSharedPreferences(
3917 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3918 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003919 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003920 if (!searchProvider.provider.flattenToString().equals(
3921 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003922 || (widgetInfo == null)
3923 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003924 // A valid widget is not already bound.
3925 if (widgetId > -1) {
3926 mAppWidgetHost.deleteAppWidgetId(widgetId);
3927 widgetId = -1;
3928 }
3929
3930 // Try to bind a new widget
3931 widgetId = mAppWidgetHost.allocateAppWidgetId();
3932
3933 if (!AppWidgetManagerCompat.getInstance(this)
3934 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3935 mAppWidgetHost.deleteAppWidgetId(widgetId);
3936 widgetId = -1;
3937 }
3938
3939 sp.edit()
3940 .putInt(QSB_WIDGET_ID, widgetId)
3941 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3942 .commit();
3943 }
3944
3945 if (widgetId != -1) {
3946 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3947 mQsb.updateAppWidgetOptions(opts);
3948 mQsb.setPadding(0, 0, 0, 0);
3949 mSearchDropTargetBar.addView(mQsb);
3950 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003951 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003952 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003953 }
3954
Michael Jurkad7c28052012-04-27 15:43:36 -07003955 @Override
3956 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003957 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003958 final List<CharSequence> text = event.getText();
3959 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003960 // Populate event with a fake title based on the current state.
3961 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07003962 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07003963 } else {
3964 text.add(getString(R.string.all_apps_home_button_label));
3965 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003966 return result;
3967 }
3968
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003969 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003970 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003971 */
3972 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3973 @Override
3974 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003975 closeSystemDialogs();
3976 }
3977 }
3978
3979 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003980 * Receives notifications whenever the appwidgets are reset.
3981 */
3982 private class AppWidgetResetObserver extends ContentObserver {
3983 public AppWidgetResetObserver() {
3984 super(new Handler());
3985 }
3986
3987 @Override
3988 public void onChange(boolean selfChange) {
3989 onAppWidgetReset();
3990 }
3991 }
3992
3993 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003994 * If the activity is currently paused, signal that we need to run the passed Runnable
3995 * in onResume.
3996 *
3997 * This needs to be called from incoming places where resources might have been loaded
3998 * while we are paused. That is becaues the Configuration might be wrong
3999 * when we're not running, and if it comes back to what it was when we
4000 * were paused, we are not restarted.
4001 *
4002 * Implementation of the method from LauncherModel.Callbacks.
4003 *
4004 * @return true if we are currently paused. The caller might be able to
4005 * skip some work in that case since we will come back again.
4006 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004007 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004008 if (mPaused) {
4009 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004010 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004011 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004012 }
4013 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004014 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004015 return true;
4016 } else {
4017 return false;
4018 }
4019 }
4020
Michael Jurkac402cd92013-05-20 15:49:32 +02004021 private boolean waitUntilResume(Runnable run) {
4022 return waitUntilResume(run, false);
4023 }
4024
Michael Jurka1e2f4652013-07-08 18:03:46 -07004025 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004026 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004027 }
4028
Michael Jurka7607c2f2013-04-03 14:33:19 -07004029 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004030 * If the activity is currently paused, signal that we need to re-run the loader
4031 * in onResume.
4032 *
4033 * This needs to be called from incoming places where resources might have been loaded
4034 * while we are paused. That is becaues the Configuration might be wrong
4035 * when we're not running, and if it comes back to what it was when we
4036 * were paused, we are not restarted.
4037 *
4038 * Implementation of the method from LauncherModel.Callbacks.
4039 *
4040 * @return true if we are currently paused. The caller might be able to
4041 * skip some work in that case since we will come back again.
4042 */
4043 public boolean setLoadOnResume() {
4044 if (mPaused) {
4045 Log.i(TAG, "setLoadOnResume");
4046 mOnResumeNeedsLoad = true;
4047 return true;
4048 } else {
4049 return false;
4050 }
4051 }
4052
4053 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004054 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004055 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004056 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004057 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004058 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004059 } else {
4060 return SCREEN_COUNT / 2;
4061 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004062 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004063
Joe Onorato9c1289c2009-08-17 11:03:03 -04004064 /**
4065 * Refreshes the shortcuts shown on the workspace.
4066 *
4067 * Implementation of the method from LauncherModel.Callbacks.
4068 */
4069 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004070 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004071
Michael Jurka7607c2f2013-04-03 14:33:19 -07004072 // If we're starting binding all over again, clear any bind calls we'd postponed in
4073 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4074 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004075 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004076
Winson Chungd64d1762013-08-20 14:37:16 -07004077 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004078 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004079 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004080
Michael Jurka05bf644e2011-11-30 20:28:53 -08004081 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004082 if (mHotseat != null) {
4083 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004084 }
4085 }
4086
Adam Cohendcd297f2013-06-18 13:13:40 -07004087 @Override
4088 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004089 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004090
Adam Cohen5084cba2013-09-03 12:01:16 -07004091 // If there are no screens, we need to have an empty screen
4092 if (orderedScreenIds.size() == 0) {
4093 mWorkspace.addExtraEmptyScreen();
4094 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004095
4096 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004097 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004098 if (hasCustomContentToLeft()) {
4099 mWorkspace.createCustomContentContainer();
4100 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004101 }
Winson Chung64359a52013-07-08 17:17:08 -07004102 }
4103
4104 @Override
4105 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004106 // Log to disk
4107 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4108 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4109 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004110 int count = orderedScreenIds.size();
4111 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004112 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004113 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004114 }
4115
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08004116 @Override
4117 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
4118 final long screenId, final int[] cell, final int spanX, final int spanY) {
4119 showWorkspace(true, new Runnable() {
4120
4121 @Override
4122 public void run() {
4123 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
4124 addPendingItem(info, container, screenId, cell, spanX, spanY);
4125 }
4126 });
4127 }
4128
Adam Cohen39a06042013-07-19 14:30:12 -07004129 private boolean shouldShowWeightWatcher() {
4130 String spKey = LauncherAppState.getSharedPreferencesKey();
4131 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07004132 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004133
4134 return show;
4135 }
4136
4137 private void toggleShowWeightWatcher() {
4138 String spKey = LauncherAppState.getSharedPreferencesKey();
4139 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4140 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4141
4142 show = !show;
4143
4144 SharedPreferences.Editor editor = sp.edit();
4145 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4146 editor.commit();
4147
4148 if (mWeightWatcher != null) {
4149 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4150 }
4151 }
4152
Winson Chungd64d1762013-08-20 14:37:16 -07004153 public void bindAppsAdded(final ArrayList<Long> newScreens,
4154 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004155 final ArrayList<ItemInfo> addAnimated,
4156 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004157 Runnable r = new Runnable() {
4158 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004159 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004160 }
4161 };
4162 if (waitUntilResume(r)) {
4163 return;
4164 }
4165
Winson Chungd64d1762013-08-20 14:37:16 -07004166 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004167 if (newScreens != null) {
4168 bindAddScreens(newScreens);
4169 }
Winson Chungd64d1762013-08-20 14:37:16 -07004170
4171 // We add the items without animation on non-visible pages, and with
4172 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004173 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004174 bindItems(addNotAnimated, 0,
4175 addNotAnimated.size(), false);
4176 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004177 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004178 bindItems(addAnimated, 0,
4179 addAnimated.size(), true);
4180 }
Winson Chungc58497e2013-09-03 17:48:37 -07004181
Winson Chung87412982013-10-03 18:34:14 -07004182 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004183 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004184
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004185 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004186 addedApps != null && mAppsCustomizeContent != null) {
4187 mAppsCustomizeContent.addApps(addedApps);
4188 }
Winson Chungd64d1762013-08-20 14:37:16 -07004189 }
4190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004191 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004192 * Bind the items start-end from the list.
4193 *
4194 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004195 */
Winson Chung64359a52013-07-08 17:17:08 -07004196 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4197 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004198 Runnable r = new Runnable() {
4199 public void run() {
4200 bindItems(shortcuts, start, end, forceAnimateIcons);
4201 }
4202 };
4203 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004204 return;
4205 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004206
Winson Chungf0c6ae02012-03-21 16:10:31 -07004207 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004208 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4209 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004210 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004211 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004212 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004213 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004214 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004215
4216 // Short circuit if we are loading dock items for a configuration which has no dock
4217 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4218 mHotseat == null) {
4219 continue;
4220 }
4221
Joe Onorato9c1289c2009-08-17 11:03:03 -04004222 switch (item.itemType) {
4223 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4224 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004225 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004226 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004227
Winson Chung64359a52013-07-08 17:17:08 -07004228 /*
4229 * TODO: FIX collision case
4230 */
Winson Chungce376632013-07-11 16:12:41 -07004231 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4232 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4233 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004234 View v = cl.getChildAt(item.cellX, item.cellY);
4235 Object tag = v.getTag();
4236 String desc = "Collision while binding workspace item: " + item
4237 + ". Collides with " + tag;
4238 if (LauncherAppState.isDogfoodBuild()) {
4239 throw (new RuntimeException(desc));
4240 } else {
4241 Log.d(TAG, desc);
4242 }
Winson Chungce376632013-07-11 16:12:41 -07004243 }
Winson Chung64359a52013-07-08 17:17:08 -07004244 }
4245
Adam Cohendcd297f2013-06-18 13:13:40 -07004246 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4247 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004248 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004249 // Animate all the applications up now
4250 shortcut.setAlpha(0f);
4251 shortcut.setScaleX(0f);
4252 shortcut.setScaleY(0f);
4253 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004254 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004255 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004256 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004257 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004258 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004259 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004260 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004261 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4262 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004263 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004264 default:
4265 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004266 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004267 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004268
Winson Chung997a9232013-07-24 15:33:46 -07004269 if (animateIcons) {
4270 // Animate to the correct page
4271 if (newShortcutsScreenId > -1) {
4272 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004273 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004274 final Runnable startBounceAnimRunnable = new Runnable() {
4275 public void run() {
4276 anim.playTogether(bounceAnims);
4277 anim.start();
4278 }
4279 };
Winson Chung997a9232013-07-24 15:33:46 -07004280 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004281 // We post the animation slightly delayed to prevent slowdowns
4282 // when we are loading right after we return to launcher.
4283 mWorkspace.postDelayed(new Runnable() {
4284 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004285 if (mWorkspace != null) {
4286 mWorkspace.snapToPage(newScreenIndex);
4287 mWorkspace.postDelayed(startBounceAnimRunnable,
4288 NEW_APPS_ANIMATION_DELAY);
4289 }
Winson Chung94d67682013-09-25 16:29:40 -07004290 }
4291 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004292 } else {
4293 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004294 }
4295 }
Winson Chung64359a52013-07-08 17:17:08 -07004296 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004297 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004298 }
4299
Joe Onorato9c1289c2009-08-17 11:03:03 -04004300 /**
4301 * Implementation of the method from LauncherModel.Callbacks.
4302 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004303 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004304 Runnable r = new Runnable() {
4305 public void run() {
4306 bindFolders(folders);
4307 }
4308 };
4309 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004310 return;
4311 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004312 sFolders.clear();
4313 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004314 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004315
4316 /**
4317 * Add the views for a widget to the workspace.
4318 *
4319 * Implementation of the method from LauncherModel.Callbacks.
4320 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004321 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004322 Runnable r = new Runnable() {
4323 public void run() {
4324 bindAppWidget(item);
4325 }
4326 };
4327 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004328 return;
4329 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004330
Daniel Sandler843e8602010-06-07 14:59:01 -04004331 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4332 if (DEBUG_WIDGETS) {
4333 Log.d(TAG, "bindAppWidget: " + item);
4334 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004335 final Workspace workspace = mWorkspace;
4336
Adam Cohen59400422014-03-05 18:07:04 -08004337 LauncherAppWidgetProviderInfo appWidgetInfo =
4338 LauncherModel.getProviderInfo(this, item.providerName);
4339
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004340 if (!mIsSafeModeEnabled
4341 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4342 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004343 if (appWidgetInfo == null) {
4344 if (DEBUG_WIDGETS) {
4345 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4346 + " belongs to component " + item.providerName
4347 + ", as the povider is null");
4348 }
4349 LauncherModel.deleteItemFromDatabase(this, item);
4350 return;
4351 }
4352 // Note: This assumes that the id remap broadcast is received before this step.
4353 // If that is not the case, the id remap will be ignored and user may see the
4354 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004355 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004356 pendingInfo.spanX = item.spanX;
4357 pendingInfo.spanY = item.spanY;
4358 pendingInfo.minSpanX = item.minSpanX;
4359 pendingInfo.minSpanY = item.minSpanY;
4360 Bundle options =
4361 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4362
Sunny Goyalff572272014-07-23 13:58:07 -07004363 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004364 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4365 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004366
4367 // TODO consider showing a permission dialog when the widget is clicked.
4368 if (!success) {
4369 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4370 if (DEBUG_WIDGETS) {
4371 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4372 + " belongs to component " + item.providerName
4373 + ", as the launcher is unable to bing a new widget id");
4374 }
4375 LauncherModel.deleteItemFromDatabase(this, item);
4376 return;
4377 }
4378
4379 item.appWidgetId = newWidgetId;
4380
4381 // If the widget has a configure activity, it is still needs to set it up, otherwise
4382 // the widget is ready to go.
4383 item.restoreStatus = (appWidgetInfo.configure == null)
4384 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4385 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4386
4387 LauncherModel.updateItemInDatabase(this, item);
4388 }
4389
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004390 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004391 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004392 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004393 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4394 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004395 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004396
Sunny Goyal651077b2014-06-30 14:15:31 -07004397 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4398 } else {
4399 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004400 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4401 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004402 view.updateIcon(mIconCache);
4403 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004404 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004405 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004406 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004407
Joe Onorato9c1289c2009-08-17 11:03:03 -04004408 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004409 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004410
Adam Cohendcd297f2013-06-18 13:13:40 -07004411 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004412 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004413 if (!item.isCustomWidget()) {
4414 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4415 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004416
Joe Onorato9c1289c2009-08-17 11:03:03 -04004417 workspace.requestLayout();
4418
Daniel Sandler843e8602010-06-07 14:59:01 -04004419 if (DEBUG_WIDGETS) {
4420 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4421 + (SystemClock.uptimeMillis()-start) + "ms");
4422 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004423 }
4424
Sunny Goyalff572272014-07-23 13:58:07 -07004425 /**
4426 * Restores a pending widget.
4427 *
4428 * @param appWidgetId The app widget id
4429 * @param cellInfo The position on screen where to create the widget.
4430 */
4431 private void completeRestoreAppWidget(final int appWidgetId) {
4432 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4433 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4434 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4435 return;
4436 }
4437
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004438 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004439 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4440
4441 mWorkspace.reinflateWidgetsIfNecessary();
4442 LauncherModel.updateItemInDatabase(this, info);
4443 }
4444
Adam Cohen1462de32012-07-24 22:34:36 -07004445 public void onPageBoundSynchronously(int page) {
4446 mSynchronouslyBoundPages.add(page);
4447 }
4448
Joe Onorato9c1289c2009-08-17 11:03:03 -04004449 /**
4450 * Callback saying that there aren't any more items to bind.
4451 *
4452 * Implementation of the method from LauncherModel.Callbacks.
4453 */
Adam Cohene25af792013-06-06 23:08:25 -07004454 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004455 Runnable r = new Runnable() {
4456 public void run() {
4457 finishBindingItems(upgradePath);
4458 }
4459 };
4460 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004461 return;
4462 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004463 if (mSavedState != null) {
4464 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004465 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004466 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004467 mSavedState = null;
4468 }
4469
Adam Cohen1462de32012-07-24 22:34:36 -07004470 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004471
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004472 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004473 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004474
4475 // If we received the result of any pending adds while the loader was running (e.g. the
4476 // widget configuration forced an orientation change), process them now.
4477 if (sPendingAddItem != null) {
4478 final long screenId = completeAdd(sPendingAddItem);
4479
4480 // TODO: this moves the user to the page where the pending item was added. Ideally,
4481 // the screen would be guaranteed to exist after bind, and the page would be set through
4482 // the workspace restore process.
4483 mWorkspace.post(new Runnable() {
4484 @Override
4485 public void run() {
4486 mWorkspace.snapToScreenId(screenId);
4487 }
4488 });
4489 sPendingAddItem = null;
4490 }
4491
Adam Cohene25af792013-06-06 23:08:25 -07004492 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004493 mWorkspace.getUniqueComponents(true, null);
4494 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004495 }
Sunny Goyale755d462014-07-22 13:48:29 -07004496 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004497
4498 if (mLauncherCallbacks != null) {
4499 mLauncherCallbacks.finishBindingItems(upgradePath);
4500 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004501 }
4502
Adam Cohen3ed316a2014-07-23 18:21:20 -07004503 private void sendLoadingCompleteBroadcastIfNecessary() {
4504 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4505 String permission =
4506 getResources().getString(R.string.receive_first_load_broadcast_permission);
4507 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4508 sendBroadcast(intent, permission);
4509 SharedPreferences.Editor editor = mSharedPrefs.edit();
4510 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4511 editor.apply();
4512 }
4513 }
4514
Winson Chunga0b7e862013-09-05 16:03:15 -07004515 public boolean isAllAppsButtonRank(int rank) {
4516 if (mHotseat != null) {
4517 return mHotseat.isAllAppsButtonRank(rank);
4518 }
4519 return false;
4520 }
4521
Winson Chunga2413752012-04-03 14:22:34 -07004522 private boolean canRunNewAppsAnimation() {
4523 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4524 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4525 }
4526
Winson Chungc9168342013-06-26 14:54:55 -07004527 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4528 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4529 PropertyValuesHolder.ofFloat("alpha", 1f),
4530 PropertyValuesHolder.ofFloat("scaleX", 1f),
4531 PropertyValuesHolder.ofFloat("scaleY", 1f));
4532 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4533 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4534 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4535 return bounceAnim;
4536 }
4537
Adam Cohen27772732013-11-11 14:00:56 +00004538 public boolean useVerticalBarLayout() {
4539 return LauncherAppState.getInstance().getDynamicGrid().
4540 getDeviceProfile().isVerticalBarLayout();
4541 }
4542
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004543 protected Rect getSearchBarBounds() {
4544 return LauncherAppState.getInstance().getDynamicGrid().
4545 getDeviceProfile().getSearchBarBounds();
4546 }
4547
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004548 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004549 if (mSearchDropTargetBar == null) {
4550 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004551 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004552 if (mQsb != null) {
4553 mSearchDropTargetBar.removeView(mQsb);
4554 mQsb = null;
4555 }
4556 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004557 }
4558
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004559 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004560 * Add the icons for all apps.
4561 *
4562 * Implementation of the method from LauncherModel.Callbacks.
4563 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004564 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004565 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004566 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4567 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4568 getHotseat().addAllAppsFolder(mIconCache, apps,
4569 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4570 }
4571 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004572 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004573 if (mAppsCustomizeContent != null) {
4574 mAppsCustomizeContent.onPackagesUpdated(
4575 LauncherModel.getSortedWidgetsAndShortcuts(this));
4576 }
Winson Chungc58497e2013-09-03 17:48:37 -07004577 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004578 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004579 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004580 mAppsCustomizeContent.onPackagesUpdated(
4581 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004582 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004583 }
Adam Cohen9211d422014-10-07 18:14:53 -07004584 if (mLauncherCallbacks != null) {
4585 mLauncherCallbacks.bindAllApplications(apps);
4586 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004587 }
4588
4589 /**
4590 * A package was updated.
4591 *
4592 * Implementation of the method from LauncherModel.Callbacks.
4593 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004594 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004595 Runnable r = new Runnable() {
4596 public void run() {
4597 bindAppsUpdated(apps);
4598 }
4599 };
4600 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004601 return;
4602 }
4603
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004604 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004605 mAppsCustomizeContent != null) {
4606 mAppsCustomizeContent.updateApps(apps);
4607 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004608 }
4609
Sunny Goyal4390ace2014-10-13 11:33:11 -07004610 @Override
4611 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4612 Runnable r = new Runnable() {
4613 public void run() {
4614 bindWidgetsRestored(widgets);
4615 }
4616 };
4617 if (waitUntilResume(r)) {
4618 return;
4619 }
4620 mWorkspace.widgetsRestored(widgets);
4621 }
4622
Joe Onorato9c1289c2009-08-17 11:03:03 -04004623 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004624 * Some shortcuts were updated in the background.
4625 *
4626 * Implementation of the method from LauncherModel.Callbacks.
4627 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004628 @Override
4629 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4630 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004631 Runnable r = new Runnable() {
4632 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004633 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004634 }
4635 };
4636 if (waitUntilResume(r)) {
4637 return;
4638 }
4639
Sunny Goyal4390ace2014-10-13 11:33:11 -07004640 if (!updated.isEmpty()) {
4641 mWorkspace.updateShortcuts(updated);
4642 }
4643
4644 if (!removed.isEmpty()) {
4645 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4646 for (ShortcutInfo si : removed) {
4647 removedComponents.add(si.getTargetComponent());
4648 }
4649 mWorkspace.removeItemsByComponentName(removedComponents, user);
4650 // Notify the drag controller
4651 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004652 }
4653 }
4654
4655 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004656 * Update the state of a package, typically related to install state.
4657 *
4658 * Implementation of the method from LauncherModel.Callbacks.
4659 */
Sunny Goyale755d462014-07-22 13:48:29 -07004660 @Override
4661 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004662 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004663 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004664 }
4665 }
4666
4667 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004668 * Update the label and icon of all the icons in a package
4669 *
4670 * Implementation of the method from LauncherModel.Callbacks.
4671 */
4672 @Override
4673 public void updatePackageBadge(String packageName) {
4674 if (mWorkspace != null) {
4675 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4676 }
4677 }
4678
4679 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004680 * A package was uninstalled. We take both the super set of packageNames
4681 * in addition to specific applications to remove, the reason being that
4682 * this can be called when a package is updated as well. In that scenario,
4683 * we only remove specific components from the workspace, where as
4684 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004685 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004686 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004687 * Implementation of the method from LauncherModel.Callbacks.
4688 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004689 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004690 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004691 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004692 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004693 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004694 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004695 }
Winson Chungd64d1762013-08-20 14:37:16 -07004696 };
4697 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004698 return;
4699 }
4700
Sunny Goyal1a745e82014-10-02 15:58:31 -07004701 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004702 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4703 for (AppInfo info : appInfos) {
4704 removedComponents.add(info.componentName);
4705 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004706 if (!packageNames.isEmpty()) {
4707 mWorkspace.removeItemsByPackageName(packageNames, user);
4708 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004709 if (!removedComponents.isEmpty()) {
4710 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004711 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004712 // Notify the drag controller
4713 mDragController.onAppsRemoved(packageNames, removedComponents);
4714
Sunny Goyal1a745e82014-10-02 15:58:31 -07004715 } else {
4716 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004717 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004718
Winson Chungdf95eb12013-10-16 14:57:07 -07004719 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004720 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004721 mAppsCustomizeContent != null) {
4722 mAppsCustomizeContent.removeApps(appInfos);
4723 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004724 }
Joe Onoratobe386092009-11-17 17:32:16 -08004725
4726 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004727 * A number of packages were updated.
4728 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004729 private ArrayList<Object> mWidgetsAndShortcuts;
4730 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004731 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004732 bindPackagesUpdated(mWidgetsAndShortcuts);
4733 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004734 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004735 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004736 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4737 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4738 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004739 return;
4740 }
4741
Winson Chung64359a52013-07-08 17:17:08 -07004742 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004743 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004744 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004745 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004746 }
4747
Winson Chung400438b2011-01-16 17:53:48 -08004748 private int mapConfigurationOriActivityInfoOri(int configOri) {
4749 final Display d = getWindowManager().getDefaultDisplay();
4750 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4751 switch (d.getRotation()) {
4752 case Surface.ROTATION_0:
4753 case Surface.ROTATION_180:
4754 // We are currently in the same basic orientation as the natural orientation
4755 naturalOri = configOri;
4756 break;
4757 case Surface.ROTATION_90:
4758 case Surface.ROTATION_270:
4759 // We are currently in the other basic orientation to the natural orientation
4760 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4761 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4762 break;
4763 }
4764
4765 int[] oriMap = {
4766 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4767 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4768 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4769 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4770 };
4771 // Since the map starts at portrait, we need to offset if this device's natural orientation
4772 // is landscape.
4773 int indexOffset = 0;
4774 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4775 indexOffset = 1;
4776 }
4777 return oriMap[(d.getRotation() + indexOffset) % 4];
4778 }
Adam Cohen446e9402011-09-15 18:21:21 -07004779
Winson Chung4b919f82012-05-01 10:44:08 -07004780 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004781 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004782 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4783 .getConfiguration().orientation));
4784 }
4785 }
4786 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004787 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004788 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004789 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004790 } else {
4791 mHandler.postDelayed(new Runnable() {
4792 public void run() {
4793 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4794 }
4795 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004796 }
Winson Chung4b919f82012-05-01 10:44:08 -07004797 }
Winson Chung400438b2011-01-16 17:53:48 -08004798 }
4799
Winson Chunge43a1e72014-01-15 10:33:02 -08004800 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004801 if (mLauncherCallbacks != null) {
4802 return mLauncherCallbacks.isLauncherPreinstalled();
4803 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004804 PackageManager pm = getPackageManager();
4805 try {
4806 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4807 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4808 return true;
4809 } else {
4810 return false;
4811 }
4812 } catch (NameNotFoundException e) {
4813 e.printStackTrace();
4814 return false;
4815 }
4816 }
4817
Adam Cohenea90f832014-05-21 15:03:34 -07004818 /**
4819 * This method indicates whether or not we should suggest default wallpaper dimensions
4820 * when our wallpaper cropper was not yet used to set a wallpaper.
4821 */
4822 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004823 if (mLauncherCallbacks != null) {
4824 return mLauncherCallbacks.overrideWallpaperDimensions();
4825 }
Adam Cohenea90f832014-05-21 15:03:34 -07004826 return true;
4827 }
4828
Adam Cohen432609a2014-03-13 17:03:22 -07004829 /**
4830 * To be overridden by subclasses to indicate that there is an activity to launch
4831 * before showing the standard launcher experience.
4832 */
4833 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004834 if (mLauncherCallbacks != null) {
4835 return mLauncherCallbacks.hasFirstRunActivity();
4836 }
Adam Cohen432609a2014-03-13 17:03:22 -07004837 return false;
4838 }
4839
4840 /**
4841 * To be overridden by subclasses to launch any first run activity
4842 */
4843 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004844 if (mLauncherCallbacks != null) {
4845 return mLauncherCallbacks.getFirstRunActivity();
4846 }
Adam Cohen432609a2014-03-13 17:03:22 -07004847 return null;
4848 }
4849
Winson Chunga6945242014-01-08 14:04:34 -08004850 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004851 return !ActivityManager.isRunningInTestHarness() &&
4852 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004853 }
4854
Adam Cohen4a9423d2014-03-28 14:22:31 -07004855 protected boolean hasRunFirstRunActivity() {
4856 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4857 }
4858
Adam Cohen432609a2014-03-13 17:03:22 -07004859 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004860 if (shouldRunFirstRunActivity() &&
4861 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004862 Intent firstRunIntent = getFirstRunActivity();
4863 if (firstRunIntent != null) {
4864 startActivity(firstRunIntent);
4865 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004866 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004867 }
4868 }
Adam Cohen432609a2014-03-13 17:03:22 -07004869 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004870 }
4871
4872 private void markFirstRunActivityShown() {
4873 SharedPreferences.Editor editor = mSharedPrefs.edit();
4874 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4875 editor.apply();
4876 }
4877
Adam Cohen432609a2014-03-13 17:03:22 -07004878 /**
4879 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4880 * screen that must be displayed and dismissed.
4881 */
4882 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004883 if (mLauncherCallbacks != null) {
4884 return mLauncherCallbacks.hasDismissableIntroScreen();
4885 }
Adam Cohen432609a2014-03-13 17:03:22 -07004886 return false;
4887 }
4888
4889 /**
4890 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4891 */
4892 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004893 if (mLauncherCallbacks != null) {
4894 return mLauncherCallbacks.getIntroScreen();
4895 }
Adam Cohen432609a2014-03-13 17:03:22 -07004896 return null;
4897 }
4898
4899 /**
4900 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4901 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4902 */
4903 private boolean shouldShowIntroScreen() {
4904 return hasDismissableIntroScreen() &&
4905 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4906 }
4907
4908 protected void showIntroScreen() {
4909 View introScreen = getIntroScreen();
4910 changeWallpaperVisiblity(false);
4911 if (introScreen != null) {
4912 mDragLayer.showOverlayView(introScreen);
4913 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004914 if (mLauncherOverlayContainer != null) {
4915 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4916 }
Adam Cohen432609a2014-03-13 17:03:22 -07004917 }
4918
4919 public void dismissIntroScreen() {
4920 markIntroScreenDismissed();
4921 if (showFirstRunActivity()) {
4922 // We delay hiding the intro view until the first run activity is showing. This
4923 // avoids a blip.
4924 mWorkspace.postDelayed(new Runnable() {
4925 @Override
4926 public void run() {
4927 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004928 if (mLauncherOverlayContainer != null) {
4929 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4930 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004931 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004932 }
4933 }, ACTIVITY_START_DELAY);
4934 } else {
4935 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004936 if (mLauncherOverlayContainer != null) {
4937 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4938 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004939 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004940 }
4941 changeWallpaperVisiblity(true);
4942 }
4943
4944 private void markIntroScreenDismissed() {
4945 SharedPreferences.Editor editor = mSharedPrefs.edit();
4946 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4947 editor.apply();
4948 }
4949
Sunny Goyal88d60f12014-09-08 03:47:29 -07004950 private void showFirstRunClings() {
4951 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4952 // on the device, then we always show the first run cling experience (or if there is no
4953 // launcher2). Otherwise, we prompt the user upon started for migration
4954 LauncherClings launcherClings = new LauncherClings(this);
4955 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4956 if (mModel.canMigrateFromOldLauncherDb(this)) {
4957 launcherClings.showMigrationCling();
4958 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004959 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004960 }
4961 }
4962 }
4963
Winson Chunga6945242014-01-08 14:04:34 -08004964 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004965 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4966 if (mHotseat != null) mHotseat.setAlpha(1f);
4967 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4968 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004969 }
4970
4971 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004972 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4973 if (mHotseat != null) mHotseat.setAlpha(0f);
4974 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4975 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004976 }
4977
Mathew Inwood72fbec12013-11-19 15:45:07 +00004978 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004979 // Called from search suggestion, not supported in other profiles.
4980 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4981 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4982 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4983 myUser);
4984 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004985 return null;
4986 }
Kenny Guyed131872014-04-30 03:02:21 +01004987 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004988 }
4989
4990 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4991 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004992 // Called from search suggestion, not supported in other profiles.
4993 return createShortcutDragInfo(shortcutIntent, caption, icon,
4994 UserHandleCompat.myUserHandle());
4995 }
4996
4997 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4998 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004999 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005000 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005001 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005002 }
5003
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005004 protected void moveWorkspaceToDefaultScreen() {
5005 mWorkspace.moveToDefaultScreen(false);
5006 }
5007
Mathew Inwood72fbec12013-11-19 15:45:07 +00005008 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5009 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005010 mWorkspace.onExternalDragStartedWithItem(dragView);
5011 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005012 }
5013
Anjali Koppalf5d29132014-02-28 13:33:27 -08005014 @Override
5015 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005016 if (mLauncherCallbacks != null) {
5017 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5018 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005019 }
5020
Winson Chung80baf5a2010-08-09 16:03:15 -07005021 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005022 * Prints out out state for debugging.
5023 */
5024 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005025 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005026 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005027 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5028 Log.d(TAG, "mRestoring=" + mRestoring);
5029 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5030 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005031 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005032 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005033
Winson Chung785d2eb2011-04-14 16:08:02 -07005034 if (mAppsCustomizeContent != null) {
5035 mAppsCustomizeContent.dumpState();
5036 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005037 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005038 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005039
5040 @Override
5041 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5042 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005043 synchronized (sDumpLogs) {
5044 writer.println(" ");
5045 writer.println("Debug logs: ");
5046 for (int i = 0; i < sDumpLogs.size(); i++) {
5047 writer.println(" " + sDumpLogs.get(i));
5048 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005049 }
Adam Cohen9211d422014-10-07 18:14:53 -07005050 if (mLauncherCallbacks != null) {
5051 mLauncherCallbacks.dump(prefix, fd, writer, args);
5052 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005053 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005054
5055 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005056 if (DEBUG_DUMP_LOG) {
5057 synchronized (sDumpLogs) {
5058 Log.d(TAG, "");
5059 Log.d(TAG, "*********************");
5060 Log.d(TAG, "Launcher debug logs: ");
5061 for (int i = 0; i < sDumpLogs.size(); i++) {
5062 Log.d(TAG, " " + sDumpLogs.get(i));
5063 }
5064 Log.d(TAG, "*********************");
5065 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005066 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005067 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005068 }
5069
5070 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005071 addDumpLog(tag, log, null, debugLog);
5072 }
5073
5074 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005075 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005076 if (e != null) {
5077 Log.d(tag, log, e);
5078 } else {
5079 Log.d(tag, log);
5080 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005081 }
Winson Chungede41292013-09-19 16:27:36 -07005082 if (DEBUG_DUMP_LOG) {
5083 sDateStamp.setTime(System.currentTimeMillis());
5084 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005085 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5086 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005087 }
Winson Chungede41292013-09-19 16:27:36 -07005088 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005089 }
5090
Adam Cohen59400422014-03-05 18:07:04 -08005091 public static CustomAppWidget getCustomAppWidget(String name) {
5092 return sCustomAppWidgets.get(name);
5093 }
5094
5095 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
5096 return sCustomAppWidgets;
5097 }
5098
Winson Chungede41292013-09-19 16:27:36 -07005099 public void dumpLogsToLocalData() {
5100 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005101 new AsyncTask<Void, Void, Void>() {
5102 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005103 boolean success = false;
5104 sDateStamp.setTime(sRunStart);
5105 String FILENAME = sDateStamp.getMonth() + "-"
5106 + sDateStamp.getDay() + "_"
5107 + sDateStamp.getHours() + "-"
5108 + sDateStamp.getMinutes() + "_"
5109 + sDateStamp.getSeconds() + ".txt";
5110
5111 FileOutputStream fos = null;
5112 File outFile = null;
5113 try {
5114 outFile = new File(getFilesDir(), FILENAME);
5115 outFile.createNewFile();
5116 fos = new FileOutputStream(outFile);
5117 } catch (Exception e) {
5118 e.printStackTrace();
5119 }
5120 if (fos != null) {
5121 PrintWriter writer = new PrintWriter(fos);
5122
5123 writer.println(" ");
5124 writer.println("Debug logs: ");
5125 synchronized (sDumpLogs) {
5126 for (int i = 0; i < sDumpLogs.size(); i++) {
5127 writer.println(" " + sDumpLogs.get(i));
5128 }
5129 }
5130 writer.close();
5131 }
5132 try {
5133 if (fos != null) {
5134 fos.close();
5135 success = true;
5136 }
5137 } catch (IOException e) {
5138 e.printStackTrace();
5139 }
Michael Jurka43467462013-11-14 12:03:58 +01005140 return null;
Winson Chungede41292013-09-19 16:27:36 -07005141 }
Michael Jurka43467462013-11-14 12:03:58 +01005142 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005143 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005145}
Michael Jurka2763be32011-02-24 11:19:57 -08005146
Michael Jurkaabded662011-03-04 12:06:57 -08005147interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005148 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005149 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005150 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005151 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005152 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005153}
Dan Sandlerd5024042014-01-09 15:01:33 -05005154
5155interface DebugIntents {
5156 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5157 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005158}