blob: e58d0dab40c0cb00afe6a8d18f24fee7a2f8366e [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;
Chet Haasea8f996d2015-02-17 12:56:04 -080098import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070099import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700100
Sunny Goyal651077b2014-06-30 14:15:31 -0700101import com.android.launcher3.DropTarget.DragObject;
102import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700103import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100104import com.android.launcher3.compat.LauncherActivityInfoCompat;
105import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700106import com.android.launcher3.compat.PackageInstallerCompat;
107import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100108import com.android.launcher3.compat.UserHandleCompat;
109import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700110
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.io.DataInputStream;
112import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700114import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700118import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800119import java.lang.reflect.Field;
120import java.lang.reflect.InvocationTargetException;
121import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700124import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700127import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000129import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131/**
132 * Default launcher application.
133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100134public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800137 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700138 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Daniel Sandlerf061f822013-06-27 13:59:36 -0400140 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400141 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700142 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400143 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700144 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700145
Dan Sandlerd5024042014-01-09 15:01:33 -0500146 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700152 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Michael Jurka8b805b12012-04-18 14:23:14 -0700154 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700155 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700156
Mathew Inwood876a8462013-06-14 14:12:41 +0100157 /**
158 * IntentStarter uses request codes starting with this. This must be greater than all activity
159 * request codes used internally.
160 */
161 protected static final int REQUEST_LAST = 100;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
164
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800165 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Michael Jurka0a457bf2012-11-19 14:05:05 -0800167 // To turn on these properties, type
168 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Winson Chung2672ff92012-05-04 16:22:30 -0700171 // The Intent extra that defines whether to ignore the launch animation
172 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400173 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
176 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700177 // Type: int
178 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700180 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
181 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
183 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700184 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700186 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: boolean
188 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
189 // Type: long
190 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700191 // Type: int
192 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
193 // Type: int
194 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
195 // Type: parcelable
196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000197 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800198 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000199 // Type: int[]
200 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Adam Cohen432609a2014-03-13 17:03:22 -0700202 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800203 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
204
Adam Cohen3ed316a2014-07-23 18:21:20 -0700205 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
206 static final String ACTION_FIRST_LOAD_COMPLETE =
207 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
208
Adam Cohen39a06042013-07-19 14:30:12 -0700209 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700210 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700211
Sunny Goyal594d76d2014-11-06 10:12:54 -0800212 private static final String QSB_WIDGET_ID = "qsb_widget_id";
213 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
214
Winson Chunga6945242014-01-08 14:04:34 -0800215 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
216
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700217 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700218 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700219 private State mState = State.WORKSPACE;
220 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700221
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700222 private boolean mIsSafeModeEnabled;
223
Adam Cohenc2d6e892014-10-16 09:49:24 -0700224 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
225 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700226 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700227
Joe Onorato9c1289c2009-08-17 11:03:03 -0400228 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700229 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
230 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700231 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000233 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
234 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
235
Winson Chunga2413752012-04-03 14:22:34 -0700236 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700237 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
238 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700239 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700240 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700241
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800242 private final BroadcastReceiver mCloseSystemDialogsReceiver
243 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800244 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private LayoutInflater mInflater;
247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700249 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800250 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800251 private DragLayer mDragLayer;
252 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700253 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700254
Sunny Goyalffe83f12014-08-14 17:39:34 -0700255 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700256 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700257
Michael Jurkac9d95c52011-08-29 14:03:34 -0700258 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800259 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800260 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700261
Michael Jurka0280c3b2010-09-17 15:00:07 -0700262 private int[] mTmpAddItemCellCoordinates = new int[2];
263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 private FolderInfo mFolderInfo;
265
Winson Chung3d503fb2011-07-13 17:25:49 -0700266 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800267 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700268
Michael Jurka838a4ca2011-02-07 13:33:06 -0800269 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700270
Winson Chungc51db6a2011-10-05 11:44:49 -0700271 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700272 private AppsCustomizeTabHost mAppsCustomizeTabHost;
273 private AppsCustomizePagedView mAppsCustomizeContent;
274 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800275 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700278 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
279 // scroll issues (because the workspace may not have been measured yet) and extra work.
280 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
281 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282
283 private SpannableStringBuilder mDefaultKeySsb = null;
284
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285 private boolean mWorkspaceLoading = true;
286
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800287 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 private boolean mRestoring;
289 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700290 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291
Michael Jurka1e2f4652013-07-08 18:03:46 -0700292 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700293 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 private Bundle mSavedInstanceState;
296
Joe Onorato9c1289c2009-08-17 11:03:03 -0400297 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800298 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800299 private boolean mUserPresent = true;
300 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700301 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800302 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400303
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700304 private static LocaleConfiguration sLocaleConfiguration = null;
305
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700306 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700307
Adam Cohen61f560d2013-09-30 15:58:20 -0700308 private View.OnTouchListener mHapticFeedbackTouchListener;
309
Adam Cohen80e6beb2015-02-13 16:14:33 -0800310 public static final int BUILD_LAYER = 0;
311 public static final int BUILD_AND_SET_LAYER = 1;
312
Adam Cohended9f8d2010-11-03 13:25:16 -0700313 // Related to the auto-advancing of widgets
314 private final int ADVANCE_MSG = 1;
315 private final int mAdvanceInterval = 20000;
316 private final int mAdvanceStagger = 250;
317 private long mAutoAdvanceSentTime;
318 private long mAutoAdvanceTimeLeft = -1;
319 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
320 new HashMap<View, AppWidgetProviderInfo>();
321
Winson Chung400438b2011-01-16 17:53:48 -0800322 // Determines how long to wait after a rotation before restoring the screen orientation to
323 // match the sensor state.
324 private final int mRestoreScreenOrientationDelay = 500;
325
Craig Mautner360310b2012-10-26 15:13:08 -0700326 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700327
Adam Cohen1462de32012-07-24 22:34:36 -0700328 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700329 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700330
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700331 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700332 static Date sDateStamp = new Date();
333 static DateFormat sDateFormat =
334 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
335 static long sRunStart = System.currentTimeMillis();
336 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700337
Winson Chung46353de2012-02-16 14:05:10 -0800338 // We only want to get the SharedPreferences once since it does an FS stat each time we get
339 // it from the context.
340 private SharedPreferences mSharedPrefs;
341
Winson Chungf0c6ae02012-03-21 16:10:31 -0700342 // Holds the page that we need to animate to, and the icon views that we need to animate up
343 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700344 private ImageView mFolderIconImageView;
345 private Bitmap mFolderIconBitmap;
346 private Canvas mFolderIconCanvas;
347 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700348
Michael Jurkaddd62e92011-02-16 17:49:14 -0800349 private BubbleTextView mWaitingForResume;
350
Adam Cohen59400422014-03-05 18:07:04 -0800351 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
352 new HashMap<String, CustomAppWidget>();
353
354 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
355 static {
356 if (ENABLE_CUSTOM_WIDGET_TEST) {
357 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
358 }
359 }
360
Chet Haasea8f996d2015-02-17 12:56:04 -0800361 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
362 private static Method sClipRevealMethod = null;
363 static {
364 Class<?> activityOptionsClass = ActivityOptions.class;
365 try {
366 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
367 View.class, int.class, int.class, int.class, int.class);
368 } catch (Exception e) {
369 // Earlier version
370 }
371 }
372
Michael Jurkac1f5d262011-09-30 19:32:27 -0700373 private Runnable mBuildLayersRunnable = new Runnable() {
374 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700375 if (mWorkspace != null) {
376 mWorkspace.buildPageHardwareLayers();
377 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700378 }
379 };
380
Adam Cohendb364c32014-05-20 14:23:40 -0700381 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800382
383 private static class PendingAddArguments {
384 int requestCode;
385 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700386 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700387 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800388 int cellX;
389 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700390 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800391 }
392
Daniel Sandlerff02d492013-08-05 02:12:05 -0400393 private Stats mStats;
394
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700395 FocusIndicatorView mFocusHandler;
396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 @Override
398 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700399 if (DEBUG_STRICT_MODE) {
400 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
401 .detectDiskReads()
402 .detectDiskWrites()
403 .detectNetwork() // or .detectAll() for all detectable problems
404 .penaltyLog()
405 .build());
406 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
407 .detectLeakedSqlLiteObjects()
408 .detectLeakedClosableObjects()
409 .penaltyLog()
410 .penaltyDeath()
411 .build());
412 }
413
Adam Cohen9211d422014-10-07 18:14:53 -0700414 if (mLauncherCallbacks != null) {
415 mLauncherCallbacks.preOnCreate();
416 }
417
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400419
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400420 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400421 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700422 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700423
Winson Chung5f8afe62013-08-12 16:19:28 -0700424 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700425 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700426
427 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400428 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700429 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700430 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800431 mModel = app.setLauncher(this);
432 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700433 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400434 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700436
Daniel Sandlerff02d492013-08-05 02:12:05 -0400437 mStats = new Stats(this);
438
Sunny Goyalffe83f12014-08-14 17:39:34 -0700439 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700440
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700441 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
442 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700443
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700444 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
445 // this also ensures that any synchronous binding below doesn't re-trigger another
446 // LauncherModel load.
447 mPaused = false;
448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200450 android.os.Debug.startMethodTracing(
451 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 }
453
454 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100458 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800460 registerContentObservers();
461
Joe Onorato7c312c12009-08-13 21:36:53 -0700462 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700463
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 mSavedState = savedInstanceState;
465 restoreState(mSavedState);
466
467 if (PROFILE_STARTUP) {
468 android.os.Debug.stopMethodTracing();
469 }
470
471 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700472 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700473 // If the user leaves launcher, then we should just load items asynchronously when
474 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500475 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700476 } else {
477 // We only load the page synchronously if the user rotates (or triggers a
478 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800479 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700480 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 }
482
483 // For handling default keys
484 mDefaultKeySsb = new SpannableStringBuilder();
485 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800486
487 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
488 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800489
Adam Cohenf9426d52012-06-04 17:26:21 -0700490 // On large interfaces, we want the screen to auto-rotate based on the current orientation
491 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700492
Adam Cohen9211d422014-10-07 18:14:53 -0700493 if (mLauncherCallbacks != null) {
494 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700495 if (mLauncherCallbacks.hasLauncherOverlay()) {
496 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700497 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
498 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
499 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700500 mWorkspace.setLauncherOverlay(mLauncherOverlay);
501 }
Adam Cohen9211d422014-10-07 18:14:53 -0700502 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700503
504 if (shouldShowIntroScreen()) {
505 showIntroScreen();
506 } else {
507 showFirstRunActivity();
508 showFirstRunClings();
509 }
Adam Cohen9211d422014-10-07 18:14:53 -0700510 }
511
512 private LauncherCallbacks mLauncherCallbacks;
513
514 public void onPostCreate(Bundle savedInstanceState) {
515 super.onPostCreate(savedInstanceState);
516 if (mLauncherCallbacks != null) {
517 mLauncherCallbacks.onPostCreate(savedInstanceState);
518 }
519 }
520
521 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
522 mLauncherCallbacks = callbacks;
523 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700524 }
525
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700526 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700527 public void onLauncherProviderChange() {
528 if (mLauncherCallbacks != null) {
529 mLauncherCallbacks.onLauncherProviderChange();
530 }
531 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700532
Adam Cohen9211d422014-10-07 18:14:53 -0700533 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700534 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700535 if (mLauncherCallbacks != null) {
536 return mLauncherCallbacks.hasCustomContentToLeft();
537 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700538 return false;
539 }
540
Dave Hawkeya8881582013-09-17 15:55:33 -0600541 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500542 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600543 * {@link #addToCustomContentPage}. This will only be invoked if
544 * {@link #hasCustomContentToLeft()} is {@code true}.
545 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500546 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700547 if (mLauncherCallbacks != null) {
548 mLauncherCallbacks.populateCustomContentContainer();
549 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600550 }
551
552 /**
553 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
554 * ensure the custom content page is added or removed if necessary.
555 */
556 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600557 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600558 // Not bound yet, wait for bindScreens to be called.
559 return;
560 }
561
562 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
563 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500564 mWorkspace.createCustomContentContainer();
565 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600566 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
567 mWorkspace.removeCustomContentPage();
568 }
569 }
570
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700572 if (sLocaleConfiguration == null) {
573 new AsyncTask<Void, Void, LocaleConfiguration>() {
574 @Override
575 protected LocaleConfiguration doInBackground(Void... unused) {
576 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
577 readConfiguration(Launcher.this, localeConfiguration);
578 return localeConfiguration;
579 }
580
581 @Override
582 protected void onPostExecute(LocaleConfiguration result) {
583 sLocaleConfiguration = result;
584 checkForLocaleChange(); // recursive, but now with a locale configuration
585 }
586 }.execute();
587 return;
588 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700589
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 final Configuration configuration = getResources().getConfiguration();
591
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700592 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 final String locale = configuration.locale.toString();
594
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700595 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 final int mcc = configuration.mcc;
597
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700598 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 final int mnc = configuration.mnc;
600
Romain Guy84f296c2009-11-04 15:00:44 -0800601 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602
Romain Guy84f296c2009-11-04 15:00:44 -0800603 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 sLocaleConfiguration.locale = locale;
605 sLocaleConfiguration.mcc = mcc;
606 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700607
Joe Onorato0589f0f2010-02-08 13:44:00 -0800608 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700609
610 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100611 new AsyncTask<Void, Void, Void>() {
612 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700613 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100614 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700615 }
Michael Jurka43467462013-11-14 12:03:58 +0100616 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700617 }
618 }
619
620 private static class LocaleConfiguration {
621 public String locale;
622 public int mcc = -1;
623 public int mnc = -1;
624 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700625
Romain Guy98d01652009-06-30 16:21:04 -0700626 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
627 DataInputStream in = null;
628 try {
Helena Josol28db2802014-10-09 17:04:09 +0100629 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700630 configuration.locale = in.readUTF();
631 configuration.mcc = in.readInt();
632 configuration.mnc = in.readInt();
633 } catch (FileNotFoundException e) {
634 // Ignore
635 } catch (IOException e) {
636 // Ignore
637 } finally {
638 if (in != null) {
639 try {
640 in.close();
641 } catch (IOException e) {
642 // Ignore
643 }
644 }
645 }
646 }
647
648 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
649 DataOutputStream out = null;
650 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100651 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100652 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700653 out.writeUTF(configuration.locale);
654 out.writeInt(configuration.mcc);
655 out.writeInt(configuration.mnc);
656 out.flush();
657 } catch (FileNotFoundException e) {
658 // Ignore
659 } catch (IOException e) {
660 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100661 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700662 } finally {
663 if (out != null) {
664 try {
665 out.close();
666 } catch (IOException e) {
667 // Ignore
668 }
669 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
671 }
672
Anton Hanssona2f665f2013-09-26 12:18:32 +0100673 public Stats getStats() {
674 return mStats;
675 }
676
Bjorn Bringertc459e522013-06-07 19:36:01 +0100677 public LayoutInflater getInflater() {
678 return mInflater;
679 }
680
Winson Chung36a62fe2012-05-06 18:04:42 -0700681 boolean isDraggingEnabled() {
682 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
683 // that is subsequently removed from the workspace in startBinding().
684 return !mModel.isLoadingWorkspace();
685 }
686
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800687 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000688 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100689 if (Build.VERSION.SDK_INT >= 17) {
690 return View.generateViewId();
691 } else {
692 // View.generateViewId() is not available. The following fallback logic is a copy
693 // of its implementation.
694 for (;;) {
695 final int result = sNextGeneratedId.get();
696 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
697 int newValue = result + 1;
698 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
699 if (sNextGeneratedId.compareAndSet(result, newValue)) {
700 return result;
701 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000702 }
703 }
704 }
705
706 public int getViewIdForItem(ItemInfo info) {
707 // This cast is safe given the > 2B range for int.
708 int itemId = (int) info.id;
709 if (mItemIdToViewId.containsKey(itemId)) {
710 return mItemIdToViewId.get(itemId);
711 }
712 int viewId = generateViewId();
713 mItemIdToViewId.put(itemId, viewId);
714 return viewId;
715 }
716
717 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700718 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
719 * a configuration step, this allows the proper animations to run after other transitions.
720 */
Adam Cohendb364c32014-05-20 14:23:40 -0700721 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700722 long screenId = args.screenId;
723 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
724 // When the screen id represents an actual screen (as opposed to a rank) we make sure
725 // that the drop page actually exists.
726 screenId = ensurePendingDropLayoutExists(args.screenId);
727 }
Adam Cohendb364c32014-05-20 14:23:40 -0700728
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800729 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800730 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700731 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700733 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800734 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700735 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700736 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700737 case REQUEST_RECONFIGURE_APPWIDGET:
738 completeRestoreAppWidget(args.appWidgetId);
739 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800740 }
Michael Jurka27614d22012-04-02 06:40:22 -0700741 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
742 // if you turned the screen off and then back while in All Apps, Launcher would not
743 // return to the workspace. Clearing mAddInfo.container here fixes this issue
744 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700745 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800746 }
747
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800748 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700749 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700750 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700751 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700752 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800753 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700754
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 Runnable exitSpringLoaded = new Runnable() {
756 @Override
757 public void run() {
758 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
759 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
760 }
761 };
762
Michael Jurka8b805b12012-04-18 14:23:14 -0700763 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
766 if (resultCode == RESULT_CANCELED) {
767 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700768 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700770 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800771 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700772 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 }
774 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200775 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
776 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
777 mWorkspace.exitOverviewMode(false);
778 }
779 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700780 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200781
Adam Cohened66b2b2012-01-23 17:28:51 -0800782 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
783 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700784
Adam Cohendb364c32014-05-20 14:23:40 -0700785 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800786 // We have special handling for widgets
787 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800788 final int appWidgetId;
789 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
790 : -1;
791 if (widgetId < 0) {
792 appWidgetId = pendingAddWidgetId;
793 } else {
794 appWidgetId = widgetId;
795 }
796
Adam Cohenad4e15c2013-10-17 16:21:35 -0700797 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800798 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700799 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
800 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700801 result = RESULT_CANCELED;
802 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700803 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700804 @Override
805 public void run() {
806 exitSpringLoadedDragModeDelayed(false, 0, null);
807 }
808 };
Adam Cohendb364c32014-05-20 14:23:40 -0700809 if (workspaceLocked) {
810 // No need to remove the empty screen if we're mid-binding, as the
811 // the bind will not add the empty screen.
812 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
813 } else {
814 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
815 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
816 }
Winson Chung5aaab772012-04-27 15:24:02 -0700817 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700818 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700819 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
820 // When the screen id represents an actual screen (as opposed to a rank)
821 // we make sure that the drop page actually exists.
822 mPendingAddInfo.screenId =
823 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
824 }
Adam Cohendb364c32014-05-20 14:23:40 -0700825 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
826
827 dropLayout.setDropPending(true);
828 final Runnable onComplete = new Runnable() {
829 @Override
830 public void run() {
831 completeTwoStageWidgetDrop(resultCode, appWidgetId);
832 dropLayout.setDropPending(false);
833 }
834 };
835 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
836 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
837 } else {
838 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
839 mPendingAddInfo);
840 sPendingAddItem = args;
841 }
Winson Chung5aaab772012-04-27 15:24:02 -0700842 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800843 return;
844 }
845
Sunny Goyalff572272014-07-23 13:58:07 -0700846 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
847 if (resultCode == RESULT_OK) {
848 // Update the widget view.
849 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
850 pendingAddWidgetId, mPendingAddInfo);
851 if (workspaceLocked) {
852 sPendingAddItem = args;
853 } else {
854 completeAdd(args);
855 }
856 }
857 // Leave the widget in the pending state if the user canceled the configure.
858 return;
859 }
860
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 // The pattern used here is that a user PICKs a specific application,
862 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700863
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
865 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700866 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700867 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
868 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800869 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700870 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800871 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700872 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700873 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
874 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 }
Adam Cohen00074722013-10-11 17:46:09 -0700876 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700877 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700878 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800881
882 }
883
884 @Override
885 protected void onActivityResult(
886 final int requestCode, final int resultCode, final Intent data) {
887 handleActivityResult(requestCode, resultCode, data);
888 if (mLauncherCallbacks != null) {
889 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
890 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 }
892
Adam Cohendb364c32014-05-20 14:23:40 -0700893 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
894 appWidgetId, ItemInfo info) {
895 PendingAddArguments args = new PendingAddArguments();
896 args.requestCode = requestCode;
897 args.intent = data;
898 args.container = info.container;
899 args.screenId = info.screenId;
900 args.cellX = info.cellX;
901 args.cellY = info.cellY;
902 args.appWidgetId = appWidgetId;
903 return args;
904 }
905
906 /**
907 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
908 *
909 * @param screenId the screen id to check
910 * @return the new screen, or screenId if it exists
911 */
912 private long ensurePendingDropLayoutExists(long screenId) {
913 CellLayout dropLayout =
914 (CellLayout) mWorkspace.getScreenWithId(screenId);
915 if (dropLayout == null) {
916 // it's possible that the add screen was removed because it was
917 // empty and a re-bind occurred
918 mWorkspace.addExtraEmptyScreen();
919 return mWorkspace.commitExtraEmptyScreen();
920 } else {
921 return screenId;
922 }
923 }
924
Adam Cohened66b2b2012-01-23 17:28:51 -0800925 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700926 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700927 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 Runnable onCompleteRunnable = null;
929 int animationType = 0;
930
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700931 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 if (resultCode == RESULT_OK) {
933 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
934 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 mPendingAddWidgetInfo);
936 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 onCompleteRunnable = new Runnable() {
938 @Override
939 public void run() {
940 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700941 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700942 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
943 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800944 }
945 };
946 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800947 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800948 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800949 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700950 if (mDragLayer.getAnimatedView() != null) {
951 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
952 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
953 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700954 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700955 // The animated view may be null in the case of a rotation during widget configuration
956 onCompleteRunnable.run();
957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 }
959
960 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700961 protected void onStop() {
962 super.onStop();
963 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700964
965 if (mLauncherCallbacks != null) {
966 mLauncherCallbacks.onStop();
967 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700968 }
969
970 @Override
971 protected void onStart() {
972 super.onStart();
973 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700974
975 if (mLauncherCallbacks != null) {
976 mLauncherCallbacks.onStart();
977 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700978 }
979
980 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200982 long startTime = 0;
983 if (DEBUG_RESUME_TIME) {
984 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400985 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200986 }
Adam Cohen9211d422014-10-07 18:14:53 -0700987
988 if (mLauncherCallbacks != null) {
989 mLauncherCallbacks.preOnResume();
990 }
991
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700993
Winson Chung4a2afa32012-07-19 14:53:05 -0700994 // Restore the previous launcher state
995 if (mOnResumeState == State.WORKSPACE) {
996 showWorkspace(false);
997 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800998 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700999 }
1000 mOnResumeState = State.NONE;
1001
Craig Mautner360310b2012-10-26 15:13:08 -07001002 // Background was set to gradient in onPause(), restore to black if in all apps.
1003 setWorkspaceBackground(mState == State.WORKSPACE);
1004
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001005 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001006 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001007 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001008 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001009 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001010 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001012 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001013 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1014 // execute them here
1015 long startTimeCallbacks = 0;
1016 if (DEBUG_RESUME_TIME) {
1017 startTimeCallbacks = System.currentTimeMillis();
1018 }
1019
1020 if (mAppsCustomizeContent != null) {
1021 mAppsCustomizeContent.setBulkBind(true);
1022 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001023 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1024 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001025 }
1026 if (mAppsCustomizeContent != null) {
1027 mAppsCustomizeContent.setBulkBind(false);
1028 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001029 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001030 if (DEBUG_RESUME_TIME) {
1031 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1032 (System.currentTimeMillis() - startTimeCallbacks));
1033 }
Michael Jurka447bf842013-05-15 14:52:15 +02001034 }
Michael Jurka54554252013-08-01 12:52:23 +02001035 if (mOnResumeCallbacks.size() > 0) {
1036 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1037 mOnResumeCallbacks.get(i).run();
1038 }
1039 mOnResumeCallbacks.clear();
1040 }
Winson Chunge4e50662012-01-23 14:45:13 -08001041
1042 // Reset the pressed state of icons that were locked in the press state while activities
1043 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001044 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001045 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001046 mWaitingForResume.setStayPressed(false);
1047 }
Winson Chung82963d52013-10-09 11:20:57 -07001048
Adam Cohen06dff352012-06-01 17:17:08 -07001049 // It is possible that widgets can receive updates while launcher is not in the foreground.
1050 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1051 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1052 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001053 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001054
Winson Chung780fe592013-09-26 14:48:44 -07001055 // Process any items that were added while Launcher was away.
1056 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1057
Michael Jurka447bf842013-05-15 14:52:15 +02001058 if (DEBUG_RESUME_TIME) {
1059 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1060 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001061
1062 if (mWorkspace.getCustomContentCallbacks() != null) {
1063 // If we are resuming and the custom content is the current page, we call onShow().
1064 // It is also poassible that onShow will instead be called slightly after first layout
1065 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1066 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001067 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001068 }
1069 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001070 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001071 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001072
1073 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001074
1075 if (mLauncherCallbacks != null) {
1076 mLauncherCallbacks.onResume();
1077 }
Adam Cohen06dff352012-06-01 17:17:08 -07001078 }
1079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001081 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001082 // Ensure that items added to Launcher are queued until Launcher returns
1083 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001084 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001085
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001086 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001087 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001088 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001089 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001090
1091 // We call onHide() aggressively. The custom content callbacks should be able to
1092 // debounce excess onHide calls.
1093 if (mWorkspace.getCustomContentCallbacks() != null) {
1094 mWorkspace.getCustomContentCallbacks().onHide();
1095 }
Romain Guycbb89e42009-06-08 15:52:54 -07001096
Adam Cohen9211d422014-10-07 18:14:53 -07001097 if (mLauncherCallbacks != null) {
1098 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001099 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001100 }
1101
1102 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001103 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1104 // by a onResume or by scrolling otherwise.
1105 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001106
1107 // Custom content is completely hidden
1108 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001109
1110 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1111 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001112
1113 // Indicates whether the user is allowed to scroll away from the custom content.
1114 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001115 }
1116
Adam Cohenc2d6e892014-10-16 09:49:24 -07001117 public interface LauncherOverlay {
1118
1119 /**
1120 * Touch interaction leading to overscroll has begun
1121 */
1122 public void onScrollInteractionBegin();
1123
1124 /**
1125 * Touch interaction related to overscroll has ended
1126 */
1127 public void onScrollInteractionEnd();
1128
1129 /**
1130 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1131 * screen (or in the case of RTL, the rightmost screen).
1132 */
1133 public void onScrollChange(int progress, boolean rtl);
1134
1135 /**
1136 * Screen has stopped scrolling
1137 */
1138 public void onScrollSettled();
1139
1140 /**
1141 * This method can be called by the Launcher in order to force the LauncherOverlay
1142 * to exit fully immersive mode.
1143 */
1144 public void forceExitFullImmersion();
1145 }
1146
1147 public interface LauncherOverlayCallbacks {
1148 /**
1149 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1150 * however it doesn't modify any state within the launcher.
1151 */
1152 public boolean canEnterFullImmersion();
1153
1154 /**
1155 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1156 * eg. by occupying the full screen and handling all touch events.
1157 *
1158 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1159 * case, Launcher will modify any necessary state and assumes the overlay is
1160 * handling all interaction. If false, the LauncherOverlay should cancel any
1161 *
1162 */
1163 public boolean enterFullImmersion();
1164
1165 /**
1166 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1167 * full control over UI and state.
1168 */
1169 public void exitFullImmersion();
1170 }
1171
1172 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1173
1174 @Override
1175 public boolean canEnterFullImmersion() {
1176 return mState == State.WORKSPACE;
1177 }
1178
1179 @Override
1180 public boolean enterFullImmersion() {
1181 if (mState == State.WORKSPACE) {
1182 // When fully immersed, disregard any touches which fall through.
1183 mDragLayer.setBlockTouch(true);
1184 return true;
1185 }
1186 return false;
1187 }
1188
1189 @Override
1190 public void exitFullImmersion() {
1191 mDragLayer.setBlockTouch(false);
1192 }
1193 }
1194
Jorim Jaggid017f882014-01-14 17:08:48 -08001195 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001196 if (mLauncherCallbacks != null) {
1197 return mLauncherCallbacks.hasSettings();
1198 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001199 return false;
1200 }
1201
Adam Cohenbffe7452013-07-22 18:21:45 -07001202
Adam Cohen9211d422014-10-07 18:14:53 -07001203 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001204 CustomContentCallbacks callbacks, String description) {
1205 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001206 }
1207
Adam Cohenedb40762013-07-18 16:45:45 -07001208 // The custom content needs to offset its content to account for the QSB
1209 public int getTopOffsetForCustomContent() {
1210 return mWorkspace.getPaddingTop();
1211 }
1212
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001213 @Override
1214 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001215 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001216 if (mModel.isCurrentCallbacks(this)) {
1217 mModel.stopLoader();
1218 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001219 if (mAppsCustomizeContent != null) {
1220 mAppsCustomizeContent.surrender();
1221 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001222 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001223 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001224
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001225 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001226 @Override
1227 public void onWindowFocusChanged(boolean hasFocus) {
1228 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001229 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001230
1231 if (mLauncherCallbacks != null) {
1232 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1233 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001234 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 private boolean acceptFilter() {
1237 final InputMethodManager inputManager = (InputMethodManager)
1238 getSystemService(Context.INPUT_METHOD_SERVICE);
1239 return !inputManager.isFullscreenMode();
1240 }
1241
1242 @Override
1243 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001244 final int uniChar = event.getUnicodeChar();
1245 final boolean handled = super.onKeyDown(keyCode, event);
1246 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1247 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1249 keyCode, event);
1250 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001251 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001252 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001253 // showSearchDialog()
1254 // If there are multiple keystrokes before the search dialog takes focus,
1255 // onSearchRequested() will be called for every keystroke,
1256 // but it is idempotent, so it's fine.
1257 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 }
1259 }
1260
Joe Onorato8a9625e2010-01-28 15:55:35 -08001261 // Eat the long press event so the keyboard doesn't come up.
1262 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1263 return true;
1264 }
1265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 return handled;
1267 }
1268
Karl Rosaen138a0412009-04-23 19:00:21 -07001269 private String getTypedText() {
1270 return mDefaultKeySsb.toString();
1271 }
1272
1273 private void clearTypedText() {
1274 mDefaultKeySsb.clear();
1275 mDefaultKeySsb.clearSpans();
1276 Selection.setSelection(mDefaultKeySsb, 0);
1277 }
1278
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001280 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1281 * State
1282 */
1283 private static State intToState(int stateOrdinal) {
1284 State state = State.WORKSPACE;
1285 final State[] stateValues = State.values();
1286 for (int i = 0; i < stateValues.length; i++) {
1287 if (stateValues[i].ordinal() == stateOrdinal) {
1288 state = stateValues[i];
1289 break;
1290 }
1291 }
1292 return state;
1293 }
1294
1295 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 * Restores the previous state, if it exists.
1297 *
1298 * @param savedState The previous state.
1299 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001300 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 private void restoreState(Bundle savedState) {
1302 if (savedState == null) {
1303 return;
1304 }
1305
Michael Jurka883f55b2010-10-21 15:47:14 -07001306 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001307 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001308 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001309 }
1310
Adam Cohen21cd0022013-10-09 18:57:02 -07001311 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1312 PagedView.INVALID_RESTORE_PAGE);
1313 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001314 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 }
1316
Winson Chung3d503fb2011-07-13 17:25:49 -07001317 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001318 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001319
Winson Chung3d503fb2011-07-13 17:25:49 -07001320 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1321 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001322 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001323 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1324 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001325 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1326 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1327 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001328 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001329 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 mRestoring = true;
1331 }
1332
1333 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1334 if (renameFolder) {
1335 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001336 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 mRestoring = true;
1338 }
Winson Chunga12a2502010-12-20 14:41:35 -08001339
Winson Chung785d2eb2011-04-14 16:08:02 -07001340 // Restore the AppsCustomize tab
1341 if (mAppsCustomizeTabHost != null) {
1342 String curTab = savedState.getString("apps_customize_currentTab");
1343 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001344 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001345 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001346 mAppsCustomizeContent.loadAssociatedPages(
1347 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001348 }
1349
Winson Chung5afbf7b2011-07-25 11:53:08 -07001350 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1351 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001352 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001353 mItemIdToViewId = (HashMap<Integer, Integer>)
1354 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 }
1356
1357 /**
1358 * Finds all the views we need and configure them properly.
1359 */
1360 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001361 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001362
Craig Mautner360310b2012-10-26 15:13:08 -07001363 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001364 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001365 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1366 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001367 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001368 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001369
John Spurlock77e1f472013-09-11 10:09:51 -04001370 mLauncherView.setSystemUiVisibility(
1371 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001372 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373
Winson Chung4c98d922011-05-31 16:50:48 -07001374 // Setup the drag layer
1375 mDragLayer.setup(this, dragController);
1376
Winson Chung3d503fb2011-07-13 17:25:49 -07001377 // Setup the hotseat
1378 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1379 if (mHotseat != null) {
1380 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001381 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001382 }
1383
Jorim Jaggid017f882014-01-14 17:08:48 -08001384 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001385 View widgetButton = findViewById(R.id.widget_button);
1386 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001387 @Override
1388 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001389 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001390 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001391 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001392 }
1393 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001394 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1395
1396 View wallpaperButton = findViewById(R.id.wallpaper_button);
1397 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001398 @Override
1399 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001400 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001401 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001402 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001403 }
1404 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001405 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1406
1407 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001408 if (hasSettings()) {
1409 settingsButton.setOnClickListener(new OnClickListener() {
1410 @Override
1411 public void onClick(View arg0) {
1412 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001413 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001414 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001415 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001416 });
1417 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1418 } else {
1419 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001420 }
1421
Adam Cohendbdff6b2013-09-18 19:09:15 -07001422 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001423
Winson Chung4c98d922011-05-31 16:50:48 -07001424 // Setup the workspace
1425 mWorkspace.setHapticFeedbackEnabled(false);
1426 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001427 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001428 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001429
Winson Chungf0ea4d32011-06-06 14:27:16 -07001430 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001431 mSearchDropTargetBar = (SearchDropTargetBar)
1432 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001433
Winson Chungf0ea4d32011-06-06 14:27:16 -07001434 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001435 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001436 mAppsCustomizeContent = (AppsCustomizePagedView)
1437 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1438 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001439
Winson Chung3d503fb2011-07-13 17:25:49 -07001440 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001441 dragController.setDragScoller(mWorkspace);
1442 dragController.setScrollView(mDragLayer);
1443 dragController.setMoveTarget(mWorkspace);
1444 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001445 if (mSearchDropTargetBar != null) {
1446 mSearchDropTargetBar.setup(this, dragController);
Sunny Goyal594d76d2014-11-06 10:12:54 -08001447 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001448 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001449
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001450 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001451 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001452 mWeightWatcher = new WeightWatcher(this);
1453 mWeightWatcher.setAlpha(0.5f);
1454 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001455 new FrameLayout.LayoutParams(
1456 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001457 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001458 Gravity.BOTTOM)
1459 );
Adam Cohen39a06042013-07-19 14:30:12 -07001460
1461 boolean show = shouldShowWeightWatcher();
1462 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 }
1465
1466 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001467 * Sets the all apps button. This method is called from {@link Hotseat}.
1468 */
1469 public void setAllAppsButton(View allAppsButton) {
1470 mAllAppsButton = allAppsButton;
1471 }
1472
1473 public View getAllAppsButton() {
1474 return mAllAppsButton;
1475 }
1476
1477 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 * Creates a view representing a shortcut.
1479 *
1480 * @param info The data structure describing the shortcut.
1481 *
1482 * @return A View inflated from R.layout.application.
1483 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001484 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001486 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 }
1488
1489 /**
1490 * Creates a view representing a shortcut inflated from the specified resource.
1491 *
1492 * @param layoutResId The id of the XML layout used to create the shortcut.
1493 * @param parent The group the shortcut belongs to.
1494 * @param info The data structure describing the shortcut.
1495 *
1496 * @return A View inflated from layoutResId.
1497 */
Adam Cohen59400422014-03-05 18:07:04 -08001498 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001499 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001500 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001502 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 return favorite;
1504 }
1505
1506 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 * Add a shortcut to the workspace.
1508 *
1509 * @param data The intent describing the shortcut.
1510 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001512 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001513 int cellY) {
1514 int[] cellXY = mTmpAddItemCellCoordinates;
1515 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001516 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001517
Michael Jurkad3ef3062010-11-23 16:23:58 -08001518 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001519
Sunny Goyal2350bc92014-10-14 16:42:54 -07001520 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001521 if (info == null) {
1522 return;
1523 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001524 final View view = createShortcut(info);
1525
Adam Cohenfbba09b2011-07-18 21:43:05 -07001526 // First we check if we already know the exact location where we want to add this item.
1527 if (cellX >= 0 && cellY >= 0) {
1528 cellXY[0] = cellX;
1529 cellXY[1] = cellY;
1530 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001531
1532 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001533 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001534 true, null,null)) {
1535 return;
1536 }
1537 DragObject dragObject = new DragObject();
1538 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001539 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1540 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001541 return;
1542 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001543 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001544 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001545 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001546 foundCellSpan = (result != null);
1547 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001548 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001549 }
1550
1551 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001552 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001553 return;
1554 }
1555
Adam Cohendcd297f2013-06-18 13:13:40 -07001556 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557
1558 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001559 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001560 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
1562 }
1563
Adam Cohen2f093b62012-04-30 18:59:53 -07001564 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1565 int minHeight) {
1566 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001567 // We want to account for the extra amount of padding that we are adding to the widget
1568 // to ensure that it gets the full amount of space that it has requested
1569 int requiredWidth = minWidth + padding.left + padding.right;
1570 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001571 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001572 }
1573
Adam Cohen2f093b62012-04-30 18:59:53 -07001574 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1575 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001576 }
1577
Adam Cohen2f093b62012-04-30 18:59:53 -07001578 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1579 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001580 }
1581
Adam Cohen2f093b62012-04-30 18:59:53 -07001582 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1583 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001584 }
1585
Adam Cohen2f093b62012-04-30 18:59:53 -07001586 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1587 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001588 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001589 }
1590
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001592 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001594 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 */
Adam Cohen59400422014-03-05 18:07:04 -08001596 private void completeAddAppWidget(int appWidgetId, long container, long screenId,
1597 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1598
1599 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001600 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001601 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1602 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001603 }
Romain Guycbb89e42009-06-08 15:52:54 -07001604
Adam Cohen59400422014-03-05 18:07:04 -08001605 if (appWidgetInfo.isCustomWidget) {
1606 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001607 }
1608
Adam Cohen59400422014-03-05 18:07:04 -08001609 LauncherAppWidgetInfo launcherInfo;
1610 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1611 launcherInfo.spanX = info.spanX;
1612 launcherInfo.spanY = info.spanY;
1613 launcherInfo.minSpanX = info.minSpanX;
1614 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001615 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001616
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001618 container, screenId, info.cellX, info.cellY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619
1620 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001621 if (hostView == null) {
1622 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001623 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1624 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001625 } else {
1626 // The AppWidgetHostView has already been inflated and instantiated
1627 launcherInfo.hostView = hostView;
1628 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001630 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001631 launcherInfo.notifyWidgetSizeChanged(this);
1632
Adam Cohen59400422014-03-05 18:07:04 -08001633 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1634 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001635
1636 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001638 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 }
Romain Guycbb89e42009-06-08 15:52:54 -07001640
Adam Cohended9f8d2010-11-03 13:25:16 -07001641 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1642 @Override
1643 public void onReceive(Context context, Intent intent) {
1644 final String action = intent.getAction();
1645 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1646 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001647 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001648 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001649
Winson Chungc100e8e2011-08-09 16:02:43 -07001650 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001651 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001652 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001653 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001654 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001655 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1656 mUserPresent = true;
1657 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001658 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1659 mModel.resetLoadedState(false, true);
1660 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1661 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1662 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1663 mModel.resetLoadedState(false, true);
1664 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1665 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1666 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001667 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1668 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1669 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001670 }
1671 }
1672 };
1673
1674 @Override
1675 public void onAttachedToWindow() {
1676 super.onAttachedToWindow();
1677
1678 // Listen for broadcasts related to user-presence
1679 final IntentFilter filter = new IntentFilter();
1680 filter.addAction(Intent.ACTION_SCREEN_OFF);
1681 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001682 // For handling managed profiles
1683 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1684 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001685 if (ENABLE_DEBUG_INTENTS) {
1686 filter.addAction(DebugIntents.DELETE_DATABASE);
1687 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1688 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001690 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001691 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001692 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001693 mVisible = true;
1694 }
1695
Adam Cohen0b395352014-06-09 22:54:36 +00001696 /**
1697 * Sets up transparent navigation and status bars in LMP.
1698 * This method is a no-op for other platform versions.
1699 */
Sunny Goyald0091012015-01-20 15:55:34 -08001700 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001701 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001702 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001703 Window window = getWindow();
1704 window.getAttributes().systemUiVisibility |=
1705 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1706 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1707 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1708 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1709 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1710 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1711 window.setStatusBarColor(Color.TRANSPARENT);
1712 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001713 }
1714 }
1715
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 @Override
1717 public void onDetachedFromWindow() {
1718 super.onDetachedFromWindow();
1719 mVisible = false;
1720
Adam Cohend113e0c2010-11-11 10:48:05 -08001721 if (mAttached) {
1722 unregisterReceiver(mReceiver);
1723 mAttached = false;
1724 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 updateRunning();
1726 }
1727
1728 public void onWindowVisibilityChanged(int visibility) {
1729 mVisible = visibility == View.VISIBLE;
1730 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001731 // The following code used to be in onResume, but it turns out onResume is called when
1732 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1733 // is a more appropriate event to handle
1734 if (mVisible) {
1735 mAppsCustomizeTabHost.onWindowVisible();
1736 if (!mWorkspaceLoading) {
1737 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001738 // We want to let Launcher draw itself at least once before we force it to build
1739 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001740 // apps is nice and speedy.
1741 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001742 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001743 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001744 if (mStarted) return;
1745 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001746 // We delay the layer building a bit in order to give
1747 // other message processing a time to run. In particular
1748 // this avoids a delay in hiding the IME if it was
1749 // currently shown, because doing that may involve
1750 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001751 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001752 final ViewTreeObserver.OnDrawListener listener = this;
1753 mWorkspace.post(new Runnable() {
1754 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001755 if (mWorkspace != null &&
1756 mWorkspace.getViewTreeObserver() != null) {
1757 mWorkspace.getViewTreeObserver().
1758 removeOnDrawListener(listener);
1759 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001760 }
1761 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001762 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001763 }
1764 });
1765 }
1766 clearTypedText();
1767 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001768 }
1769
1770 private void sendAdvanceMessage(long delay) {
1771 mHandler.removeMessages(ADVANCE_MSG);
1772 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1773 mHandler.sendMessageDelayed(msg, delay);
1774 mAutoAdvanceSentTime = System.currentTimeMillis();
1775 }
1776
1777 private void updateRunning() {
1778 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1779 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1780 mAutoAdvanceRunning = autoAdvanceRunning;
1781 if (autoAdvanceRunning) {
1782 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1783 sendAdvanceMessage(delay);
1784 } else {
1785 if (!mWidgetsToAdvance.isEmpty()) {
1786 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1787 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1788 }
1789 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001790 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001791 }
1792 }
1793 }
1794
1795 private final Handler mHandler = new Handler() {
1796 @Override
1797 public void handleMessage(Message msg) {
1798 if (msg.what == ADVANCE_MSG) {
1799 int i = 0;
1800 for (View key: mWidgetsToAdvance.keySet()) {
1801 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1802 final int delay = mAdvanceStagger * i;
1803 if (v instanceof Advanceable) {
1804 postDelayed(new Runnable() {
1805 public void run() {
1806 ((Advanceable) v).advance();
1807 }
1808 }, delay);
1809 }
1810 i++;
1811 }
1812 sendAdvanceMessage(mAdvanceInterval);
1813 }
1814 }
1815 };
1816
1817 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001818 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001819 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1820 if (v instanceof Advanceable) {
1821 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001822 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001823 updateRunning();
1824 }
1825 }
1826
1827 void removeWidgetToAutoAdvance(View hostView) {
1828 if (mWidgetsToAdvance.containsKey(hostView)) {
1829 mWidgetsToAdvance.remove(hostView);
1830 updateRunning();
1831 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001832 }
1833
Joe Onorato9c1289c2009-08-17 11:03:03 -04001834 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001835 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001836 launcherInfo.hostView = null;
1837 }
1838
Winson Chung93eef082012-03-23 15:59:27 -07001839 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1840 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1841 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001842 }
1843
Adam Cohen59400422014-03-05 18:07:04 -08001844 public ArrayList<AppInfo> getAllAppsList() {
1845 return mAppsCustomizeContent.getApps();
1846 }
1847
Winson Chunga6945242014-01-08 14:04:34 -08001848 public DragLayer getDragLayer() {
1849 return mDragLayer;
1850 }
1851
1852 public Workspace getWorkspace() {
1853 return mWorkspace;
1854 }
1855
1856 public Hotseat getHotseat() {
1857 return mHotseat;
1858 }
1859
Jorim Jaggid017f882014-01-14 17:08:48 -08001860 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001861 return mOverviewPanel;
1862 }
1863
1864 public SearchDropTargetBar getSearchBar() {
1865 return mSearchDropTargetBar;
1866 }
1867
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001868 public LauncherAppWidgetHost getAppWidgetHost() {
1869 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 }
Romain Guycbb89e42009-06-08 15:52:54 -07001871
Winson Chunga9abd0e2010-10-27 17:18:37 -07001872 public LauncherModel getModel() {
1873 return mModel;
1874 }
1875
Winson Chunga6945242014-01-08 14:04:34 -08001876 protected SharedPreferences getSharedPrefs() {
1877 return mSharedPrefs;
1878 }
1879
Bjorn Bringertc459e522013-06-07 19:36:01 +01001880 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001881 getWindow().closeAllPanels();
1882
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001883 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001884 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001885 }
1886
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 @Override
1888 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001889 long startTime = 0;
1890 if (DEBUG_RESUME_TIME) {
1891 startTime = System.currentTimeMillis();
1892 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 super.onNewIntent(intent);
1894
1895 // Close the menu
1896 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001897 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001898 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001899
Adam Cohened307df2013-10-02 09:37:31 -07001900 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1901 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1902 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001903
Adam Cohen6fecd412013-10-02 17:41:50 -07001904 if (mWorkspace == null) {
1905 // Can be cases where mWorkspace is null, this prevents a NPE
1906 return;
1907 }
1908 Folder openFolder = mWorkspace.getOpenFolder();
1909 // In all these cases, only animate if we're already on home
1910 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001911
1912 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1913 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001914 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001915 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001916 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001917 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001918
Adam Cohen6fecd412013-10-02 17:41:50 -07001919 closeFolder();
1920 exitSpringLoadedDragMode();
1921
1922 // If we are already on home, then just animate back to the workspace,
1923 // otherwise, just wait until onResume to set the state back to Workspace
1924 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001925 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001926 } else {
1927 mOnResumeState = State.WORKSPACE;
1928 }
1929
1930 final View v = getWindow().peekDecorView();
1931 if (v != null && v.getWindowToken() != null) {
1932 InputMethodManager imm = (InputMethodManager)getSystemService(
1933 INPUT_METHOD_SERVICE);
1934 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1935 }
1936
1937 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001938 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001939 mAppsCustomizeTabHost.reset();
1940 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001941
Adam Cohen9211d422014-10-07 18:14:53 -07001942 if (mLauncherCallbacks != null) {
1943 mLauncherCallbacks.onHomeIntent();
1944 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
Adam Cohened307df2013-10-02 09:37:31 -07001946
Michael Jurka447bf842013-05-15 14:52:15 +02001947 if (DEBUG_RESUME_TIME) {
1948 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950
Adam Cohen9211d422014-10-07 18:14:53 -07001951 if (mLauncherCallbacks != null) {
1952 mLauncherCallbacks.onNewIntent(intent);
1953 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001954 }
1955
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001957 public void onRestoreInstanceState(Bundle state) {
1958 super.onRestoreInstanceState(state);
1959 for (int page: mSynchronouslyBoundPages) {
1960 mWorkspace.restoreInstanceStateForChild(page);
1961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 }
1963
1964 @Override
1965 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001966 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001967 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1968 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001969 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001970 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971
Michael Jurka883f55b2010-10-21 15:47:14 -07001972 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001973 // We close any open folder since it will not be re-opened, and we need to make sure
1974 // this state is reflected.
1975 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976
Adam Cohendcd297f2013-06-18 13:13:40 -07001977 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001978 mWaitingForResult) {
1979 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001980 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001981 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1982 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001983 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1984 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1985 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001986 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 }
1988
1989 if (mFolderInfo != null && mWaitingForResult) {
1990 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1991 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1992 }
Michael Jurka946ad472010-07-09 18:05:18 -07001993
Winson Chung785d2eb2011-04-14 16:08:02 -07001994 // Save the current AppsCustomize tab
1995 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001996 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1997 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001998 if (currentTabTag != null) {
1999 outState.putString("apps_customize_currentTab", currentTabTag);
2000 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002001 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2002 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002003 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002004 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002005
2006 if (mLauncherCallbacks != null) {
2007 mLauncherCallbacks.onSaveInstanceState(outState);
2008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010
2011 @Override
2012 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002014
Winson Chunge7a03942011-08-05 15:05:12 -07002015 // Remove all pending runnables
2016 mHandler.removeMessages(ADVANCE_MSG);
2017 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002018 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002019
Winson Chungcd2b0142011-06-08 16:02:26 -07002020 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002021 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002022
2023 // It's possible to receive onDestroy after a new Launcher activity has
2024 // been created. In this case, don't interfere with the new Launcher.
2025 if (mModel.isCurrentCallbacks(this)) {
2026 mModel.stopLoader();
2027 app.setLauncher(null);
2028 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002029
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002031 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002033 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002035 mAppWidgetHost = null;
2036
2037 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038
2039 TextKeyListener.getInstance().release();
2040
Winson Chung81b52252012-08-27 15:34:29 -07002041 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2042 // to prevent leaking Launcher activities on orientation change.
2043 if (mModel != null) {
2044 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2045 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002046
2047 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002048 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002049
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002050 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002051 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002052 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002053 mWorkspace = null;
2054 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002055
Michael Jurka2ecf9952012-06-18 12:52:28 -07002056 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002057
2058 if (mLauncherCallbacks != null) {
2059 mLauncherCallbacks.onDestroy();
2060 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 }
2062
Adam Cohena9cf38f2011-05-02 15:36:58 -07002063 public DragController getDragController() {
2064 return mDragController;
2065 }
2066
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 @Override
2068 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002069 if (requestCode >= 0) {
2070 setWaitingForResult(true);
2071 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 super.startActivityForResult(intent, requestCode);
2073 }
2074
Winson Chung70d72102011-08-12 11:24:35 -07002075 /**
2076 * Indicates that we want global search for this activity by setting the globalSearch
2077 * argument for {@link #startSearch} to true.
2078 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002080 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002082
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002083 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002084
Karl Rosaen138a0412009-04-23 19:00:21 -07002085 if (initialQuery == null) {
2086 // Use any text typed in the launcher as the initial query
2087 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 if (appSearchData == null) {
2090 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002091 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 }
Winson Chungadf0c182012-08-23 14:59:07 -07002093 Rect sourceBounds = new Rect();
2094 if (mSearchDropTargetBar != null) {
2095 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2096 }
Romain Guycbb89e42009-06-08 15:52:54 -07002097
Ian Parkinson047036e2014-09-01 15:40:53 +01002098 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002099 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002100 if (clearTextImmediately) {
2101 clearTypedText();
2102 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002103 }
2104
Ian Parkinson047036e2014-09-01 15:40:53 +01002105 /**
2106 * Start a text search.
2107 *
2108 * @return {@code true} if the search will start immediately, so any further keypresses
2109 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2110 * to buffer keypresses.
2111 */
2112 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002113 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002114 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2115 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2116 sourceBounds);
2117 }
2118
Michael Jurkaa33411c2012-06-14 16:18:21 -07002119 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002120 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002121 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002122 }
2123
2124 /**
2125 * Starts the global search activity. This code is a copied from SearchManager
2126 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002127 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002128 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002129 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002130 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2131 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2132 if (globalSearchActivity == null) {
2133 Log.w(TAG, "No global search activity found.");
2134 return;
2135 }
2136 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2137 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2138 intent.setComponent(globalSearchActivity);
2139 // Make sure that we have a Bundle to put source in
2140 if (appSearchData == null) {
2141 appSearchData = new Bundle();
2142 } else {
2143 appSearchData = new Bundle(appSearchData);
2144 }
Adam Cohen9211d422014-10-07 18:14:53 -07002145 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002146 if (!appSearchData.containsKey("source")) {
2147 appSearchData.putString("source", getPackageName());
2148 }
2149 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2150 if (!TextUtils.isEmpty(initialQuery)) {
2151 intent.putExtra(SearchManager.QUERY, initialQuery);
2152 }
2153 if (selectInitialQuery) {
2154 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2155 }
2156 intent.setSourceBounds(sourceBounds);
2157 try {
2158 startActivity(intent);
2159 } catch (ActivityNotFoundException ex) {
2160 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 }
2163
Yura4f93ec62014-02-04 14:15:21 +00002164 public boolean isOnCustomContent() {
2165 return mWorkspace.isOnOrMovingToCustomContent();
2166 }
2167
Winson Chung70d72102011-08-12 11:24:35 -07002168 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002169 public boolean onPrepareOptionsMenu(Menu menu) {
2170 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002171 if (!isOnCustomContent()) {
2172 // Close any open folders
2173 closeFolder();
2174 // Stop resizing any widgets
2175 mWorkspace.exitWidgetResizeMode();
2176 if (!mWorkspace.isInOverviewMode()) {
2177 // Show the overview mode
2178 showOverviewMode(true);
2179 } else {
2180 showWorkspace(true);
2181 }
Winson Chunge0298742014-01-17 12:03:00 -08002182 }
Adam Cohen9211d422014-10-07 18:14:53 -07002183 if (mLauncherCallbacks != null) {
2184 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2185 }
2186
Michael Jurkab94f3f82013-09-11 18:08:54 +02002187 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002188 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002190 @Override
2191 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002192 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002193 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002194 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002195 }
2196
Joe Onorato9c1289c2009-08-17 11:03:03 -04002197 public boolean isWorkspaceLocked() {
2198 return mWorkspaceLoading || mWaitingForResult;
2199 }
2200
Adam Cohen517a7f52014-03-01 12:12:59 -08002201 public boolean isWorkspaceLoading() {
2202 return mWorkspaceLoading;
2203 }
2204
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002205 private void setWorkspaceLoading(boolean value) {
2206 boolean isLocked = isWorkspaceLocked();
2207 mWorkspaceLoading = value;
2208 if (isLocked != isWorkspaceLocked()) {
2209 onWorkspaceLockedChanged();
2210 }
2211 }
2212
2213 private void setWaitingForResult(boolean value) {
2214 boolean isLocked = isWorkspaceLocked();
2215 mWaitingForResult = value;
2216 if (isLocked != isWorkspaceLocked()) {
2217 onWorkspaceLockedChanged();
2218 }
2219 }
2220
Adam Cohen9211d422014-10-07 18:14:53 -07002221 protected void onWorkspaceLockedChanged() {
2222 if (mLauncherCallbacks != null) {
2223 mLauncherCallbacks.onWorkspaceLockedChanged();
2224 }
2225 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002226
Michael Jurka0280c3b2010-09-17 15:00:07 -07002227 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002228 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002229 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002230 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2231 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002232 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002233 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002234 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002235
Sunny Goyale6b63a32015-01-16 16:14:29 -08002236 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002237 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002238 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2239 }
2240
Sunny Goyale6b63a32015-01-16 16:14:29 -08002241 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002242 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2243 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002244 if (appWidgetInfo.configure != null) {
2245 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002246 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002247
Bjorn Bringert7984c942009-12-09 15:38:25 +00002248 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002249 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2250 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002252 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002253 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002254 Runnable onComplete = new Runnable() {
2255 @Override
2256 public void run() {
2257 // Exit spring loaded mode if necessary after adding the widget
2258 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2259 null);
2260 }
2261 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002262 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002263 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002264 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 }
2266 }
Romain Guycbb89e42009-06-08 15:52:54 -07002267
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002268 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002269 // Close any folders that may be open.
2270 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002271 mWorkspace.moveToCustomContentScreen(animate);
2272 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002273
2274 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2275 int[] cell, int spanX, int spanY) {
2276 switch (info.itemType) {
2277 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2278 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2279 int span[] = new int[2];
2280 span[0] = spanX;
2281 span[1] = spanY;
2282 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2283 container, screenId, cell, span);
2284 break;
2285 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2286 processShortcutFromDrop(info.componentName, container, screenId, cell);
2287 break;
2288 default:
2289 throw new IllegalStateException("Unknown item type: " + info.itemType);
2290 }
2291 }
2292
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293 /**
2294 * Process a shortcut drop.
2295 *
2296 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002297 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002298 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002299 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002300 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2301 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002302 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002303 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002304 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002305 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002306
2307 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002308 mPendingAddInfo.cellX = cell[0];
2309 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002310 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002311
2312 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2313 createShortcutIntent.setComponent(componentName);
2314 processShortcut(createShortcutIntent);
2315 }
2316
Adam Cohenfbba09b2011-07-18 21:43:05 -07002317 /**
2318 * Process a widget drop.
2319 *
2320 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002321 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002322 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002323 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002324 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2325 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002326 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002327 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002328 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002329 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002330 mPendingAddInfo.minSpanX = info.minSpanX;
2331 mPendingAddInfo.minSpanY = info.minSpanY;
2332
Adam Cohenfbba09b2011-07-18 21:43:05 -07002333 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002334 mPendingAddInfo.cellX = cell[0];
2335 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002336 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002337 if (span != null) {
2338 mPendingAddInfo.spanX = span[0];
2339 mPendingAddInfo.spanY = span[1];
2340 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002341
Adam Cohened66b2b2012-01-23 17:28:51 -08002342 AppWidgetHostView hostView = info.boundWidget;
2343 int appWidgetId;
2344 if (hostView != null) {
2345 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002346 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002347 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002348 // In this case, we either need to start an activity to get permission to bind
2349 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002350 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002351 Bundle options = info.bindOptions;
2352
Sunny Goyalffe83f12014-08-14 17:39:34 -07002353 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2354 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002355 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002356 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002357 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002358 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002359 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2360 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2361 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002362 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2363 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002364 // TODO: we need to make sure that this accounts for the options bundle.
2365 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002366 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2367 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002368 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002369 }
2370
Joe Onoratodeb98af2010-02-19 14:59:39 -08002371 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002372 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002373 }
2374
Winson Chung24ab2f12010-09-16 14:10:47 -07002375 void processWallpaper(Intent intent) {
2376 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2377 }
2378
Adam Cohendcd297f2013-06-18 13:13:40 -07002379 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002380 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002381 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 folderInfo.title = getText(R.string.folder_name);
2383
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002385 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002386 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002387 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388
2389 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002390 FolderIcon newFolder =
2391 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002392 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002393 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002394 // Force measure the new folder icon
2395 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2396 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002397 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 }
Romain Guycbb89e42009-06-08 15:52:54 -07002399
Joe Onorato9c1289c2009-08-17 11:03:03 -04002400 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002401 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002402 }
2403
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002404 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002405 if (mLauncherCallbacks != null) {
2406 return mLauncherCallbacks.getWallpaperPickerComponent();
2407 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002408 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002409 }
2410
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002411 /**
2412 * Registers various content observers. The current implementation registers
2413 * only a favorites observer to keep track of the favorites applications.
2414 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002415 private void registerContentObservers() {
2416 ContentResolver resolver = getContentResolver();
2417 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2418 true, mWidgetObserver);
2419 }
2420
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 @Override
2422 public boolean dispatchKeyEvent(KeyEvent event) {
2423 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2424 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002426 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002427 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002428 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002429 dumpState();
2430 return true;
2431 }
2432 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002433 }
2434 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2435 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002436 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437 return true;
2438 }
2439 }
2440
2441 return super.dispatchKeyEvent(event);
2442 }
2443
Joe Onorato88ec0992009-11-19 13:16:06 -08002444 @Override
2445 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002446 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2447 return;
2448 }
2449
Adam Cohenc9735cf2015-01-23 16:11:55 -08002450 if (LauncherAppState.getInstance().getAccessibilityDelegate().onBackPressed()) {
2451 return;
2452 }
2453
Winson Chungc93e5ae2012-07-23 20:48:26 -07002454 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002455 if (mAppsCustomizeContent.getContentType() ==
2456 AppsCustomizePagedView.ContentType.Applications) {
2457 showWorkspace(true);
2458 } else {
2459 showOverviewMode(true);
2460 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002461 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002462 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002463 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002464 Folder openFolder = mWorkspace.getOpenFolder();
2465 if (openFolder.isEditingName()) {
2466 openFolder.dismissEditingName();
2467 } else {
2468 closeFolder();
2469 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002470 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002471 mWorkspace.exitWidgetResizeMode();
2472
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002473 // Back button is a no-op here, but give at least some feedback for the button press
2474 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002475 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002476 }
2477
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002479 * Re-listen when widgets are reset.
2480 */
2481 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002482 if (mAppWidgetHost != null) {
2483 mAppWidgetHost.startListening();
2484 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002485 }
2486
2487 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002488 * Launches the intent referred by the clicked shortcut.
2489 *
2490 * @param v The view representing the clicked shortcut.
2491 */
2492 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002493 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2494 // view has detached (it's possible for this to happen if the view is removed mid touch).
2495 if (v.getWindowToken() == null) {
2496 return;
2497 }
2498
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002499 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002500 return;
2501 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002502
Adam Cohen1697b792013-09-17 19:08:21 -07002503 if (v instanceof Workspace) {
2504 if (mWorkspace.isInOverviewMode()) {
2505 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002506 }
2507 return;
2508 }
2509
2510 if (v instanceof CellLayout) {
2511 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002512 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002513 }
2514 }
2515
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002517 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002518 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002520 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002521 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002522 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002523 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002524 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002525 } else if (tag instanceof AppInfo) {
2526 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002527 } else if (tag instanceof LauncherAppWidgetInfo) {
2528 if (v instanceof PendingAppWidgetHostView) {
2529 onClickPendingWidget((PendingAppWidgetHostView) v);
2530 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531 }
2532 }
2533
Sunny Goyal508da152014-08-14 10:53:27 -07002534 public void onClickPagedViewIcon(View v) {
2535 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002536 if (mLauncherCallbacks != null) {
2537 mLauncherCallbacks.onClickPagedViewIcon(v);
2538 }
Sunny Goyal508da152014-08-14 10:53:27 -07002539 }
2540
Michael Jurka0e260592010-06-30 17:07:39 -07002541 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002542 return false;
2543 }
2544
Michael Jurkaaf442092010-06-10 17:01:57 -07002545 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002546 * Event handler for the app widget view which has not fully restored.
2547 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002548 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002549 if (mIsSafeModeEnabled) {
2550 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2551 return;
2552 }
2553
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002554 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002555 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002556 int widgetId = info.appWidgetId;
2557 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2558 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002559 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2560 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002561 mPendingAddInfo.copyFrom(info);
2562 mPendingAddWidgetId = widgetId;
2563
Sunny Goyalffe83f12014-08-14 17:39:34 -07002564 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2565 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002566 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002567 } else if (info.installProgress < 0) {
2568 // The install has not been queued
2569 final String packageName = info.providerName.getPackageName();
2570 showBrokenAppInstallDialog(packageName,
2571 new DialogInterface.OnClickListener() {
2572 public void onClick(DialogInterface dialog, int id) {
2573 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2574 }
2575 });
2576 } else {
2577 // Download has started.
2578 final String packageName = info.providerName.getPackageName();
2579 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002580 }
2581 }
2582
2583 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002584 * Event handler for the "grid" button that appears on the home screen, which
2585 * enters all apps mode.
2586 *
2587 * @param v The view that was clicked.
2588 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002589 protected void onClickAllAppsButton(View v) {
2590 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2591 if (isAllAppsVisible()) {
2592 showWorkspace(true);
2593 } else {
2594 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2595 }
Adam Cohen9211d422014-10-07 18:14:53 -07002596 if (mLauncherCallbacks != null) {
2597 mLauncherCallbacks.onClickAllAppsButton(v);
2598 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002599 }
2600
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002601 private void showBrokenAppInstallDialog(final String packageName,
2602 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002603 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002604 .setTitle(R.string.abandoned_promises_title)
2605 .setMessage(R.string.abandoned_promise_explanation)
2606 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2607 .setNeutralButton(R.string.abandoned_clean_this,
2608 new DialogInterface.OnClickListener() {
2609 public void onClick(DialogInterface dialog, int id) {
2610 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2611 mWorkspace.removeAbandonedPromise(packageName, user);
2612 }
2613 })
2614 .create().show();
2615 return;
2616 }
2617
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002618 /**
2619 * Event handler for an app shortcut click.
2620 *
2621 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2622 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002623 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2625 Object tag = v.getTag();
2626 if (!(tag instanceof ShortcutInfo)) {
2627 throw new IllegalArgumentException("Input must be a Shortcut");
2628 }
2629
2630 // Open shortcut
2631 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002632
2633 if (shortcut.isDisabled != 0) {
2634 int error = R.string.activity_not_available;
2635 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2636 error = R.string.safemode_shortcut_error;
2637 }
2638 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2639 return;
2640 }
2641
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002642 final Intent intent = shortcut.intent;
2643
2644 // Check for special shortcuts
2645 if (intent.getComponent() != null) {
2646 final String shortcutClass = intent.getComponent().getClassName();
2647
2648 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2649 MemoryDumpActivity.startDump(this);
2650 return;
2651 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2652 toggleShowWeightWatcher();
2653 return;
2654 }
2655 }
2656
Chris Wren40c5ed32014-06-24 18:24:23 -04002657 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002658 if ((v instanceof BubbleTextView)
2659 && shortcut.isPromise()
2660 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002661 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002662 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002663 new DialogInterface.OnClickListener() {
2664 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002665 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002666 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002667 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002668 return;
2669 }
2670
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002671 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002672 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002673
2674 if (mLauncherCallbacks != null) {
2675 mLauncherCallbacks.onClickAppShortcut(v);
2676 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 }
2678
Sunny Goyal508da152014-08-14 10:53:27 -07002679 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002680 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002681 final ShortcutInfo shortcut;
2682 final Intent intent;
2683 if (tag instanceof ShortcutInfo) {
2684 shortcut = (ShortcutInfo) tag;
2685 intent = shortcut.intent;
2686 int[] pos = new int[2];
2687 v.getLocationOnScreen(pos);
2688 intent.setSourceBounds(new Rect(pos[0], pos[1],
2689 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002690
Sunny Goyal508da152014-08-14 10:53:27 -07002691 } else if (tag instanceof AppInfo) {
2692 shortcut = null;
2693 intent = ((AppInfo) tag).intent;
2694 } else {
2695 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2696 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002697
2698 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002699 mStats.recordLaunch(intent, shortcut);
2700
2701 if (success && v instanceof BubbleTextView) {
2702 mWaitingForResume = (BubbleTextView) v;
2703 mWaitingForResume.setStayPressed(true);
2704 }
2705 }
2706
2707 /**
2708 * Event handler for a folder icon click.
2709 *
2710 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2711 */
2712 protected void onClickFolderIcon(View v) {
2713 if (LOGD) Log.d(TAG, "onClickFolder");
2714 if (!(v instanceof FolderIcon)){
2715 throw new IllegalArgumentException("Input must be a FolderIcon");
2716 }
2717
2718 FolderIcon folderIcon = (FolderIcon) v;
2719 final FolderInfo info = folderIcon.getFolderInfo();
2720 Folder openFolder = mWorkspace.getFolderForTag(info);
2721
2722 // If the folder info reports that the associated folder is open, then verify that
2723 // it is actually opened. There have been a few instances where this gets out of sync.
2724 if (info.opened && openFolder == null) {
2725 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2726 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2727 info.opened = false;
2728 }
2729
2730 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2731 // Close any open folder
2732 closeFolder();
2733 // Open the requested folder
2734 openFolder(folderIcon);
2735 } else {
2736 // Find the open folder...
2737 int folderScreen;
2738 if (openFolder != null) {
2739 folderScreen = mWorkspace.getPageForView(openFolder);
2740 // .. and close it
2741 closeFolder(openFolder);
2742 if (folderScreen != mWorkspace.getCurrentPage()) {
2743 // Close any folder open on the current screen
2744 closeFolder();
2745 // Pull the folder onto this screen
2746 openFolder(folderIcon);
2747 }
2748 }
2749 }
Adam Cohen9211d422014-10-07 18:14:53 -07002750
2751 if (mLauncherCallbacks != null) {
2752 mLauncherCallbacks.onClickFolderIcon(v);
2753 }
Michael Jurka5130e402011-10-13 04:55:35 -07002754 }
2755
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002756 /**
2757 * Event handler for the (Add) Widgets button that appears after a long press
2758 * on the home screen.
2759 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002760 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002761 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002762 if (mIsSafeModeEnabled) {
2763 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2764 } else {
2765 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2766 if (mLauncherCallbacks != null) {
2767 mLauncherCallbacks.onClickAddWidgetButton(view);
2768 }
Adam Cohen9211d422014-10-07 18:14:53 -07002769 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002770 }
2771
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002772 /**
2773 * Event handler for the wallpaper picker button that appears after a long press
2774 * on the home screen.
2775 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002776 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002777 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2778 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2779 pickWallpaper.setComponent(getWallpaperPickerComponent());
2780 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002781
2782 if (mLauncherCallbacks != null) {
2783 mLauncherCallbacks.onClickWallpaperPicker(v);
2784 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002785 }
2786
2787 /**
2788 * Event handler for a click on the settings button that appears after a long press
2789 * on the home screen.
2790 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002791 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002792 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002793 if (mLauncherCallbacks != null) {
2794 mLauncherCallbacks.onClickSettingsButton(v);
2795 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002796 }
2797
Michael Jurka5130e402011-10-13 04:55:35 -07002798 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002799 // Provide the same haptic feedback that the system offers for virtual keys.
2800 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002801 }
2802
Adam Cohen61f560d2013-09-30 15:58:20 -07002803 public void performHapticFeedbackOnTouchDown(View v) {
2804 // Provide the same haptic feedback that the system offers for virtual keys.
2805 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2806 }
2807
2808 public View.OnTouchListener getHapticFeedbackTouchListener() {
2809 if (mHapticFeedbackTouchListener == null) {
2810 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2811 @Override
2812 public boolean onTouch(View v, MotionEvent event) {
2813 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2814 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2815 }
2816 return false;
2817 }
2818 };
2819 }
2820 return mHapticFeedbackTouchListener;
2821 }
2822
Adam Cohen9211d422014-10-07 18:14:53 -07002823 public void onDragStarted(View view) {
2824 if (isOnCustomContent()) {
2825 // Custom content screen doesn't participate in drag and drop. If on custom
2826 // content screen, move to default.
2827 moveWorkspaceToDefaultScreen();
2828 }
2829
2830 if (mLauncherCallbacks != null) {
2831 mLauncherCallbacks.onDragStarted(view);
2832 }
2833 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002834
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002835 /**
2836 * Called when the user stops interacting with the launcher.
2837 * This implies that the user is now on the homescreen and is not doing housekeeping.
2838 */
Adam Cohen9211d422014-10-07 18:14:53 -07002839 protected void onInteractionEnd() {
2840 if (mLauncherCallbacks != null) {
2841 mLauncherCallbacks.onInteractionEnd();
2842 }
2843 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002844
2845 /**
2846 * Called when the user starts interacting with the launcher.
2847 * The possible interactions are:
2848 * - open all apps
2849 * - reorder an app shortcut, or a widget
2850 * - open the overview mode.
2851 * This is a good time to stop doing things that only make sense
2852 * when the user is on the homescreen and not doing housekeeping.
2853 */
Adam Cohen9211d422014-10-07 18:14:53 -07002854 protected void onInteractionBegin() {
2855 if (mLauncherCallbacks != null) {
2856 mLauncherCallbacks.onInteractionBegin();
2857 }
2858 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002859
Kenny Guyf07af7b2014-07-31 11:39:16 +01002860 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002861 try {
2862 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002863 launcherApps.showAppDetailsForProfile(componentName, user);
2864 } catch (SecurityException e) {
2865 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2866 Log.e(TAG, "Launcher does not have permission to launch settings");
2867 } catch (ActivityNotFoundException e) {
2868 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2869 Log.e(TAG, "Unable to launch settings");
2870 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002871 }
2872
Michael Jurka1e2f4652013-07-08 18:03:46 -07002873 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002874 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2875 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002876 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002877 // System applications cannot be installed. For now, show a toast explaining that.
2878 // We may give them the option of disabling apps this way.
2879 int messageId = R.string.uninstall_system_app_text;
2880 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002881 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002882 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002883 String packageName = componentName.getPackageName();
2884 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002885 Intent intent = new Intent(
2886 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002887 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2888 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002889 if (user != null) {
2890 user.addToIntent(intent, Intent.EXTRA_USER);
2891 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002892 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002893 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002894 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002895 }
2896
Michael Jurka86a720e2012-05-09 11:23:23 -07002897 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002898 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002899 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002900 // Only launch using the new animation if the shortcut has not opted out (this is a
2901 // private contract between launcher and may be ignored in the future).
2902 boolean useLaunchAnimation = (v != null) &&
2903 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002904 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2905 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002906
Kenny Guy1317e2d2014-05-08 18:52:50 +01002907 UserHandleCompat user = null;
2908 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2909 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2910 user = userManager.getUserForSerialNumber(serialNumber);
2911 }
2912
2913 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002914 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002915 ActivityOptions opts = null;
2916 if (sClipRevealMethod != null) {
2917 // TODO: call method directly when Launcher3 can depend on M APIs
2918 int left = 0, top = 0;
2919 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2920 if (v instanceof TextView) {
2921 // Launch from center of icon, not entire view
2922 TextView tv = (TextView) v;
2923 Drawable[] drawables = tv.getCompoundDrawables();
2924 if (drawables != null && drawables[1] != null) {
2925 Rect bounds = drawables[1].getBounds();
2926 left = (width - bounds.width()) / 2;
2927 top = tv.getPaddingTop();
2928 width = bounds.width();
2929 height = bounds.height();
2930 }
2931 }
2932 try {
2933 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2934 left, top, width, height);
2935 } catch (IllegalAccessException e) {
2936 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2937 sClipRevealMethod = null;
2938 } catch (InvocationTargetException e) {
2939 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2940 sClipRevealMethod = null;
2941 }
2942 }
2943 if (opts == null) {
2944 opts = Utilities.isLmpOrAbove() ?
2945 ActivityOptions.makeCustomAnimation(this,
2946 R.anim.task_open_enter, R.anim.no_anim) :
2947 ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2948 v.getMeasuredWidth(), v.getMeasuredHeight());
2949 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002950 optsBundle = opts.toBundle();
2951 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002952
2953 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2954 // Could be launching some bookkeeping activity
2955 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002956 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002957 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002958 launcherApps.startActivityForProfile(intent.getComponent(), user,
2959 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002960 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002961 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 } catch (SecurityException e) {
2963 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002964 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002966 "or use the exported attribute for this activity. "
2967 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002969 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002970 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002971
Michael Jurka86a720e2012-05-09 11:23:23 -07002972 boolean startActivitySafely(View v, Intent intent, Object tag) {
2973 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002974 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2975 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2976 return false;
2977 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002978 try {
2979 success = startActivity(v, intent, tag);
2980 } catch (ActivityNotFoundException e) {
2981 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2982 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2983 }
2984 return success;
2985 }
2986
Adam Cohen268c4752012-06-06 17:47:33 -07002987 /**
2988 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2989 * in the DragLayer in the exact absolute location of the original FolderIcon.
2990 */
2991 private void copyFolderIconToImage(FolderIcon fi) {
2992 final int width = fi.getMeasuredWidth();
2993 final int height = fi.getMeasuredHeight();
2994
2995 // Lazy load ImageView, Bitmap and Canvas
2996 if (mFolderIconImageView == null) {
2997 mFolderIconImageView = new ImageView(this);
2998 }
2999 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3000 mFolderIconBitmap.getHeight() != height) {
3001 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3002 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3003 }
3004
3005 DragLayer.LayoutParams lp;
3006 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3007 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3008 } else {
3009 lp = new DragLayer.LayoutParams(width, height);
3010 }
3011
Adam Cohen307fe232012-08-16 17:55:58 -07003012 // The layout from which the folder is being opened may be scaled, adjust the starting
3013 // view size by this scale factor.
3014 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003015 lp.customPosition = true;
3016 lp.x = mRectForFolderAnimation.left;
3017 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003018 lp.width = (int) (scale * width);
3019 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003020
3021 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3022 fi.draw(mFolderIconCanvas);
3023 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003024 if (fi.getFolder() != null) {
3025 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3026 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003027 }
Adam Cohen268c4752012-06-06 17:47:33 -07003028 // Just in case this image view is still in the drag layer from a previous animation,
3029 // we remove it and re-add it.
3030 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3031 mDragLayer.removeView(mFolderIconImageView);
3032 }
3033 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003034 if (fi.getFolder() != null) {
3035 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003036 }
Adam Cohen268c4752012-06-06 17:47:33 -07003037 }
3038
Adam Cohen2801caf2011-05-13 20:57:39 -07003039 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003040 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003041 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3042 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3043 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3044
Adam Cohenc51934b2011-07-26 21:07:43 -07003045 FolderInfo info = (FolderInfo) fi.getTag();
3046 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3047 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003048 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3049 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003050 }
3051
Adam Cohen268c4752012-06-06 17:47:33 -07003052 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3053 copyFolderIconToImage(fi);
3054 fi.setVisibility(View.INVISIBLE);
3055
Michael Jurka2ecf9952012-06-18 12:52:28 -07003056 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003057 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003058 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003059 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3060 }
3061 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003062 oa.start();
3063 }
3064
Adam Cohen268c4752012-06-06 17:47:33 -07003065 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003066 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003067 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3068 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3069 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3070
Adam Cohen268c4752012-06-06 17:47:33 -07003071 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003072
Adam Cohen268c4752012-06-06 17:47:33 -07003073 // We remove and re-draw the FolderIcon in-case it has changed
3074 mDragLayer.removeView(mFolderIconImageView);
3075 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003076 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003077 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003078 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003079 oa.addListener(new AnimatorListenerAdapter() {
3080 @Override
3081 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003082 if (cl != null) {
3083 cl.clearFolderLeaveBehind();
3084 // Remove the ImageView copy of the FolderIcon and make the original visible.
3085 mDragLayer.removeView(mFolderIconImageView);
3086 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003087 }
3088 }
3089 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003090 oa.start();
3091 }
3092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003093 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003094 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003095 * is animated relative to the specified View. If the View is null, no animation
3096 * is played.
3097 *
3098 * @param folderInfo The FolderInfo describing the folder to open.
3099 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003100 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003101 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003102 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003103
Adam Cohena9cf38f2011-05-02 15:36:58 -07003104 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003105
Adam Cohen4554ee12011-08-03 16:13:21 -07003106 // Just verify that the folder hasn't already been added to the DragLayer.
3107 // There was a one-off crash where the folder had a parent already.
3108 if (folder.getParent() == null) {
3109 mDragLayer.addView(folder);
3110 mDragController.addDropTarget((DropTarget) folder);
3111 } else {
3112 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3113 folder.getParent() + ").");
3114 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003115 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003116 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003117
3118 // Notify the accessibility manager that this folder "window" has appeared and occluded
3119 // the workspace items
3120 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3121 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003122 }
3123
3124 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003125 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003126 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003127 if (folder.isEditingName()) {
3128 folder.dismissEditingName();
3129 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003130 closeFolder(folder);
3131 }
3132 }
3133
3134 void closeFolder(Folder folder) {
3135 folder.getInfo().opened = false;
3136
3137 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3138 if (parent != null) {
3139 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3140 shrinkAndFadeInFolderIcon(fi);
3141 }
3142 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003143
3144 // Notify the accessibility manager that this folder "window" has disappeard and no
3145 // longer occludeds the workspace items
3146 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147 }
3148
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003149 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003150 if (!isDraggingEnabled()) return false;
3151 if (isWorkspaceLocked()) return false;
3152 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153
Adam Cohen1697b792013-09-17 19:08:21 -07003154 if (v instanceof Workspace) {
3155 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003156 if (mWorkspace.enterOverviewMode()) {
3157 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3158 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3159 return true;
3160 } else {
3161 return false;
3162 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003163 } else {
3164 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003165 }
Adam Cohen1697b792013-09-17 19:08:21 -07003166 }
3167
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003168 CellLayout.CellInfo longClickCellInfo = null;
3169 View itemUnderLongClick = null;
3170 if (v.getTag() instanceof ItemInfo) {
3171 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003172 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003173 itemUnderLongClick = longClickCellInfo.cell;
3174 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003175 }
3176
Winson Chung3d503fb2011-07-13 17:25:49 -07003177 // The hotseat touch handling does not go through Workspace, and we always allow long press
3178 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003179 final boolean inHotseat = isHotseatLayout(v);
3180 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003181 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003182 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003183 // User long pressed on empty space
3184 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3185 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003186 if (mWorkspace.isInOverviewMode()) {
3187 mWorkspace.startReordering(v);
3188 } else {
3189 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003191 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003192 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3193 mHotseat.getOrderInHotseat(
3194 longClickCellInfo.cellX,
3195 longClickCellInfo.cellY));
3196 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003197 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003198 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003199 }
3200 }
3201 }
3202 return true;
3203 }
3204
Winson Chung3d503fb2011-07-13 17:25:49 -07003205 boolean isHotseatLayout(View layout) {
3206 return mHotseat != null && layout != null &&
3207 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3208 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003209
Winson Chung3d503fb2011-07-13 17:25:49 -07003210 /**
3211 * Returns the CellLayout of the specified container at the specified screen.
3212 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003213 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003214 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3215 if (mHotseat != null) {
3216 return mHotseat.getLayout();
3217 } else {
3218 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003219 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003220 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003221 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003222 }
3223 }
3224
Daniel Sandler843e8602010-06-07 14:59:01 -04003225 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003226 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003227 }
3228
Craig Mautner360310b2012-10-26 15:13:08 -07003229 private void setWorkspaceBackground(boolean workspace) {
3230 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003231 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003232 }
3233
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003234 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003235 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3236 int curflags = getWindow().getAttributes().flags
3237 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3238 if (wpflags != curflags) {
3239 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3240 }
Craig Mautner360310b2012-10-26 15:13:08 -07003241 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003242 }
3243
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003244 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3245 if (v instanceof LauncherTransitionable) {
3246 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3247 }
3248 }
3249
Michael Jurkabed61d22012-02-14 22:51:29 -08003250 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3251 if (v instanceof LauncherTransitionable) {
3252 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3253 }
Winson Chung70442722012-02-10 15:43:22 -08003254
3255 // Update the workspace transition step as well
3256 dispatchOnLauncherTransitionStep(v, 0f);
3257 }
3258
3259 private void dispatchOnLauncherTransitionStep(View v, float t) {
3260 if (v instanceof LauncherTransitionable) {
3261 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3262 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003263 }
3264
3265 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3266 if (v instanceof LauncherTransitionable) {
3267 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3268 }
Winson Chung70442722012-02-10 15:43:22 -08003269
3270 // Update the workspace transition step as well
3271 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003272 }
3273
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003274 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003275 * Things to test when changing the following seven functions.
3276 * - Home from workspace
3277 * - from center screen
3278 * - from other screens
3279 * - Home from all apps
3280 * - from center screen
3281 * - from other screens
3282 * - Back from all apps
3283 * - from center screen
3284 * - from other screens
3285 * - Launch app from workspace and quit
3286 * - with back
3287 * - with home
3288 * - Launch app from all apps and quit
3289 * - with back
3290 * - with home
3291 * - Go to a screen that's not the default, then all
3292 * apps, and launch and app, and go back
3293 * - with back
3294 * -with home
3295 * - On workspace, long press power and go back
3296 * - with back
3297 * - with home
3298 * - On all apps, long press power and go back
3299 * - with back
3300 * - with home
3301 * - On workspace, power off
3302 * - On all apps, power off
3303 * - Launch an app and turn off the screen while in that app
3304 * - Go back with home key
3305 * - Go back with back key TODO: make this not go to workspace
3306 * - From all apps
3307 * - From workspace
3308 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3309 * - From all apps
3310 * - From the center workspace
3311 * - From another workspace
3312 */
3313
3314 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003315 * Zoom the camera out from the workspace to reveal 'toView'.
3316 * Assumes that the view to show is anchored at either the very top or very bottom
3317 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003318 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003319 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003320 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3321 showAppsCustomizeHelper(animated, springLoaded, contentType);
3322 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003323
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003324 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chungc58497e2013-09-03 17:48:37 -07003325 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3326 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003327 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003328 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003329 mStateAnimation.cancel();
3330 mStateAnimation = null;
3331 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003332
Kenny Guyd794a3f2014-09-16 15:17:58 +01003333 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003334
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003335 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003336
Adam Cohen6c5891a2014-07-09 23:53:15 -07003337 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003338 final int itemsAlphaStagger =
3339 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003340
Michael Jurkabed61d22012-02-14 22:51:29 -08003341 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003342 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003343
Adam Cohen80e6beb2015-02-13 16:14:33 -08003344 final HashMap<View, Integer> layerViews = new HashMap<View, Integer>();
Adam Cohen2854d252014-08-27 16:04:07 -07003345
Adam Cohen6c5891a2014-07-09 23:53:15 -07003346 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3347 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003348 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003349 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003350 if (contentType == AppsCustomizePagedView.ContentType.Widgets) {
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003351 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003352 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3353 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003354
Adam Cohena38dc902014-09-07 17:48:55 +02003355 // If for some reason our views aren't initialized, don't animate
3356 boolean initialized = getAllAppsButton() != null;
3357
3358 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003359 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003360 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3361 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003362
Adam Cohen9bfdb762014-07-21 17:44:06 -07003363 final View page = content.getPageAt(content.getCurrentPage());
3364 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003365
Adam Cohen63f1ec02014-08-12 09:23:13 -07003366 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3367 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003368 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3369 } else {
3370 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3371 }
3372
Adam Cohen9bfdb762014-07-21 17:44:06 -07003373 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003374 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003375 revealView.setVisibility(View.VISIBLE);
3376 // We need to hide this view as the animation start will be posted.
3377 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003378
Adam Cohen9bfdb762014-07-21 17:44:06 -07003379 int width = revealView.getMeasuredWidth();
3380 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003381 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003382
Adam Cohen63f1ec02014-08-12 09:23:13 -07003383 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003384 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003385
Adam Cohen63f1ec02014-08-12 09:23:13 -07003386 // Get the y delta between the center of the page and the center of the all apps button
3387 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3388 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003389
Adam Cohen2854d252014-08-27 16:04:07 -07003390 float alpha = 0;
3391 float xDrift = 0;
3392 float yDrift = 0;
3393 if (material) {
3394 alpha = isWidgetTray ? 0.3f : 1f;
3395 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3396 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3397 } else {
3398 yDrift = 2 * height / 3;
3399 xDrift = 0;
3400 }
3401 final float initAlpha = alpha;
3402
Adam Cohen80e6beb2015-02-13 16:14:33 -08003403 layerViews.put(revealView, BUILD_AND_SET_LAYER);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003404 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003405 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003406 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003407 PropertyValuesHolder panelDriftX =
3408 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003409
Adam Cohen2854d252014-08-27 16:04:07 -07003410 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3411 panelAlpha, panelDriftY, panelDriftX);
3412
Adam Cohen9bfdb762014-07-21 17:44:06 -07003413 panelAlphaAndDrift.setDuration(revealDuration);
3414 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003415
Adam Cohen9bfdb762014-07-21 17:44:06 -07003416 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003417
Adam Cohen4e243a22014-08-10 18:30:55 -07003418 if (page != null) {
3419 page.setVisibility(View.VISIBLE);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003420 layerViews.put(page, BUILD_AND_SET_LAYER);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003421
Adam Cohen2854d252014-08-27 16:04:07 -07003422 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3423 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003424 pageDrift.setDuration(revealDuration);
3425 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003426 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003427 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003428
Adam Cohen63f1ec02014-08-12 09:23:13 -07003429 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003430 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003431 itemsAlpha.setDuration(revealDuration);
3432 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3433 itemsAlpha.setStartDelay(itemsAlphaStagger);
3434 mStateAnimation.play(itemsAlpha);
3435 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003436
Adam Cohen4e243a22014-08-10 18:30:55 -07003437 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3438 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003439 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003440 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003441 indicatorsAlpha.setDuration(revealDuration);
3442 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003443
Adam Cohen9bfdb762014-07-21 17:44:06 -07003444 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003445 final View allApps = getAllAppsButton();
3446 int allAppsButtonSize = LauncherAppState.getInstance().
3447 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3448 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003449 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003450 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003451 reveal.setDuration(revealDuration);
3452 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003453
3454 reveal.addListener(new AnimatorListenerAdapter() {
3455 public void onAnimationStart(Animator animation) {
3456 if (!isWidgetTray) {
3457 allApps.setVisibility(View.INVISIBLE);
3458 }
3459 }
3460 public void onAnimationEnd(Animator animation) {
3461 if (!isWidgetTray) {
3462 allApps.setVisibility(View.VISIBLE);
3463 }
3464 }
3465 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003466 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003467 }
Michael Jurka1899a362011-11-03 13:50:45 -07003468
Adam Cohen9bfdb762014-07-21 17:44:06 -07003469 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3470 @Override
3471 public void onAnimationEnd(Animator animation) {
3472 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3473 dispatchOnLauncherTransitionEnd(toView, animated, false);
3474
3475 revealView.setVisibility(View.INVISIBLE);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003476
3477 for (View v : layerViews.keySet()) {
3478 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3479 v.setLayerType(View.LAYER_TYPE_NONE, null);
3480 }
Adam Cohen4e243a22014-08-10 18:30:55 -07003481 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003482 content.setPageBackgroundsVisible(true);
3483
3484 // Hide the search bar
3485 if (mSearchDropTargetBar != null) {
3486 mSearchDropTargetBar.hideSearchBar(false);
3487 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003488
3489 // This can hold unnecessary references to views.
3490 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003491 }
3492
Adam Cohen9bfdb762014-07-21 17:44:06 -07003493 });
3494
Adam Cohen6c5891a2014-07-09 23:53:15 -07003495 if (workspaceAnim != null) {
3496 mStateAnimation.play(workspaceAnim);
3497 }
Adam Cohen2854d252014-08-27 16:04:07 -07003498
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003499 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3500 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003501 final AnimatorSet stateAnimation = mStateAnimation;
3502 final Runnable startAnimRunnable = new Runnable() {
3503 public void run() {
3504 // Check that mStateAnimation hasn't changed while
3505 // we waited for a layout/draw pass
3506 if (mStateAnimation != stateAnimation)
3507 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003508 dispatchOnLauncherTransitionStart(fromView, animated, false);
3509 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003510
3511 revealView.setAlpha(initAlpha);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003512
3513 for (View v : layerViews.keySet()) {
3514 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3515 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3516 }
3517 }
3518
Kenny Guyd794a3f2014-09-16 15:17:58 +01003519 if (Utilities.isLmpOrAbove()) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003520 for (View v : layerViews.keySet()) {
3521 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen2854d252014-08-27 16:04:07 -07003522 }
3523 }
3524 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003525 }
3526 };
Adam Cohen2854d252014-08-27 16:04:07 -07003527 toView.bringToFront();
3528 toView.setVisibility(View.VISIBLE);
3529 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003530 } else {
3531 toView.setTranslationX(0.0f);
3532 toView.setTranslationY(0.0f);
3533 toView.setScaleX(1.0f);
3534 toView.setScaleY(1.0f);
3535 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003536 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003537
Daniel Sandlere4f98912013-06-25 15:13:26 -04003538 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003539 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003540 if (mSearchDropTargetBar != null) {
3541 mSearchDropTargetBar.hideSearchBar(false);
3542 }
Michael Jurkaabded662011-03-04 12:06:57 -08003543 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003544 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003545 dispatchOnLauncherTransitionStart(fromView, animated, false);
3546 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003547 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003548 dispatchOnLauncherTransitionStart(toView, animated, false);
3549 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003550 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003551 }
3552
3553 /**
3554 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003555 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003556 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003557 */
Adam Cohened307df2013-10-02 09:37:31 -07003558 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003559 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003560
Michael Jurkab3e22d92011-10-31 15:58:33 -07003561 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003562 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003563 mStateAnimation.cancel();
3564 mStateAnimation = null;
3565 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003566
Kenny Guyd794a3f2014-09-16 15:17:58 +01003567 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003568 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003569
Adam Cohen63f1ec02014-08-12 09:23:13 -07003570 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003571 final int itemsAlphaStagger =
3572 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003573
Winson Chungf0ea4d32011-06-06 14:27:16 -07003574 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003575 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003576 Animator workspaceAnim = null;
Adam Cohen80e6beb2015-02-13 16:14:33 -08003577 final HashMap<View, Integer> layerViews = new HashMap<View, Integer>();
Adam Cohen2854d252014-08-27 16:04:07 -07003578
Adam Cohened307df2013-10-02 09:37:31 -07003579 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003580 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003581 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003582 } else if (toState == Workspace.State.SPRING_LOADED ||
3583 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003584 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003585 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003586 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003587
Adam Cohena38dc902014-09-07 17:48:55 +02003588 // If for some reason our views aren't initialized, don't animate
3589 boolean initialized = getAllAppsButton() != null;
3590
3591 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003592 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003593 if (workspaceAnim != null) {
3594 mStateAnimation.play(workspaceAnim);
3595 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003596
Adam Cohen9bfdb762014-07-21 17:44:06 -07003597 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3598 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003599
Adam Cohen9bfdb762014-07-21 17:44:06 -07003600 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003601
3602 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3603 int count = content.getChildCount();
3604 for (int i = 0; i < count; i++) {
3605 View child = content.getChildAt(i);
3606 if (child != page) {
3607 child.setVisibility(View.INVISIBLE);
3608 }
3609 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003610 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003611
Adam Cohen2854d252014-08-27 16:04:07 -07003612 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3613 // don't perform all these no-op animations. In particularly, this was causing
3614 // the all-apps button to pop in and out.
3615 if (fromView.getVisibility() == View.VISIBLE) {
3616 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3617 final boolean isWidgetTray =
3618 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003619
Adam Cohen2854d252014-08-27 16:04:07 -07003620 if (isWidgetTray) {
3621 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3622 } else {
3623 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003624 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003625
Adam Cohen2854d252014-08-27 16:04:07 -07003626 int width = revealView.getMeasuredWidth();
3627 int height = revealView.getMeasuredHeight();
3628 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3629
3630 // Hide the real page background, and swap in the fake one
3631 revealView.setVisibility(View.VISIBLE);
3632 content.setPageBackgroundsVisible(false);
3633
3634 final View allAppsButton = getAllAppsButton();
3635 revealView.setTranslationY(0);
3636 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3637 allAppsButton, null);
3638
3639 float xDrift = 0;
3640 float yDrift = 0;
3641 if (material) {
3642 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3643 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3644 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003645 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003646 xDrift = 0;
3647 }
3648
Adam Cohen80e6beb2015-02-13 16:14:33 -08003649 layerViews.put(revealView, BUILD_AND_SET_LAYER);
Adam Cohen2854d252014-08-27 16:04:07 -07003650 TimeInterpolator decelerateInterpolator = material ?
3651 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003652 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003653
3654 // The vertical motion of the apps panel should be delayed by one frame
3655 // from the conceal animation in order to give the right feel. We correpsondingly
3656 // shorten the duration so that the slide and conceal end at the same time.
3657 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3658 0, yDrift);
3659 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3660 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3661 panelDriftY.setInterpolator(decelerateInterpolator);
3662 mStateAnimation.play(panelDriftY);
3663
3664 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3665 0, xDrift);
3666 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3667 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3668 panelDriftX.setInterpolator(decelerateInterpolator);
3669 mStateAnimation.play(panelDriftX);
3670
3671 if (isWidgetTray || !material) {
3672 float finalAlpha = material ? 0.4f : 0f;
3673 revealView.setAlpha(1f);
3674 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3675 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003676 panelAlpha.setDuration(material ? revealDuration : 150);
3677 panelAlpha.setInterpolator(decelerateInterpolator);
3678 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003679 mStateAnimation.play(panelAlpha);
3680 }
3681
3682 if (page != null) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003683 layerViews.put(page, BUILD_AND_SET_LAYER);
Adam Cohen2854d252014-08-27 16:04:07 -07003684
3685 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3686 0, yDrift);
3687 page.setTranslationY(0);
3688 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3689 pageDrift.setInterpolator(decelerateInterpolator);
3690 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3691 mStateAnimation.play(pageDrift);
3692
3693 page.setAlpha(1f);
3694 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3695 itemsAlpha.setDuration(100);
3696 itemsAlpha.setInterpolator(decelerateInterpolator);
3697 mStateAnimation.play(itemsAlpha);
3698 }
3699
3700 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3701 pageIndicators.setAlpha(1f);
3702 ObjectAnimator indicatorsAlpha =
3703 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3704 indicatorsAlpha.setDuration(revealDuration);
3705 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3706 mStateAnimation.play(indicatorsAlpha);
3707
3708 width = revealView.getMeasuredWidth();
3709
3710 if (material) {
3711 if (!isWidgetTray) {
3712 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003713 }
Adam Cohen2854d252014-08-27 16:04:07 -07003714 int allAppsButtonSize = LauncherAppState.getInstance().
3715 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3716 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3717 Animator reveal =
3718 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3719 height / 2, revealRadius, finalRadius);
3720 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3721 reveal.setDuration(revealDuration);
3722 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003723
Adam Cohen2854d252014-08-27 16:04:07 -07003724 reveal.addListener(new AnimatorListenerAdapter() {
3725 public void onAnimationEnd(Animator animation) {
3726 revealView.setVisibility(View.INVISIBLE);
3727 if (!isWidgetTray) {
3728 allAppsButton.setVisibility(View.VISIBLE);
3729 }
3730 }
3731 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003732
Adam Cohen2854d252014-08-27 16:04:07 -07003733 mStateAnimation.play(reveal);
3734 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003735
Adam Cohen2854d252014-08-27 16:04:07 -07003736 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3737 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3738 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003739 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003740
3741 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003742 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003743 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003744 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003745 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3746 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003747 if (onCompleteRunnable != null) {
3748 onCompleteRunnable.run();
3749 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003750
Adam Cohen80e6beb2015-02-13 16:14:33 -08003751 for (View v : layerViews.keySet()) {
3752 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3753 v.setLayerType(View.LAYER_TYPE_NONE, null);
3754 }
Adam Cohen4e243a22014-08-10 18:30:55 -07003755 }
Adam Cohen80e6beb2015-02-13 16:14:33 -08003756
Adam Cohen9bfdb762014-07-21 17:44:06 -07003757 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003758 // Unhide side pages
3759 int count = content.getChildCount();
3760 for (int i = 0; i < count; i++) {
3761 View child = content.getChildAt(i);
3762 child.setVisibility(View.VISIBLE);
3763 }
3764
3765 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003766 if (page != null) {
3767 page.setTranslationX(0);
3768 page.setTranslationY(0);
3769 page.setAlpha(1);
3770 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003771 content.setCurrentPage(content.getNextPage());
3772
Chet Haasebc2f0822012-10-26 17:59:53 -07003773 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003774
3775 // This can hold unnecessary references to views.
3776 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003777 }
3778 });
3779
Adam Cohen2854d252014-08-27 16:04:07 -07003780 final AnimatorSet stateAnimation = mStateAnimation;
3781 final Runnable startAnimRunnable = new Runnable() {
3782 public void run() {
3783 // Check that mStateAnimation hasn't changed while
3784 // we waited for a layout/draw pass
3785 if (mStateAnimation != stateAnimation)
3786 return;
3787 dispatchOnLauncherTransitionStart(fromView, animated, false);
3788 dispatchOnLauncherTransitionStart(toView, animated, false);
3789
Adam Cohen80e6beb2015-02-13 16:14:33 -08003790 for (View v : layerViews.keySet()) {
3791 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3792 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3793 }
3794 }
3795
Kenny Guyd794a3f2014-09-16 15:17:58 +01003796 if (Utilities.isLmpOrAbove()) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003797 for (View v : layerViews.keySet()) {
3798 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen2854d252014-08-27 16:04:07 -07003799 }
3800 }
3801 mStateAnimation.start();
3802 }
3803 };
3804 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003805 } else {
3806 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003807 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003808 dispatchOnLauncherTransitionStart(fromView, animated, true);
3809 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003810 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003811 dispatchOnLauncherTransitionStart(toView, animated, true);
3812 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003813 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003814 }
3815
Michael Jurkae326f182011-11-21 14:05:46 -08003816 @Override
3817 public void onTrimMemory(int level) {
3818 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003819 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3820 // The widget preview db can result in holding onto over
3821 // 3MB of memory for caching which isn't necessary.
3822 SQLiteDatabase.releaseMemory();
3823
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003824 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003825 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003826 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003827 }
Michael Jurkae326f182011-11-21 14:05:46 -08003828 }
3829 }
3830
Adam Cohened307df2013-10-02 09:37:31 -07003831 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003832 showWorkspace(animated, null);
3833 }
3834
Adam Cohened307df2013-10-02 09:37:31 -07003835 protected void showWorkspace() {
3836 showWorkspace(true);
3837 }
3838
Adam Cohened66b2b2012-01-23 17:28:51 -08003839 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003840 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003841 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003842 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003843 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003844
Winson Chungc7d2b602012-05-16 17:02:20 -07003845 // Show the search bar (only animate if we were showing the drop target bar in spring
3846 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003847 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003848 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003849 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003850
Michael Jurkab3e22d92011-10-31 15:58:33 -07003851 // Set focus to the AppsCustomize button
3852 if (mAllAppsButton != null) {
3853 mAllAppsButton.requestFocus();
3854 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003855 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003856
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003857 // Change the state *after* we've called all the transition code
3858 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003859
Winson Chung5fb63472011-02-02 17:03:37 -08003860 // Resume the auto-advance of widgets
3861 mUserPresent = true;
3862 updateRunning();
3863
alanv1d4fde62012-10-17 13:15:47 -07003864 // Send an accessibility event to announce the context change
3865 getWindow().getDecorView()
3866 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003867
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003868 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003869 }
3870
Adam Cohened307df2013-10-02 09:37:31 -07003871 void showOverviewMode(boolean animated) {
3872 mWorkspace.setVisibility(View.VISIBLE);
3873 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3874 mState = State.WORKSPACE;
3875 onWorkspaceShown(animated);
3876 }
3877
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003878 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003879 }
3880
Winson Chung82963d52013-10-09 11:20:57 -07003881 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3882 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003883 if (mState != State.WORKSPACE) return;
3884
Winson Chung82963d52013-10-09 11:20:57 -07003885 if (resetPageToZero) {
3886 mAppsCustomizeTabHost.reset();
3887 }
Winson Chungc58497e2013-09-03 17:48:37 -07003888 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003889 mAppsCustomizeTabHost.post(new Runnable() {
3890 @Override
3891 public void run() {
3892 // We post this in-case the all apps view isn't yet constructed.
3893 mAppsCustomizeTabHost.requestFocus();
3894 }
3895 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003896
Michael Jurkab3e22d92011-10-31 15:58:33 -07003897 // Change the state *after* we've called all the transition code
3898 mState = State.APPS_CUSTOMIZE;
3899
3900 // Pause the auto-advance of widgets until we are out of AllApps
3901 mUserPresent = false;
3902 updateRunning();
3903 closeFolder();
3904
3905 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003906 getWindow().getDecorView()
3907 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003908 }
3909
Adam Cohen7777d962011-08-18 18:58:38 -07003910 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003911 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003912 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003913 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003914 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003915 }
Adam Cohen7777d962011-08-18 18:58:38 -07003916
Adam Cohenad4e15c2013-10-17 16:21:35 -07003917 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003918 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003919 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3920
Winson Chunge7a03942011-08-05 15:05:12 -07003921 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003922 @Override
3923 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003924 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003925 // Before we show workspace, hide all apps again because
3926 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3927 // clean up our state transition functions
3928 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003929 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003930 } else {
3931 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003932 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003933 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003934 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003935 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003936
Michael Jurkad3ef3062010-11-23 16:23:58 -08003937 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003938 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003939 final boolean animated = true;
3940 final boolean springLoaded = true;
3941 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003942 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003943 }
3944 // Otherwise, we are not in spring loaded mode, so don't do anything.
3945 }
3946
Michael Jurkab3e22d92011-10-31 15:58:33 -07003947 void lockAllApps() {
3948 // TODO
3949 }
3950
3951 void unlockAllApps() {
3952 // TODO
3953 }
3954
Sunny Goyal594d76d2014-11-06 10:12:54 -08003955 protected void disableVoiceButtonProxy(boolean disable) {
3956 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003957 }
3958
Cristina Stancu476493b2013-08-07 17:20:14 +01003959 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003960 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3961 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003962 }
3963
Adam Cohen24ce0b32014-01-14 16:18:14 -08003964 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003965 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3966 if (searchProvider == null) {
3967 return null;
3968 }
3969
3970 Bundle opts = new Bundle();
3971 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003972 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003973
3974 SharedPreferences sp = getSharedPreferences(
3975 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3976 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003977 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003978 if (!searchProvider.provider.flattenToString().equals(
3979 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003980 || (widgetInfo == null)
3981 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003982 // A valid widget is not already bound.
3983 if (widgetId > -1) {
3984 mAppWidgetHost.deleteAppWidgetId(widgetId);
3985 widgetId = -1;
3986 }
3987
3988 // Try to bind a new widget
3989 widgetId = mAppWidgetHost.allocateAppWidgetId();
3990
3991 if (!AppWidgetManagerCompat.getInstance(this)
3992 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3993 mAppWidgetHost.deleteAppWidgetId(widgetId);
3994 widgetId = -1;
3995 }
3996
3997 sp.edit()
3998 .putInt(QSB_WIDGET_ID, widgetId)
3999 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
4000 .commit();
4001 }
4002
4003 if (widgetId != -1) {
4004 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
4005 mQsb.updateAppWidgetOptions(opts);
4006 mQsb.setPadding(0, 0, 0, 0);
4007 mSearchDropTargetBar.addView(mQsb);
4008 }
Cristina Stancu476493b2013-08-07 17:20:14 +01004009 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08004010 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01004011 }
4012
Michael Jurkad7c28052012-04-27 15:43:36 -07004013 @Override
4014 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004015 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004016 final List<CharSequence> text = event.getText();
4017 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004018 // Populate event with a fake title based on the current state.
4019 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004020 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004021 } else {
4022 text.add(getString(R.string.all_apps_home_button_label));
4023 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004024 return result;
4025 }
4026
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004027 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004028 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004029 */
4030 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4031 @Override
4032 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004033 closeSystemDialogs();
4034 }
4035 }
4036
4037 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004038 * Receives notifications whenever the appwidgets are reset.
4039 */
4040 private class AppWidgetResetObserver extends ContentObserver {
4041 public AppWidgetResetObserver() {
4042 super(new Handler());
4043 }
4044
4045 @Override
4046 public void onChange(boolean selfChange) {
4047 onAppWidgetReset();
4048 }
4049 }
4050
4051 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004052 * If the activity is currently paused, signal that we need to run the passed Runnable
4053 * in onResume.
4054 *
4055 * This needs to be called from incoming places where resources might have been loaded
4056 * while we are paused. That is becaues the Configuration might be wrong
4057 * when we're not running, and if it comes back to what it was when we
4058 * were paused, we are not restarted.
4059 *
4060 * Implementation of the method from LauncherModel.Callbacks.
4061 *
4062 * @return true if we are currently paused. The caller might be able to
4063 * skip some work in that case since we will come back again.
4064 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004065 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004066 if (mPaused) {
4067 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004068 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004069 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004070 }
4071 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004072 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004073 return true;
4074 } else {
4075 return false;
4076 }
4077 }
4078
Michael Jurkac402cd92013-05-20 15:49:32 +02004079 private boolean waitUntilResume(Runnable run) {
4080 return waitUntilResume(run, false);
4081 }
4082
Michael Jurka1e2f4652013-07-08 18:03:46 -07004083 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004084 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004085 }
4086
Michael Jurka7607c2f2013-04-03 14:33:19 -07004087 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004088 * If the activity is currently paused, signal that we need to re-run the loader
4089 * in onResume.
4090 *
4091 * This needs to be called from incoming places where resources might have been loaded
4092 * while we are paused. That is becaues the Configuration might be wrong
4093 * when we're not running, and if it comes back to what it was when we
4094 * were paused, we are not restarted.
4095 *
4096 * Implementation of the method from LauncherModel.Callbacks.
4097 *
4098 * @return true if we are currently paused. The caller might be able to
4099 * skip some work in that case since we will come back again.
4100 */
4101 public boolean setLoadOnResume() {
4102 if (mPaused) {
4103 Log.i(TAG, "setLoadOnResume");
4104 mOnResumeNeedsLoad = true;
4105 return true;
4106 } else {
4107 return false;
4108 }
4109 }
4110
4111 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004112 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004113 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004114 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004115 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004116 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004117 } else {
4118 return SCREEN_COUNT / 2;
4119 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004120 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004121
Joe Onorato9c1289c2009-08-17 11:03:03 -04004122 /**
4123 * Refreshes the shortcuts shown on the workspace.
4124 *
4125 * Implementation of the method from LauncherModel.Callbacks.
4126 */
4127 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004128 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004129
Michael Jurka7607c2f2013-04-03 14:33:19 -07004130 // If we're starting binding all over again, clear any bind calls we'd postponed in
4131 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4132 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004133 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004134
Winson Chungd64d1762013-08-20 14:37:16 -07004135 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004136 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004137 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004138
Michael Jurka05bf6442011-11-30 20:28:53 -08004139 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004140 if (mHotseat != null) {
4141 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004142 }
4143 }
4144
Adam Cohendcd297f2013-06-18 13:13:40 -07004145 @Override
4146 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004147 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004148
Adam Cohen5084cba2013-09-03 12:01:16 -07004149 // If there are no screens, we need to have an empty screen
4150 if (orderedScreenIds.size() == 0) {
4151 mWorkspace.addExtraEmptyScreen();
4152 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004153
4154 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004155 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004156 if (hasCustomContentToLeft()) {
4157 mWorkspace.createCustomContentContainer();
4158 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004159 }
Winson Chung64359a52013-07-08 17:17:08 -07004160 }
4161
4162 @Override
4163 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004164 // Log to disk
4165 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4166 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4167 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004168 int count = orderedScreenIds.size();
4169 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004170 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004171 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004172 }
4173
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08004174 @Override
4175 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
4176 final long screenId, final int[] cell, final int spanX, final int spanY) {
4177 showWorkspace(true, new Runnable() {
4178
4179 @Override
4180 public void run() {
4181 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
4182 addPendingItem(info, container, screenId, cell, spanX, spanY);
4183 }
4184 });
4185 }
4186
Adam Cohen39a06042013-07-19 14:30:12 -07004187 private boolean shouldShowWeightWatcher() {
4188 String spKey = LauncherAppState.getSharedPreferencesKey();
4189 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004190 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004191
4192 return show;
4193 }
4194
4195 private void toggleShowWeightWatcher() {
4196 String spKey = LauncherAppState.getSharedPreferencesKey();
4197 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4198 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4199
4200 show = !show;
4201
4202 SharedPreferences.Editor editor = sp.edit();
4203 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4204 editor.commit();
4205
4206 if (mWeightWatcher != null) {
4207 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4208 }
4209 }
4210
Winson Chungd64d1762013-08-20 14:37:16 -07004211 public void bindAppsAdded(final ArrayList<Long> newScreens,
4212 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004213 final ArrayList<ItemInfo> addAnimated,
4214 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004215 Runnable r = new Runnable() {
4216 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004217 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004218 }
4219 };
4220 if (waitUntilResume(r)) {
4221 return;
4222 }
4223
Winson Chungd64d1762013-08-20 14:37:16 -07004224 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004225 if (newScreens != null) {
4226 bindAddScreens(newScreens);
4227 }
Winson Chungd64d1762013-08-20 14:37:16 -07004228
4229 // We add the items without animation on non-visible pages, and with
4230 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004231 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004232 bindItems(addNotAnimated, 0,
4233 addNotAnimated.size(), false);
4234 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004235 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004236 bindItems(addAnimated, 0,
4237 addAnimated.size(), true);
4238 }
Winson Chungc58497e2013-09-03 17:48:37 -07004239
Winson Chung87412982013-10-03 18:34:14 -07004240 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004241 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004242
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004243 if (addedApps != null && mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004244 mAppsCustomizeContent.addApps(addedApps);
4245 }
Winson Chungd64d1762013-08-20 14:37:16 -07004246 }
4247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004248 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004249 * Bind the items start-end from the list.
4250 *
4251 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004252 */
Winson Chung64359a52013-07-08 17:17:08 -07004253 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4254 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004255 Runnable r = new Runnable() {
4256 public void run() {
4257 bindItems(shortcuts, start, end, forceAnimateIcons);
4258 }
4259 };
4260 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004261 return;
4262 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004263
Winson Chungf0c6ae02012-03-21 16:10:31 -07004264 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004265 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4266 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004267 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004268 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004269 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004270 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004271 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004272
4273 // Short circuit if we are loading dock items for a configuration which has no dock
4274 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4275 mHotseat == null) {
4276 continue;
4277 }
4278
Joe Onorato9c1289c2009-08-17 11:03:03 -04004279 switch (item.itemType) {
4280 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4281 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004282 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004283 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004284
Winson Chung64359a52013-07-08 17:17:08 -07004285 /*
4286 * TODO: FIX collision case
4287 */
Winson Chungce376632013-07-11 16:12:41 -07004288 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4289 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4290 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004291 View v = cl.getChildAt(item.cellX, item.cellY);
4292 Object tag = v.getTag();
4293 String desc = "Collision while binding workspace item: " + item
4294 + ". Collides with " + tag;
4295 if (LauncherAppState.isDogfoodBuild()) {
4296 throw (new RuntimeException(desc));
4297 } else {
4298 Log.d(TAG, desc);
4299 }
Winson Chungce376632013-07-11 16:12:41 -07004300 }
Winson Chung64359a52013-07-08 17:17:08 -07004301 }
4302
Adam Cohendcd297f2013-06-18 13:13:40 -07004303 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4304 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004305 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004306 // Animate all the applications up now
4307 shortcut.setAlpha(0f);
4308 shortcut.setScaleX(0f);
4309 shortcut.setScaleY(0f);
4310 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004311 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004312 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004313 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004314 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004315 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004316 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004317 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004318 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4319 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004320 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004321 default:
4322 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004323 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004324 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004325
Winson Chung997a9232013-07-24 15:33:46 -07004326 if (animateIcons) {
4327 // Animate to the correct page
4328 if (newShortcutsScreenId > -1) {
4329 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004330 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004331 final Runnable startBounceAnimRunnable = new Runnable() {
4332 public void run() {
4333 anim.playTogether(bounceAnims);
4334 anim.start();
4335 }
4336 };
Winson Chung997a9232013-07-24 15:33:46 -07004337 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004338 // We post the animation slightly delayed to prevent slowdowns
4339 // when we are loading right after we return to launcher.
4340 mWorkspace.postDelayed(new Runnable() {
4341 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004342 if (mWorkspace != null) {
4343 mWorkspace.snapToPage(newScreenIndex);
4344 mWorkspace.postDelayed(startBounceAnimRunnable,
4345 NEW_APPS_ANIMATION_DELAY);
4346 }
Winson Chung94d67682013-09-25 16:29:40 -07004347 }
4348 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004349 } else {
4350 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004351 }
4352 }
Winson Chung64359a52013-07-08 17:17:08 -07004353 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004354 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004355 }
4356
Joe Onorato9c1289c2009-08-17 11:03:03 -04004357 /**
4358 * Implementation of the method from LauncherModel.Callbacks.
4359 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004360 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004361 Runnable r = new Runnable() {
4362 public void run() {
4363 bindFolders(folders);
4364 }
4365 };
4366 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004367 return;
4368 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004369 sFolders.clear();
4370 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004371 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004372
4373 /**
4374 * Add the views for a widget to the workspace.
4375 *
4376 * Implementation of the method from LauncherModel.Callbacks.
4377 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004378 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004379 Runnable r = new Runnable() {
4380 public void run() {
4381 bindAppWidget(item);
4382 }
4383 };
4384 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004385 return;
4386 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004387
Daniel Sandler843e8602010-06-07 14:59:01 -04004388 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4389 if (DEBUG_WIDGETS) {
4390 Log.d(TAG, "bindAppWidget: " + item);
4391 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004392 final Workspace workspace = mWorkspace;
4393
Adam Cohen59400422014-03-05 18:07:04 -08004394 LauncherAppWidgetProviderInfo appWidgetInfo =
4395 LauncherModel.getProviderInfo(this, item.providerName);
4396
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004397 if (!mIsSafeModeEnabled
4398 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4399 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004400 if (appWidgetInfo == null) {
4401 if (DEBUG_WIDGETS) {
4402 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4403 + " belongs to component " + item.providerName
4404 + ", as the povider is null");
4405 }
4406 LauncherModel.deleteItemFromDatabase(this, item);
4407 return;
4408 }
4409 // Note: This assumes that the id remap broadcast is received before this step.
4410 // If that is not the case, the id remap will be ignored and user may see the
4411 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004412 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004413 pendingInfo.spanX = item.spanX;
4414 pendingInfo.spanY = item.spanY;
4415 pendingInfo.minSpanX = item.minSpanX;
4416 pendingInfo.minSpanY = item.minSpanY;
4417 Bundle options =
4418 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4419
Sunny Goyalff572272014-07-23 13:58:07 -07004420 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004421 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4422 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004423
4424 // TODO consider showing a permission dialog when the widget is clicked.
4425 if (!success) {
4426 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4427 if (DEBUG_WIDGETS) {
4428 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4429 + " belongs to component " + item.providerName
4430 + ", as the launcher is unable to bing a new widget id");
4431 }
4432 LauncherModel.deleteItemFromDatabase(this, item);
4433 return;
4434 }
4435
4436 item.appWidgetId = newWidgetId;
4437
4438 // If the widget has a configure activity, it is still needs to set it up, otherwise
4439 // the widget is ready to go.
4440 item.restoreStatus = (appWidgetInfo.configure == null)
4441 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4442 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4443
4444 LauncherModel.updateItemInDatabase(this, item);
4445 }
4446
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004447 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004448 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004449 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004450 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4451 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004452 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004453
Sunny Goyal651077b2014-06-30 14:15:31 -07004454 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4455 } else {
4456 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004457 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4458 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004459 view.updateIcon(mIconCache);
4460 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004461 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004462 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004463 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004464
Joe Onorato9c1289c2009-08-17 11:03:03 -04004465 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004466 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004467
Adam Cohendcd297f2013-06-18 13:13:40 -07004468 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004469 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004470 if (!item.isCustomWidget()) {
4471 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4472 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004473
Joe Onorato9c1289c2009-08-17 11:03:03 -04004474 workspace.requestLayout();
4475
Daniel Sandler843e8602010-06-07 14:59:01 -04004476 if (DEBUG_WIDGETS) {
4477 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4478 + (SystemClock.uptimeMillis()-start) + "ms");
4479 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004480 }
4481
Sunny Goyalff572272014-07-23 13:58:07 -07004482 /**
4483 * Restores a pending widget.
4484 *
4485 * @param appWidgetId The app widget id
4486 * @param cellInfo The position on screen where to create the widget.
4487 */
4488 private void completeRestoreAppWidget(final int appWidgetId) {
4489 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4490 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4491 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4492 return;
4493 }
4494
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004495 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004496 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4497
4498 mWorkspace.reinflateWidgetsIfNecessary();
4499 LauncherModel.updateItemInDatabase(this, info);
4500 }
4501
Adam Cohen1462de32012-07-24 22:34:36 -07004502 public void onPageBoundSynchronously(int page) {
4503 mSynchronouslyBoundPages.add(page);
4504 }
4505
Joe Onorato9c1289c2009-08-17 11:03:03 -04004506 /**
4507 * Callback saying that there aren't any more items to bind.
4508 *
4509 * Implementation of the method from LauncherModel.Callbacks.
4510 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004511 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004512 Runnable r = new Runnable() {
4513 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004514 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004515 }
4516 };
4517 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004518 return;
4519 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004520 if (mSavedState != null) {
4521 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004522 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004523 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004524 mSavedState = null;
4525 }
4526
Adam Cohen1462de32012-07-24 22:34:36 -07004527 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004528
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004529 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004530 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004531
4532 // If we received the result of any pending adds while the loader was running (e.g. the
4533 // widget configuration forced an orientation change), process them now.
4534 if (sPendingAddItem != null) {
4535 final long screenId = completeAdd(sPendingAddItem);
4536
4537 // TODO: this moves the user to the page where the pending item was added. Ideally,
4538 // the screen would be guaranteed to exist after bind, and the page would be set through
4539 // the workspace restore process.
4540 mWorkspace.post(new Runnable() {
4541 @Override
4542 public void run() {
4543 mWorkspace.snapToScreenId(screenId);
4544 }
4545 });
4546 sPendingAddItem = null;
4547 }
4548
Sunny Goyale755d462014-07-22 13:48:29 -07004549 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004550
4551 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004552 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004553 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004554 }
4555
Adam Cohen3ed316a2014-07-23 18:21:20 -07004556 private void sendLoadingCompleteBroadcastIfNecessary() {
4557 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4558 String permission =
4559 getResources().getString(R.string.receive_first_load_broadcast_permission);
4560 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4561 sendBroadcast(intent, permission);
4562 SharedPreferences.Editor editor = mSharedPrefs.edit();
4563 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4564 editor.apply();
4565 }
4566 }
4567
Winson Chunga0b7e862013-09-05 16:03:15 -07004568 public boolean isAllAppsButtonRank(int rank) {
4569 if (mHotseat != null) {
4570 return mHotseat.isAllAppsButtonRank(rank);
4571 }
4572 return false;
4573 }
4574
Winson Chunga2413752012-04-03 14:22:34 -07004575 private boolean canRunNewAppsAnimation() {
4576 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4577 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4578 }
4579
Winson Chungc9168342013-06-26 14:54:55 -07004580 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4581 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4582 PropertyValuesHolder.ofFloat("alpha", 1f),
4583 PropertyValuesHolder.ofFloat("scaleX", 1f),
4584 PropertyValuesHolder.ofFloat("scaleY", 1f));
4585 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4586 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4587 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4588 return bounceAnim;
4589 }
4590
Adam Cohen27772732013-11-11 14:00:56 +00004591 public boolean useVerticalBarLayout() {
4592 return LauncherAppState.getInstance().getDynamicGrid().
4593 getDeviceProfile().isVerticalBarLayout();
4594 }
4595
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004596 protected Rect getSearchBarBounds() {
4597 return LauncherAppState.getInstance().getDynamicGrid().
4598 getDeviceProfile().getSearchBarBounds();
4599 }
4600
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004601 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004602 if (mSearchDropTargetBar == null) {
4603 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004604 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004605 if (mQsb != null) {
4606 mSearchDropTargetBar.removeView(mQsb);
4607 mQsb = null;
4608 }
4609 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004610 }
4611
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004612 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004613 * Add the icons for all apps.
4614 *
4615 * Implementation of the method from LauncherModel.Callbacks.
4616 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004617 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004618 if (mAppsCustomizeContent != null) {
4619 mAppsCustomizeContent.setApps(apps);
4620 mAppsCustomizeContent.onPackagesUpdated(
4621 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -07004622 }
Adam Cohen9211d422014-10-07 18:14:53 -07004623 if (mLauncherCallbacks != null) {
4624 mLauncherCallbacks.bindAllApplications(apps);
4625 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004626 }
4627
4628 /**
4629 * A package was updated.
4630 *
4631 * Implementation of the method from LauncherModel.Callbacks.
4632 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004633 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004634 Runnable r = new Runnable() {
4635 public void run() {
4636 bindAppsUpdated(apps);
4637 }
4638 };
4639 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004640 return;
4641 }
4642
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004643 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004644 mAppsCustomizeContent.updateApps(apps);
4645 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004646 }
4647
Sunny Goyal4390ace2014-10-13 11:33:11 -07004648 @Override
4649 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4650 Runnable r = new Runnable() {
4651 public void run() {
4652 bindWidgetsRestored(widgets);
4653 }
4654 };
4655 if (waitUntilResume(r)) {
4656 return;
4657 }
4658 mWorkspace.widgetsRestored(widgets);
4659 }
4660
Joe Onorato9c1289c2009-08-17 11:03:03 -04004661 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004662 * Some shortcuts were updated in the background.
4663 *
4664 * Implementation of the method from LauncherModel.Callbacks.
4665 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004666 @Override
4667 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4668 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004669 Runnable r = new Runnable() {
4670 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004671 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004672 }
4673 };
4674 if (waitUntilResume(r)) {
4675 return;
4676 }
4677
Sunny Goyal4390ace2014-10-13 11:33:11 -07004678 if (!updated.isEmpty()) {
4679 mWorkspace.updateShortcuts(updated);
4680 }
4681
4682 if (!removed.isEmpty()) {
4683 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4684 for (ShortcutInfo si : removed) {
4685 removedComponents.add(si.getTargetComponent());
4686 }
4687 mWorkspace.removeItemsByComponentName(removedComponents, user);
4688 // Notify the drag controller
4689 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004690 }
4691 }
4692
4693 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004694 * Update the state of a package, typically related to install state.
4695 *
4696 * Implementation of the method from LauncherModel.Callbacks.
4697 */
Sunny Goyale755d462014-07-22 13:48:29 -07004698 @Override
4699 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004700 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004701 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004702 }
4703 }
4704
4705 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004706 * Update the label and icon of all the icons in a package
4707 *
4708 * Implementation of the method from LauncherModel.Callbacks.
4709 */
4710 @Override
4711 public void updatePackageBadge(String packageName) {
4712 if (mWorkspace != null) {
4713 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4714 }
4715 }
4716
4717 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004718 * A package was uninstalled. We take both the super set of packageNames
4719 * in addition to specific applications to remove, the reason being that
4720 * this can be called when a package is updated as well. In that scenario,
4721 * we only remove specific components from the workspace, where as
4722 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004723 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004724 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004725 * Implementation of the method from LauncherModel.Callbacks.
4726 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004727 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004728 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004729 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004730 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004731 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004732 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004733 }
Winson Chungd64d1762013-08-20 14:37:16 -07004734 };
4735 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004736 return;
4737 }
4738
Sunny Goyal1a745e82014-10-02 15:58:31 -07004739 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004740 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4741 for (AppInfo info : appInfos) {
4742 removedComponents.add(info.componentName);
4743 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004744 if (!packageNames.isEmpty()) {
4745 mWorkspace.removeItemsByPackageName(packageNames, user);
4746 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004747 if (!removedComponents.isEmpty()) {
4748 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004749 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004750 // Notify the drag controller
4751 mDragController.onAppsRemoved(packageNames, removedComponents);
4752
Sunny Goyal1a745e82014-10-02 15:58:31 -07004753 } else {
4754 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004755 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004756
Winson Chungdf95eb12013-10-16 14:57:07 -07004757 // Update AllApps
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004758 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004759 mAppsCustomizeContent.removeApps(appInfos);
4760 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004761 }
Joe Onoratobe386092009-11-17 17:32:16 -08004762
4763 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004764 * A number of packages were updated.
4765 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004766 private ArrayList<Object> mWidgetsAndShortcuts;
4767 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004768 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004769 bindPackagesUpdated(mWidgetsAndShortcuts);
4770 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004771 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004772 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004773 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4774 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4775 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004776 return;
4777 }
4778
Winson Chung64359a52013-07-08 17:17:08 -07004779 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004780 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004781 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004782 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004783 }
4784
Winson Chung400438b2011-01-16 17:53:48 -08004785 private int mapConfigurationOriActivityInfoOri(int configOri) {
4786 final Display d = getWindowManager().getDefaultDisplay();
4787 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4788 switch (d.getRotation()) {
4789 case Surface.ROTATION_0:
4790 case Surface.ROTATION_180:
4791 // We are currently in the same basic orientation as the natural orientation
4792 naturalOri = configOri;
4793 break;
4794 case Surface.ROTATION_90:
4795 case Surface.ROTATION_270:
4796 // We are currently in the other basic orientation to the natural orientation
4797 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4798 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4799 break;
4800 }
4801
4802 int[] oriMap = {
4803 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4804 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4805 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4806 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4807 };
4808 // Since the map starts at portrait, we need to offset if this device's natural orientation
4809 // is landscape.
4810 int indexOffset = 0;
4811 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4812 indexOffset = 1;
4813 }
4814 return oriMap[(d.getRotation() + indexOffset) % 4];
4815 }
Adam Cohen446e9402011-09-15 18:21:21 -07004816
Winson Chung4b919f82012-05-01 10:44:08 -07004817 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004818 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004819 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4820 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4821 .getConfiguration().orientation));
4822 } else {
4823 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4824 }
Winson Chung4b919f82012-05-01 10:44:08 -07004825 }
4826 }
4827 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004828 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004829 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004830 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004831 } else {
4832 mHandler.postDelayed(new Runnable() {
4833 public void run() {
4834 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4835 }
4836 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004837 }
Winson Chung4b919f82012-05-01 10:44:08 -07004838 }
Winson Chung400438b2011-01-16 17:53:48 -08004839 }
4840
Winson Chunge43a1e72014-01-15 10:33:02 -08004841 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004842 if (mLauncherCallbacks != null) {
4843 return mLauncherCallbacks.isLauncherPreinstalled();
4844 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004845 PackageManager pm = getPackageManager();
4846 try {
4847 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4848 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4849 return true;
4850 } else {
4851 return false;
4852 }
4853 } catch (NameNotFoundException e) {
4854 e.printStackTrace();
4855 return false;
4856 }
4857 }
4858
Adam Cohenea90f832014-05-21 15:03:34 -07004859 /**
4860 * This method indicates whether or not we should suggest default wallpaper dimensions
4861 * when our wallpaper cropper was not yet used to set a wallpaper.
4862 */
4863 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004864 if (mLauncherCallbacks != null) {
4865 return mLauncherCallbacks.overrideWallpaperDimensions();
4866 }
Adam Cohenea90f832014-05-21 15:03:34 -07004867 return true;
4868 }
4869
Adam Cohen432609a2014-03-13 17:03:22 -07004870 /**
4871 * To be overridden by subclasses to indicate that there is an activity to launch
4872 * before showing the standard launcher experience.
4873 */
4874 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004875 if (mLauncherCallbacks != null) {
4876 return mLauncherCallbacks.hasFirstRunActivity();
4877 }
Adam Cohen432609a2014-03-13 17:03:22 -07004878 return false;
4879 }
4880
4881 /**
4882 * To be overridden by subclasses to launch any first run activity
4883 */
4884 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004885 if (mLauncherCallbacks != null) {
4886 return mLauncherCallbacks.getFirstRunActivity();
4887 }
Adam Cohen432609a2014-03-13 17:03:22 -07004888 return null;
4889 }
4890
Winson Chunga6945242014-01-08 14:04:34 -08004891 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004892 return !ActivityManager.isRunningInTestHarness() &&
4893 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004894 }
4895
Adam Cohen4a9423d2014-03-28 14:22:31 -07004896 protected boolean hasRunFirstRunActivity() {
4897 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4898 }
4899
Adam Cohen432609a2014-03-13 17:03:22 -07004900 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004901 if (shouldRunFirstRunActivity() &&
4902 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004903 Intent firstRunIntent = getFirstRunActivity();
4904 if (firstRunIntent != null) {
4905 startActivity(firstRunIntent);
4906 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004907 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004908 }
4909 }
Adam Cohen432609a2014-03-13 17:03:22 -07004910 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004911 }
4912
4913 private void markFirstRunActivityShown() {
4914 SharedPreferences.Editor editor = mSharedPrefs.edit();
4915 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4916 editor.apply();
4917 }
4918
Adam Cohen432609a2014-03-13 17:03:22 -07004919 /**
4920 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4921 * screen that must be displayed and dismissed.
4922 */
4923 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004924 if (mLauncherCallbacks != null) {
4925 return mLauncherCallbacks.hasDismissableIntroScreen();
4926 }
Adam Cohen432609a2014-03-13 17:03:22 -07004927 return false;
4928 }
4929
4930 /**
4931 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4932 */
4933 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004934 if (mLauncherCallbacks != null) {
4935 return mLauncherCallbacks.getIntroScreen();
4936 }
Adam Cohen432609a2014-03-13 17:03:22 -07004937 return null;
4938 }
4939
4940 /**
4941 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4942 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4943 */
4944 private boolean shouldShowIntroScreen() {
4945 return hasDismissableIntroScreen() &&
4946 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4947 }
4948
4949 protected void showIntroScreen() {
4950 View introScreen = getIntroScreen();
4951 changeWallpaperVisiblity(false);
4952 if (introScreen != null) {
4953 mDragLayer.showOverlayView(introScreen);
4954 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004955 if (mLauncherOverlayContainer != null) {
4956 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4957 }
Adam Cohen432609a2014-03-13 17:03:22 -07004958 }
4959
4960 public void dismissIntroScreen() {
4961 markIntroScreenDismissed();
4962 if (showFirstRunActivity()) {
4963 // We delay hiding the intro view until the first run activity is showing. This
4964 // avoids a blip.
4965 mWorkspace.postDelayed(new Runnable() {
4966 @Override
4967 public void run() {
4968 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004969 if (mLauncherOverlayContainer != null) {
4970 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4971 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004972 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004973 }
4974 }, ACTIVITY_START_DELAY);
4975 } else {
4976 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004977 if (mLauncherOverlayContainer != null) {
4978 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4979 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004980 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004981 }
4982 changeWallpaperVisiblity(true);
4983 }
4984
4985 private void markIntroScreenDismissed() {
4986 SharedPreferences.Editor editor = mSharedPrefs.edit();
4987 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4988 editor.apply();
4989 }
4990
Sunny Goyal88d60f12014-09-08 03:47:29 -07004991 private void showFirstRunClings() {
4992 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4993 // on the device, then we always show the first run cling experience (or if there is no
4994 // launcher2). Otherwise, we prompt the user upon started for migration
4995 LauncherClings launcherClings = new LauncherClings(this);
4996 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4997 if (mModel.canMigrateFromOldLauncherDb(this)) {
4998 launcherClings.showMigrationCling();
4999 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005000 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005001 }
5002 }
5003 }
5004
Winson Chunga6945242014-01-08 14:04:34 -08005005 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005006 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5007 if (mHotseat != null) mHotseat.setAlpha(1f);
5008 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5009 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005010 }
5011
5012 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005013 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5014 if (mHotseat != null) mHotseat.setAlpha(0f);
5015 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5016 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005017 }
5018
Mathew Inwood72fbec12013-11-19 15:45:07 +00005019 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005020 // Called from search suggestion, not supported in other profiles.
5021 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5022 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5023 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5024 myUser);
5025 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005026 return null;
5027 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08005028 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005029 }
5030
5031 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5032 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005033 // Called from search suggestion, not supported in other profiles.
5034 return createShortcutDragInfo(shortcutIntent, caption, icon,
5035 UserHandleCompat.myUserHandle());
5036 }
5037
5038 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5039 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005040 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005041 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005042 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005043 }
5044
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005045 protected void moveWorkspaceToDefaultScreen() {
5046 mWorkspace.moveToDefaultScreen(false);
5047 }
5048
Mathew Inwood72fbec12013-11-19 15:45:07 +00005049 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5050 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005051 mWorkspace.onExternalDragStartedWithItem(dragView);
5052 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005053 }
5054
Anjali Koppalf5d29132014-02-28 13:33:27 -08005055 @Override
5056 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005057 if (mLauncherCallbacks != null) {
5058 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5059 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005060 }
5061
Winson Chung80baf5a2010-08-09 16:03:15 -07005062 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005063 * Prints out out state for debugging.
5064 */
5065 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005066 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005067 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005068 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5069 Log.d(TAG, "mRestoring=" + mRestoring);
5070 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5071 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005072 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005073 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005074
Winson Chung785d2eb2011-04-14 16:08:02 -07005075 if (mAppsCustomizeContent != null) {
5076 mAppsCustomizeContent.dumpState();
5077 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005078 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005079 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005080
5081 @Override
5082 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5083 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005084 synchronized (sDumpLogs) {
5085 writer.println(" ");
5086 writer.println("Debug logs: ");
5087 for (int i = 0; i < sDumpLogs.size(); i++) {
5088 writer.println(" " + sDumpLogs.get(i));
5089 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005090 }
Adam Cohen9211d422014-10-07 18:14:53 -07005091 if (mLauncherCallbacks != null) {
5092 mLauncherCallbacks.dump(prefix, fd, writer, args);
5093 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005094 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005095
5096 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005097 if (DEBUG_DUMP_LOG) {
5098 synchronized (sDumpLogs) {
5099 Log.d(TAG, "");
5100 Log.d(TAG, "*********************");
5101 Log.d(TAG, "Launcher debug logs: ");
5102 for (int i = 0; i < sDumpLogs.size(); i++) {
5103 Log.d(TAG, " " + sDumpLogs.get(i));
5104 }
5105 Log.d(TAG, "*********************");
5106 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005107 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005108 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005109 }
5110
5111 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005112 addDumpLog(tag, log, null, debugLog);
5113 }
5114
5115 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005116 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005117 if (e != null) {
5118 Log.d(tag, log, e);
5119 } else {
5120 Log.d(tag, log);
5121 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005122 }
Winson Chungede41292013-09-19 16:27:36 -07005123 if (DEBUG_DUMP_LOG) {
5124 sDateStamp.setTime(System.currentTimeMillis());
5125 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005126 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5127 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005128 }
Winson Chungede41292013-09-19 16:27:36 -07005129 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005130 }
5131
Adam Cohen59400422014-03-05 18:07:04 -08005132 public static CustomAppWidget getCustomAppWidget(String name) {
5133 return sCustomAppWidgets.get(name);
5134 }
5135
5136 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
5137 return sCustomAppWidgets;
5138 }
5139
Winson Chungede41292013-09-19 16:27:36 -07005140 public void dumpLogsToLocalData() {
5141 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005142 new AsyncTask<Void, Void, Void>() {
5143 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005144 boolean success = false;
5145 sDateStamp.setTime(sRunStart);
5146 String FILENAME = sDateStamp.getMonth() + "-"
5147 + sDateStamp.getDay() + "_"
5148 + sDateStamp.getHours() + "-"
5149 + sDateStamp.getMinutes() + "_"
5150 + sDateStamp.getSeconds() + ".txt";
5151
5152 FileOutputStream fos = null;
5153 File outFile = null;
5154 try {
5155 outFile = new File(getFilesDir(), FILENAME);
5156 outFile.createNewFile();
5157 fos = new FileOutputStream(outFile);
5158 } catch (Exception e) {
5159 e.printStackTrace();
5160 }
5161 if (fos != null) {
5162 PrintWriter writer = new PrintWriter(fos);
5163
5164 writer.println(" ");
5165 writer.println("Debug logs: ");
5166 synchronized (sDumpLogs) {
5167 for (int i = 0; i < sDumpLogs.size(); i++) {
5168 writer.println(" " + sDumpLogs.get(i));
5169 }
5170 }
5171 writer.close();
5172 }
5173 try {
5174 if (fos != null) {
5175 fos.close();
5176 success = true;
5177 }
5178 } catch (IOException e) {
5179 e.printStackTrace();
5180 }
Michael Jurka43467462013-11-14 12:03:58 +01005181 return null;
Winson Chungede41292013-09-19 16:27:36 -07005182 }
Michael Jurka43467462013-11-14 12:03:58 +01005183 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005184 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005185 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005186}
Michael Jurka2763be32011-02-24 11:19:57 -08005187
Michael Jurkaabded662011-03-04 12:06:57 -08005188interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005189 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005190 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005191 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005192 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005193 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005194}
Dan Sandlerd5024042014-01-09 15:01:33 -05005195
5196interface DebugIntents {
5197 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5198 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005199}