blob: 984d536a566c3d63b12f4617d6e9c7502d323249 [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.InvocationTargetException;
120import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700121import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700122import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700123import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700124import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700125import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700126import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700127import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000128import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130/**
131 * Default launcher application.
132 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100133public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700134 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700135 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800136 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700137 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400140 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700141 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400142 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700143 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700144
Dan Sandlerd5024042014-01-09 15:01:33 -0500145 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700150 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Michael Jurka8b805b12012-04-18 14:23:14 -0700152 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700153 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700154
Mathew Inwood876a8462013-06-14 14:12:41 +0100155 /**
156 * IntentStarter uses request codes starting with this. This must be greater than all activity
157 * request codes used internally.
158 */
159 protected static final int REQUEST_LAST = 100;
160
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
162
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800163 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
Michael Jurka0a457bf2012-11-19 14:05:05 -0800165 // To turn on these properties, type
166 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800167 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Winson Chung2672ff92012-05-04 16:22:30 -0700169 // The Intent extra that defines whether to ignore the launch animation
170 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400171 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 // Type: int
174 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700175 // Type: int
176 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700178 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
179 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
181 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700182 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700184 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: boolean
186 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
187 // Type: long
188 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700189 // Type: int
190 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
191 // Type: int
192 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
193 // Type: parcelable
194 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000195 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000197 // Type: int[]
198 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Adam Cohen432609a2014-03-13 17:03:22 -0700200 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800201 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
202
Adam Cohen3ed316a2014-07-23 18:21:20 -0700203 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
204 static final String ACTION_FIRST_LOAD_COMPLETE =
205 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
206
Adam Cohen39a06042013-07-19 14:30:12 -0700207 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700208 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700209
Sunny Goyal594d76d2014-11-06 10:12:54 -0800210 private static final String QSB_WIDGET_ID = "qsb_widget_id";
211 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
212
Winson Chunga6945242014-01-08 14:04:34 -0800213 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
214
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700215 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700216 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700217 private State mState = State.WORKSPACE;
218 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700219
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700220 private boolean mIsSafeModeEnabled;
221
Adam Cohenc2d6e892014-10-16 09:49:24 -0700222 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
223 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700224 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700225
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700227 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
228 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700229 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000231 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
232 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
233
Winson Chunga2413752012-04-03 14:22:34 -0700234 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700235 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
236 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700237 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700238 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700239
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800240 private final BroadcastReceiver mCloseSystemDialogsReceiver
241 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800242 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 private LayoutInflater mInflater;
245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700247 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800248 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800249 private DragLayer mDragLayer;
250 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700251 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700252
Sunny Goyalffe83f12014-08-14 17:39:34 -0700253 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700254 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700255
Michael Jurkac9d95c52011-08-29 14:03:34 -0700256 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800257 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800258 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700259
Michael Jurka0280c3b2010-09-17 15:00:07 -0700260 private int[] mTmpAddItemCellCoordinates = new int[2];
261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 private FolderInfo mFolderInfo;
263
Winson Chung3d503fb2011-07-13 17:25:49 -0700264 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800265 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700266
Michael Jurka838a4ca2011-02-07 13:33:06 -0800267 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700268
Winson Chungc51db6a2011-10-05 11:44:49 -0700269 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700270 private AppsCustomizeTabHost mAppsCustomizeTabHost;
271 private AppsCustomizePagedView mAppsCustomizeContent;
272 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800273 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700276 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
277 // scroll issues (because the workspace may not have been measured yet) and extra work.
278 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
279 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280
281 private SpannableStringBuilder mDefaultKeySsb = null;
282
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283 private boolean mWorkspaceLoading = true;
284
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800285 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 private boolean mRestoring;
287 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700288 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289
Michael Jurka1e2f4652013-07-08 18:03:46 -0700290 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700291 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 private Bundle mSavedInstanceState;
294
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800296 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800297 private boolean mUserPresent = true;
298 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700299 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800300 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400301
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700302 private static LocaleConfiguration sLocaleConfiguration = null;
303
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700304 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700305
Adam Cohen61f560d2013-09-30 15:58:20 -0700306 private View.OnTouchListener mHapticFeedbackTouchListener;
307
Adam Cohen80e6beb2015-02-13 16:14:33 -0800308 public static final int BUILD_LAYER = 0;
309 public static final int BUILD_AND_SET_LAYER = 1;
310
Adam Cohended9f8d2010-11-03 13:25:16 -0700311 // Related to the auto-advancing of widgets
312 private final int ADVANCE_MSG = 1;
313 private final int mAdvanceInterval = 20000;
314 private final int mAdvanceStagger = 250;
315 private long mAutoAdvanceSentTime;
316 private long mAutoAdvanceTimeLeft = -1;
317 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
318 new HashMap<View, AppWidgetProviderInfo>();
319
Winson Chung400438b2011-01-16 17:53:48 -0800320 // Determines how long to wait after a rotation before restoring the screen orientation to
321 // match the sensor state.
322 private final int mRestoreScreenOrientationDelay = 500;
323
Craig Mautner360310b2012-10-26 15:13:08 -0700324 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700325
Adam Cohen1462de32012-07-24 22:34:36 -0700326 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700327 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700328
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700329 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700330 static Date sDateStamp = new Date();
331 static DateFormat sDateFormat =
332 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
333 static long sRunStart = System.currentTimeMillis();
334 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700335
Winson Chung46353de2012-02-16 14:05:10 -0800336 // We only want to get the SharedPreferences once since it does an FS stat each time we get
337 // it from the context.
338 private SharedPreferences mSharedPrefs;
339
Winson Chungf0c6ae02012-03-21 16:10:31 -0700340 // Holds the page that we need to animate to, and the icon views that we need to animate up
341 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700342 private ImageView mFolderIconImageView;
343 private Bitmap mFolderIconBitmap;
344 private Canvas mFolderIconCanvas;
345 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700346
Michael Jurkaddd62e92011-02-16 17:49:14 -0800347 private BubbleTextView mWaitingForResume;
348
Adam Cohen59400422014-03-05 18:07:04 -0800349 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
350 new HashMap<String, CustomAppWidget>();
351
352 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
353 static {
354 if (ENABLE_CUSTOM_WIDGET_TEST) {
355 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
356 }
357 }
358
Chet Haasea8f996d2015-02-17 12:56:04 -0800359 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
360 private static Method sClipRevealMethod = null;
361 static {
362 Class<?> activityOptionsClass = ActivityOptions.class;
363 try {
364 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
365 View.class, int.class, int.class, int.class, int.class);
366 } catch (Exception e) {
367 // Earlier version
368 }
369 }
370
Michael Jurkac1f5d262011-09-30 19:32:27 -0700371 private Runnable mBuildLayersRunnable = new Runnable() {
372 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700373 if (mWorkspace != null) {
374 mWorkspace.buildPageHardwareLayers();
375 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700376 }
377 };
378
Adam Cohendb364c32014-05-20 14:23:40 -0700379 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800380
381 private static class PendingAddArguments {
382 int requestCode;
383 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700384 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700385 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800386 int cellX;
387 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700388 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800389 }
390
Daniel Sandlerff02d492013-08-05 02:12:05 -0400391 private Stats mStats;
392
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700393 FocusIndicatorView mFocusHandler;
394
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 @Override
396 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700397 if (DEBUG_STRICT_MODE) {
398 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
399 .detectDiskReads()
400 .detectDiskWrites()
401 .detectNetwork() // or .detectAll() for all detectable problems
402 .penaltyLog()
403 .build());
404 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
405 .detectLeakedSqlLiteObjects()
406 .detectLeakedClosableObjects()
407 .penaltyLog()
408 .penaltyDeath()
409 .build());
410 }
411
Adam Cohen9211d422014-10-07 18:14:53 -0700412 if (mLauncherCallbacks != null) {
413 mLauncherCallbacks.preOnCreate();
414 }
415
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400417
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400418 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400419 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700420 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700421
Winson Chung5f8afe62013-08-12 16:19:28 -0700422 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700423 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700424
425 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400426 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700427 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700428 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800429 mModel = app.setLauncher(this);
430 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700431 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400432 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700434
Daniel Sandlerff02d492013-08-05 02:12:05 -0400435 mStats = new Stats(this);
436
Sunny Goyalffe83f12014-08-14 17:39:34 -0700437 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700438
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700439 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
440 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700441
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700442 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
443 // this also ensures that any synchronous binding below doesn't re-trigger another
444 // LauncherModel load.
445 mPaused = false;
446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200448 android.os.Debug.startMethodTracing(
449 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 }
451
452 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100456 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800458 registerContentObservers();
459
Joe Onorato7c312c12009-08-13 21:36:53 -0700460 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 mSavedState = savedInstanceState;
463 restoreState(mSavedState);
464
465 if (PROFILE_STARTUP) {
466 android.os.Debug.stopMethodTracing();
467 }
468
469 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700470 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700471 // If the user leaves launcher, then we should just load items asynchronously when
472 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500473 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700474 } else {
475 // We only load the page synchronously if the user rotates (or triggers a
476 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800477 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 }
480
481 // For handling default keys
482 mDefaultKeySsb = new SpannableStringBuilder();
483 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800484
485 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
486 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800487
Adam Cohenf9426d52012-06-04 17:26:21 -0700488 // On large interfaces, we want the screen to auto-rotate based on the current orientation
489 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700490
Adam Cohen9211d422014-10-07 18:14:53 -0700491 if (mLauncherCallbacks != null) {
492 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700493 if (mLauncherCallbacks.hasLauncherOverlay()) {
494 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700495 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
496 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
497 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700498 mWorkspace.setLauncherOverlay(mLauncherOverlay);
499 }
Adam Cohen9211d422014-10-07 18:14:53 -0700500 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700501
502 if (shouldShowIntroScreen()) {
503 showIntroScreen();
504 } else {
505 showFirstRunActivity();
506 showFirstRunClings();
507 }
Adam Cohen9211d422014-10-07 18:14:53 -0700508 }
509
510 private LauncherCallbacks mLauncherCallbacks;
511
512 public void onPostCreate(Bundle savedInstanceState) {
513 super.onPostCreate(savedInstanceState);
514 if (mLauncherCallbacks != null) {
515 mLauncherCallbacks.onPostCreate(savedInstanceState);
516 }
517 }
518
519 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
520 mLauncherCallbacks = callbacks;
521 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700522 }
523
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700524 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700525 public void onLauncherProviderChange() {
526 if (mLauncherCallbacks != null) {
527 mLauncherCallbacks.onLauncherProviderChange();
528 }
529 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700530
Adam Cohen9211d422014-10-07 18:14:53 -0700531 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700532 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700533 if (mLauncherCallbacks != null) {
534 return mLauncherCallbacks.hasCustomContentToLeft();
535 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700536 return false;
537 }
538
Dave Hawkeya8881582013-09-17 15:55:33 -0600539 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500540 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600541 * {@link #addToCustomContentPage}. This will only be invoked if
542 * {@link #hasCustomContentToLeft()} is {@code true}.
543 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500544 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700545 if (mLauncherCallbacks != null) {
546 mLauncherCallbacks.populateCustomContentContainer();
547 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600548 }
549
550 /**
551 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
552 * ensure the custom content page is added or removed if necessary.
553 */
554 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600555 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600556 // Not bound yet, wait for bindScreens to be called.
557 return;
558 }
559
560 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
561 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500562 mWorkspace.createCustomContentContainer();
563 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600564 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
565 mWorkspace.removeCustomContentPage();
566 }
567 }
568
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700570 if (sLocaleConfiguration == null) {
571 new AsyncTask<Void, Void, LocaleConfiguration>() {
572 @Override
573 protected LocaleConfiguration doInBackground(Void... unused) {
574 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
575 readConfiguration(Launcher.this, localeConfiguration);
576 return localeConfiguration;
577 }
578
579 @Override
580 protected void onPostExecute(LocaleConfiguration result) {
581 sLocaleConfiguration = result;
582 checkForLocaleChange(); // recursive, but now with a locale configuration
583 }
584 }.execute();
585 return;
586 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700587
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 final Configuration configuration = getResources().getConfiguration();
589
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700590 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 final String locale = configuration.locale.toString();
592
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700593 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 final int mcc = configuration.mcc;
595
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700596 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 final int mnc = configuration.mnc;
598
Romain Guy84f296c2009-11-04 15:00:44 -0800599 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600
Romain Guy84f296c2009-11-04 15:00:44 -0800601 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700602 sLocaleConfiguration.locale = locale;
603 sLocaleConfiguration.mcc = mcc;
604 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700605
Joe Onorato0589f0f2010-02-08 13:44:00 -0800606 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700607
608 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100609 new AsyncTask<Void, Void, Void>() {
610 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700611 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100612 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700613 }
Michael Jurka43467462013-11-14 12:03:58 +0100614 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700615 }
616 }
617
618 private static class LocaleConfiguration {
619 public String locale;
620 public int mcc = -1;
621 public int mnc = -1;
622 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700623
Romain Guy98d01652009-06-30 16:21:04 -0700624 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
625 DataInputStream in = null;
626 try {
Helena Josol28db2802014-10-09 17:04:09 +0100627 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700628 configuration.locale = in.readUTF();
629 configuration.mcc = in.readInt();
630 configuration.mnc = in.readInt();
631 } catch (FileNotFoundException e) {
632 // Ignore
633 } catch (IOException e) {
634 // Ignore
635 } finally {
636 if (in != null) {
637 try {
638 in.close();
639 } catch (IOException e) {
640 // Ignore
641 }
642 }
643 }
644 }
645
646 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
647 DataOutputStream out = null;
648 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100649 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100650 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700651 out.writeUTF(configuration.locale);
652 out.writeInt(configuration.mcc);
653 out.writeInt(configuration.mnc);
654 out.flush();
655 } catch (FileNotFoundException e) {
656 // Ignore
657 } catch (IOException e) {
658 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100659 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700660 } finally {
661 if (out != null) {
662 try {
663 out.close();
664 } catch (IOException e) {
665 // Ignore
666 }
667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
669 }
670
Anton Hanssona2f665f2013-09-26 12:18:32 +0100671 public Stats getStats() {
672 return mStats;
673 }
674
Bjorn Bringertc459e522013-06-07 19:36:01 +0100675 public LayoutInflater getInflater() {
676 return mInflater;
677 }
678
Winson Chung36a62fe2012-05-06 18:04:42 -0700679 boolean isDraggingEnabled() {
680 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
681 // that is subsequently removed from the workspace in startBinding().
682 return !mModel.isLoadingWorkspace();
683 }
684
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800685 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000686 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100687 if (Build.VERSION.SDK_INT >= 17) {
688 return View.generateViewId();
689 } else {
690 // View.generateViewId() is not available. The following fallback logic is a copy
691 // of its implementation.
692 for (;;) {
693 final int result = sNextGeneratedId.get();
694 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
695 int newValue = result + 1;
696 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
697 if (sNextGeneratedId.compareAndSet(result, newValue)) {
698 return result;
699 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000700 }
701 }
702 }
703
704 public int getViewIdForItem(ItemInfo info) {
705 // This cast is safe given the > 2B range for int.
706 int itemId = (int) info.id;
707 if (mItemIdToViewId.containsKey(itemId)) {
708 return mItemIdToViewId.get(itemId);
709 }
710 int viewId = generateViewId();
711 mItemIdToViewId.put(itemId, viewId);
712 return viewId;
713 }
714
715 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700716 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
717 * a configuration step, this allows the proper animations to run after other transitions.
718 */
Adam Cohendb364c32014-05-20 14:23:40 -0700719 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700720 long screenId = args.screenId;
721 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
722 // When the screen id represents an actual screen (as opposed to a rank) we make sure
723 // that the drop page actually exists.
724 screenId = ensurePendingDropLayoutExists(args.screenId);
725 }
Adam Cohendb364c32014-05-20 14:23:40 -0700726
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800727 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800728 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700729 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700730 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700731 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800732 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700733 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700734 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700735 case REQUEST_RECONFIGURE_APPWIDGET:
736 completeRestoreAppWidget(args.appWidgetId);
737 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800738 }
Michael Jurka27614d22012-04-02 06:40:22 -0700739 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
740 // if you turned the screen off and then back while in All Apps, Launcher would not
741 // return to the workspace. Clearing mAddInfo.container here fixes this issue
742 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700743 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800744 }
745
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800746 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700747 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700748 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700749 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700750 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800751 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700752
Adam Cohenad4e15c2013-10-17 16:21:35 -0700753 Runnable exitSpringLoaded = new Runnable() {
754 @Override
755 public void run() {
756 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
757 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
758 }
759 };
760
Michael Jurka8b805b12012-04-18 14:23:14 -0700761 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700763 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
764 if (resultCode == RESULT_CANCELED) {
765 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700766 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700767 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700768 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800769 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700770 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700771 }
772 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200773 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
774 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
775 mWorkspace.exitOverviewMode(false);
776 }
777 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700778 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200779
Adam Cohened66b2b2012-01-23 17:28:51 -0800780 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
781 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700782
Adam Cohendb364c32014-05-20 14:23:40 -0700783 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800784 // We have special handling for widgets
785 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800786 final int appWidgetId;
787 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
788 : -1;
789 if (widgetId < 0) {
790 appWidgetId = pendingAddWidgetId;
791 } else {
792 appWidgetId = widgetId;
793 }
794
Adam Cohenad4e15c2013-10-17 16:21:35 -0700795 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800796 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700797 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
798 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700799 result = RESULT_CANCELED;
800 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700801 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700802 @Override
803 public void run() {
804 exitSpringLoadedDragModeDelayed(false, 0, null);
805 }
806 };
Adam Cohendb364c32014-05-20 14:23:40 -0700807 if (workspaceLocked) {
808 // No need to remove the empty screen if we're mid-binding, as the
809 // the bind will not add the empty screen.
810 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
811 } else {
812 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
813 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
814 }
Winson Chung5aaab772012-04-27 15:24:02 -0700815 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700816 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700817 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
818 // When the screen id represents an actual screen (as opposed to a rank)
819 // we make sure that the drop page actually exists.
820 mPendingAddInfo.screenId =
821 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
822 }
Adam Cohendb364c32014-05-20 14:23:40 -0700823 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
824
825 dropLayout.setDropPending(true);
826 final Runnable onComplete = new Runnable() {
827 @Override
828 public void run() {
829 completeTwoStageWidgetDrop(resultCode, appWidgetId);
830 dropLayout.setDropPending(false);
831 }
832 };
833 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
834 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
835 } else {
836 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
837 mPendingAddInfo);
838 sPendingAddItem = args;
839 }
Winson Chung5aaab772012-04-27 15:24:02 -0700840 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800841 return;
842 }
843
Sunny Goyalff572272014-07-23 13:58:07 -0700844 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
845 if (resultCode == RESULT_OK) {
846 // Update the widget view.
847 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
848 pendingAddWidgetId, mPendingAddInfo);
849 if (workspaceLocked) {
850 sPendingAddItem = args;
851 } else {
852 completeAdd(args);
853 }
854 }
855 // Leave the widget in the pending state if the user canceled the configure.
856 return;
857 }
858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 // The pattern used here is that a user PICKs a specific application,
860 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700861
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
863 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700864 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700865 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
866 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800867 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700868 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800869 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700870 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700871 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
872 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 }
Adam Cohen00074722013-10-11 17:46:09 -0700874 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700875 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700876 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800878 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800879
880 }
881
882 @Override
883 protected void onActivityResult(
884 final int requestCode, final int resultCode, final Intent data) {
885 handleActivityResult(requestCode, resultCode, data);
886 if (mLauncherCallbacks != null) {
887 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
888 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800889 }
890
Adam Cohendb364c32014-05-20 14:23:40 -0700891 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
892 appWidgetId, ItemInfo info) {
893 PendingAddArguments args = new PendingAddArguments();
894 args.requestCode = requestCode;
895 args.intent = data;
896 args.container = info.container;
897 args.screenId = info.screenId;
898 args.cellX = info.cellX;
899 args.cellY = info.cellY;
900 args.appWidgetId = appWidgetId;
901 return args;
902 }
903
904 /**
905 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
906 *
907 * @param screenId the screen id to check
908 * @return the new screen, or screenId if it exists
909 */
910 private long ensurePendingDropLayoutExists(long screenId) {
911 CellLayout dropLayout =
912 (CellLayout) mWorkspace.getScreenWithId(screenId);
913 if (dropLayout == null) {
914 // it's possible that the add screen was removed because it was
915 // empty and a re-bind occurred
916 mWorkspace.addExtraEmptyScreen();
917 return mWorkspace.commitExtraEmptyScreen();
918 } else {
919 return screenId;
920 }
921 }
922
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700924 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700925 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 Runnable onCompleteRunnable = null;
927 int animationType = 0;
928
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700929 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 if (resultCode == RESULT_OK) {
931 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
932 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700933 mPendingAddWidgetInfo);
934 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800935 onCompleteRunnable = new Runnable() {
936 @Override
937 public void run() {
938 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700939 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700940 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
941 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800942 }
943 };
944 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800945 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800946 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800947 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700948 if (mDragLayer.getAnimatedView() != null) {
949 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
950 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
951 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700952 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700953 // The animated view may be null in the case of a rotation during widget configuration
954 onCompleteRunnable.run();
955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 }
957
958 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700959 protected void onStop() {
960 super.onStop();
961 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700962
963 if (mLauncherCallbacks != null) {
964 mLauncherCallbacks.onStop();
965 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700966 }
967
968 @Override
969 protected void onStart() {
970 super.onStart();
971 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700972
973 if (mLauncherCallbacks != null) {
974 mLauncherCallbacks.onStart();
975 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700976 }
977
978 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200980 long startTime = 0;
981 if (DEBUG_RESUME_TIME) {
982 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400983 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200984 }
Adam Cohen9211d422014-10-07 18:14:53 -0700985
986 if (mLauncherCallbacks != null) {
987 mLauncherCallbacks.preOnResume();
988 }
989
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700991
Winson Chung4a2afa32012-07-19 14:53:05 -0700992 // Restore the previous launcher state
993 if (mOnResumeState == State.WORKSPACE) {
994 showWorkspace(false);
995 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800996 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700997 }
998 mOnResumeState = State.NONE;
999
Craig Mautner360310b2012-10-26 15:13:08 -07001000 // Background was set to gradient in onPause(), restore to black if in all apps.
1001 setWorkspaceBackground(mState == State.WORKSPACE);
1002
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001003 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001004 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001005 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001006 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001007 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001008 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001010 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001011 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1012 // execute them here
1013 long startTimeCallbacks = 0;
1014 if (DEBUG_RESUME_TIME) {
1015 startTimeCallbacks = System.currentTimeMillis();
1016 }
1017
1018 if (mAppsCustomizeContent != null) {
1019 mAppsCustomizeContent.setBulkBind(true);
1020 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001021 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1022 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001023 }
1024 if (mAppsCustomizeContent != null) {
1025 mAppsCustomizeContent.setBulkBind(false);
1026 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001027 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001028 if (DEBUG_RESUME_TIME) {
1029 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1030 (System.currentTimeMillis() - startTimeCallbacks));
1031 }
Michael Jurka447bf842013-05-15 14:52:15 +02001032 }
Michael Jurka54554252013-08-01 12:52:23 +02001033 if (mOnResumeCallbacks.size() > 0) {
1034 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1035 mOnResumeCallbacks.get(i).run();
1036 }
1037 mOnResumeCallbacks.clear();
1038 }
Winson Chunge4e50662012-01-23 14:45:13 -08001039
1040 // Reset the pressed state of icons that were locked in the press state while activities
1041 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001042 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001043 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001044 mWaitingForResume.setStayPressed(false);
1045 }
Winson Chung82963d52013-10-09 11:20:57 -07001046
Adam Cohen06dff352012-06-01 17:17:08 -07001047 // It is possible that widgets can receive updates while launcher is not in the foreground.
1048 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1049 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1050 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001051 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001052
Winson Chung780fe592013-09-26 14:48:44 -07001053 // Process any items that were added while Launcher was away.
1054 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1055
Michael Jurka447bf842013-05-15 14:52:15 +02001056 if (DEBUG_RESUME_TIME) {
1057 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1058 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001059
1060 if (mWorkspace.getCustomContentCallbacks() != null) {
1061 // If we are resuming and the custom content is the current page, we call onShow().
1062 // It is also poassible that onShow will instead be called slightly after first layout
1063 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1064 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001065 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001066 }
1067 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001068 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001069 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001070
1071 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001072
1073 if (mLauncherCallbacks != null) {
1074 mLauncherCallbacks.onResume();
1075 }
Adam Cohen06dff352012-06-01 17:17:08 -07001076 }
1077
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001079 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001080 // Ensure that items added to Launcher are queued until Launcher returns
1081 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001082 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001083
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001084 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001085 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001086 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001087 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001088
1089 // We call onHide() aggressively. The custom content callbacks should be able to
1090 // debounce excess onHide calls.
1091 if (mWorkspace.getCustomContentCallbacks() != null) {
1092 mWorkspace.getCustomContentCallbacks().onHide();
1093 }
Romain Guycbb89e42009-06-08 15:52:54 -07001094
Adam Cohen9211d422014-10-07 18:14:53 -07001095 if (mLauncherCallbacks != null) {
1096 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001097 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001098 }
1099
1100 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001101 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1102 // by a onResume or by scrolling otherwise.
1103 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001104
1105 // Custom content is completely hidden
1106 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001107
1108 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1109 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001110
1111 // Indicates whether the user is allowed to scroll away from the custom content.
1112 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001113 }
1114
Adam Cohenc2d6e892014-10-16 09:49:24 -07001115 public interface LauncherOverlay {
1116
1117 /**
1118 * Touch interaction leading to overscroll has begun
1119 */
1120 public void onScrollInteractionBegin();
1121
1122 /**
1123 * Touch interaction related to overscroll has ended
1124 */
1125 public void onScrollInteractionEnd();
1126
1127 /**
1128 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1129 * screen (or in the case of RTL, the rightmost screen).
1130 */
1131 public void onScrollChange(int progress, boolean rtl);
1132
1133 /**
1134 * Screen has stopped scrolling
1135 */
1136 public void onScrollSettled();
1137
1138 /**
1139 * This method can be called by the Launcher in order to force the LauncherOverlay
1140 * to exit fully immersive mode.
1141 */
1142 public void forceExitFullImmersion();
1143 }
1144
1145 public interface LauncherOverlayCallbacks {
1146 /**
1147 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1148 * however it doesn't modify any state within the launcher.
1149 */
1150 public boolean canEnterFullImmersion();
1151
1152 /**
1153 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1154 * eg. by occupying the full screen and handling all touch events.
1155 *
1156 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1157 * case, Launcher will modify any necessary state and assumes the overlay is
1158 * handling all interaction. If false, the LauncherOverlay should cancel any
1159 *
1160 */
1161 public boolean enterFullImmersion();
1162
1163 /**
1164 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1165 * full control over UI and state.
1166 */
1167 public void exitFullImmersion();
1168 }
1169
1170 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1171
1172 @Override
1173 public boolean canEnterFullImmersion() {
1174 return mState == State.WORKSPACE;
1175 }
1176
1177 @Override
1178 public boolean enterFullImmersion() {
1179 if (mState == State.WORKSPACE) {
1180 // When fully immersed, disregard any touches which fall through.
1181 mDragLayer.setBlockTouch(true);
1182 return true;
1183 }
1184 return false;
1185 }
1186
1187 @Override
1188 public void exitFullImmersion() {
1189 mDragLayer.setBlockTouch(false);
1190 }
1191 }
1192
Jorim Jaggid017f882014-01-14 17:08:48 -08001193 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001194 if (mLauncherCallbacks != null) {
1195 return mLauncherCallbacks.hasSettings();
1196 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001197 return false;
1198 }
1199
Adam Cohenbffe7452013-07-22 18:21:45 -07001200
Adam Cohen9211d422014-10-07 18:14:53 -07001201 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001202 CustomContentCallbacks callbacks, String description) {
1203 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001204 }
1205
Adam Cohenedb40762013-07-18 16:45:45 -07001206 // The custom content needs to offset its content to account for the QSB
1207 public int getTopOffsetForCustomContent() {
1208 return mWorkspace.getPaddingTop();
1209 }
1210
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001211 @Override
1212 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001213 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001214 if (mModel.isCurrentCallbacks(this)) {
1215 mModel.stopLoader();
1216 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001217 if (mAppsCustomizeContent != null) {
1218 mAppsCustomizeContent.surrender();
1219 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001220 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001221 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001222
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001223 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001224 @Override
1225 public void onWindowFocusChanged(boolean hasFocus) {
1226 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001227 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001228
1229 if (mLauncherCallbacks != null) {
1230 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1231 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001232 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 private boolean acceptFilter() {
1235 final InputMethodManager inputManager = (InputMethodManager)
1236 getSystemService(Context.INPUT_METHOD_SERVICE);
1237 return !inputManager.isFullscreenMode();
1238 }
1239
1240 @Override
1241 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001242 final int uniChar = event.getUnicodeChar();
1243 final boolean handled = super.onKeyDown(keyCode, event);
1244 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1245 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1247 keyCode, event);
1248 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001249 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001250 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001251 // showSearchDialog()
1252 // If there are multiple keystrokes before the search dialog takes focus,
1253 // onSearchRequested() will be called for every keystroke,
1254 // but it is idempotent, so it's fine.
1255 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 }
1257 }
1258
Joe Onorato8a9625e2010-01-28 15:55:35 -08001259 // Eat the long press event so the keyboard doesn't come up.
1260 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1261 return true;
1262 }
1263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 return handled;
1265 }
1266
Karl Rosaen138a0412009-04-23 19:00:21 -07001267 private String getTypedText() {
1268 return mDefaultKeySsb.toString();
1269 }
1270
1271 private void clearTypedText() {
1272 mDefaultKeySsb.clear();
1273 mDefaultKeySsb.clearSpans();
1274 Selection.setSelection(mDefaultKeySsb, 0);
1275 }
1276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001278 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1279 * State
1280 */
1281 private static State intToState(int stateOrdinal) {
1282 State state = State.WORKSPACE;
1283 final State[] stateValues = State.values();
1284 for (int i = 0; i < stateValues.length; i++) {
1285 if (stateValues[i].ordinal() == stateOrdinal) {
1286 state = stateValues[i];
1287 break;
1288 }
1289 }
1290 return state;
1291 }
1292
1293 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 * Restores the previous state, if it exists.
1295 *
1296 * @param savedState The previous state.
1297 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001298 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 private void restoreState(Bundle savedState) {
1300 if (savedState == null) {
1301 return;
1302 }
1303
Michael Jurka883f55b2010-10-21 15:47:14 -07001304 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001305 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001306 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001307 }
1308
Adam Cohen21cd0022013-10-09 18:57:02 -07001309 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1310 PagedView.INVALID_RESTORE_PAGE);
1311 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001312 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
1314
Winson Chung3d503fb2011-07-13 17:25:49 -07001315 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001316 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001317
Winson Chung3d503fb2011-07-13 17:25:49 -07001318 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1319 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001320 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001321 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1322 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001323 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1324 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1325 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001326 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001327 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 mRestoring = true;
1329 }
1330
1331 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1332 if (renameFolder) {
1333 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001334 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 mRestoring = true;
1336 }
Winson Chunga12a2502010-12-20 14:41:35 -08001337
Winson Chung785d2eb2011-04-14 16:08:02 -07001338 // Restore the AppsCustomize tab
1339 if (mAppsCustomizeTabHost != null) {
1340 String curTab = savedState.getString("apps_customize_currentTab");
1341 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001342 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001343 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001344 mAppsCustomizeContent.loadAssociatedPages(
1345 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001346 }
1347
Winson Chung5afbf7b2011-07-25 11:53:08 -07001348 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1349 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001350 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001351 mItemIdToViewId = (HashMap<Integer, Integer>)
1352 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 }
1354
1355 /**
1356 * Finds all the views we need and configure them properly.
1357 */
1358 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001359 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001360
Craig Mautner360310b2012-10-26 15:13:08 -07001361 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001362 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001363 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1364 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001365 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001366 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001367
John Spurlock77e1f472013-09-11 10:09:51 -04001368 mLauncherView.setSystemUiVisibility(
1369 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001370 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371
Winson Chung4c98d922011-05-31 16:50:48 -07001372 // Setup the drag layer
1373 mDragLayer.setup(this, dragController);
1374
Winson Chung3d503fb2011-07-13 17:25:49 -07001375 // Setup the hotseat
1376 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1377 if (mHotseat != null) {
1378 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001379 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001380 }
1381
Jorim Jaggid017f882014-01-14 17:08:48 -08001382 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001383 View widgetButton = findViewById(R.id.widget_button);
1384 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001385 @Override
1386 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001387 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001388 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001389 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001390 }
1391 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001392 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1393
1394 View wallpaperButton = findViewById(R.id.wallpaper_button);
1395 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001396 @Override
1397 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001398 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001399 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001400 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001401 }
1402 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001403 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1404
1405 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001406 if (hasSettings()) {
1407 settingsButton.setOnClickListener(new OnClickListener() {
1408 @Override
1409 public void onClick(View arg0) {
1410 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001411 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001412 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001413 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001414 });
1415 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1416 } else {
1417 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001418 }
1419
Adam Cohendbdff6b2013-09-18 19:09:15 -07001420 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001421
Winson Chung4c98d922011-05-31 16:50:48 -07001422 // Setup the workspace
1423 mWorkspace.setHapticFeedbackEnabled(false);
1424 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001425 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001426 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001427
Winson Chungf0ea4d32011-06-06 14:27:16 -07001428 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001429 mSearchDropTargetBar = (SearchDropTargetBar)
1430 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001431
Winson Chungf0ea4d32011-06-06 14:27:16 -07001432 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001433 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001434 mAppsCustomizeContent = (AppsCustomizePagedView)
1435 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1436 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001437
Winson Chung3d503fb2011-07-13 17:25:49 -07001438 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001439 dragController.setDragScoller(mWorkspace);
1440 dragController.setScrollView(mDragLayer);
1441 dragController.setMoveTarget(mWorkspace);
1442 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001443 if (mSearchDropTargetBar != null) {
1444 mSearchDropTargetBar.setup(this, dragController);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08001445 if (getOrCreateQsbBar() == null) {
1446 // Explicitly set it to null during initialization.
1447 mSearchDropTargetBar.setQsbSearchBar(null);
1448 }
Michael Jurkae0f5a612011-02-07 16:45:41 -08001449 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001450
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001451 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001452 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001453 mWeightWatcher = new WeightWatcher(this);
1454 mWeightWatcher.setAlpha(0.5f);
1455 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001456 new FrameLayout.LayoutParams(
1457 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001458 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001459 Gravity.BOTTOM)
1460 );
Adam Cohen39a06042013-07-19 14:30:12 -07001461
1462 boolean show = shouldShowWeightWatcher();
1463 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 }
1466
1467 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001468 * Sets the all apps button. This method is called from {@link Hotseat}.
1469 */
1470 public void setAllAppsButton(View allAppsButton) {
1471 mAllAppsButton = allAppsButton;
1472 }
1473
1474 public View getAllAppsButton() {
1475 return mAllAppsButton;
1476 }
1477
1478 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 * Creates a view representing a shortcut.
1480 *
1481 * @param info The data structure describing the shortcut.
1482 *
1483 * @return A View inflated from R.layout.application.
1484 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001485 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001487 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489
1490 /**
1491 * Creates a view representing a shortcut inflated from the specified resource.
1492 *
1493 * @param layoutResId The id of the XML layout used to create the shortcut.
1494 * @param parent The group the shortcut belongs to.
1495 * @param info The data structure describing the shortcut.
1496 *
1497 * @return A View inflated from layoutResId.
1498 */
Adam Cohen59400422014-03-05 18:07:04 -08001499 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001500 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001501 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001503 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 return favorite;
1505 }
1506
1507 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 * Add a shortcut to the workspace.
1509 *
1510 * @param data The intent describing the shortcut.
1511 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001513 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001514 int cellY) {
1515 int[] cellXY = mTmpAddItemCellCoordinates;
1516 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001517 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001518
Michael Jurkad3ef3062010-11-23 16:23:58 -08001519 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001520
Sunny Goyal2350bc92014-10-14 16:42:54 -07001521 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001522 if (info == null) {
1523 return;
1524 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001525 final View view = createShortcut(info);
1526
Adam Cohenfbba09b2011-07-18 21:43:05 -07001527 // First we check if we already know the exact location where we want to add this item.
1528 if (cellX >= 0 && cellY >= 0) {
1529 cellXY[0] = cellX;
1530 cellXY[1] = cellY;
1531 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001532
1533 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001534 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001535 true, null,null)) {
1536 return;
1537 }
1538 DragObject dragObject = new DragObject();
1539 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001540 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1541 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001542 return;
1543 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001544 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001545 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001546 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001547 foundCellSpan = (result != null);
1548 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001549 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001550 }
1551
1552 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001553 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001554 return;
1555 }
1556
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558
1559 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001561 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
1563 }
1564
Adam Cohen2f093b62012-04-30 18:59:53 -07001565 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1566 int minHeight) {
1567 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001568 // We want to account for the extra amount of padding that we are adding to the widget
1569 // to ensure that it gets the full amount of space that it has requested
1570 int requiredWidth = minWidth + padding.left + padding.right;
1571 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001572 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001573 }
1574
Adam Cohen2f093b62012-04-30 18:59:53 -07001575 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1576 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001577 }
1578
Adam Cohen2f093b62012-04-30 18:59:53 -07001579 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1580 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001581 }
1582
Adam Cohen2f093b62012-04-30 18:59:53 -07001583 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1584 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001585 }
1586
Adam Cohen2f093b62012-04-30 18:59:53 -07001587 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1588 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001589 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001590 }
1591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001593 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001595 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 */
Adam Cohen59400422014-03-05 18:07:04 -08001597 private void completeAddAppWidget(int appWidgetId, long container, long screenId,
1598 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1599
1600 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001601 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001602 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1603 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001604 }
Romain Guycbb89e42009-06-08 15:52:54 -07001605
Adam Cohen59400422014-03-05 18:07:04 -08001606 if (appWidgetInfo.isCustomWidget) {
1607 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001608 }
1609
Adam Cohen59400422014-03-05 18:07:04 -08001610 LauncherAppWidgetInfo launcherInfo;
1611 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1612 launcherInfo.spanX = info.spanX;
1613 launcherInfo.spanY = info.spanY;
1614 launcherInfo.minSpanX = info.minSpanX;
1615 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001616 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001617
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001619 container, screenId, info.cellX, info.cellY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620
1621 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001622 if (hostView == null) {
1623 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001624 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1625 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001626 } else {
1627 // The AppWidgetHostView has already been inflated and instantiated
1628 launcherInfo.hostView = hostView;
1629 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001631 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001632 launcherInfo.notifyWidgetSizeChanged(this);
1633
Adam Cohen59400422014-03-05 18:07:04 -08001634 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1635 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001636
1637 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001639 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 }
Romain Guycbb89e42009-06-08 15:52:54 -07001641
Adam Cohended9f8d2010-11-03 13:25:16 -07001642 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1643 @Override
1644 public void onReceive(Context context, Intent intent) {
1645 final String action = intent.getAction();
1646 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1647 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001648 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001649 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001650
Winson Chungc100e8e2011-08-09 16:02:43 -07001651 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001652 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001653 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001654 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001655 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001656 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1657 mUserPresent = true;
1658 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001659 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1660 mModel.resetLoadedState(false, true);
1661 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1662 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1663 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1664 mModel.resetLoadedState(false, true);
1665 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1666 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1667 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001668 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1669 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1670 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001671 }
1672 }
1673 };
1674
1675 @Override
1676 public void onAttachedToWindow() {
1677 super.onAttachedToWindow();
1678
1679 // Listen for broadcasts related to user-presence
1680 final IntentFilter filter = new IntentFilter();
1681 filter.addAction(Intent.ACTION_SCREEN_OFF);
1682 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001683 // For handling managed profiles
1684 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1685 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001686 if (ENABLE_DEBUG_INTENTS) {
1687 filter.addAction(DebugIntents.DELETE_DATABASE);
1688 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1689 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001690 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001691 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001692 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001693 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001694 mVisible = true;
1695 }
1696
Adam Cohen0b395352014-06-09 22:54:36 +00001697 /**
1698 * Sets up transparent navigation and status bars in LMP.
1699 * This method is a no-op for other platform versions.
1700 */
Sunny Goyald0091012015-01-20 15:55:34 -08001701 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001702 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001703 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001704 Window window = getWindow();
1705 window.getAttributes().systemUiVisibility |=
1706 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1707 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1708 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1709 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1710 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1711 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1712 window.setStatusBarColor(Color.TRANSPARENT);
1713 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001714 }
1715 }
1716
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 @Override
1718 public void onDetachedFromWindow() {
1719 super.onDetachedFromWindow();
1720 mVisible = false;
1721
Adam Cohend113e0c2010-11-11 10:48:05 -08001722 if (mAttached) {
1723 unregisterReceiver(mReceiver);
1724 mAttached = false;
1725 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001726 updateRunning();
1727 }
1728
1729 public void onWindowVisibilityChanged(int visibility) {
1730 mVisible = visibility == View.VISIBLE;
1731 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001732 // The following code used to be in onResume, but it turns out onResume is called when
1733 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1734 // is a more appropriate event to handle
1735 if (mVisible) {
1736 mAppsCustomizeTabHost.onWindowVisible();
1737 if (!mWorkspaceLoading) {
1738 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001739 // We want to let Launcher draw itself at least once before we force it to build
1740 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001741 // apps is nice and speedy.
1742 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001743 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001744 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001745 if (mStarted) return;
1746 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001747 // We delay the layer building a bit in order to give
1748 // other message processing a time to run. In particular
1749 // this avoids a delay in hiding the IME if it was
1750 // currently shown, because doing that may involve
1751 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001752 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001753 final ViewTreeObserver.OnDrawListener listener = this;
1754 mWorkspace.post(new Runnable() {
1755 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001756 if (mWorkspace != null &&
1757 mWorkspace.getViewTreeObserver() != null) {
1758 mWorkspace.getViewTreeObserver().
1759 removeOnDrawListener(listener);
1760 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001761 }
1762 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001763 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001764 }
1765 });
1766 }
1767 clearTypedText();
1768 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001769 }
1770
1771 private void sendAdvanceMessage(long delay) {
1772 mHandler.removeMessages(ADVANCE_MSG);
1773 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1774 mHandler.sendMessageDelayed(msg, delay);
1775 mAutoAdvanceSentTime = System.currentTimeMillis();
1776 }
1777
1778 private void updateRunning() {
1779 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1780 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1781 mAutoAdvanceRunning = autoAdvanceRunning;
1782 if (autoAdvanceRunning) {
1783 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1784 sendAdvanceMessage(delay);
1785 } else {
1786 if (!mWidgetsToAdvance.isEmpty()) {
1787 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1788 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1789 }
1790 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001791 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001792 }
1793 }
1794 }
1795
1796 private final Handler mHandler = new Handler() {
1797 @Override
1798 public void handleMessage(Message msg) {
1799 if (msg.what == ADVANCE_MSG) {
1800 int i = 0;
1801 for (View key: mWidgetsToAdvance.keySet()) {
1802 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1803 final int delay = mAdvanceStagger * i;
1804 if (v instanceof Advanceable) {
1805 postDelayed(new Runnable() {
1806 public void run() {
1807 ((Advanceable) v).advance();
1808 }
1809 }, delay);
1810 }
1811 i++;
1812 }
1813 sendAdvanceMessage(mAdvanceInterval);
1814 }
1815 }
1816 };
1817
1818 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001819 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001820 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1821 if (v instanceof Advanceable) {
1822 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001823 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001824 updateRunning();
1825 }
1826 }
1827
1828 void removeWidgetToAutoAdvance(View hostView) {
1829 if (mWidgetsToAdvance.containsKey(hostView)) {
1830 mWidgetsToAdvance.remove(hostView);
1831 updateRunning();
1832 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001833 }
1834
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001836 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001837 launcherInfo.hostView = null;
1838 }
1839
Winson Chung93eef082012-03-23 15:59:27 -07001840 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1841 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1842 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001843 }
1844
Adam Cohen59400422014-03-05 18:07:04 -08001845 public ArrayList<AppInfo> getAllAppsList() {
1846 return mAppsCustomizeContent.getApps();
1847 }
1848
Winson Chunga6945242014-01-08 14:04:34 -08001849 public DragLayer getDragLayer() {
1850 return mDragLayer;
1851 }
1852
1853 public Workspace getWorkspace() {
1854 return mWorkspace;
1855 }
1856
1857 public Hotseat getHotseat() {
1858 return mHotseat;
1859 }
1860
Jorim Jaggid017f882014-01-14 17:08:48 -08001861 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001862 return mOverviewPanel;
1863 }
1864
1865 public SearchDropTargetBar getSearchBar() {
1866 return mSearchDropTargetBar;
1867 }
1868
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001869 public LauncherAppWidgetHost getAppWidgetHost() {
1870 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 }
Romain Guycbb89e42009-06-08 15:52:54 -07001872
Winson Chunga9abd0e2010-10-27 17:18:37 -07001873 public LauncherModel getModel() {
1874 return mModel;
1875 }
1876
Winson Chunga6945242014-01-08 14:04:34 -08001877 protected SharedPreferences getSharedPrefs() {
1878 return mSharedPrefs;
1879 }
1880
Bjorn Bringertc459e522013-06-07 19:36:01 +01001881 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001882 getWindow().closeAllPanels();
1883
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001884 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001885 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001886 }
1887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 @Override
1889 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001890 long startTime = 0;
1891 if (DEBUG_RESUME_TIME) {
1892 startTime = System.currentTimeMillis();
1893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 super.onNewIntent(intent);
1895
1896 // Close the menu
1897 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001898 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001899 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001900
Adam Cohened307df2013-10-02 09:37:31 -07001901 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1902 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1903 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001904
Adam Cohen6fecd412013-10-02 17:41:50 -07001905 if (mWorkspace == null) {
1906 // Can be cases where mWorkspace is null, this prevents a NPE
1907 return;
1908 }
1909 Folder openFolder = mWorkspace.getOpenFolder();
1910 // In all these cases, only animate if we're already on home
1911 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001912
1913 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1914 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001915 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001916 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001917 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001918 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001919
Adam Cohen6fecd412013-10-02 17:41:50 -07001920 closeFolder();
1921 exitSpringLoadedDragMode();
1922
1923 // If we are already on home, then just animate back to the workspace,
1924 // otherwise, just wait until onResume to set the state back to Workspace
1925 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001926 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001927 } else {
1928 mOnResumeState = State.WORKSPACE;
1929 }
1930
1931 final View v = getWindow().peekDecorView();
1932 if (v != null && v.getWindowToken() != null) {
1933 InputMethodManager imm = (InputMethodManager)getSystemService(
1934 INPUT_METHOD_SERVICE);
1935 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1936 }
1937
1938 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001939 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001940 mAppsCustomizeTabHost.reset();
1941 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001942
Adam Cohen9211d422014-10-07 18:14:53 -07001943 if (mLauncherCallbacks != null) {
1944 mLauncherCallbacks.onHomeIntent();
1945 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
Adam Cohened307df2013-10-02 09:37:31 -07001947
Michael Jurka447bf842013-05-15 14:52:15 +02001948 if (DEBUG_RESUME_TIME) {
1949 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951
Adam Cohen9211d422014-10-07 18:14:53 -07001952 if (mLauncherCallbacks != null) {
1953 mLauncherCallbacks.onNewIntent(intent);
1954 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001955 }
1956
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001958 public void onRestoreInstanceState(Bundle state) {
1959 super.onRestoreInstanceState(state);
1960 for (int page: mSynchronouslyBoundPages) {
1961 mWorkspace.restoreInstanceStateForChild(page);
1962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 }
1964
1965 @Override
1966 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001967 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001968 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1969 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001970 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001971 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972
Michael Jurka883f55b2010-10-21 15:47:14 -07001973 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001974 // We close any open folder since it will not be re-opened, and we need to make sure
1975 // this state is reflected.
1976 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977
Adam Cohendcd297f2013-06-18 13:13:40 -07001978 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001979 mWaitingForResult) {
1980 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001981 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001982 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1983 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001984 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1985 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1986 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001987 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
1989
1990 if (mFolderInfo != null && mWaitingForResult) {
1991 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1992 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1993 }
Michael Jurka946ad472010-07-09 18:05:18 -07001994
Winson Chung785d2eb2011-04-14 16:08:02 -07001995 // Save the current AppsCustomize tab
1996 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001997 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1998 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001999 if (currentTabTag != null) {
2000 outState.putString("apps_customize_currentTab", currentTabTag);
2001 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002002 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2003 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002004 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002005 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002006
2007 if (mLauncherCallbacks != null) {
2008 mLauncherCallbacks.onSaveInstanceState(outState);
2009 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 }
2011
2012 @Override
2013 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002015
Winson Chunge7a03942011-08-05 15:05:12 -07002016 // Remove all pending runnables
2017 mHandler.removeMessages(ADVANCE_MSG);
2018 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002019 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002020
Winson Chungcd2b0142011-06-08 16:02:26 -07002021 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002022 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002023
2024 // It's possible to receive onDestroy after a new Launcher activity has
2025 // been created. In this case, don't interfere with the new Launcher.
2026 if (mModel.isCurrentCallbacks(this)) {
2027 mModel.stopLoader();
2028 app.setLauncher(null);
2029 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002030
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002032 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002034 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002036 mAppWidgetHost = null;
2037
2038 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039
2040 TextKeyListener.getInstance().release();
2041
Winson Chung81b52252012-08-27 15:34:29 -07002042 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2043 // to prevent leaking Launcher activities on orientation change.
2044 if (mModel != null) {
2045 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2046 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002047
2048 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002049 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002050
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002051 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002052 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002053 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002054 mWorkspace = null;
2055 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002056
Michael Jurka2ecf9952012-06-18 12:52:28 -07002057 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002058
2059 if (mLauncherCallbacks != null) {
2060 mLauncherCallbacks.onDestroy();
2061 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
2063
Adam Cohena9cf38f2011-05-02 15:36:58 -07002064 public DragController getDragController() {
2065 return mDragController;
2066 }
2067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 @Override
2069 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002070 if (requestCode >= 0) {
2071 setWaitingForResult(true);
2072 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 super.startActivityForResult(intent, requestCode);
2074 }
2075
Winson Chung70d72102011-08-12 11:24:35 -07002076 /**
2077 * Indicates that we want global search for this activity by setting the globalSearch
2078 * argument for {@link #startSearch} to true.
2079 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002081 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002083
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002084 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002085
Karl Rosaen138a0412009-04-23 19:00:21 -07002086 if (initialQuery == null) {
2087 // Use any text typed in the launcher as the initial query
2088 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 if (appSearchData == null) {
2091 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002092 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 }
Winson Chungadf0c182012-08-23 14:59:07 -07002094 Rect sourceBounds = new Rect();
2095 if (mSearchDropTargetBar != null) {
2096 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2097 }
Romain Guycbb89e42009-06-08 15:52:54 -07002098
Ian Parkinson047036e2014-09-01 15:40:53 +01002099 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002100 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002101 if (clearTextImmediately) {
2102 clearTypedText();
2103 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002104 }
2105
Ian Parkinson047036e2014-09-01 15:40:53 +01002106 /**
2107 * Start a text search.
2108 *
2109 * @return {@code true} if the search will start immediately, so any further keypresses
2110 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2111 * to buffer keypresses.
2112 */
2113 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002114 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002115 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2116 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2117 sourceBounds);
2118 }
2119
Michael Jurkaa33411c2012-06-14 16:18:21 -07002120 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002121 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002122 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002123 }
2124
2125 /**
2126 * Starts the global search activity. This code is a copied from SearchManager
2127 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002128 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002129 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002130 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002131 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2132 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2133 if (globalSearchActivity == null) {
2134 Log.w(TAG, "No global search activity found.");
2135 return;
2136 }
2137 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2138 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2139 intent.setComponent(globalSearchActivity);
2140 // Make sure that we have a Bundle to put source in
2141 if (appSearchData == null) {
2142 appSearchData = new Bundle();
2143 } else {
2144 appSearchData = new Bundle(appSearchData);
2145 }
Adam Cohen9211d422014-10-07 18:14:53 -07002146 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002147 if (!appSearchData.containsKey("source")) {
2148 appSearchData.putString("source", getPackageName());
2149 }
2150 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2151 if (!TextUtils.isEmpty(initialQuery)) {
2152 intent.putExtra(SearchManager.QUERY, initialQuery);
2153 }
2154 if (selectInitialQuery) {
2155 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2156 }
2157 intent.setSourceBounds(sourceBounds);
2158 try {
2159 startActivity(intent);
2160 } catch (ActivityNotFoundException ex) {
2161 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2162 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 }
2164
Yura4f93ec62014-02-04 14:15:21 +00002165 public boolean isOnCustomContent() {
2166 return mWorkspace.isOnOrMovingToCustomContent();
2167 }
2168
Winson Chung70d72102011-08-12 11:24:35 -07002169 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002170 public boolean onPrepareOptionsMenu(Menu menu) {
2171 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002172 if (!isOnCustomContent()) {
2173 // Close any open folders
2174 closeFolder();
2175 // Stop resizing any widgets
2176 mWorkspace.exitWidgetResizeMode();
2177 if (!mWorkspace.isInOverviewMode()) {
2178 // Show the overview mode
2179 showOverviewMode(true);
2180 } else {
2181 showWorkspace(true);
2182 }
Winson Chunge0298742014-01-17 12:03:00 -08002183 }
Adam Cohen9211d422014-10-07 18:14:53 -07002184 if (mLauncherCallbacks != null) {
2185 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2186 }
2187
Michael Jurkab94f3f82013-09-11 18:08:54 +02002188 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002191 @Override
2192 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002193 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002194 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002195 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002196 }
2197
Joe Onorato9c1289c2009-08-17 11:03:03 -04002198 public boolean isWorkspaceLocked() {
2199 return mWorkspaceLoading || mWaitingForResult;
2200 }
2201
Adam Cohen517a7f52014-03-01 12:12:59 -08002202 public boolean isWorkspaceLoading() {
2203 return mWorkspaceLoading;
2204 }
2205
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002206 private void setWorkspaceLoading(boolean value) {
2207 boolean isLocked = isWorkspaceLocked();
2208 mWorkspaceLoading = value;
2209 if (isLocked != isWorkspaceLocked()) {
2210 onWorkspaceLockedChanged();
2211 }
2212 }
2213
2214 private void setWaitingForResult(boolean value) {
2215 boolean isLocked = isWorkspaceLocked();
2216 mWaitingForResult = value;
2217 if (isLocked != isWorkspaceLocked()) {
2218 onWorkspaceLockedChanged();
2219 }
2220 }
2221
Adam Cohen9211d422014-10-07 18:14:53 -07002222 protected void onWorkspaceLockedChanged() {
2223 if (mLauncherCallbacks != null) {
2224 mLauncherCallbacks.onWorkspaceLockedChanged();
2225 }
2226 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002227
Michael Jurka0280c3b2010-09-17 15:00:07 -07002228 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002229 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002230 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002231 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2232 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002233 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002234 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002235 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002236
Sunny Goyale6b63a32015-01-16 16:14:29 -08002237 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002238 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002239 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2240 }
2241
Sunny Goyale6b63a32015-01-16 16:14:29 -08002242 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002243 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2244 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002245 if (appWidgetInfo.configure != null) {
2246 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002247 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002248
Bjorn Bringert7984c942009-12-09 15:38:25 +00002249 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002250 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2251 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002254 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002255 Runnable onComplete = new Runnable() {
2256 @Override
2257 public void run() {
2258 // Exit spring loaded mode if necessary after adding the widget
2259 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2260 null);
2261 }
2262 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002263 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002264 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002265 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 }
2267 }
Romain Guycbb89e42009-06-08 15:52:54 -07002268
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002269 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002270 // Close any folders that may be open.
2271 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002272 mWorkspace.moveToCustomContentScreen(animate);
2273 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002274
2275 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2276 int[] cell, int spanX, int spanY) {
2277 switch (info.itemType) {
2278 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2279 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2280 int span[] = new int[2];
2281 span[0] = spanX;
2282 span[1] = spanY;
2283 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2284 container, screenId, cell, span);
2285 break;
2286 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2287 processShortcutFromDrop(info.componentName, container, screenId, cell);
2288 break;
2289 default:
2290 throw new IllegalStateException("Unknown item type: " + info.itemType);
2291 }
2292 }
2293
Adam Cohenfbba09b2011-07-18 21:43:05 -07002294 /**
2295 * Process a shortcut drop.
2296 *
2297 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002298 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002299 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002300 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002301 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2302 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002303 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002304 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002305 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002306 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002307
2308 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002309 mPendingAddInfo.cellX = cell[0];
2310 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002311 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002312
2313 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2314 createShortcutIntent.setComponent(componentName);
2315 processShortcut(createShortcutIntent);
2316 }
2317
Adam Cohenfbba09b2011-07-18 21:43:05 -07002318 /**
2319 * Process a widget drop.
2320 *
2321 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002322 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002323 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002324 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002325 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2326 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002328 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002329 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002330 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002331 mPendingAddInfo.minSpanX = info.minSpanX;
2332 mPendingAddInfo.minSpanY = info.minSpanY;
2333
Adam Cohenfbba09b2011-07-18 21:43:05 -07002334 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002335 mPendingAddInfo.cellX = cell[0];
2336 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002337 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002338 if (span != null) {
2339 mPendingAddInfo.spanX = span[0];
2340 mPendingAddInfo.spanY = span[1];
2341 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342
Adam Cohened66b2b2012-01-23 17:28:51 -08002343 AppWidgetHostView hostView = info.boundWidget;
2344 int appWidgetId;
2345 if (hostView != null) {
2346 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002347 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002348 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002349 // In this case, we either need to start an activity to get permission to bind
2350 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002351 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002352 Bundle options = info.bindOptions;
2353
Sunny Goyalffe83f12014-08-14 17:39:34 -07002354 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2355 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002356 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002357 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002358 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002359 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002360 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2361 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2362 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002363 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2364 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002365 // TODO: we need to make sure that this accounts for the options bundle.
2366 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002367 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2368 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002369 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002370 }
2371
Joe Onoratodeb98af2010-02-19 14:59:39 -08002372 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002373 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 }
2375
Winson Chung24ab2f12010-09-16 14:10:47 -07002376 void processWallpaper(Intent intent) {
2377 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2378 }
2379
Adam Cohendcd297f2013-06-18 13:13:40 -07002380 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002381 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002382 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 folderInfo.title = getText(R.string.folder_name);
2384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002385 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002386 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002387 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002388 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389
2390 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002391 FolderIcon newFolder =
2392 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002393 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002394 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002395 // Force measure the new folder icon
2396 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2397 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002398 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 }
Romain Guycbb89e42009-06-08 15:52:54 -07002400
Joe Onorato9c1289c2009-08-17 11:03:03 -04002401 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002402 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002403 }
2404
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002405 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002406 if (mLauncherCallbacks != null) {
2407 return mLauncherCallbacks.getWallpaperPickerComponent();
2408 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002409 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002410 }
2411
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002412 /**
2413 * Registers various content observers. The current implementation registers
2414 * only a favorites observer to keep track of the favorites applications.
2415 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002416 private void registerContentObservers() {
2417 ContentResolver resolver = getContentResolver();
2418 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2419 true, mWidgetObserver);
2420 }
2421
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 @Override
2423 public boolean dispatchKeyEvent(KeyEvent event) {
2424 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2425 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002426 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002427 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002428 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002429 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002430 dumpState();
2431 return true;
2432 }
2433 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002434 }
2435 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2436 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002437 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 return true;
2439 }
2440 }
2441
2442 return super.dispatchKeyEvent(event);
2443 }
2444
Joe Onorato88ec0992009-11-19 13:16:06 -08002445 @Override
2446 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002447 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2448 return;
2449 }
2450
Adam Cohenc9735cf2015-01-23 16:11:55 -08002451 if (LauncherAppState.getInstance().getAccessibilityDelegate().onBackPressed()) {
2452 return;
2453 }
2454
Winson Chungc93e5ae2012-07-23 20:48:26 -07002455 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002456 if (mAppsCustomizeContent.getContentType() ==
2457 AppsCustomizePagedView.ContentType.Applications) {
2458 showWorkspace(true);
2459 } else {
2460 showOverviewMode(true);
2461 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002462 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002463 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002464 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002465 Folder openFolder = mWorkspace.getOpenFolder();
2466 if (openFolder.isEditingName()) {
2467 openFolder.dismissEditingName();
2468 } else {
2469 closeFolder();
2470 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002471 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002472 mWorkspace.exitWidgetResizeMode();
2473
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002474 // Back button is a no-op here, but give at least some feedback for the button press
2475 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002476 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002477 }
2478
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002479 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002480 * Re-listen when widgets are reset.
2481 */
2482 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002483 if (mAppWidgetHost != null) {
2484 mAppWidgetHost.startListening();
2485 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002486 }
2487
2488 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 * Launches the intent referred by the clicked shortcut.
2490 *
2491 * @param v The view representing the clicked shortcut.
2492 */
2493 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002494 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2495 // view has detached (it's possible for this to happen if the view is removed mid touch).
2496 if (v.getWindowToken() == null) {
2497 return;
2498 }
2499
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002500 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002501 return;
2502 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002503
Adam Cohen1697b792013-09-17 19:08:21 -07002504 if (v instanceof Workspace) {
2505 if (mWorkspace.isInOverviewMode()) {
2506 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002507 }
2508 return;
2509 }
2510
2511 if (v instanceof CellLayout) {
2512 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002513 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002514 }
2515 }
2516
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002518 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002519 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002521 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002522 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002523 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002524 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002525 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002526 } else if (tag instanceof AppInfo) {
2527 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002528 } else if (tag instanceof LauncherAppWidgetInfo) {
2529 if (v instanceof PendingAppWidgetHostView) {
2530 onClickPendingWidget((PendingAppWidgetHostView) v);
2531 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002532 }
2533 }
2534
Sunny Goyal508da152014-08-14 10:53:27 -07002535 public void onClickPagedViewIcon(View v) {
2536 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002537 if (mLauncherCallbacks != null) {
2538 mLauncherCallbacks.onClickPagedViewIcon(v);
2539 }
Sunny Goyal508da152014-08-14 10:53:27 -07002540 }
2541
Michael Jurka0e260592010-06-30 17:07:39 -07002542 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002543 return false;
2544 }
2545
Michael Jurkaaf442092010-06-10 17:01:57 -07002546 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002547 * Event handler for the app widget view which has not fully restored.
2548 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002549 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002550 if (mIsSafeModeEnabled) {
2551 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2552 return;
2553 }
2554
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002555 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002556 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002557 int widgetId = info.appWidgetId;
2558 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2559 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002560 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2561 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002562 mPendingAddInfo.copyFrom(info);
2563 mPendingAddWidgetId = widgetId;
2564
Sunny Goyalffe83f12014-08-14 17:39:34 -07002565 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2566 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002567 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002568 } else if (info.installProgress < 0) {
2569 // The install has not been queued
2570 final String packageName = info.providerName.getPackageName();
2571 showBrokenAppInstallDialog(packageName,
2572 new DialogInterface.OnClickListener() {
2573 public void onClick(DialogInterface dialog, int id) {
2574 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2575 }
2576 });
2577 } else {
2578 // Download has started.
2579 final String packageName = info.providerName.getPackageName();
2580 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002581 }
2582 }
2583
2584 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002585 * Event handler for the "grid" button that appears on the home screen, which
2586 * enters all apps mode.
2587 *
2588 * @param v The view that was clicked.
2589 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002590 protected void onClickAllAppsButton(View v) {
2591 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2592 if (isAllAppsVisible()) {
2593 showWorkspace(true);
2594 } else {
2595 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2596 }
Adam Cohen9211d422014-10-07 18:14:53 -07002597 if (mLauncherCallbacks != null) {
2598 mLauncherCallbacks.onClickAllAppsButton(v);
2599 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002600 }
2601
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002602 private void showBrokenAppInstallDialog(final String packageName,
2603 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002604 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002605 .setTitle(R.string.abandoned_promises_title)
2606 .setMessage(R.string.abandoned_promise_explanation)
2607 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2608 .setNeutralButton(R.string.abandoned_clean_this,
2609 new DialogInterface.OnClickListener() {
2610 public void onClick(DialogInterface dialog, int id) {
2611 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2612 mWorkspace.removeAbandonedPromise(packageName, user);
2613 }
2614 })
2615 .create().show();
2616 return;
2617 }
2618
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002619 /**
2620 * Event handler for an app shortcut click.
2621 *
2622 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2623 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002624 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2626 Object tag = v.getTag();
2627 if (!(tag instanceof ShortcutInfo)) {
2628 throw new IllegalArgumentException("Input must be a Shortcut");
2629 }
2630
2631 // Open shortcut
2632 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002633
2634 if (shortcut.isDisabled != 0) {
2635 int error = R.string.activity_not_available;
2636 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2637 error = R.string.safemode_shortcut_error;
2638 }
2639 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2640 return;
2641 }
2642
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002643 final Intent intent = shortcut.intent;
2644
2645 // Check for special shortcuts
2646 if (intent.getComponent() != null) {
2647 final String shortcutClass = intent.getComponent().getClassName();
2648
2649 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2650 MemoryDumpActivity.startDump(this);
2651 return;
2652 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2653 toggleShowWeightWatcher();
2654 return;
2655 }
2656 }
2657
Chris Wren40c5ed32014-06-24 18:24:23 -04002658 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002659 if ((v instanceof BubbleTextView)
2660 && shortcut.isPromise()
2661 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002662 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002663 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002664 new DialogInterface.OnClickListener() {
2665 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002666 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002667 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002668 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002669 return;
2670 }
2671
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002672 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002673 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002674
2675 if (mLauncherCallbacks != null) {
2676 mLauncherCallbacks.onClickAppShortcut(v);
2677 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002678 }
2679
Sunny Goyal508da152014-08-14 10:53:27 -07002680 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002681 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002682 final ShortcutInfo shortcut;
2683 final Intent intent;
2684 if (tag instanceof ShortcutInfo) {
2685 shortcut = (ShortcutInfo) tag;
2686 intent = shortcut.intent;
2687 int[] pos = new int[2];
2688 v.getLocationOnScreen(pos);
2689 intent.setSourceBounds(new Rect(pos[0], pos[1],
2690 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002691
Sunny Goyal508da152014-08-14 10:53:27 -07002692 } else if (tag instanceof AppInfo) {
2693 shortcut = null;
2694 intent = ((AppInfo) tag).intent;
2695 } else {
2696 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2697 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002698
2699 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002700 mStats.recordLaunch(intent, shortcut);
2701
2702 if (success && v instanceof BubbleTextView) {
2703 mWaitingForResume = (BubbleTextView) v;
2704 mWaitingForResume.setStayPressed(true);
2705 }
2706 }
2707
2708 /**
2709 * Event handler for a folder icon click.
2710 *
2711 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2712 */
2713 protected void onClickFolderIcon(View v) {
2714 if (LOGD) Log.d(TAG, "onClickFolder");
2715 if (!(v instanceof FolderIcon)){
2716 throw new IllegalArgumentException("Input must be a FolderIcon");
2717 }
2718
2719 FolderIcon folderIcon = (FolderIcon) v;
2720 final FolderInfo info = folderIcon.getFolderInfo();
2721 Folder openFolder = mWorkspace.getFolderForTag(info);
2722
2723 // If the folder info reports that the associated folder is open, then verify that
2724 // it is actually opened. There have been a few instances where this gets out of sync.
2725 if (info.opened && openFolder == null) {
2726 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2727 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2728 info.opened = false;
2729 }
2730
2731 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2732 // Close any open folder
2733 closeFolder();
2734 // Open the requested folder
2735 openFolder(folderIcon);
2736 } else {
2737 // Find the open folder...
2738 int folderScreen;
2739 if (openFolder != null) {
2740 folderScreen = mWorkspace.getPageForView(openFolder);
2741 // .. and close it
2742 closeFolder(openFolder);
2743 if (folderScreen != mWorkspace.getCurrentPage()) {
2744 // Close any folder open on the current screen
2745 closeFolder();
2746 // Pull the folder onto this screen
2747 openFolder(folderIcon);
2748 }
2749 }
2750 }
Adam Cohen9211d422014-10-07 18:14:53 -07002751
2752 if (mLauncherCallbacks != null) {
2753 mLauncherCallbacks.onClickFolderIcon(v);
2754 }
Michael Jurka5130e402011-10-13 04:55:35 -07002755 }
2756
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002757 /**
2758 * Event handler for the (Add) Widgets button that appears after a long press
2759 * on the home screen.
2760 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002761 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002762 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002763 if (mIsSafeModeEnabled) {
2764 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2765 } else {
2766 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2767 if (mLauncherCallbacks != null) {
2768 mLauncherCallbacks.onClickAddWidgetButton(view);
2769 }
Adam Cohen9211d422014-10-07 18:14:53 -07002770 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002771 }
2772
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002773 /**
2774 * Event handler for the wallpaper picker button that appears after a long press
2775 * on the home screen.
2776 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002777 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002778 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2779 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2780 pickWallpaper.setComponent(getWallpaperPickerComponent());
2781 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002782
2783 if (mLauncherCallbacks != null) {
2784 mLauncherCallbacks.onClickWallpaperPicker(v);
2785 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002786 }
2787
2788 /**
2789 * Event handler for a click on the settings button that appears after a long press
2790 * on the home screen.
2791 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002792 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002793 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002794 if (mLauncherCallbacks != null) {
2795 mLauncherCallbacks.onClickSettingsButton(v);
2796 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002797 }
2798
Michael Jurka5130e402011-10-13 04:55:35 -07002799 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002800 // Provide the same haptic feedback that the system offers for virtual keys.
2801 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002802 }
2803
Adam Cohen61f560d2013-09-30 15:58:20 -07002804 public void performHapticFeedbackOnTouchDown(View v) {
2805 // Provide the same haptic feedback that the system offers for virtual keys.
2806 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2807 }
2808
2809 public View.OnTouchListener getHapticFeedbackTouchListener() {
2810 if (mHapticFeedbackTouchListener == null) {
2811 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2812 @Override
2813 public boolean onTouch(View v, MotionEvent event) {
2814 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2815 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2816 }
2817 return false;
2818 }
2819 };
2820 }
2821 return mHapticFeedbackTouchListener;
2822 }
2823
Adam Cohen9211d422014-10-07 18:14:53 -07002824 public void onDragStarted(View view) {
2825 if (isOnCustomContent()) {
2826 // Custom content screen doesn't participate in drag and drop. If on custom
2827 // content screen, move to default.
2828 moveWorkspaceToDefaultScreen();
2829 }
2830
2831 if (mLauncherCallbacks != null) {
2832 mLauncherCallbacks.onDragStarted(view);
2833 }
2834 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002835
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002836 /**
2837 * Called when the user stops interacting with the launcher.
2838 * This implies that the user is now on the homescreen and is not doing housekeeping.
2839 */
Adam Cohen9211d422014-10-07 18:14:53 -07002840 protected void onInteractionEnd() {
2841 if (mLauncherCallbacks != null) {
2842 mLauncherCallbacks.onInteractionEnd();
2843 }
2844 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002845
2846 /**
2847 * Called when the user starts interacting with the launcher.
2848 * The possible interactions are:
2849 * - open all apps
2850 * - reorder an app shortcut, or a widget
2851 * - open the overview mode.
2852 * This is a good time to stop doing things that only make sense
2853 * when the user is on the homescreen and not doing housekeeping.
2854 */
Adam Cohen9211d422014-10-07 18:14:53 -07002855 protected void onInteractionBegin() {
2856 if (mLauncherCallbacks != null) {
2857 mLauncherCallbacks.onInteractionBegin();
2858 }
2859 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002860
Kenny Guyf07af7b2014-07-31 11:39:16 +01002861 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002862 try {
2863 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002864 launcherApps.showAppDetailsForProfile(componentName, user);
2865 } catch (SecurityException e) {
2866 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2867 Log.e(TAG, "Launcher does not have permission to launch settings");
2868 } catch (ActivityNotFoundException e) {
2869 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2870 Log.e(TAG, "Unable to launch settings");
2871 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002872 }
2873
Michael Jurka1e2f4652013-07-08 18:03:46 -07002874 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002875 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2876 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002877 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002878 // System applications cannot be installed. For now, show a toast explaining that.
2879 // We may give them the option of disabling apps this way.
2880 int messageId = R.string.uninstall_system_app_text;
2881 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002882 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002883 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002884 String packageName = componentName.getPackageName();
2885 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002886 Intent intent = new Intent(
2887 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002888 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2889 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002890 if (user != null) {
2891 user.addToIntent(intent, Intent.EXTRA_USER);
2892 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002893 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002894 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002895 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002896 }
2897
Michael Jurka86a720e2012-05-09 11:23:23 -07002898 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002899 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002900 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002901 // Only launch using the new animation if the shortcut has not opted out (this is a
2902 // private contract between launcher and may be ignored in the future).
2903 boolean useLaunchAnimation = (v != null) &&
2904 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002905 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2906 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002907
Kenny Guy1317e2d2014-05-08 18:52:50 +01002908 UserHandleCompat user = null;
2909 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2910 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2911 user = userManager.getUserForSerialNumber(serialNumber);
2912 }
2913
2914 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002915 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002916 ActivityOptions opts = null;
2917 if (sClipRevealMethod != null) {
2918 // TODO: call method directly when Launcher3 can depend on M APIs
2919 int left = 0, top = 0;
2920 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2921 if (v instanceof TextView) {
2922 // Launch from center of icon, not entire view
2923 TextView tv = (TextView) v;
2924 Drawable[] drawables = tv.getCompoundDrawables();
2925 if (drawables != null && drawables[1] != null) {
2926 Rect bounds = drawables[1].getBounds();
2927 left = (width - bounds.width()) / 2;
2928 top = tv.getPaddingTop();
2929 width = bounds.width();
2930 height = bounds.height();
2931 }
2932 }
2933 try {
2934 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2935 left, top, width, height);
2936 } catch (IllegalAccessException e) {
2937 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2938 sClipRevealMethod = null;
2939 } catch (InvocationTargetException e) {
2940 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2941 sClipRevealMethod = null;
2942 }
2943 }
2944 if (opts == null) {
2945 opts = Utilities.isLmpOrAbove() ?
2946 ActivityOptions.makeCustomAnimation(this,
2947 R.anim.task_open_enter, R.anim.no_anim) :
2948 ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2949 v.getMeasuredWidth(), v.getMeasuredHeight());
2950 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002951 optsBundle = opts.toBundle();
2952 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002953
2954 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2955 // Could be launching some bookkeeping activity
2956 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002957 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002958 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002959 launcherApps.startActivityForProfile(intent.getComponent(), user,
2960 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002961 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002962 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 } catch (SecurityException e) {
2964 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002965 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002967 "or use the exported attribute for this activity. "
2968 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002970 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002972
Michael Jurka86a720e2012-05-09 11:23:23 -07002973 boolean startActivitySafely(View v, Intent intent, Object tag) {
2974 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002975 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2976 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2977 return false;
2978 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002979 try {
2980 success = startActivity(v, intent, tag);
2981 } catch (ActivityNotFoundException e) {
2982 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2983 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2984 }
2985 return success;
2986 }
2987
Adam Cohen268c4752012-06-06 17:47:33 -07002988 /**
2989 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2990 * in the DragLayer in the exact absolute location of the original FolderIcon.
2991 */
2992 private void copyFolderIconToImage(FolderIcon fi) {
2993 final int width = fi.getMeasuredWidth();
2994 final int height = fi.getMeasuredHeight();
2995
2996 // Lazy load ImageView, Bitmap and Canvas
2997 if (mFolderIconImageView == null) {
2998 mFolderIconImageView = new ImageView(this);
2999 }
3000 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3001 mFolderIconBitmap.getHeight() != height) {
3002 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3003 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3004 }
3005
3006 DragLayer.LayoutParams lp;
3007 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3008 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3009 } else {
3010 lp = new DragLayer.LayoutParams(width, height);
3011 }
3012
Adam Cohen307fe232012-08-16 17:55:58 -07003013 // The layout from which the folder is being opened may be scaled, adjust the starting
3014 // view size by this scale factor.
3015 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003016 lp.customPosition = true;
3017 lp.x = mRectForFolderAnimation.left;
3018 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003019 lp.width = (int) (scale * width);
3020 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003021
3022 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3023 fi.draw(mFolderIconCanvas);
3024 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003025 if (fi.getFolder() != null) {
3026 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3027 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003028 }
Adam Cohen268c4752012-06-06 17:47:33 -07003029 // Just in case this image view is still in the drag layer from a previous animation,
3030 // we remove it and re-add it.
3031 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3032 mDragLayer.removeView(mFolderIconImageView);
3033 }
3034 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003035 if (fi.getFolder() != null) {
3036 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003037 }
Adam Cohen268c4752012-06-06 17:47:33 -07003038 }
3039
Adam Cohen2801caf2011-05-13 20:57:39 -07003040 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003041 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003042 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3043 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3044 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3045
Adam Cohenc51934b2011-07-26 21:07:43 -07003046 FolderInfo info = (FolderInfo) fi.getTag();
3047 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3048 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003049 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3050 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003051 }
3052
Adam Cohen268c4752012-06-06 17:47:33 -07003053 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3054 copyFolderIconToImage(fi);
3055 fi.setVisibility(View.INVISIBLE);
3056
Michael Jurka2ecf9952012-06-18 12:52:28 -07003057 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003058 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003059 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003060 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3061 }
3062 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003063 oa.start();
3064 }
3065
Adam Cohen268c4752012-06-06 17:47:33 -07003066 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003067 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003068 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3069 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3070 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3071
Adam Cohen268c4752012-06-06 17:47:33 -07003072 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003073
Adam Cohen268c4752012-06-06 17:47:33 -07003074 // We remove and re-draw the FolderIcon in-case it has changed
3075 mDragLayer.removeView(mFolderIconImageView);
3076 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003077 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003078 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003079 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003080 oa.addListener(new AnimatorListenerAdapter() {
3081 @Override
3082 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003083 if (cl != null) {
3084 cl.clearFolderLeaveBehind();
3085 // Remove the ImageView copy of the FolderIcon and make the original visible.
3086 mDragLayer.removeView(mFolderIconImageView);
3087 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003088 }
3089 }
3090 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003091 oa.start();
3092 }
3093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003094 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003095 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003096 * is animated relative to the specified View. If the View is null, no animation
3097 * is played.
3098 *
3099 * @param folderInfo The FolderInfo describing the folder to open.
3100 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003101 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003102 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003103 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003104
Adam Cohena9cf38f2011-05-02 15:36:58 -07003105 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106
Adam Cohen4554ee12011-08-03 16:13:21 -07003107 // Just verify that the folder hasn't already been added to the DragLayer.
3108 // There was a one-off crash where the folder had a parent already.
3109 if (folder.getParent() == null) {
3110 mDragLayer.addView(folder);
3111 mDragController.addDropTarget((DropTarget) folder);
3112 } else {
3113 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3114 folder.getParent() + ").");
3115 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003116 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003117 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003118
3119 // Notify the accessibility manager that this folder "window" has appeared and occluded
3120 // the workspace items
3121 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3122 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003123 }
3124
3125 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003126 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003127 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003128 if (folder.isEditingName()) {
3129 folder.dismissEditingName();
3130 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003131 closeFolder(folder);
3132 }
3133 }
3134
3135 void closeFolder(Folder folder) {
3136 folder.getInfo().opened = false;
3137
3138 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3139 if (parent != null) {
3140 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3141 shrinkAndFadeInFolderIcon(fi);
3142 }
3143 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003144
3145 // Notify the accessibility manager that this folder "window" has disappeard and no
3146 // longer occludeds the workspace items
3147 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003148 }
3149
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003150 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003151 if (!isDraggingEnabled()) return false;
3152 if (isWorkspaceLocked()) return false;
3153 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003154
Adam Cohen1697b792013-09-17 19:08:21 -07003155 if (v instanceof Workspace) {
3156 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003157 if (mWorkspace.enterOverviewMode()) {
3158 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3159 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3160 return true;
3161 } else {
3162 return false;
3163 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003164 } else {
3165 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003166 }
Adam Cohen1697b792013-09-17 19:08:21 -07003167 }
3168
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003169 CellLayout.CellInfo longClickCellInfo = null;
3170 View itemUnderLongClick = null;
3171 if (v.getTag() instanceof ItemInfo) {
3172 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003173 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003174 itemUnderLongClick = longClickCellInfo.cell;
3175 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003176 }
3177
Winson Chung3d503fb2011-07-13 17:25:49 -07003178 // The hotseat touch handling does not go through Workspace, and we always allow long press
3179 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003180 final boolean inHotseat = isHotseatLayout(v);
3181 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003182 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003183 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003184 // User long pressed on empty space
3185 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3186 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003187 if (mWorkspace.isInOverviewMode()) {
3188 mWorkspace.startReordering(v);
3189 } else {
3190 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003193 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3194 mHotseat.getOrderInHotseat(
3195 longClickCellInfo.cellX,
3196 longClickCellInfo.cellY));
3197 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003198 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003199 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003200 }
3201 }
3202 }
3203 return true;
3204 }
3205
Winson Chung3d503fb2011-07-13 17:25:49 -07003206 boolean isHotseatLayout(View layout) {
3207 return mHotseat != null && layout != null &&
3208 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3209 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003210
Winson Chung3d503fb2011-07-13 17:25:49 -07003211 /**
3212 * Returns the CellLayout of the specified container at the specified screen.
3213 */
Sunny Goyal92820592015-03-02 11:31:35 -08003214 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003215 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3216 if (mHotseat != null) {
3217 return mHotseat.getLayout();
3218 } else {
3219 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003220 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003221 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003222 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003223 }
3224 }
3225
Daniel Sandler843e8602010-06-07 14:59:01 -04003226 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003227 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003228 }
3229
Craig Mautner360310b2012-10-26 15:13:08 -07003230 private void setWorkspaceBackground(boolean workspace) {
3231 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003232 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003233 }
3234
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003235 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003236 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3237 int curflags = getWindow().getAttributes().flags
3238 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3239 if (wpflags != curflags) {
3240 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3241 }
Craig Mautner360310b2012-10-26 15:13:08 -07003242 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003243 }
3244
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003245 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3246 if (v instanceof LauncherTransitionable) {
3247 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3248 }
3249 }
3250
Michael Jurkabed61d22012-02-14 22:51:29 -08003251 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3252 if (v instanceof LauncherTransitionable) {
3253 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3254 }
Winson Chung70442722012-02-10 15:43:22 -08003255
3256 // Update the workspace transition step as well
3257 dispatchOnLauncherTransitionStep(v, 0f);
3258 }
3259
3260 private void dispatchOnLauncherTransitionStep(View v, float t) {
3261 if (v instanceof LauncherTransitionable) {
3262 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3263 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003264 }
3265
3266 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3267 if (v instanceof LauncherTransitionable) {
3268 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3269 }
Winson Chung70442722012-02-10 15:43:22 -08003270
3271 // Update the workspace transition step as well
3272 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003273 }
3274
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003275 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003276 * Things to test when changing the following seven functions.
3277 * - Home from workspace
3278 * - from center screen
3279 * - from other screens
3280 * - Home from all apps
3281 * - from center screen
3282 * - from other screens
3283 * - Back from all apps
3284 * - from center screen
3285 * - from other screens
3286 * - Launch app from workspace and quit
3287 * - with back
3288 * - with home
3289 * - Launch app from all apps and quit
3290 * - with back
3291 * - with home
3292 * - Go to a screen that's not the default, then all
3293 * apps, and launch and app, and go back
3294 * - with back
3295 * -with home
3296 * - On workspace, long press power and go back
3297 * - with back
3298 * - with home
3299 * - On all apps, long press power and go back
3300 * - with back
3301 * - with home
3302 * - On workspace, power off
3303 * - On all apps, power off
3304 * - Launch an app and turn off the screen while in that app
3305 * - Go back with home key
3306 * - Go back with back key TODO: make this not go to workspace
3307 * - From all apps
3308 * - From workspace
3309 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3310 * - From all apps
3311 * - From the center workspace
3312 * - From another workspace
3313 */
3314
3315 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003316 * Zoom the camera out from the workspace to reveal 'toView'.
3317 * Assumes that the view to show is anchored at either the very top or very bottom
3318 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003319 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003320 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003321 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3322 showAppsCustomizeHelper(animated, springLoaded, contentType);
3323 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003324
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003325 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chungc58497e2013-09-03 17:48:37 -07003326 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3327 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003328 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003329 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003330 mStateAnimation.cancel();
3331 mStateAnimation = null;
3332 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003333
Kenny Guyd794a3f2014-09-16 15:17:58 +01003334 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003335
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003336 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003337
Adam Cohen6c5891a2014-07-09 23:53:15 -07003338 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003339 final int itemsAlphaStagger =
3340 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003341
Michael Jurkabed61d22012-02-14 22:51:29 -08003342 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003343 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003344
Adam Cohen80e6beb2015-02-13 16:14:33 -08003345 final HashMap<View, Integer> layerViews = new HashMap<View, Integer>();
Adam Cohen2854d252014-08-27 16:04:07 -07003346
Adam Cohen6c5891a2014-07-09 23:53:15 -07003347 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3348 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003349 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003350 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003351 if (contentType == AppsCustomizePagedView.ContentType.Widgets) {
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003352 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003353 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3354 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003355
Adam Cohena38dc902014-09-07 17:48:55 +02003356 // If for some reason our views aren't initialized, don't animate
3357 boolean initialized = getAllAppsButton() != null;
3358
3359 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003360 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003361 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3362 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003363
Adam Cohen9bfdb762014-07-21 17:44:06 -07003364 final View page = content.getPageAt(content.getCurrentPage());
3365 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003366
Adam Cohen63f1ec02014-08-12 09:23:13 -07003367 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3368 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003369 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3370 } else {
3371 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3372 }
3373
Adam Cohen9bfdb762014-07-21 17:44:06 -07003374 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003375 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003376 revealView.setVisibility(View.VISIBLE);
3377 // We need to hide this view as the animation start will be posted.
3378 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003379
Adam Cohen9bfdb762014-07-21 17:44:06 -07003380 int width = revealView.getMeasuredWidth();
3381 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003382 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003383
Adam Cohen63f1ec02014-08-12 09:23:13 -07003384 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003385 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003386
Adam Cohen63f1ec02014-08-12 09:23:13 -07003387 // Get the y delta between the center of the page and the center of the all apps button
3388 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3389 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003390
Adam Cohen2854d252014-08-27 16:04:07 -07003391 float alpha = 0;
3392 float xDrift = 0;
3393 float yDrift = 0;
3394 if (material) {
3395 alpha = isWidgetTray ? 0.3f : 1f;
3396 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3397 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3398 } else {
3399 yDrift = 2 * height / 3;
3400 xDrift = 0;
3401 }
3402 final float initAlpha = alpha;
3403
Adam Cohen80e6beb2015-02-13 16:14:33 -08003404 layerViews.put(revealView, BUILD_AND_SET_LAYER);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003405 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003406 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003407 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003408 PropertyValuesHolder panelDriftX =
3409 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003410
Adam Cohen2854d252014-08-27 16:04:07 -07003411 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3412 panelAlpha, panelDriftY, panelDriftX);
3413
Adam Cohen9bfdb762014-07-21 17:44:06 -07003414 panelAlphaAndDrift.setDuration(revealDuration);
3415 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003416
Adam Cohen9bfdb762014-07-21 17:44:06 -07003417 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003418
Adam Cohen4e243a22014-08-10 18:30:55 -07003419 if (page != null) {
3420 page.setVisibility(View.VISIBLE);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003421 layerViews.put(page, BUILD_AND_SET_LAYER);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003422
Adam Cohen2854d252014-08-27 16:04:07 -07003423 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3424 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003425 pageDrift.setDuration(revealDuration);
3426 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003427 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003428 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003429
Adam Cohen63f1ec02014-08-12 09:23:13 -07003430 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003431 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003432 itemsAlpha.setDuration(revealDuration);
3433 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3434 itemsAlpha.setStartDelay(itemsAlphaStagger);
3435 mStateAnimation.play(itemsAlpha);
3436 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003437
Adam Cohen4e243a22014-08-10 18:30:55 -07003438 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3439 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003440 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003441 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003442 indicatorsAlpha.setDuration(revealDuration);
3443 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003444
Adam Cohen9bfdb762014-07-21 17:44:06 -07003445 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003446 final View allApps = getAllAppsButton();
3447 int allAppsButtonSize = LauncherAppState.getInstance().
3448 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3449 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003450 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003451 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003452 reveal.setDuration(revealDuration);
3453 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003454
3455 reveal.addListener(new AnimatorListenerAdapter() {
3456 public void onAnimationStart(Animator animation) {
3457 if (!isWidgetTray) {
3458 allApps.setVisibility(View.INVISIBLE);
3459 }
3460 }
3461 public void onAnimationEnd(Animator animation) {
3462 if (!isWidgetTray) {
3463 allApps.setVisibility(View.VISIBLE);
3464 }
3465 }
3466 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003467 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003468 }
Michael Jurka1899a362011-11-03 13:50:45 -07003469
Adam Cohen9bfdb762014-07-21 17:44:06 -07003470 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3471 @Override
3472 public void onAnimationEnd(Animator animation) {
3473 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3474 dispatchOnLauncherTransitionEnd(toView, animated, false);
3475
3476 revealView.setVisibility(View.INVISIBLE);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003477
3478 for (View v : layerViews.keySet()) {
3479 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3480 v.setLayerType(View.LAYER_TYPE_NONE, null);
3481 }
Adam Cohen4e243a22014-08-10 18:30:55 -07003482 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003483 content.setPageBackgroundsVisible(true);
3484
3485 // Hide the search bar
3486 if (mSearchDropTargetBar != null) {
3487 mSearchDropTargetBar.hideSearchBar(false);
3488 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003489
3490 // This can hold unnecessary references to views.
3491 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003492 }
3493
Adam Cohen9bfdb762014-07-21 17:44:06 -07003494 });
3495
Adam Cohen6c5891a2014-07-09 23:53:15 -07003496 if (workspaceAnim != null) {
3497 mStateAnimation.play(workspaceAnim);
3498 }
Adam Cohen2854d252014-08-27 16:04:07 -07003499
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003500 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3501 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003502 final AnimatorSet stateAnimation = mStateAnimation;
3503 final Runnable startAnimRunnable = new Runnable() {
3504 public void run() {
3505 // Check that mStateAnimation hasn't changed while
3506 // we waited for a layout/draw pass
3507 if (mStateAnimation != stateAnimation)
3508 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003509 dispatchOnLauncherTransitionStart(fromView, animated, false);
3510 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003511
3512 revealView.setAlpha(initAlpha);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003513
3514 for (View v : layerViews.keySet()) {
3515 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3516 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3517 }
3518 }
3519
Kenny Guyd794a3f2014-09-16 15:17:58 +01003520 if (Utilities.isLmpOrAbove()) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003521 for (View v : layerViews.keySet()) {
3522 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen2854d252014-08-27 16:04:07 -07003523 }
3524 }
3525 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003526 }
3527 };
Adam Cohen2854d252014-08-27 16:04:07 -07003528 toView.bringToFront();
3529 toView.setVisibility(View.VISIBLE);
3530 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003531 } else {
3532 toView.setTranslationX(0.0f);
3533 toView.setTranslationY(0.0f);
3534 toView.setScaleX(1.0f);
3535 toView.setScaleY(1.0f);
3536 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003537 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003538
Daniel Sandlere4f98912013-06-25 15:13:26 -04003539 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003540 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003541 if (mSearchDropTargetBar != null) {
3542 mSearchDropTargetBar.hideSearchBar(false);
3543 }
Michael Jurkaabded662011-03-04 12:06:57 -08003544 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003545 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003546 dispatchOnLauncherTransitionStart(fromView, animated, false);
3547 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003548 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003549 dispatchOnLauncherTransitionStart(toView, animated, false);
3550 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003551 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003552 }
3553
3554 /**
3555 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003556 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003557 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003558 */
Adam Cohened307df2013-10-02 09:37:31 -07003559 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003560 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003561
Michael Jurkab3e22d92011-10-31 15:58:33 -07003562 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003563 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003564 mStateAnimation.cancel();
3565 mStateAnimation = null;
3566 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003567
Kenny Guyd794a3f2014-09-16 15:17:58 +01003568 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003569 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003570
Adam Cohen63f1ec02014-08-12 09:23:13 -07003571 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003572 final int itemsAlphaStagger =
3573 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003574
Winson Chungf0ea4d32011-06-06 14:27:16 -07003575 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003576 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003577 Animator workspaceAnim = null;
Adam Cohen80e6beb2015-02-13 16:14:33 -08003578 final HashMap<View, Integer> layerViews = new HashMap<View, Integer>();
Adam Cohen2854d252014-08-27 16:04:07 -07003579
Adam Cohened307df2013-10-02 09:37:31 -07003580 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003581 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003582 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003583 } else if (toState == Workspace.State.SPRING_LOADED ||
3584 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003585 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003586 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003587 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003588
Adam Cohena38dc902014-09-07 17:48:55 +02003589 // If for some reason our views aren't initialized, don't animate
3590 boolean initialized = getAllAppsButton() != null;
3591
3592 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003593 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003594 if (workspaceAnim != null) {
3595 mStateAnimation.play(workspaceAnim);
3596 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003597
Adam Cohen9bfdb762014-07-21 17:44:06 -07003598 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3599 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003600
Adam Cohen9bfdb762014-07-21 17:44:06 -07003601 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003602
3603 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3604 int count = content.getChildCount();
3605 for (int i = 0; i < count; i++) {
3606 View child = content.getChildAt(i);
3607 if (child != page) {
3608 child.setVisibility(View.INVISIBLE);
3609 }
3610 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003611 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003612
Adam Cohen2854d252014-08-27 16:04:07 -07003613 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3614 // don't perform all these no-op animations. In particularly, this was causing
3615 // the all-apps button to pop in and out.
3616 if (fromView.getVisibility() == View.VISIBLE) {
3617 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3618 final boolean isWidgetTray =
3619 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003620
Adam Cohen2854d252014-08-27 16:04:07 -07003621 if (isWidgetTray) {
3622 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3623 } else {
3624 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003625 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003626
Adam Cohen2854d252014-08-27 16:04:07 -07003627 int width = revealView.getMeasuredWidth();
3628 int height = revealView.getMeasuredHeight();
3629 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3630
3631 // Hide the real page background, and swap in the fake one
3632 revealView.setVisibility(View.VISIBLE);
3633 content.setPageBackgroundsVisible(false);
3634
3635 final View allAppsButton = getAllAppsButton();
3636 revealView.setTranslationY(0);
3637 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3638 allAppsButton, null);
3639
3640 float xDrift = 0;
3641 float yDrift = 0;
3642 if (material) {
3643 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3644 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3645 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003646 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003647 xDrift = 0;
3648 }
3649
Adam Cohen80e6beb2015-02-13 16:14:33 -08003650 layerViews.put(revealView, BUILD_AND_SET_LAYER);
Adam Cohen2854d252014-08-27 16:04:07 -07003651 TimeInterpolator decelerateInterpolator = material ?
3652 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003653 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003654
3655 // The vertical motion of the apps panel should be delayed by one frame
3656 // from the conceal animation in order to give the right feel. We correpsondingly
3657 // shorten the duration so that the slide and conceal end at the same time.
3658 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3659 0, yDrift);
3660 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3661 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3662 panelDriftY.setInterpolator(decelerateInterpolator);
3663 mStateAnimation.play(panelDriftY);
3664
3665 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3666 0, xDrift);
3667 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3668 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3669 panelDriftX.setInterpolator(decelerateInterpolator);
3670 mStateAnimation.play(panelDriftX);
3671
3672 if (isWidgetTray || !material) {
3673 float finalAlpha = material ? 0.4f : 0f;
3674 revealView.setAlpha(1f);
3675 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3676 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003677 panelAlpha.setDuration(material ? revealDuration : 150);
3678 panelAlpha.setInterpolator(decelerateInterpolator);
3679 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003680 mStateAnimation.play(panelAlpha);
3681 }
3682
3683 if (page != null) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003684 layerViews.put(page, BUILD_AND_SET_LAYER);
Adam Cohen2854d252014-08-27 16:04:07 -07003685
3686 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3687 0, yDrift);
3688 page.setTranslationY(0);
3689 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3690 pageDrift.setInterpolator(decelerateInterpolator);
3691 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3692 mStateAnimation.play(pageDrift);
3693
3694 page.setAlpha(1f);
3695 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3696 itemsAlpha.setDuration(100);
3697 itemsAlpha.setInterpolator(decelerateInterpolator);
3698 mStateAnimation.play(itemsAlpha);
3699 }
3700
3701 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3702 pageIndicators.setAlpha(1f);
3703 ObjectAnimator indicatorsAlpha =
3704 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3705 indicatorsAlpha.setDuration(revealDuration);
3706 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3707 mStateAnimation.play(indicatorsAlpha);
3708
3709 width = revealView.getMeasuredWidth();
3710
3711 if (material) {
3712 if (!isWidgetTray) {
3713 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003714 }
Adam Cohen2854d252014-08-27 16:04:07 -07003715 int allAppsButtonSize = LauncherAppState.getInstance().
3716 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3717 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3718 Animator reveal =
3719 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3720 height / 2, revealRadius, finalRadius);
3721 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3722 reveal.setDuration(revealDuration);
3723 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003724
Adam Cohen2854d252014-08-27 16:04:07 -07003725 reveal.addListener(new AnimatorListenerAdapter() {
3726 public void onAnimationEnd(Animator animation) {
3727 revealView.setVisibility(View.INVISIBLE);
3728 if (!isWidgetTray) {
3729 allAppsButton.setVisibility(View.VISIBLE);
3730 }
3731 }
3732 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003733
Adam Cohen2854d252014-08-27 16:04:07 -07003734 mStateAnimation.play(reveal);
3735 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003736
Adam Cohen2854d252014-08-27 16:04:07 -07003737 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3738 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3739 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003740 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003741
3742 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003743 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003744 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003745 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003746 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3747 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003748 if (onCompleteRunnable != null) {
3749 onCompleteRunnable.run();
3750 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003751
Adam Cohen80e6beb2015-02-13 16:14:33 -08003752 for (View v : layerViews.keySet()) {
3753 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3754 v.setLayerType(View.LAYER_TYPE_NONE, null);
3755 }
Adam Cohen4e243a22014-08-10 18:30:55 -07003756 }
Adam Cohen80e6beb2015-02-13 16:14:33 -08003757
Adam Cohen9bfdb762014-07-21 17:44:06 -07003758 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003759 // Unhide side pages
3760 int count = content.getChildCount();
3761 for (int i = 0; i < count; i++) {
3762 View child = content.getChildAt(i);
3763 child.setVisibility(View.VISIBLE);
3764 }
3765
3766 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003767 if (page != null) {
3768 page.setTranslationX(0);
3769 page.setTranslationY(0);
3770 page.setAlpha(1);
3771 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003772 content.setCurrentPage(content.getNextPage());
3773
Chet Haasebc2f0822012-10-26 17:59:53 -07003774 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003775
3776 // This can hold unnecessary references to views.
3777 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003778 }
3779 });
3780
Adam Cohen2854d252014-08-27 16:04:07 -07003781 final AnimatorSet stateAnimation = mStateAnimation;
3782 final Runnable startAnimRunnable = new Runnable() {
3783 public void run() {
3784 // Check that mStateAnimation hasn't changed while
3785 // we waited for a layout/draw pass
3786 if (mStateAnimation != stateAnimation)
3787 return;
3788 dispatchOnLauncherTransitionStart(fromView, animated, false);
3789 dispatchOnLauncherTransitionStart(toView, animated, false);
3790
Adam Cohen80e6beb2015-02-13 16:14:33 -08003791 for (View v : layerViews.keySet()) {
3792 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3793 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3794 }
3795 }
3796
Kenny Guyd794a3f2014-09-16 15:17:58 +01003797 if (Utilities.isLmpOrAbove()) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003798 for (View v : layerViews.keySet()) {
3799 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen2854d252014-08-27 16:04:07 -07003800 }
3801 }
3802 mStateAnimation.start();
3803 }
3804 };
3805 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003806 } else {
3807 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003808 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003809 dispatchOnLauncherTransitionStart(fromView, animated, true);
3810 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003811 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003812 dispatchOnLauncherTransitionStart(toView, animated, true);
3813 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003814 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003815 }
3816
Michael Jurkae326f182011-11-21 14:05:46 -08003817 @Override
3818 public void onTrimMemory(int level) {
3819 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003820 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3821 // The widget preview db can result in holding onto over
3822 // 3MB of memory for caching which isn't necessary.
3823 SQLiteDatabase.releaseMemory();
3824
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003825 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003826 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003827 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003828 }
Michael Jurkae326f182011-11-21 14:05:46 -08003829 }
3830 }
3831
Adam Cohened307df2013-10-02 09:37:31 -07003832 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003833 showWorkspace(animated, null);
3834 }
3835
Adam Cohened307df2013-10-02 09:37:31 -07003836 protected void showWorkspace() {
3837 showWorkspace(true);
3838 }
3839
Adam Cohened66b2b2012-01-23 17:28:51 -08003840 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003841 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003842 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003843 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003844 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003845
Winson Chungc7d2b602012-05-16 17:02:20 -07003846 // Show the search bar (only animate if we were showing the drop target bar in spring
3847 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003848 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003849 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003850 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003851
Michael Jurkab3e22d92011-10-31 15:58:33 -07003852 // Set focus to the AppsCustomize button
3853 if (mAllAppsButton != null) {
3854 mAllAppsButton.requestFocus();
3855 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003856 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003857
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003858 // Change the state *after* we've called all the transition code
3859 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003860
Winson Chung5fb63472011-02-02 17:03:37 -08003861 // Resume the auto-advance of widgets
3862 mUserPresent = true;
3863 updateRunning();
3864
alanv1d4fde62012-10-17 13:15:47 -07003865 // Send an accessibility event to announce the context change
3866 getWindow().getDecorView()
3867 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003868
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003869 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003870 }
3871
Adam Cohened307df2013-10-02 09:37:31 -07003872 void showOverviewMode(boolean animated) {
3873 mWorkspace.setVisibility(View.VISIBLE);
3874 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3875 mState = State.WORKSPACE;
3876 onWorkspaceShown(animated);
3877 }
3878
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003879 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003880 }
3881
Winson Chung82963d52013-10-09 11:20:57 -07003882 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3883 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003884 if (mState != State.WORKSPACE) return;
3885
Winson Chung82963d52013-10-09 11:20:57 -07003886 if (resetPageToZero) {
3887 mAppsCustomizeTabHost.reset();
3888 }
Winson Chungc58497e2013-09-03 17:48:37 -07003889 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003890 mAppsCustomizeTabHost.post(new Runnable() {
3891 @Override
3892 public void run() {
3893 // We post this in-case the all apps view isn't yet constructed.
3894 mAppsCustomizeTabHost.requestFocus();
3895 }
3896 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003897
Michael Jurkab3e22d92011-10-31 15:58:33 -07003898 // Change the state *after* we've called all the transition code
3899 mState = State.APPS_CUSTOMIZE;
3900
3901 // Pause the auto-advance of widgets until we are out of AllApps
3902 mUserPresent = false;
3903 updateRunning();
3904 closeFolder();
3905
3906 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003907 getWindow().getDecorView()
3908 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003909 }
3910
Adam Cohen7777d962011-08-18 18:58:38 -07003911 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003912 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003913 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003914 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003915 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003916 }
Adam Cohen7777d962011-08-18 18:58:38 -07003917
Adam Cohenad4e15c2013-10-17 16:21:35 -07003918 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003919 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003920 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3921
Winson Chunge7a03942011-08-05 15:05:12 -07003922 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003923 @Override
3924 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003925 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003926 // Before we show workspace, hide all apps again because
3927 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3928 // clean up our state transition functions
3929 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003930 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003931 } else {
3932 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003933 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003934 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003935 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003936 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003937
Michael Jurkad3ef3062010-11-23 16:23:58 -08003938 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003939 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003940 final boolean animated = true;
3941 final boolean springLoaded = true;
3942 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003943 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003944 }
3945 // Otherwise, we are not in spring loaded mode, so don't do anything.
3946 }
3947
Michael Jurkab3e22d92011-10-31 15:58:33 -07003948 void lockAllApps() {
3949 // TODO
3950 }
3951
3952 void unlockAllApps() {
3953 // TODO
3954 }
3955
Sunny Goyal594d76d2014-11-06 10:12:54 -08003956 protected void disableVoiceButtonProxy(boolean disable) {
3957 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003958 }
3959
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003960 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003961 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3962 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003963 }
3964
Adam Cohen24ce0b32014-01-14 16:18:14 -08003965 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003966 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3967 if (searchProvider == null) {
3968 return null;
3969 }
3970
3971 Bundle opts = new Bundle();
3972 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003973 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003974
3975 SharedPreferences sp = getSharedPreferences(
3976 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3977 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003978 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003979 if (!searchProvider.provider.flattenToString().equals(
3980 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003981 || (widgetInfo == null)
3982 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003983 // A valid widget is not already bound.
3984 if (widgetId > -1) {
3985 mAppWidgetHost.deleteAppWidgetId(widgetId);
3986 widgetId = -1;
3987 }
3988
3989 // Try to bind a new widget
3990 widgetId = mAppWidgetHost.allocateAppWidgetId();
3991
3992 if (!AppWidgetManagerCompat.getInstance(this)
3993 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3994 mAppWidgetHost.deleteAppWidgetId(widgetId);
3995 widgetId = -1;
3996 }
3997
3998 sp.edit()
3999 .putInt(QSB_WIDGET_ID, widgetId)
4000 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
4001 .commit();
4002 }
4003
4004 if (widgetId != -1) {
4005 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
4006 mQsb.updateAppWidgetOptions(opts);
4007 mQsb.setPadding(0, 0, 0, 0);
4008 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08004009 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08004010 }
Cristina Stancu476493b2013-08-07 17:20:14 +01004011 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08004012 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01004013 }
4014
Michael Jurkad7c28052012-04-27 15:43:36 -07004015 @Override
4016 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004017 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004018 final List<CharSequence> text = event.getText();
4019 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004020 // Populate event with a fake title based on the current state.
4021 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004022 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004023 } else {
4024 text.add(getString(R.string.all_apps_home_button_label));
4025 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004026 return result;
4027 }
4028
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004029 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004030 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004031 */
4032 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4033 @Override
4034 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004035 closeSystemDialogs();
4036 }
4037 }
4038
4039 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004040 * Receives notifications whenever the appwidgets are reset.
4041 */
4042 private class AppWidgetResetObserver extends ContentObserver {
4043 public AppWidgetResetObserver() {
4044 super(new Handler());
4045 }
4046
4047 @Override
4048 public void onChange(boolean selfChange) {
4049 onAppWidgetReset();
4050 }
4051 }
4052
4053 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004054 * If the activity is currently paused, signal that we need to run the passed Runnable
4055 * in onResume.
4056 *
4057 * This needs to be called from incoming places where resources might have been loaded
4058 * while we are paused. That is becaues the Configuration might be wrong
4059 * when we're not running, and if it comes back to what it was when we
4060 * were paused, we are not restarted.
4061 *
4062 * Implementation of the method from LauncherModel.Callbacks.
4063 *
4064 * @return true if we are currently paused. The caller might be able to
4065 * skip some work in that case since we will come back again.
4066 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004067 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004068 if (mPaused) {
4069 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004070 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004071 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004072 }
4073 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004074 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004075 return true;
4076 } else {
4077 return false;
4078 }
4079 }
4080
Michael Jurkac402cd92013-05-20 15:49:32 +02004081 private boolean waitUntilResume(Runnable run) {
4082 return waitUntilResume(run, false);
4083 }
4084
Michael Jurka1e2f4652013-07-08 18:03:46 -07004085 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004086 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004087 }
4088
Michael Jurka7607c2f2013-04-03 14:33:19 -07004089 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004090 * If the activity is currently paused, signal that we need to re-run the loader
4091 * in onResume.
4092 *
4093 * This needs to be called from incoming places where resources might have been loaded
4094 * while we are paused. That is becaues the Configuration might be wrong
4095 * when we're not running, and if it comes back to what it was when we
4096 * were paused, we are not restarted.
4097 *
4098 * Implementation of the method from LauncherModel.Callbacks.
4099 *
4100 * @return true if we are currently paused. The caller might be able to
4101 * skip some work in that case since we will come back again.
4102 */
4103 public boolean setLoadOnResume() {
4104 if (mPaused) {
4105 Log.i(TAG, "setLoadOnResume");
4106 mOnResumeNeedsLoad = true;
4107 return true;
4108 } else {
4109 return false;
4110 }
4111 }
4112
4113 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004114 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004115 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004116 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004117 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004118 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004119 } else {
4120 return SCREEN_COUNT / 2;
4121 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004122 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004123
Joe Onorato9c1289c2009-08-17 11:03:03 -04004124 /**
4125 * Refreshes the shortcuts shown on the workspace.
4126 *
4127 * Implementation of the method from LauncherModel.Callbacks.
4128 */
4129 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004130 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004131
Michael Jurka7607c2f2013-04-03 14:33:19 -07004132 // If we're starting binding all over again, clear any bind calls we'd postponed in
4133 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4134 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004135 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004136
Winson Chungd64d1762013-08-20 14:37:16 -07004137 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004138 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004139 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004140
Michael Jurka05bf6442011-11-30 20:28:53 -08004141 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004142 if (mHotseat != null) {
4143 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004144 }
4145 }
4146
Adam Cohendcd297f2013-06-18 13:13:40 -07004147 @Override
4148 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004149 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004150
Adam Cohen5084cba2013-09-03 12:01:16 -07004151 // If there are no screens, we need to have an empty screen
4152 if (orderedScreenIds.size() == 0) {
4153 mWorkspace.addExtraEmptyScreen();
4154 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004155
4156 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004157 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004158 if (hasCustomContentToLeft()) {
4159 mWorkspace.createCustomContentContainer();
4160 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004161 }
Winson Chung64359a52013-07-08 17:17:08 -07004162 }
4163
4164 @Override
4165 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004166 // Log to disk
4167 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4168 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4169 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004170 int count = orderedScreenIds.size();
4171 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004172 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004173 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004174 }
4175
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08004176 @Override
4177 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
4178 final long screenId, final int[] cell, final int spanX, final int spanY) {
4179 showWorkspace(true, new Runnable() {
4180
4181 @Override
4182 public void run() {
4183 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
4184 addPendingItem(info, container, screenId, cell, spanX, spanY);
4185 }
4186 });
4187 }
4188
Adam Cohen39a06042013-07-19 14:30:12 -07004189 private boolean shouldShowWeightWatcher() {
4190 String spKey = LauncherAppState.getSharedPreferencesKey();
4191 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004192 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004193
4194 return show;
4195 }
4196
4197 private void toggleShowWeightWatcher() {
4198 String spKey = LauncherAppState.getSharedPreferencesKey();
4199 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4200 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4201
4202 show = !show;
4203
4204 SharedPreferences.Editor editor = sp.edit();
4205 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4206 editor.commit();
4207
4208 if (mWeightWatcher != null) {
4209 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4210 }
4211 }
4212
Winson Chungd64d1762013-08-20 14:37:16 -07004213 public void bindAppsAdded(final ArrayList<Long> newScreens,
4214 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004215 final ArrayList<ItemInfo> addAnimated,
4216 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004217 Runnable r = new Runnable() {
4218 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004219 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004220 }
4221 };
4222 if (waitUntilResume(r)) {
4223 return;
4224 }
4225
Winson Chungd64d1762013-08-20 14:37:16 -07004226 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004227 if (newScreens != null) {
4228 bindAddScreens(newScreens);
4229 }
Winson Chungd64d1762013-08-20 14:37:16 -07004230
4231 // We add the items without animation on non-visible pages, and with
4232 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004233 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004234 bindItems(addNotAnimated, 0,
4235 addNotAnimated.size(), false);
4236 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004237 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004238 bindItems(addAnimated, 0,
4239 addAnimated.size(), true);
4240 }
Winson Chungc58497e2013-09-03 17:48:37 -07004241
Winson Chung87412982013-10-03 18:34:14 -07004242 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004243 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004244
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004245 if (addedApps != null && mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004246 mAppsCustomizeContent.addApps(addedApps);
4247 }
Winson Chungd64d1762013-08-20 14:37:16 -07004248 }
4249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004250 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004251 * Bind the items start-end from the list.
4252 *
4253 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004254 */
Winson Chung64359a52013-07-08 17:17:08 -07004255 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4256 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004257 Runnable r = new Runnable() {
4258 public void run() {
4259 bindItems(shortcuts, start, end, forceAnimateIcons);
4260 }
4261 };
4262 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004263 return;
4264 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004265
Winson Chungf0c6ae02012-03-21 16:10:31 -07004266 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004267 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4268 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004269 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004270 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004271 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004272 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004273 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004274
4275 // Short circuit if we are loading dock items for a configuration which has no dock
4276 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4277 mHotseat == null) {
4278 continue;
4279 }
4280
Joe Onorato9c1289c2009-08-17 11:03:03 -04004281 switch (item.itemType) {
4282 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4283 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004284 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004285 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004286
Winson Chung64359a52013-07-08 17:17:08 -07004287 /*
4288 * TODO: FIX collision case
4289 */
Winson Chungce376632013-07-11 16:12:41 -07004290 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4291 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4292 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004293 View v = cl.getChildAt(item.cellX, item.cellY);
4294 Object tag = v.getTag();
4295 String desc = "Collision while binding workspace item: " + item
4296 + ". Collides with " + tag;
4297 if (LauncherAppState.isDogfoodBuild()) {
4298 throw (new RuntimeException(desc));
4299 } else {
4300 Log.d(TAG, desc);
4301 }
Winson Chungce376632013-07-11 16:12:41 -07004302 }
Winson Chung64359a52013-07-08 17:17:08 -07004303 }
4304
Adam Cohendcd297f2013-06-18 13:13:40 -07004305 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4306 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004307 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004308 // Animate all the applications up now
4309 shortcut.setAlpha(0f);
4310 shortcut.setScaleX(0f);
4311 shortcut.setScaleY(0f);
4312 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004313 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004314 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004315 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004316 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004317 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004318 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004319 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004320 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4321 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004322 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004323 default:
4324 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004326 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004327
Winson Chung997a9232013-07-24 15:33:46 -07004328 if (animateIcons) {
4329 // Animate to the correct page
4330 if (newShortcutsScreenId > -1) {
4331 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004332 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004333 final Runnable startBounceAnimRunnable = new Runnable() {
4334 public void run() {
4335 anim.playTogether(bounceAnims);
4336 anim.start();
4337 }
4338 };
Winson Chung997a9232013-07-24 15:33:46 -07004339 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004340 // We post the animation slightly delayed to prevent slowdowns
4341 // when we are loading right after we return to launcher.
4342 mWorkspace.postDelayed(new Runnable() {
4343 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004344 if (mWorkspace != null) {
4345 mWorkspace.snapToPage(newScreenIndex);
4346 mWorkspace.postDelayed(startBounceAnimRunnable,
4347 NEW_APPS_ANIMATION_DELAY);
4348 }
Winson Chung94d67682013-09-25 16:29:40 -07004349 }
4350 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004351 } else {
4352 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004353 }
4354 }
Winson Chung64359a52013-07-08 17:17:08 -07004355 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004356 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004357 }
4358
Joe Onorato9c1289c2009-08-17 11:03:03 -04004359 /**
4360 * Implementation of the method from LauncherModel.Callbacks.
4361 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004362 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004363 Runnable r = new Runnable() {
4364 public void run() {
4365 bindFolders(folders);
4366 }
4367 };
4368 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004369 return;
4370 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004371 sFolders.clear();
4372 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004373 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004374
4375 /**
4376 * Add the views for a widget to the workspace.
4377 *
4378 * Implementation of the method from LauncherModel.Callbacks.
4379 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004380 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004381 Runnable r = new Runnable() {
4382 public void run() {
4383 bindAppWidget(item);
4384 }
4385 };
4386 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004387 return;
4388 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004389
Daniel Sandler843e8602010-06-07 14:59:01 -04004390 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4391 if (DEBUG_WIDGETS) {
4392 Log.d(TAG, "bindAppWidget: " + item);
4393 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004394 final Workspace workspace = mWorkspace;
4395
Adam Cohen59400422014-03-05 18:07:04 -08004396 LauncherAppWidgetProviderInfo appWidgetInfo =
4397 LauncherModel.getProviderInfo(this, item.providerName);
4398
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004399 if (!mIsSafeModeEnabled
4400 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4401 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004402 if (appWidgetInfo == null) {
4403 if (DEBUG_WIDGETS) {
4404 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4405 + " belongs to component " + item.providerName
4406 + ", as the povider is null");
4407 }
4408 LauncherModel.deleteItemFromDatabase(this, item);
4409 return;
4410 }
4411 // Note: This assumes that the id remap broadcast is received before this step.
4412 // If that is not the case, the id remap will be ignored and user may see the
4413 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004414 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004415 pendingInfo.spanX = item.spanX;
4416 pendingInfo.spanY = item.spanY;
4417 pendingInfo.minSpanX = item.minSpanX;
4418 pendingInfo.minSpanY = item.minSpanY;
4419 Bundle options =
4420 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4421
Sunny Goyalff572272014-07-23 13:58:07 -07004422 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004423 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4424 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004425
4426 // TODO consider showing a permission dialog when the widget is clicked.
4427 if (!success) {
4428 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4429 if (DEBUG_WIDGETS) {
4430 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4431 + " belongs to component " + item.providerName
4432 + ", as the launcher is unable to bing a new widget id");
4433 }
4434 LauncherModel.deleteItemFromDatabase(this, item);
4435 return;
4436 }
4437
4438 item.appWidgetId = newWidgetId;
4439
4440 // If the widget has a configure activity, it is still needs to set it up, otherwise
4441 // the widget is ready to go.
4442 item.restoreStatus = (appWidgetInfo.configure == null)
4443 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4444 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4445
4446 LauncherModel.updateItemInDatabase(this, item);
4447 }
4448
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004449 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004450 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004451 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004452 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4453 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004454 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004455
Sunny Goyal651077b2014-06-30 14:15:31 -07004456 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4457 } else {
4458 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004459 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4460 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004461 view.updateIcon(mIconCache);
4462 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004463 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004464 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004465 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004466
Joe Onorato9c1289c2009-08-17 11:03:03 -04004467 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004468 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004469
Adam Cohendcd297f2013-06-18 13:13:40 -07004470 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004471 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004472 if (!item.isCustomWidget()) {
4473 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4474 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004475
Joe Onorato9c1289c2009-08-17 11:03:03 -04004476 workspace.requestLayout();
4477
Daniel Sandler843e8602010-06-07 14:59:01 -04004478 if (DEBUG_WIDGETS) {
4479 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4480 + (SystemClock.uptimeMillis()-start) + "ms");
4481 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004482 }
4483
Sunny Goyalff572272014-07-23 13:58:07 -07004484 /**
4485 * Restores a pending widget.
4486 *
4487 * @param appWidgetId The app widget id
4488 * @param cellInfo The position on screen where to create the widget.
4489 */
4490 private void completeRestoreAppWidget(final int appWidgetId) {
4491 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4492 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4493 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4494 return;
4495 }
4496
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004497 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004498 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4499
4500 mWorkspace.reinflateWidgetsIfNecessary();
4501 LauncherModel.updateItemInDatabase(this, info);
4502 }
4503
Adam Cohen1462de32012-07-24 22:34:36 -07004504 public void onPageBoundSynchronously(int page) {
4505 mSynchronouslyBoundPages.add(page);
4506 }
4507
Joe Onorato9c1289c2009-08-17 11:03:03 -04004508 /**
4509 * Callback saying that there aren't any more items to bind.
4510 *
4511 * Implementation of the method from LauncherModel.Callbacks.
4512 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004513 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004514 Runnable r = new Runnable() {
4515 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004516 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004517 }
4518 };
4519 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004520 return;
4521 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004522 if (mSavedState != null) {
4523 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004524 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004525 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004526 mSavedState = null;
4527 }
4528
Adam Cohen1462de32012-07-24 22:34:36 -07004529 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004530
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004531 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004532 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004533
4534 // If we received the result of any pending adds while the loader was running (e.g. the
4535 // widget configuration forced an orientation change), process them now.
4536 if (sPendingAddItem != null) {
4537 final long screenId = completeAdd(sPendingAddItem);
4538
4539 // TODO: this moves the user to the page where the pending item was added. Ideally,
4540 // the screen would be guaranteed to exist after bind, and the page would be set through
4541 // the workspace restore process.
4542 mWorkspace.post(new Runnable() {
4543 @Override
4544 public void run() {
4545 mWorkspace.snapToScreenId(screenId);
4546 }
4547 });
4548 sPendingAddItem = null;
4549 }
4550
Sunny Goyale755d462014-07-22 13:48:29 -07004551 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004552
4553 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004554 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004555 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004556 }
4557
Adam Cohen3ed316a2014-07-23 18:21:20 -07004558 private void sendLoadingCompleteBroadcastIfNecessary() {
4559 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4560 String permission =
4561 getResources().getString(R.string.receive_first_load_broadcast_permission);
4562 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4563 sendBroadcast(intent, permission);
4564 SharedPreferences.Editor editor = mSharedPrefs.edit();
4565 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4566 editor.apply();
4567 }
4568 }
4569
Winson Chunga0b7e862013-09-05 16:03:15 -07004570 public boolean isAllAppsButtonRank(int rank) {
4571 if (mHotseat != null) {
4572 return mHotseat.isAllAppsButtonRank(rank);
4573 }
4574 return false;
4575 }
4576
Winson Chunga2413752012-04-03 14:22:34 -07004577 private boolean canRunNewAppsAnimation() {
4578 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4579 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4580 }
4581
Winson Chungc9168342013-06-26 14:54:55 -07004582 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4583 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4584 PropertyValuesHolder.ofFloat("alpha", 1f),
4585 PropertyValuesHolder.ofFloat("scaleX", 1f),
4586 PropertyValuesHolder.ofFloat("scaleY", 1f));
4587 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4588 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4589 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4590 return bounceAnim;
4591 }
4592
Adam Cohen27772732013-11-11 14:00:56 +00004593 public boolean useVerticalBarLayout() {
4594 return LauncherAppState.getInstance().getDynamicGrid().
4595 getDeviceProfile().isVerticalBarLayout();
4596 }
4597
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004598 protected Rect getSearchBarBounds() {
4599 return LauncherAppState.getInstance().getDynamicGrid().
4600 getDeviceProfile().getSearchBarBounds();
4601 }
4602
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004603 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004604 if (mSearchDropTargetBar == null) {
4605 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004606 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004607 if (mQsb != null) {
4608 mSearchDropTargetBar.removeView(mQsb);
4609 mQsb = null;
4610 }
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08004611 getOrCreateQsbBar();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004612 }
4613
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004614 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004615 * Add the icons for all apps.
4616 *
4617 * Implementation of the method from LauncherModel.Callbacks.
4618 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004619 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004620 if (mAppsCustomizeContent != null) {
4621 mAppsCustomizeContent.setApps(apps);
4622 mAppsCustomizeContent.onPackagesUpdated(
4623 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -07004624 }
Adam Cohen9211d422014-10-07 18:14:53 -07004625 if (mLauncherCallbacks != null) {
4626 mLauncherCallbacks.bindAllApplications(apps);
4627 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004628 }
4629
4630 /**
4631 * A package was updated.
4632 *
4633 * Implementation of the method from LauncherModel.Callbacks.
4634 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004635 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004636 Runnable r = new Runnable() {
4637 public void run() {
4638 bindAppsUpdated(apps);
4639 }
4640 };
4641 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004642 return;
4643 }
4644
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004645 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004646 mAppsCustomizeContent.updateApps(apps);
4647 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004648 }
4649
Sunny Goyal4390ace2014-10-13 11:33:11 -07004650 @Override
4651 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4652 Runnable r = new Runnable() {
4653 public void run() {
4654 bindWidgetsRestored(widgets);
4655 }
4656 };
4657 if (waitUntilResume(r)) {
4658 return;
4659 }
4660 mWorkspace.widgetsRestored(widgets);
4661 }
4662
Joe Onorato9c1289c2009-08-17 11:03:03 -04004663 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004664 * Some shortcuts were updated in the background.
4665 *
4666 * Implementation of the method from LauncherModel.Callbacks.
4667 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004668 @Override
4669 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4670 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004671 Runnable r = new Runnable() {
4672 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004673 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004674 }
4675 };
4676 if (waitUntilResume(r)) {
4677 return;
4678 }
4679
Sunny Goyal4390ace2014-10-13 11:33:11 -07004680 if (!updated.isEmpty()) {
4681 mWorkspace.updateShortcuts(updated);
4682 }
4683
4684 if (!removed.isEmpty()) {
4685 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4686 for (ShortcutInfo si : removed) {
4687 removedComponents.add(si.getTargetComponent());
4688 }
4689 mWorkspace.removeItemsByComponentName(removedComponents, user);
4690 // Notify the drag controller
4691 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004692 }
4693 }
4694
4695 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004696 * Update the state of a package, typically related to install state.
4697 *
4698 * Implementation of the method from LauncherModel.Callbacks.
4699 */
Sunny Goyale755d462014-07-22 13:48:29 -07004700 @Override
4701 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004702 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004703 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004704 }
4705 }
4706
4707 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004708 * Update the label and icon of all the icons in a package
4709 *
4710 * Implementation of the method from LauncherModel.Callbacks.
4711 */
4712 @Override
4713 public void updatePackageBadge(String packageName) {
4714 if (mWorkspace != null) {
4715 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4716 }
4717 }
4718
4719 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004720 * A package was uninstalled. We take both the super set of packageNames
4721 * in addition to specific applications to remove, the reason being that
4722 * this can be called when a package is updated as well. In that scenario,
4723 * we only remove specific components from the workspace, where as
4724 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004725 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004726 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004727 * Implementation of the method from LauncherModel.Callbacks.
4728 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004729 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004730 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004731 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004732 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004733 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004734 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004735 }
Winson Chungd64d1762013-08-20 14:37:16 -07004736 };
4737 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004738 return;
4739 }
4740
Sunny Goyal1a745e82014-10-02 15:58:31 -07004741 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004742 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4743 for (AppInfo info : appInfos) {
4744 removedComponents.add(info.componentName);
4745 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004746 if (!packageNames.isEmpty()) {
4747 mWorkspace.removeItemsByPackageName(packageNames, user);
4748 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004749 if (!removedComponents.isEmpty()) {
4750 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004751 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004752 // Notify the drag controller
4753 mDragController.onAppsRemoved(packageNames, removedComponents);
4754
Sunny Goyal1a745e82014-10-02 15:58:31 -07004755 } else {
4756 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004757 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004758
Winson Chungdf95eb12013-10-16 14:57:07 -07004759 // Update AllApps
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004760 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004761 mAppsCustomizeContent.removeApps(appInfos);
4762 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004763 }
Joe Onoratobe386092009-11-17 17:32:16 -08004764
4765 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004766 * A number of packages were updated.
4767 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004768 private ArrayList<Object> mWidgetsAndShortcuts;
4769 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004770 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004771 bindPackagesUpdated(mWidgetsAndShortcuts);
4772 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004773 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004774 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004775 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4776 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4777 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004778 return;
4779 }
4780
Winson Chung64359a52013-07-08 17:17:08 -07004781 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004782 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004783 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004784 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004785 }
4786
Winson Chung400438b2011-01-16 17:53:48 -08004787 private int mapConfigurationOriActivityInfoOri(int configOri) {
4788 final Display d = getWindowManager().getDefaultDisplay();
4789 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4790 switch (d.getRotation()) {
4791 case Surface.ROTATION_0:
4792 case Surface.ROTATION_180:
4793 // We are currently in the same basic orientation as the natural orientation
4794 naturalOri = configOri;
4795 break;
4796 case Surface.ROTATION_90:
4797 case Surface.ROTATION_270:
4798 // We are currently in the other basic orientation to the natural orientation
4799 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4800 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4801 break;
4802 }
4803
4804 int[] oriMap = {
4805 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4806 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4807 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4808 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4809 };
4810 // Since the map starts at portrait, we need to offset if this device's natural orientation
4811 // is landscape.
4812 int indexOffset = 0;
4813 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4814 indexOffset = 1;
4815 }
4816 return oriMap[(d.getRotation() + indexOffset) % 4];
4817 }
Adam Cohen446e9402011-09-15 18:21:21 -07004818
Winson Chung4b919f82012-05-01 10:44:08 -07004819 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004820 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004821 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4822 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4823 .getConfiguration().orientation));
4824 } else {
4825 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4826 }
Winson Chung4b919f82012-05-01 10:44:08 -07004827 }
4828 }
4829 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004830 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004831 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004832 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004833 } else {
4834 mHandler.postDelayed(new Runnable() {
4835 public void run() {
4836 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4837 }
4838 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004839 }
Winson Chung4b919f82012-05-01 10:44:08 -07004840 }
Winson Chung400438b2011-01-16 17:53:48 -08004841 }
4842
Winson Chunge43a1e72014-01-15 10:33:02 -08004843 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004844 if (mLauncherCallbacks != null) {
4845 return mLauncherCallbacks.isLauncherPreinstalled();
4846 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004847 PackageManager pm = getPackageManager();
4848 try {
4849 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4850 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4851 return true;
4852 } else {
4853 return false;
4854 }
4855 } catch (NameNotFoundException e) {
4856 e.printStackTrace();
4857 return false;
4858 }
4859 }
4860
Adam Cohenea90f832014-05-21 15:03:34 -07004861 /**
4862 * This method indicates whether or not we should suggest default wallpaper dimensions
4863 * when our wallpaper cropper was not yet used to set a wallpaper.
4864 */
4865 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004866 if (mLauncherCallbacks != null) {
4867 return mLauncherCallbacks.overrideWallpaperDimensions();
4868 }
Adam Cohenea90f832014-05-21 15:03:34 -07004869 return true;
4870 }
4871
Adam Cohen432609a2014-03-13 17:03:22 -07004872 /**
4873 * To be overridden by subclasses to indicate that there is an activity to launch
4874 * before showing the standard launcher experience.
4875 */
4876 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004877 if (mLauncherCallbacks != null) {
4878 return mLauncherCallbacks.hasFirstRunActivity();
4879 }
Adam Cohen432609a2014-03-13 17:03:22 -07004880 return false;
4881 }
4882
4883 /**
4884 * To be overridden by subclasses to launch any first run activity
4885 */
4886 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004887 if (mLauncherCallbacks != null) {
4888 return mLauncherCallbacks.getFirstRunActivity();
4889 }
Adam Cohen432609a2014-03-13 17:03:22 -07004890 return null;
4891 }
4892
Winson Chunga6945242014-01-08 14:04:34 -08004893 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004894 return !ActivityManager.isRunningInTestHarness() &&
4895 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004896 }
4897
Adam Cohen4a9423d2014-03-28 14:22:31 -07004898 protected boolean hasRunFirstRunActivity() {
4899 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4900 }
4901
Adam Cohen432609a2014-03-13 17:03:22 -07004902 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004903 if (shouldRunFirstRunActivity() &&
4904 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004905 Intent firstRunIntent = getFirstRunActivity();
4906 if (firstRunIntent != null) {
4907 startActivity(firstRunIntent);
4908 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004909 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004910 }
4911 }
Adam Cohen432609a2014-03-13 17:03:22 -07004912 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004913 }
4914
4915 private void markFirstRunActivityShown() {
4916 SharedPreferences.Editor editor = mSharedPrefs.edit();
4917 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4918 editor.apply();
4919 }
4920
Adam Cohen432609a2014-03-13 17:03:22 -07004921 /**
4922 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4923 * screen that must be displayed and dismissed.
4924 */
4925 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004926 if (mLauncherCallbacks != null) {
4927 return mLauncherCallbacks.hasDismissableIntroScreen();
4928 }
Adam Cohen432609a2014-03-13 17:03:22 -07004929 return false;
4930 }
4931
4932 /**
4933 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4934 */
4935 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004936 if (mLauncherCallbacks != null) {
4937 return mLauncherCallbacks.getIntroScreen();
4938 }
Adam Cohen432609a2014-03-13 17:03:22 -07004939 return null;
4940 }
4941
4942 /**
4943 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4944 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4945 */
4946 private boolean shouldShowIntroScreen() {
4947 return hasDismissableIntroScreen() &&
4948 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4949 }
4950
4951 protected void showIntroScreen() {
4952 View introScreen = getIntroScreen();
4953 changeWallpaperVisiblity(false);
4954 if (introScreen != null) {
4955 mDragLayer.showOverlayView(introScreen);
4956 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004957 if (mLauncherOverlayContainer != null) {
4958 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4959 }
Adam Cohen432609a2014-03-13 17:03:22 -07004960 }
4961
4962 public void dismissIntroScreen() {
4963 markIntroScreenDismissed();
4964 if (showFirstRunActivity()) {
4965 // We delay hiding the intro view until the first run activity is showing. This
4966 // avoids a blip.
4967 mWorkspace.postDelayed(new Runnable() {
4968 @Override
4969 public void run() {
4970 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004971 if (mLauncherOverlayContainer != null) {
4972 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4973 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004974 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004975 }
4976 }, ACTIVITY_START_DELAY);
4977 } else {
4978 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004979 if (mLauncherOverlayContainer != null) {
4980 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4981 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004982 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004983 }
4984 changeWallpaperVisiblity(true);
4985 }
4986
4987 private void markIntroScreenDismissed() {
4988 SharedPreferences.Editor editor = mSharedPrefs.edit();
4989 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4990 editor.apply();
4991 }
4992
Sunny Goyal88d60f12014-09-08 03:47:29 -07004993 private void showFirstRunClings() {
4994 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4995 // on the device, then we always show the first run cling experience (or if there is no
4996 // launcher2). Otherwise, we prompt the user upon started for migration
4997 LauncherClings launcherClings = new LauncherClings(this);
4998 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4999 if (mModel.canMigrateFromOldLauncherDb(this)) {
5000 launcherClings.showMigrationCling();
5001 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005002 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005003 }
5004 }
5005 }
5006
Winson Chunga6945242014-01-08 14:04:34 -08005007 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005008 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5009 if (mHotseat != null) mHotseat.setAlpha(1f);
5010 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5011 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005012 }
5013
5014 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005015 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5016 if (mHotseat != null) mHotseat.setAlpha(0f);
5017 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5018 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005019 }
5020
Mathew Inwood72fbec12013-11-19 15:45:07 +00005021 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005022 // Called from search suggestion, not supported in other profiles.
5023 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5024 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5025 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5026 myUser);
5027 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005028 return null;
5029 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08005030 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005031 }
5032
5033 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5034 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005035 // Called from search suggestion, not supported in other profiles.
5036 return createShortcutDragInfo(shortcutIntent, caption, icon,
5037 UserHandleCompat.myUserHandle());
5038 }
5039
5040 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5041 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005042 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005043 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005044 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005045 }
5046
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005047 protected void moveWorkspaceToDefaultScreen() {
5048 mWorkspace.moveToDefaultScreen(false);
5049 }
5050
Mathew Inwood72fbec12013-11-19 15:45:07 +00005051 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5052 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005053 mWorkspace.onExternalDragStartedWithItem(dragView);
5054 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005055 }
5056
Anjali Koppalf5d29132014-02-28 13:33:27 -08005057 @Override
5058 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005059 if (mLauncherCallbacks != null) {
5060 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5061 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005062 }
5063
Winson Chung80baf5a2010-08-09 16:03:15 -07005064 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005065 * Prints out out state for debugging.
5066 */
5067 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005068 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005069 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005070 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5071 Log.d(TAG, "mRestoring=" + mRestoring);
5072 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5073 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005074 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005075 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005076
Winson Chung785d2eb2011-04-14 16:08:02 -07005077 if (mAppsCustomizeContent != null) {
5078 mAppsCustomizeContent.dumpState();
5079 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005080 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005081 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005082
5083 @Override
5084 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5085 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005086 synchronized (sDumpLogs) {
5087 writer.println(" ");
5088 writer.println("Debug logs: ");
5089 for (int i = 0; i < sDumpLogs.size(); i++) {
5090 writer.println(" " + sDumpLogs.get(i));
5091 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005092 }
Adam Cohen9211d422014-10-07 18:14:53 -07005093 if (mLauncherCallbacks != null) {
5094 mLauncherCallbacks.dump(prefix, fd, writer, args);
5095 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005096 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005097
5098 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005099 if (DEBUG_DUMP_LOG) {
5100 synchronized (sDumpLogs) {
5101 Log.d(TAG, "");
5102 Log.d(TAG, "*********************");
5103 Log.d(TAG, "Launcher debug logs: ");
5104 for (int i = 0; i < sDumpLogs.size(); i++) {
5105 Log.d(TAG, " " + sDumpLogs.get(i));
5106 }
5107 Log.d(TAG, "*********************");
5108 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005109 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005110 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005111 }
5112
5113 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005114 addDumpLog(tag, log, null, debugLog);
5115 }
5116
5117 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005118 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005119 if (e != null) {
5120 Log.d(tag, log, e);
5121 } else {
5122 Log.d(tag, log);
5123 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005124 }
Winson Chungede41292013-09-19 16:27:36 -07005125 if (DEBUG_DUMP_LOG) {
5126 sDateStamp.setTime(System.currentTimeMillis());
5127 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005128 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5129 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005130 }
Winson Chungede41292013-09-19 16:27:36 -07005131 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005132 }
5133
Adam Cohen59400422014-03-05 18:07:04 -08005134 public static CustomAppWidget getCustomAppWidget(String name) {
5135 return sCustomAppWidgets.get(name);
5136 }
5137
5138 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
5139 return sCustomAppWidgets;
5140 }
5141
Winson Chungede41292013-09-19 16:27:36 -07005142 public void dumpLogsToLocalData() {
5143 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005144 new AsyncTask<Void, Void, Void>() {
5145 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005146 boolean success = false;
5147 sDateStamp.setTime(sRunStart);
5148 String FILENAME = sDateStamp.getMonth() + "-"
5149 + sDateStamp.getDay() + "_"
5150 + sDateStamp.getHours() + "-"
5151 + sDateStamp.getMinutes() + "_"
5152 + sDateStamp.getSeconds() + ".txt";
5153
5154 FileOutputStream fos = null;
5155 File outFile = null;
5156 try {
5157 outFile = new File(getFilesDir(), FILENAME);
5158 outFile.createNewFile();
5159 fos = new FileOutputStream(outFile);
5160 } catch (Exception e) {
5161 e.printStackTrace();
5162 }
5163 if (fos != null) {
5164 PrintWriter writer = new PrintWriter(fos);
5165
5166 writer.println(" ");
5167 writer.println("Debug logs: ");
5168 synchronized (sDumpLogs) {
5169 for (int i = 0; i < sDumpLogs.size(); i++) {
5170 writer.println(" " + sDumpLogs.get(i));
5171 }
5172 }
5173 writer.close();
5174 }
5175 try {
5176 if (fos != null) {
5177 fos.close();
5178 success = true;
5179 }
5180 } catch (IOException e) {
5181 e.printStackTrace();
5182 }
Michael Jurka43467462013-11-14 12:03:58 +01005183 return null;
Winson Chungede41292013-09-19 16:27:36 -07005184 }
Michael Jurka43467462013-11-14 12:03:58 +01005185 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005186 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005188}
Michael Jurka2763be32011-02-24 11:19:57 -08005189
Michael Jurkaabded662011-03-04 12:06:57 -08005190interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005191 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005192 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005193 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005194 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005195 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005196}
Dan Sandlerd5024042014-01-09 15:01:33 -05005197
5198interface DebugIntents {
5199 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5200 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005201}