blob: e3a2d7c5f4e3a758249c69093a93122ed1182fc7 [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 Sandler6053b8002013-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 Chung07e045c62013-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 Chung07e045c62013-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
Winson Chungc93e5ae2012-07-23 20:48:26 -07002451 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002452 if (mAppsCustomizeContent.getContentType() ==
2453 AppsCustomizePagedView.ContentType.Applications) {
2454 showWorkspace(true);
2455 } else {
2456 showOverviewMode(true);
2457 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002458 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002459 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002460 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002461 Folder openFolder = mWorkspace.getOpenFolder();
2462 if (openFolder.isEditingName()) {
2463 openFolder.dismissEditingName();
2464 } else {
2465 closeFolder();
2466 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002467 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002468 mWorkspace.exitWidgetResizeMode();
2469
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002470 // Back button is a no-op here, but give at least some feedback for the button press
2471 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002472 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002473 }
2474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002475 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002476 * Re-listen when widgets are reset.
2477 */
2478 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002479 if (mAppWidgetHost != null) {
2480 mAppWidgetHost.startListening();
2481 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002482 }
2483
2484 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485 * Launches the intent referred by the clicked shortcut.
2486 *
2487 * @param v The view representing the clicked shortcut.
2488 */
2489 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002490 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2491 // view has detached (it's possible for this to happen if the view is removed mid touch).
2492 if (v.getWindowToken() == null) {
2493 return;
2494 }
2495
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002496 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002497 return;
2498 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002499
Adam Cohen1697b792013-09-17 19:08:21 -07002500 if (v instanceof Workspace) {
2501 if (mWorkspace.isInOverviewMode()) {
2502 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002503 }
2504 return;
2505 }
2506
2507 if (v instanceof CellLayout) {
2508 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002509 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002510 }
2511 }
2512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002513 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002514 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002515 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002517 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002518 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002519 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002520 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002521 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002522 } else if (tag instanceof AppInfo) {
2523 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002524 } else if (tag instanceof LauncherAppWidgetInfo) {
2525 if (v instanceof PendingAppWidgetHostView) {
2526 onClickPendingWidget((PendingAppWidgetHostView) v);
2527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 }
2529 }
2530
Sunny Goyal508da152014-08-14 10:53:27 -07002531 public void onClickPagedViewIcon(View v) {
2532 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002533 if (mLauncherCallbacks != null) {
2534 mLauncherCallbacks.onClickPagedViewIcon(v);
2535 }
Sunny Goyal508da152014-08-14 10:53:27 -07002536 }
2537
Michael Jurka0e260592010-06-30 17:07:39 -07002538 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002539 return false;
2540 }
2541
Michael Jurkaaf442092010-06-10 17:01:57 -07002542 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002543 * Event handler for the app widget view which has not fully restored.
2544 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002545 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002546 if (mIsSafeModeEnabled) {
2547 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2548 return;
2549 }
2550
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002551 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002552 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002553 int widgetId = info.appWidgetId;
2554 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2555 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002556 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2557 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002558 mPendingAddInfo.copyFrom(info);
2559 mPendingAddWidgetId = widgetId;
2560
Sunny Goyalffe83f12014-08-14 17:39:34 -07002561 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2562 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002563 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002564 } else if (info.installProgress < 0) {
2565 // The install has not been queued
2566 final String packageName = info.providerName.getPackageName();
2567 showBrokenAppInstallDialog(packageName,
2568 new DialogInterface.OnClickListener() {
2569 public void onClick(DialogInterface dialog, int id) {
2570 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2571 }
2572 });
2573 } else {
2574 // Download has started.
2575 final String packageName = info.providerName.getPackageName();
2576 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002577 }
2578 }
2579
2580 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002581 * Event handler for the "grid" button that appears on the home screen, which
2582 * enters all apps mode.
2583 *
2584 * @param v The view that was clicked.
2585 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002586 protected void onClickAllAppsButton(View v) {
2587 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2588 if (isAllAppsVisible()) {
2589 showWorkspace(true);
2590 } else {
2591 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2592 }
Adam Cohen9211d422014-10-07 18:14:53 -07002593 if (mLauncherCallbacks != null) {
2594 mLauncherCallbacks.onClickAllAppsButton(v);
2595 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002596 }
2597
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002598 private void showBrokenAppInstallDialog(final String packageName,
2599 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002600 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002601 .setTitle(R.string.abandoned_promises_title)
2602 .setMessage(R.string.abandoned_promise_explanation)
2603 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2604 .setNeutralButton(R.string.abandoned_clean_this,
2605 new DialogInterface.OnClickListener() {
2606 public void onClick(DialogInterface dialog, int id) {
2607 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2608 mWorkspace.removeAbandonedPromise(packageName, user);
2609 }
2610 })
2611 .create().show();
2612 return;
2613 }
2614
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002615 /**
2616 * Event handler for an app shortcut click.
2617 *
2618 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2619 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002620 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002621 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2622 Object tag = v.getTag();
2623 if (!(tag instanceof ShortcutInfo)) {
2624 throw new IllegalArgumentException("Input must be a Shortcut");
2625 }
2626
2627 // Open shortcut
2628 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002629
2630 if (shortcut.isDisabled != 0) {
2631 int error = R.string.activity_not_available;
2632 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2633 error = R.string.safemode_shortcut_error;
2634 }
2635 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2636 return;
2637 }
2638
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002639 final Intent intent = shortcut.intent;
2640
2641 // Check for special shortcuts
2642 if (intent.getComponent() != null) {
2643 final String shortcutClass = intent.getComponent().getClassName();
2644
2645 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2646 MemoryDumpActivity.startDump(this);
2647 return;
2648 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2649 toggleShowWeightWatcher();
2650 return;
2651 }
2652 }
2653
Chris Wren40c5ed32014-06-24 18:24:23 -04002654 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002655 if ((v instanceof BubbleTextView)
2656 && shortcut.isPromise()
2657 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002658 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002659 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002660 new DialogInterface.OnClickListener() {
2661 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002662 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002663 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002664 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002665 return;
2666 }
2667
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002668 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002669 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002670
2671 if (mLauncherCallbacks != null) {
2672 mLauncherCallbacks.onClickAppShortcut(v);
2673 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 }
2675
Sunny Goyal508da152014-08-14 10:53:27 -07002676 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002678 final ShortcutInfo shortcut;
2679 final Intent intent;
2680 if (tag instanceof ShortcutInfo) {
2681 shortcut = (ShortcutInfo) tag;
2682 intent = shortcut.intent;
2683 int[] pos = new int[2];
2684 v.getLocationOnScreen(pos);
2685 intent.setSourceBounds(new Rect(pos[0], pos[1],
2686 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002687
Sunny Goyal508da152014-08-14 10:53:27 -07002688 } else if (tag instanceof AppInfo) {
2689 shortcut = null;
2690 intent = ((AppInfo) tag).intent;
2691 } else {
2692 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2693 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002694
2695 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002696 mStats.recordLaunch(intent, shortcut);
2697
2698 if (success && v instanceof BubbleTextView) {
2699 mWaitingForResume = (BubbleTextView) v;
2700 mWaitingForResume.setStayPressed(true);
2701 }
2702 }
2703
2704 /**
2705 * Event handler for a folder icon click.
2706 *
2707 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2708 */
2709 protected void onClickFolderIcon(View v) {
2710 if (LOGD) Log.d(TAG, "onClickFolder");
2711 if (!(v instanceof FolderIcon)){
2712 throw new IllegalArgumentException("Input must be a FolderIcon");
2713 }
2714
2715 FolderIcon folderIcon = (FolderIcon) v;
2716 final FolderInfo info = folderIcon.getFolderInfo();
2717 Folder openFolder = mWorkspace.getFolderForTag(info);
2718
2719 // If the folder info reports that the associated folder is open, then verify that
2720 // it is actually opened. There have been a few instances where this gets out of sync.
2721 if (info.opened && openFolder == null) {
2722 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2723 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2724 info.opened = false;
2725 }
2726
2727 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2728 // Close any open folder
2729 closeFolder();
2730 // Open the requested folder
2731 openFolder(folderIcon);
2732 } else {
2733 // Find the open folder...
2734 int folderScreen;
2735 if (openFolder != null) {
2736 folderScreen = mWorkspace.getPageForView(openFolder);
2737 // .. and close it
2738 closeFolder(openFolder);
2739 if (folderScreen != mWorkspace.getCurrentPage()) {
2740 // Close any folder open on the current screen
2741 closeFolder();
2742 // Pull the folder onto this screen
2743 openFolder(folderIcon);
2744 }
2745 }
2746 }
Adam Cohen9211d422014-10-07 18:14:53 -07002747
2748 if (mLauncherCallbacks != null) {
2749 mLauncherCallbacks.onClickFolderIcon(v);
2750 }
Michael Jurka5130e402011-10-13 04:55:35 -07002751 }
2752
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002753 /**
2754 * Event handler for the (Add) Widgets button that appears after a long press
2755 * on the home screen.
2756 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002757 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002758 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002759 if (mIsSafeModeEnabled) {
2760 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2761 } else {
2762 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2763 if (mLauncherCallbacks != null) {
2764 mLauncherCallbacks.onClickAddWidgetButton(view);
2765 }
Adam Cohen9211d422014-10-07 18:14:53 -07002766 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002767 }
2768
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002769 /**
2770 * Event handler for the wallpaper picker button that appears after a long press
2771 * on the home screen.
2772 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002773 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002774 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2775 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2776 pickWallpaper.setComponent(getWallpaperPickerComponent());
2777 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002778
2779 if (mLauncherCallbacks != null) {
2780 mLauncherCallbacks.onClickWallpaperPicker(v);
2781 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002782 }
2783
2784 /**
2785 * Event handler for a click on the settings button that appears after a long press
2786 * on the home screen.
2787 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002788 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002789 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002790 if (mLauncherCallbacks != null) {
2791 mLauncherCallbacks.onClickSettingsButton(v);
2792 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002793 }
2794
Michael Jurka5130e402011-10-13 04:55:35 -07002795 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002796 // Provide the same haptic feedback that the system offers for virtual keys.
2797 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002798 }
2799
Adam Cohen61f560d2013-09-30 15:58:20 -07002800 public void performHapticFeedbackOnTouchDown(View v) {
2801 // Provide the same haptic feedback that the system offers for virtual keys.
2802 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2803 }
2804
2805 public View.OnTouchListener getHapticFeedbackTouchListener() {
2806 if (mHapticFeedbackTouchListener == null) {
2807 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2808 @Override
2809 public boolean onTouch(View v, MotionEvent event) {
2810 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2811 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2812 }
2813 return false;
2814 }
2815 };
2816 }
2817 return mHapticFeedbackTouchListener;
2818 }
2819
Adam Cohen9211d422014-10-07 18:14:53 -07002820 public void onDragStarted(View view) {
2821 if (isOnCustomContent()) {
2822 // Custom content screen doesn't participate in drag and drop. If on custom
2823 // content screen, move to default.
2824 moveWorkspaceToDefaultScreen();
2825 }
2826
2827 if (mLauncherCallbacks != null) {
2828 mLauncherCallbacks.onDragStarted(view);
2829 }
2830 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002831
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002832 /**
2833 * Called when the user stops interacting with the launcher.
2834 * This implies that the user is now on the homescreen and is not doing housekeeping.
2835 */
Adam Cohen9211d422014-10-07 18:14:53 -07002836 protected void onInteractionEnd() {
2837 if (mLauncherCallbacks != null) {
2838 mLauncherCallbacks.onInteractionEnd();
2839 }
2840 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002841
2842 /**
2843 * Called when the user starts interacting with the launcher.
2844 * The possible interactions are:
2845 * - open all apps
2846 * - reorder an app shortcut, or a widget
2847 * - open the overview mode.
2848 * This is a good time to stop doing things that only make sense
2849 * when the user is on the homescreen and not doing housekeeping.
2850 */
Adam Cohen9211d422014-10-07 18:14:53 -07002851 protected void onInteractionBegin() {
2852 if (mLauncherCallbacks != null) {
2853 mLauncherCallbacks.onInteractionBegin();
2854 }
2855 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002856
Kenny Guyf07af7b2014-07-31 11:39:16 +01002857 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002858 try {
2859 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002860 launcherApps.showAppDetailsForProfile(componentName, user);
2861 } catch (SecurityException e) {
2862 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2863 Log.e(TAG, "Launcher does not have permission to launch settings");
2864 } catch (ActivityNotFoundException e) {
2865 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2866 Log.e(TAG, "Unable to launch settings");
2867 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002868 }
2869
Michael Jurka1e2f4652013-07-08 18:03:46 -07002870 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002871 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2872 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002873 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002874 // System applications cannot be installed. For now, show a toast explaining that.
2875 // We may give them the option of disabling apps this way.
2876 int messageId = R.string.uninstall_system_app_text;
2877 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002878 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002879 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002880 String packageName = componentName.getPackageName();
2881 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002882 Intent intent = new Intent(
2883 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002884 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2885 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002886 if (user != null) {
2887 user.addToIntent(intent, Intent.EXTRA_USER);
2888 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002889 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002890 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002891 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002892 }
2893
Michael Jurka86a720e2012-05-09 11:23:23 -07002894 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002895 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002896 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002897 // Only launch using the new animation if the shortcut has not opted out (this is a
2898 // private contract between launcher and may be ignored in the future).
2899 boolean useLaunchAnimation = (v != null) &&
2900 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002901 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2902 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002903
Kenny Guy1317e2d2014-05-08 18:52:50 +01002904 UserHandleCompat user = null;
2905 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2906 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2907 user = userManager.getUserForSerialNumber(serialNumber);
2908 }
2909
2910 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002911 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002912 ActivityOptions opts = null;
2913 if (sClipRevealMethod != null) {
2914 // TODO: call method directly when Launcher3 can depend on M APIs
2915 int left = 0, top = 0;
2916 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2917 if (v instanceof TextView) {
2918 // Launch from center of icon, not entire view
2919 TextView tv = (TextView) v;
2920 Drawable[] drawables = tv.getCompoundDrawables();
2921 if (drawables != null && drawables[1] != null) {
2922 Rect bounds = drawables[1].getBounds();
2923 left = (width - bounds.width()) / 2;
2924 top = tv.getPaddingTop();
2925 width = bounds.width();
2926 height = bounds.height();
2927 }
2928 }
2929 try {
2930 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2931 left, top, width, height);
2932 } catch (IllegalAccessException e) {
2933 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2934 sClipRevealMethod = null;
2935 } catch (InvocationTargetException e) {
2936 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2937 sClipRevealMethod = null;
2938 }
2939 }
2940 if (opts == null) {
2941 opts = Utilities.isLmpOrAbove() ?
2942 ActivityOptions.makeCustomAnimation(this,
2943 R.anim.task_open_enter, R.anim.no_anim) :
2944 ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2945 v.getMeasuredWidth(), v.getMeasuredHeight());
2946 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002947 optsBundle = opts.toBundle();
2948 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002949
2950 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2951 // Could be launching some bookkeeping activity
2952 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002953 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002954 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002955 launcherApps.startActivityForProfile(intent.getComponent(), user,
2956 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002957 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002958 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 } catch (SecurityException e) {
2960 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002961 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002963 "or use the exported attribute for this activity. "
2964 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002966 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002968
Michael Jurka86a720e2012-05-09 11:23:23 -07002969 boolean startActivitySafely(View v, Intent intent, Object tag) {
2970 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002971 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2972 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2973 return false;
2974 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002975 try {
2976 success = startActivity(v, intent, tag);
2977 } catch (ActivityNotFoundException e) {
2978 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2979 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2980 }
2981 return success;
2982 }
2983
Adam Cohen268c4752012-06-06 17:47:33 -07002984 /**
2985 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2986 * in the DragLayer in the exact absolute location of the original FolderIcon.
2987 */
2988 private void copyFolderIconToImage(FolderIcon fi) {
2989 final int width = fi.getMeasuredWidth();
2990 final int height = fi.getMeasuredHeight();
2991
2992 // Lazy load ImageView, Bitmap and Canvas
2993 if (mFolderIconImageView == null) {
2994 mFolderIconImageView = new ImageView(this);
2995 }
2996 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2997 mFolderIconBitmap.getHeight() != height) {
2998 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2999 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3000 }
3001
3002 DragLayer.LayoutParams lp;
3003 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3004 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3005 } else {
3006 lp = new DragLayer.LayoutParams(width, height);
3007 }
3008
Adam Cohen307fe232012-08-16 17:55:58 -07003009 // The layout from which the folder is being opened may be scaled, adjust the starting
3010 // view size by this scale factor.
3011 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003012 lp.customPosition = true;
3013 lp.x = mRectForFolderAnimation.left;
3014 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003015 lp.width = (int) (scale * width);
3016 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003017
3018 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3019 fi.draw(mFolderIconCanvas);
3020 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003021 if (fi.getFolder() != null) {
3022 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3023 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003024 }
Adam Cohen268c4752012-06-06 17:47:33 -07003025 // Just in case this image view is still in the drag layer from a previous animation,
3026 // we remove it and re-add it.
3027 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3028 mDragLayer.removeView(mFolderIconImageView);
3029 }
3030 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003031 if (fi.getFolder() != null) {
3032 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003033 }
Adam Cohen268c4752012-06-06 17:47:33 -07003034 }
3035
Adam Cohen2801caf2011-05-13 20:57:39 -07003036 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003037 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003038 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3039 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3040 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3041
Adam Cohenc51934b2011-07-26 21:07:43 -07003042 FolderInfo info = (FolderInfo) fi.getTag();
3043 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3044 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003045 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3046 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003047 }
3048
Adam Cohen268c4752012-06-06 17:47:33 -07003049 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3050 copyFolderIconToImage(fi);
3051 fi.setVisibility(View.INVISIBLE);
3052
Michael Jurka2ecf9952012-06-18 12:52:28 -07003053 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003054 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003055 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003056 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3057 }
3058 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003059 oa.start();
3060 }
3061
Adam Cohen268c4752012-06-06 17:47:33 -07003062 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003063 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003064 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3065 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3066 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3067
Adam Cohen268c4752012-06-06 17:47:33 -07003068 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003069
Adam Cohen268c4752012-06-06 17:47:33 -07003070 // We remove and re-draw the FolderIcon in-case it has changed
3071 mDragLayer.removeView(mFolderIconImageView);
3072 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003073 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003074 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003075 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003076 oa.addListener(new AnimatorListenerAdapter() {
3077 @Override
3078 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003079 if (cl != null) {
3080 cl.clearFolderLeaveBehind();
3081 // Remove the ImageView copy of the FolderIcon and make the original visible.
3082 mDragLayer.removeView(mFolderIconImageView);
3083 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003084 }
3085 }
3086 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003087 oa.start();
3088 }
3089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003090 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003091 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003092 * is animated relative to the specified View. If the View is null, no animation
3093 * is played.
3094 *
3095 * @param folderInfo The FolderInfo describing the folder to open.
3096 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003097 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003098 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003099 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003100
Adam Cohena9cf38f2011-05-02 15:36:58 -07003101 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102
Adam Cohen4554ee12011-08-03 16:13:21 -07003103 // Just verify that the folder hasn't already been added to the DragLayer.
3104 // There was a one-off crash where the folder had a parent already.
3105 if (folder.getParent() == null) {
3106 mDragLayer.addView(folder);
3107 mDragController.addDropTarget((DropTarget) folder);
3108 } else {
3109 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3110 folder.getParent() + ").");
3111 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003112 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003113 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003114
3115 // Notify the accessibility manager that this folder "window" has appeared and occluded
3116 // the workspace items
3117 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3118 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003119 }
3120
3121 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003122 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003123 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003124 if (folder.isEditingName()) {
3125 folder.dismissEditingName();
3126 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003127 closeFolder(folder);
3128 }
3129 }
3130
3131 void closeFolder(Folder folder) {
3132 folder.getInfo().opened = false;
3133
3134 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3135 if (parent != null) {
3136 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3137 shrinkAndFadeInFolderIcon(fi);
3138 }
3139 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003140
3141 // Notify the accessibility manager that this folder "window" has disappeard and no
3142 // longer occludeds the workspace items
3143 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003144 }
3145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003146 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003147 if (!isDraggingEnabled()) return false;
3148 if (isWorkspaceLocked()) return false;
3149 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003150
Adam Cohen1697b792013-09-17 19:08:21 -07003151 if (v instanceof Workspace) {
3152 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003153 if (mWorkspace.enterOverviewMode()) {
3154 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3155 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3156 return true;
3157 } else {
3158 return false;
3159 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003160 } else {
3161 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003162 }
Adam Cohen1697b792013-09-17 19:08:21 -07003163 }
3164
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003165 CellLayout.CellInfo longClickCellInfo = null;
3166 View itemUnderLongClick = null;
3167 if (v.getTag() instanceof ItemInfo) {
3168 ItemInfo info = (ItemInfo) v.getTag();
3169 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3170 itemUnderLongClick = longClickCellInfo.cell;
3171 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 }
3173
Winson Chung3d503fb2011-07-13 17:25:49 -07003174 // The hotseat touch handling does not go through Workspace, and we always allow long press
3175 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003176 final boolean inHotseat = isHotseatLayout(v);
3177 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003178 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003179 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003180 // User long pressed on empty space
3181 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3182 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003183 if (mWorkspace.isInOverviewMode()) {
3184 mWorkspace.startReordering(v);
3185 } else {
3186 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003188 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003189 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3190 mHotseat.getOrderInHotseat(
3191 longClickCellInfo.cellX,
3192 longClickCellInfo.cellY));
3193 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003194 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003195 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003196 }
3197 }
3198 }
3199 return true;
3200 }
3201
Winson Chung3d503fb2011-07-13 17:25:49 -07003202 boolean isHotseatLayout(View layout) {
3203 return mHotseat != null && layout != null &&
3204 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3205 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003206
Winson Chung3d503fb2011-07-13 17:25:49 -07003207 /**
3208 * Returns the CellLayout of the specified container at the specified screen.
3209 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003210 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003211 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3212 if (mHotseat != null) {
3213 return mHotseat.getLayout();
3214 } else {
3215 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003216 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003217 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003218 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003219 }
3220 }
3221
Daniel Sandler843e8602010-06-07 14:59:01 -04003222 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003223 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003224 }
3225
Craig Mautner360310b2012-10-26 15:13:08 -07003226 private void setWorkspaceBackground(boolean workspace) {
3227 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003228 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003229 }
3230
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003231 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003232 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3233 int curflags = getWindow().getAttributes().flags
3234 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3235 if (wpflags != curflags) {
3236 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3237 }
Craig Mautner360310b2012-10-26 15:13:08 -07003238 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003239 }
3240
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003241 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3242 if (v instanceof LauncherTransitionable) {
3243 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3244 }
3245 }
3246
Michael Jurkabed61d22012-02-14 22:51:29 -08003247 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3248 if (v instanceof LauncherTransitionable) {
3249 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3250 }
Winson Chung70442722012-02-10 15:43:22 -08003251
3252 // Update the workspace transition step as well
3253 dispatchOnLauncherTransitionStep(v, 0f);
3254 }
3255
3256 private void dispatchOnLauncherTransitionStep(View v, float t) {
3257 if (v instanceof LauncherTransitionable) {
3258 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3259 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003260 }
3261
3262 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3263 if (v instanceof LauncherTransitionable) {
3264 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3265 }
Winson Chung70442722012-02-10 15:43:22 -08003266
3267 // Update the workspace transition step as well
3268 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003269 }
3270
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003271 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003272 * Things to test when changing the following seven functions.
3273 * - Home from workspace
3274 * - from center screen
3275 * - from other screens
3276 * - Home from all apps
3277 * - from center screen
3278 * - from other screens
3279 * - Back from all apps
3280 * - from center screen
3281 * - from other screens
3282 * - Launch app from workspace and quit
3283 * - with back
3284 * - with home
3285 * - Launch app from all apps and quit
3286 * - with back
3287 * - with home
3288 * - Go to a screen that's not the default, then all
3289 * apps, and launch and app, and go back
3290 * - with back
3291 * -with home
3292 * - On workspace, long press power and go back
3293 * - with back
3294 * - with home
3295 * - On all apps, long press power and go back
3296 * - with back
3297 * - with home
3298 * - On workspace, power off
3299 * - On all apps, power off
3300 * - Launch an app and turn off the screen while in that app
3301 * - Go back with home key
3302 * - Go back with back key TODO: make this not go to workspace
3303 * - From all apps
3304 * - From workspace
3305 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3306 * - From all apps
3307 * - From the center workspace
3308 * - From another workspace
3309 */
3310
3311 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003312 * Zoom the camera out from the workspace to reveal 'toView'.
3313 * Assumes that the view to show is anchored at either the very top or very bottom
3314 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003315 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003316 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003317 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3318 showAppsCustomizeHelper(animated, springLoaded, contentType);
3319 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003320
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003321 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chungc58497e2013-09-03 17:48:37 -07003322 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3323 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003324 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003325 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003326 mStateAnimation.cancel();
3327 mStateAnimation = null;
3328 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003329
Kenny Guyd794a3f2014-09-16 15:17:58 +01003330 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003331
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003332 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003333
Adam Cohen6c5891a2014-07-09 23:53:15 -07003334 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003335 final int itemsAlphaStagger =
3336 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003337
Michael Jurkabed61d22012-02-14 22:51:29 -08003338 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003339 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003340
Adam Cohen80e6beb2015-02-13 16:14:33 -08003341 final HashMap<View, Integer> layerViews = new HashMap<View, Integer>();
Adam Cohen2854d252014-08-27 16:04:07 -07003342
Adam Cohen6c5891a2014-07-09 23:53:15 -07003343 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3344 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003345 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003346 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Sunny Goyalc9acdd52015-02-26 12:34:42 -08003347 if (contentType == AppsCustomizePagedView.ContentType.Widgets) {
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003348 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003349 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3350 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003351
Adam Cohena38dc902014-09-07 17:48:55 +02003352 // If for some reason our views aren't initialized, don't animate
3353 boolean initialized = getAllAppsButton() != null;
3354
3355 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003356 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003357 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3358 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003359
Adam Cohen9bfdb762014-07-21 17:44:06 -07003360 final View page = content.getPageAt(content.getCurrentPage());
3361 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003362
Adam Cohen63f1ec02014-08-12 09:23:13 -07003363 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3364 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003365 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3366 } else {
3367 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3368 }
3369
Adam Cohen9bfdb762014-07-21 17:44:06 -07003370 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003371 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003372 revealView.setVisibility(View.VISIBLE);
3373 // We need to hide this view as the animation start will be posted.
3374 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003375
Adam Cohen9bfdb762014-07-21 17:44:06 -07003376 int width = revealView.getMeasuredWidth();
3377 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003378 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003379
Adam Cohen63f1ec02014-08-12 09:23:13 -07003380 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003381 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003382
Adam Cohen63f1ec02014-08-12 09:23:13 -07003383 // Get the y delta between the center of the page and the center of the all apps button
3384 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3385 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003386
Adam Cohen2854d252014-08-27 16:04:07 -07003387 float alpha = 0;
3388 float xDrift = 0;
3389 float yDrift = 0;
3390 if (material) {
3391 alpha = isWidgetTray ? 0.3f : 1f;
3392 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3393 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3394 } else {
3395 yDrift = 2 * height / 3;
3396 xDrift = 0;
3397 }
3398 final float initAlpha = alpha;
3399
Adam Cohen80e6beb2015-02-13 16:14:33 -08003400 layerViews.put(revealView, BUILD_AND_SET_LAYER);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003401 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003402 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003403 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003404 PropertyValuesHolder panelDriftX =
3405 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003406
Adam Cohen2854d252014-08-27 16:04:07 -07003407 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3408 panelAlpha, panelDriftY, panelDriftX);
3409
Adam Cohen9bfdb762014-07-21 17:44:06 -07003410 panelAlphaAndDrift.setDuration(revealDuration);
3411 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003412
Adam Cohen9bfdb762014-07-21 17:44:06 -07003413 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003414
Adam Cohen4e243a22014-08-10 18:30:55 -07003415 if (page != null) {
3416 page.setVisibility(View.VISIBLE);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003417 layerViews.put(page, BUILD_AND_SET_LAYER);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003418
Adam Cohen2854d252014-08-27 16:04:07 -07003419 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3420 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003421 pageDrift.setDuration(revealDuration);
3422 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003423 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003424 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003425
Adam Cohen63f1ec02014-08-12 09:23:13 -07003426 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003427 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003428 itemsAlpha.setDuration(revealDuration);
3429 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3430 itemsAlpha.setStartDelay(itemsAlphaStagger);
3431 mStateAnimation.play(itemsAlpha);
3432 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003433
Adam Cohen4e243a22014-08-10 18:30:55 -07003434 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3435 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003436 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003437 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003438 indicatorsAlpha.setDuration(revealDuration);
3439 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003440
Adam Cohen9bfdb762014-07-21 17:44:06 -07003441 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003442 final View allApps = getAllAppsButton();
3443 int allAppsButtonSize = LauncherAppState.getInstance().
3444 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3445 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003446 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003447 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003448 reveal.setDuration(revealDuration);
3449 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003450
3451 reveal.addListener(new AnimatorListenerAdapter() {
3452 public void onAnimationStart(Animator animation) {
3453 if (!isWidgetTray) {
3454 allApps.setVisibility(View.INVISIBLE);
3455 }
3456 }
3457 public void onAnimationEnd(Animator animation) {
3458 if (!isWidgetTray) {
3459 allApps.setVisibility(View.VISIBLE);
3460 }
3461 }
3462 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003463 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003464 }
Michael Jurka1899a362011-11-03 13:50:45 -07003465
Adam Cohen9bfdb762014-07-21 17:44:06 -07003466 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3467 @Override
3468 public void onAnimationEnd(Animator animation) {
3469 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3470 dispatchOnLauncherTransitionEnd(toView, animated, false);
3471
3472 revealView.setVisibility(View.INVISIBLE);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003473
3474 for (View v : layerViews.keySet()) {
3475 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3476 v.setLayerType(View.LAYER_TYPE_NONE, null);
3477 }
Adam Cohen4e243a22014-08-10 18:30:55 -07003478 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003479 content.setPageBackgroundsVisible(true);
3480
3481 // Hide the search bar
3482 if (mSearchDropTargetBar != null) {
3483 mSearchDropTargetBar.hideSearchBar(false);
3484 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003485
3486 // This can hold unnecessary references to views.
3487 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003488 }
3489
Adam Cohen9bfdb762014-07-21 17:44:06 -07003490 });
3491
Adam Cohen6c5891a2014-07-09 23:53:15 -07003492 if (workspaceAnim != null) {
3493 mStateAnimation.play(workspaceAnim);
3494 }
Adam Cohen2854d252014-08-27 16:04:07 -07003495
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003496 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3497 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003498 final AnimatorSet stateAnimation = mStateAnimation;
3499 final Runnable startAnimRunnable = new Runnable() {
3500 public void run() {
3501 // Check that mStateAnimation hasn't changed while
3502 // we waited for a layout/draw pass
3503 if (mStateAnimation != stateAnimation)
3504 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003505 dispatchOnLauncherTransitionStart(fromView, animated, false);
3506 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003507
3508 revealView.setAlpha(initAlpha);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003509
3510 for (View v : layerViews.keySet()) {
3511 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3512 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3513 }
3514 }
3515
Kenny Guyd794a3f2014-09-16 15:17:58 +01003516 if (Utilities.isLmpOrAbove()) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003517 for (View v : layerViews.keySet()) {
3518 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen2854d252014-08-27 16:04:07 -07003519 }
3520 }
3521 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003522 }
3523 };
Adam Cohen2854d252014-08-27 16:04:07 -07003524 toView.bringToFront();
3525 toView.setVisibility(View.VISIBLE);
3526 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003527 } else {
3528 toView.setTranslationX(0.0f);
3529 toView.setTranslationY(0.0f);
3530 toView.setScaleX(1.0f);
3531 toView.setScaleY(1.0f);
3532 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003533 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003534
Daniel Sandlere4f98912013-06-25 15:13:26 -04003535 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003536 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003537 if (mSearchDropTargetBar != null) {
3538 mSearchDropTargetBar.hideSearchBar(false);
3539 }
Michael Jurkaabded662011-03-04 12:06:57 -08003540 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003541 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003542 dispatchOnLauncherTransitionStart(fromView, animated, false);
3543 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003544 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003545 dispatchOnLauncherTransitionStart(toView, animated, false);
3546 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003547 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003548 }
3549
3550 /**
3551 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003552 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003553 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003554 */
Adam Cohened307df2013-10-02 09:37:31 -07003555 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003556 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003557
Michael Jurkab3e22d92011-10-31 15:58:33 -07003558 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003559 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003560 mStateAnimation.cancel();
3561 mStateAnimation = null;
3562 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003563
Kenny Guyd794a3f2014-09-16 15:17:58 +01003564 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003565 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003566
Adam Cohen63f1ec02014-08-12 09:23:13 -07003567 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003568 final int itemsAlphaStagger =
3569 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003570
Winson Chungf0ea4d32011-06-06 14:27:16 -07003571 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003572 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003573 Animator workspaceAnim = null;
Adam Cohen80e6beb2015-02-13 16:14:33 -08003574 final HashMap<View, Integer> layerViews = new HashMap<View, Integer>();
Adam Cohen2854d252014-08-27 16:04:07 -07003575
Adam Cohened307df2013-10-02 09:37:31 -07003576 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003577 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003578 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003579 } else if (toState == Workspace.State.SPRING_LOADED ||
3580 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003581 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003582 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003583 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003584
Adam Cohena38dc902014-09-07 17:48:55 +02003585 // If for some reason our views aren't initialized, don't animate
3586 boolean initialized = getAllAppsButton() != null;
3587
3588 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003589 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003590 if (workspaceAnim != null) {
3591 mStateAnimation.play(workspaceAnim);
3592 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003593
Adam Cohen9bfdb762014-07-21 17:44:06 -07003594 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3595 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003596
Adam Cohen9bfdb762014-07-21 17:44:06 -07003597 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003598
3599 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3600 int count = content.getChildCount();
3601 for (int i = 0; i < count; i++) {
3602 View child = content.getChildAt(i);
3603 if (child != page) {
3604 child.setVisibility(View.INVISIBLE);
3605 }
3606 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003607 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003608
Adam Cohen2854d252014-08-27 16:04:07 -07003609 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3610 // don't perform all these no-op animations. In particularly, this was causing
3611 // the all-apps button to pop in and out.
3612 if (fromView.getVisibility() == View.VISIBLE) {
3613 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3614 final boolean isWidgetTray =
3615 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003616
Adam Cohen2854d252014-08-27 16:04:07 -07003617 if (isWidgetTray) {
3618 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3619 } else {
3620 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003621 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003622
Adam Cohen2854d252014-08-27 16:04:07 -07003623 int width = revealView.getMeasuredWidth();
3624 int height = revealView.getMeasuredHeight();
3625 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3626
3627 // Hide the real page background, and swap in the fake one
3628 revealView.setVisibility(View.VISIBLE);
3629 content.setPageBackgroundsVisible(false);
3630
3631 final View allAppsButton = getAllAppsButton();
3632 revealView.setTranslationY(0);
3633 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3634 allAppsButton, null);
3635
3636 float xDrift = 0;
3637 float yDrift = 0;
3638 if (material) {
3639 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3640 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3641 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003642 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003643 xDrift = 0;
3644 }
3645
Adam Cohen80e6beb2015-02-13 16:14:33 -08003646 layerViews.put(revealView, BUILD_AND_SET_LAYER);
Adam Cohen2854d252014-08-27 16:04:07 -07003647 TimeInterpolator decelerateInterpolator = material ?
3648 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003649 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003650
3651 // The vertical motion of the apps panel should be delayed by one frame
3652 // from the conceal animation in order to give the right feel. We correpsondingly
3653 // shorten the duration so that the slide and conceal end at the same time.
3654 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3655 0, yDrift);
3656 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3657 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3658 panelDriftY.setInterpolator(decelerateInterpolator);
3659 mStateAnimation.play(panelDriftY);
3660
3661 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3662 0, xDrift);
3663 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3664 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3665 panelDriftX.setInterpolator(decelerateInterpolator);
3666 mStateAnimation.play(panelDriftX);
3667
3668 if (isWidgetTray || !material) {
3669 float finalAlpha = material ? 0.4f : 0f;
3670 revealView.setAlpha(1f);
3671 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3672 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003673 panelAlpha.setDuration(material ? revealDuration : 150);
3674 panelAlpha.setInterpolator(decelerateInterpolator);
3675 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003676 mStateAnimation.play(panelAlpha);
3677 }
3678
3679 if (page != null) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003680 layerViews.put(page, BUILD_AND_SET_LAYER);
Adam Cohen2854d252014-08-27 16:04:07 -07003681
3682 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3683 0, yDrift);
3684 page.setTranslationY(0);
3685 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3686 pageDrift.setInterpolator(decelerateInterpolator);
3687 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3688 mStateAnimation.play(pageDrift);
3689
3690 page.setAlpha(1f);
3691 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3692 itemsAlpha.setDuration(100);
3693 itemsAlpha.setInterpolator(decelerateInterpolator);
3694 mStateAnimation.play(itemsAlpha);
3695 }
3696
3697 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3698 pageIndicators.setAlpha(1f);
3699 ObjectAnimator indicatorsAlpha =
3700 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3701 indicatorsAlpha.setDuration(revealDuration);
3702 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3703 mStateAnimation.play(indicatorsAlpha);
3704
3705 width = revealView.getMeasuredWidth();
3706
3707 if (material) {
3708 if (!isWidgetTray) {
3709 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003710 }
Adam Cohen2854d252014-08-27 16:04:07 -07003711 int allAppsButtonSize = LauncherAppState.getInstance().
3712 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3713 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3714 Animator reveal =
3715 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3716 height / 2, revealRadius, finalRadius);
3717 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3718 reveal.setDuration(revealDuration);
3719 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003720
Adam Cohen2854d252014-08-27 16:04:07 -07003721 reveal.addListener(new AnimatorListenerAdapter() {
3722 public void onAnimationEnd(Animator animation) {
3723 revealView.setVisibility(View.INVISIBLE);
3724 if (!isWidgetTray) {
3725 allAppsButton.setVisibility(View.VISIBLE);
3726 }
3727 }
3728 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003729
Adam Cohen2854d252014-08-27 16:04:07 -07003730 mStateAnimation.play(reveal);
3731 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003732
Adam Cohen2854d252014-08-27 16:04:07 -07003733 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3734 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3735 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003736 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003737
3738 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003739 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003740 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003741 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003742 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3743 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003744 if (onCompleteRunnable != null) {
3745 onCompleteRunnable.run();
3746 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003747
Adam Cohen80e6beb2015-02-13 16:14:33 -08003748 for (View v : layerViews.keySet()) {
3749 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3750 v.setLayerType(View.LAYER_TYPE_NONE, null);
3751 }
Adam Cohen4e243a22014-08-10 18:30:55 -07003752 }
Adam Cohen80e6beb2015-02-13 16:14:33 -08003753
Adam Cohen9bfdb762014-07-21 17:44:06 -07003754 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003755 // Unhide side pages
3756 int count = content.getChildCount();
3757 for (int i = 0; i < count; i++) {
3758 View child = content.getChildAt(i);
3759 child.setVisibility(View.VISIBLE);
3760 }
3761
3762 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003763 if (page != null) {
3764 page.setTranslationX(0);
3765 page.setTranslationY(0);
3766 page.setAlpha(1);
3767 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003768 content.setCurrentPage(content.getNextPage());
3769
Chet Haasebc2f0822012-10-26 17:59:53 -07003770 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003771
3772 // This can hold unnecessary references to views.
3773 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003774 }
3775 });
3776
Adam Cohen2854d252014-08-27 16:04:07 -07003777 final AnimatorSet stateAnimation = mStateAnimation;
3778 final Runnable startAnimRunnable = new Runnable() {
3779 public void run() {
3780 // Check that mStateAnimation hasn't changed while
3781 // we waited for a layout/draw pass
3782 if (mStateAnimation != stateAnimation)
3783 return;
3784 dispatchOnLauncherTransitionStart(fromView, animated, false);
3785 dispatchOnLauncherTransitionStart(toView, animated, false);
3786
Adam Cohen80e6beb2015-02-13 16:14:33 -08003787 for (View v : layerViews.keySet()) {
3788 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3789 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3790 }
3791 }
3792
Kenny Guyd794a3f2014-09-16 15:17:58 +01003793 if (Utilities.isLmpOrAbove()) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003794 for (View v : layerViews.keySet()) {
3795 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen2854d252014-08-27 16:04:07 -07003796 }
3797 }
3798 mStateAnimation.start();
3799 }
3800 };
3801 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003802 } else {
3803 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003804 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003805 dispatchOnLauncherTransitionStart(fromView, animated, true);
3806 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003807 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003808 dispatchOnLauncherTransitionStart(toView, animated, true);
3809 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003810 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003811 }
3812
Michael Jurkae326f182011-11-21 14:05:46 -08003813 @Override
3814 public void onTrimMemory(int level) {
3815 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003816 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3817 // The widget preview db can result in holding onto over
3818 // 3MB of memory for caching which isn't necessary.
3819 SQLiteDatabase.releaseMemory();
3820
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003821 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003822 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003823 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003824 }
Michael Jurkae326f182011-11-21 14:05:46 -08003825 }
3826 }
3827
Adam Cohened307df2013-10-02 09:37:31 -07003828 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003829 showWorkspace(animated, null);
3830 }
3831
Adam Cohened307df2013-10-02 09:37:31 -07003832 protected void showWorkspace() {
3833 showWorkspace(true);
3834 }
3835
Adam Cohened66b2b2012-01-23 17:28:51 -08003836 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003837 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003838 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003839 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003840 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003841
Winson Chungc7d2b602012-05-16 17:02:20 -07003842 // Show the search bar (only animate if we were showing the drop target bar in spring
3843 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003844 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003845 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003846 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003847
Michael Jurkab3e22d92011-10-31 15:58:33 -07003848 // Set focus to the AppsCustomize button
3849 if (mAllAppsButton != null) {
3850 mAllAppsButton.requestFocus();
3851 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003852 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003853
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003854 // Change the state *after* we've called all the transition code
3855 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003856
Winson Chung5fb63472011-02-02 17:03:37 -08003857 // Resume the auto-advance of widgets
3858 mUserPresent = true;
3859 updateRunning();
3860
alanv1d4fde62012-10-17 13:15:47 -07003861 // Send an accessibility event to announce the context change
3862 getWindow().getDecorView()
3863 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003864
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003865 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003866 }
3867
Adam Cohened307df2013-10-02 09:37:31 -07003868 void showOverviewMode(boolean animated) {
3869 mWorkspace.setVisibility(View.VISIBLE);
3870 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3871 mState = State.WORKSPACE;
3872 onWorkspaceShown(animated);
3873 }
3874
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003875 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003876 }
3877
Winson Chung82963d52013-10-09 11:20:57 -07003878 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3879 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003880 if (mState != State.WORKSPACE) return;
3881
Winson Chung82963d52013-10-09 11:20:57 -07003882 if (resetPageToZero) {
3883 mAppsCustomizeTabHost.reset();
3884 }
Winson Chungc58497e2013-09-03 17:48:37 -07003885 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003886 mAppsCustomizeTabHost.post(new Runnable() {
3887 @Override
3888 public void run() {
3889 // We post this in-case the all apps view isn't yet constructed.
3890 mAppsCustomizeTabHost.requestFocus();
3891 }
3892 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003893
Michael Jurkab3e22d92011-10-31 15:58:33 -07003894 // Change the state *after* we've called all the transition code
3895 mState = State.APPS_CUSTOMIZE;
3896
3897 // Pause the auto-advance of widgets until we are out of AllApps
3898 mUserPresent = false;
3899 updateRunning();
3900 closeFolder();
3901
3902 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003903 getWindow().getDecorView()
3904 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003905 }
3906
Adam Cohen7777d962011-08-18 18:58:38 -07003907 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003908 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003909 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003910 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003911 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003912 }
Adam Cohen7777d962011-08-18 18:58:38 -07003913
Adam Cohenad4e15c2013-10-17 16:21:35 -07003914 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003915 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003916 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3917
Winson Chunge7a03942011-08-05 15:05:12 -07003918 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003919 @Override
3920 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003921 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003922 // Before we show workspace, hide all apps again because
3923 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3924 // clean up our state transition functions
3925 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003926 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003927 } else {
3928 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003929 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003930 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003931 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003932 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003933
Michael Jurkad3ef3062010-11-23 16:23:58 -08003934 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003935 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003936 final boolean animated = true;
3937 final boolean springLoaded = true;
3938 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003939 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003940 }
3941 // Otherwise, we are not in spring loaded mode, so don't do anything.
3942 }
3943
Michael Jurkab3e22d92011-10-31 15:58:33 -07003944 void lockAllApps() {
3945 // TODO
3946 }
3947
3948 void unlockAllApps() {
3949 // TODO
3950 }
3951
Sunny Goyal594d76d2014-11-06 10:12:54 -08003952 protected void disableVoiceButtonProxy(boolean disable) {
3953 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003954 }
3955
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003956 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003957 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3958 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003959 }
3960
Adam Cohen24ce0b32014-01-14 16:18:14 -08003961 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003962 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3963 if (searchProvider == null) {
3964 return null;
3965 }
3966
3967 Bundle opts = new Bundle();
3968 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003969 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003970
3971 SharedPreferences sp = getSharedPreferences(
3972 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3973 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003974 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003975 if (!searchProvider.provider.flattenToString().equals(
3976 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003977 || (widgetInfo == null)
3978 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003979 // A valid widget is not already bound.
3980 if (widgetId > -1) {
3981 mAppWidgetHost.deleteAppWidgetId(widgetId);
3982 widgetId = -1;
3983 }
3984
3985 // Try to bind a new widget
3986 widgetId = mAppWidgetHost.allocateAppWidgetId();
3987
3988 if (!AppWidgetManagerCompat.getInstance(this)
3989 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3990 mAppWidgetHost.deleteAppWidgetId(widgetId);
3991 widgetId = -1;
3992 }
3993
3994 sp.edit()
3995 .putInt(QSB_WIDGET_ID, widgetId)
3996 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3997 .commit();
3998 }
3999
4000 if (widgetId != -1) {
4001 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
4002 mQsb.updateAppWidgetOptions(opts);
4003 mQsb.setPadding(0, 0, 0, 0);
4004 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08004005 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08004006 }
Cristina Stancu476493b2013-08-07 17:20:14 +01004007 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08004008 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01004009 }
4010
Michael Jurkad7c28052012-04-27 15:43:36 -07004011 @Override
4012 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004013 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004014 final List<CharSequence> text = event.getText();
4015 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004016 // Populate event with a fake title based on the current state.
4017 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004018 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004019 } else {
4020 text.add(getString(R.string.all_apps_home_button_label));
4021 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004022 return result;
4023 }
4024
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004025 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004026 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004027 */
4028 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4029 @Override
4030 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004031 closeSystemDialogs();
4032 }
4033 }
4034
4035 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004036 * Receives notifications whenever the appwidgets are reset.
4037 */
4038 private class AppWidgetResetObserver extends ContentObserver {
4039 public AppWidgetResetObserver() {
4040 super(new Handler());
4041 }
4042
4043 @Override
4044 public void onChange(boolean selfChange) {
4045 onAppWidgetReset();
4046 }
4047 }
4048
4049 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004050 * If the activity is currently paused, signal that we need to run the passed Runnable
4051 * in onResume.
4052 *
4053 * This needs to be called from incoming places where resources might have been loaded
4054 * while we are paused. That is becaues the Configuration might be wrong
4055 * when we're not running, and if it comes back to what it was when we
4056 * were paused, we are not restarted.
4057 *
4058 * Implementation of the method from LauncherModel.Callbacks.
4059 *
4060 * @return true if we are currently paused. The caller might be able to
4061 * skip some work in that case since we will come back again.
4062 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004063 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004064 if (mPaused) {
4065 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004066 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004067 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004068 }
4069 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004070 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004071 return true;
4072 } else {
4073 return false;
4074 }
4075 }
4076
Michael Jurkac402cd92013-05-20 15:49:32 +02004077 private boolean waitUntilResume(Runnable run) {
4078 return waitUntilResume(run, false);
4079 }
4080
Michael Jurka1e2f4652013-07-08 18:03:46 -07004081 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004082 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004083 }
4084
Michael Jurka7607c2f2013-04-03 14:33:19 -07004085 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004086 * If the activity is currently paused, signal that we need to re-run the loader
4087 * in onResume.
4088 *
4089 * This needs to be called from incoming places where resources might have been loaded
4090 * while we are paused. That is becaues the Configuration might be wrong
4091 * when we're not running, and if it comes back to what it was when we
4092 * were paused, we are not restarted.
4093 *
4094 * Implementation of the method from LauncherModel.Callbacks.
4095 *
4096 * @return true if we are currently paused. The caller might be able to
4097 * skip some work in that case since we will come back again.
4098 */
4099 public boolean setLoadOnResume() {
4100 if (mPaused) {
4101 Log.i(TAG, "setLoadOnResume");
4102 mOnResumeNeedsLoad = true;
4103 return true;
4104 } else {
4105 return false;
4106 }
4107 }
4108
4109 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004110 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004111 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004112 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004113 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004114 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004115 } else {
4116 return SCREEN_COUNT / 2;
4117 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004118 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004119
Joe Onorato9c1289c2009-08-17 11:03:03 -04004120 /**
4121 * Refreshes the shortcuts shown on the workspace.
4122 *
4123 * Implementation of the method from LauncherModel.Callbacks.
4124 */
4125 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004126 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004127
Michael Jurka7607c2f2013-04-03 14:33:19 -07004128 // If we're starting binding all over again, clear any bind calls we'd postponed in
4129 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4130 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004131 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004132
Winson Chungd64d1762013-08-20 14:37:16 -07004133 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004134 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004135 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004136
Michael Jurka05bf644e2011-11-30 20:28:53 -08004137 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004138 if (mHotseat != null) {
4139 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004140 }
4141 }
4142
Adam Cohendcd297f2013-06-18 13:13:40 -07004143 @Override
4144 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004145 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004146
Adam Cohen5084cba2013-09-03 12:01:16 -07004147 // If there are no screens, we need to have an empty screen
4148 if (orderedScreenIds.size() == 0) {
4149 mWorkspace.addExtraEmptyScreen();
4150 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004151
4152 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004153 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004154 if (hasCustomContentToLeft()) {
4155 mWorkspace.createCustomContentContainer();
4156 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004157 }
Winson Chung64359a52013-07-08 17:17:08 -07004158 }
4159
4160 @Override
4161 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004162 // Log to disk
4163 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4164 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4165 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004166 int count = orderedScreenIds.size();
4167 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004168 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004169 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004170 }
4171
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08004172 @Override
4173 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
4174 final long screenId, final int[] cell, final int spanX, final int spanY) {
4175 showWorkspace(true, new Runnable() {
4176
4177 @Override
4178 public void run() {
4179 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
4180 addPendingItem(info, container, screenId, cell, spanX, spanY);
4181 }
4182 });
4183 }
4184
Adam Cohen39a06042013-07-19 14:30:12 -07004185 private boolean shouldShowWeightWatcher() {
4186 String spKey = LauncherAppState.getSharedPreferencesKey();
4187 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07004188 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004189
4190 return show;
4191 }
4192
4193 private void toggleShowWeightWatcher() {
4194 String spKey = LauncherAppState.getSharedPreferencesKey();
4195 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4196 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4197
4198 show = !show;
4199
4200 SharedPreferences.Editor editor = sp.edit();
4201 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4202 editor.commit();
4203
4204 if (mWeightWatcher != null) {
4205 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4206 }
4207 }
4208
Winson Chungd64d1762013-08-20 14:37:16 -07004209 public void bindAppsAdded(final ArrayList<Long> newScreens,
4210 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004211 final ArrayList<ItemInfo> addAnimated,
4212 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004213 Runnable r = new Runnable() {
4214 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004215 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004216 }
4217 };
4218 if (waitUntilResume(r)) {
4219 return;
4220 }
4221
Winson Chungd64d1762013-08-20 14:37:16 -07004222 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004223 if (newScreens != null) {
4224 bindAddScreens(newScreens);
4225 }
Winson Chungd64d1762013-08-20 14:37:16 -07004226
4227 // We add the items without animation on non-visible pages, and with
4228 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004229 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004230 bindItems(addNotAnimated, 0,
4231 addNotAnimated.size(), false);
4232 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004233 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004234 bindItems(addAnimated, 0,
4235 addAnimated.size(), true);
4236 }
Winson Chungc58497e2013-09-03 17:48:37 -07004237
Winson Chung87412982013-10-03 18:34:14 -07004238 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004239 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004240
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004241 if (addedApps != null && mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004242 mAppsCustomizeContent.addApps(addedApps);
4243 }
Winson Chungd64d1762013-08-20 14:37:16 -07004244 }
4245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004246 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004247 * Bind the items start-end from the list.
4248 *
4249 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004250 */
Winson Chung64359a52013-07-08 17:17:08 -07004251 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4252 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004253 Runnable r = new Runnable() {
4254 public void run() {
4255 bindItems(shortcuts, start, end, forceAnimateIcons);
4256 }
4257 };
4258 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004259 return;
4260 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004261
Winson Chungf0c6ae02012-03-21 16:10:31 -07004262 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004263 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4264 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004265 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004266 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004267 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004268 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004269 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004270
4271 // Short circuit if we are loading dock items for a configuration which has no dock
4272 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4273 mHotseat == null) {
4274 continue;
4275 }
4276
Joe Onorato9c1289c2009-08-17 11:03:03 -04004277 switch (item.itemType) {
4278 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4279 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004280 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004281 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004282
Winson Chung64359a52013-07-08 17:17:08 -07004283 /*
4284 * TODO: FIX collision case
4285 */
Winson Chungce376632013-07-11 16:12:41 -07004286 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4287 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4288 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004289 View v = cl.getChildAt(item.cellX, item.cellY);
4290 Object tag = v.getTag();
4291 String desc = "Collision while binding workspace item: " + item
4292 + ". Collides with " + tag;
4293 if (LauncherAppState.isDogfoodBuild()) {
4294 throw (new RuntimeException(desc));
4295 } else {
4296 Log.d(TAG, desc);
4297 }
Winson Chungce376632013-07-11 16:12:41 -07004298 }
Winson Chung64359a52013-07-08 17:17:08 -07004299 }
4300
Adam Cohendcd297f2013-06-18 13:13:40 -07004301 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4302 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004303 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004304 // Animate all the applications up now
4305 shortcut.setAlpha(0f);
4306 shortcut.setScaleX(0f);
4307 shortcut.setScaleY(0f);
4308 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004309 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004310 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004311 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004312 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004313 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004314 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004315 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004316 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4317 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004318 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004319 default:
4320 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004321 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004322 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004323
Winson Chung997a9232013-07-24 15:33:46 -07004324 if (animateIcons) {
4325 // Animate to the correct page
4326 if (newShortcutsScreenId > -1) {
4327 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004328 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004329 final Runnable startBounceAnimRunnable = new Runnable() {
4330 public void run() {
4331 anim.playTogether(bounceAnims);
4332 anim.start();
4333 }
4334 };
Winson Chung997a9232013-07-24 15:33:46 -07004335 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004336 // We post the animation slightly delayed to prevent slowdowns
4337 // when we are loading right after we return to launcher.
4338 mWorkspace.postDelayed(new Runnable() {
4339 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004340 if (mWorkspace != null) {
4341 mWorkspace.snapToPage(newScreenIndex);
4342 mWorkspace.postDelayed(startBounceAnimRunnable,
4343 NEW_APPS_ANIMATION_DELAY);
4344 }
Winson Chung94d67682013-09-25 16:29:40 -07004345 }
4346 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004347 } else {
4348 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004349 }
4350 }
Winson Chung64359a52013-07-08 17:17:08 -07004351 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004352 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004353 }
4354
Joe Onorato9c1289c2009-08-17 11:03:03 -04004355 /**
4356 * Implementation of the method from LauncherModel.Callbacks.
4357 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004358 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004359 Runnable r = new Runnable() {
4360 public void run() {
4361 bindFolders(folders);
4362 }
4363 };
4364 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004365 return;
4366 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004367 sFolders.clear();
4368 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004369 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004370
4371 /**
4372 * Add the views for a widget to the workspace.
4373 *
4374 * Implementation of the method from LauncherModel.Callbacks.
4375 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004376 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004377 Runnable r = new Runnable() {
4378 public void run() {
4379 bindAppWidget(item);
4380 }
4381 };
4382 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004383 return;
4384 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004385
Daniel Sandler843e8602010-06-07 14:59:01 -04004386 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4387 if (DEBUG_WIDGETS) {
4388 Log.d(TAG, "bindAppWidget: " + item);
4389 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004390 final Workspace workspace = mWorkspace;
4391
Adam Cohen59400422014-03-05 18:07:04 -08004392 LauncherAppWidgetProviderInfo appWidgetInfo =
4393 LauncherModel.getProviderInfo(this, item.providerName);
4394
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004395 if (!mIsSafeModeEnabled
4396 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4397 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004398 if (appWidgetInfo == null) {
4399 if (DEBUG_WIDGETS) {
4400 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4401 + " belongs to component " + item.providerName
4402 + ", as the povider is null");
4403 }
4404 LauncherModel.deleteItemFromDatabase(this, item);
4405 return;
4406 }
4407 // Note: This assumes that the id remap broadcast is received before this step.
4408 // If that is not the case, the id remap will be ignored and user may see the
4409 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004410 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004411 pendingInfo.spanX = item.spanX;
4412 pendingInfo.spanY = item.spanY;
4413 pendingInfo.minSpanX = item.minSpanX;
4414 pendingInfo.minSpanY = item.minSpanY;
4415 Bundle options =
4416 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4417
Sunny Goyalff572272014-07-23 13:58:07 -07004418 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004419 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4420 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004421
4422 // TODO consider showing a permission dialog when the widget is clicked.
4423 if (!success) {
4424 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4425 if (DEBUG_WIDGETS) {
4426 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4427 + " belongs to component " + item.providerName
4428 + ", as the launcher is unable to bing a new widget id");
4429 }
4430 LauncherModel.deleteItemFromDatabase(this, item);
4431 return;
4432 }
4433
4434 item.appWidgetId = newWidgetId;
4435
4436 // If the widget has a configure activity, it is still needs to set it up, otherwise
4437 // the widget is ready to go.
4438 item.restoreStatus = (appWidgetInfo.configure == null)
4439 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4440 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4441
4442 LauncherModel.updateItemInDatabase(this, item);
4443 }
4444
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004445 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004446 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004447 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004448 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4449 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004450 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004451
Sunny Goyal651077b2014-06-30 14:15:31 -07004452 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4453 } else {
4454 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004455 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4456 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004457 view.updateIcon(mIconCache);
4458 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004459 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004460 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004461 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004462
Joe Onorato9c1289c2009-08-17 11:03:03 -04004463 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004464 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004465
Adam Cohendcd297f2013-06-18 13:13:40 -07004466 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004467 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004468 if (!item.isCustomWidget()) {
4469 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4470 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004471
Joe Onorato9c1289c2009-08-17 11:03:03 -04004472 workspace.requestLayout();
4473
Daniel Sandler843e8602010-06-07 14:59:01 -04004474 if (DEBUG_WIDGETS) {
4475 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4476 + (SystemClock.uptimeMillis()-start) + "ms");
4477 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004478 }
4479
Sunny Goyalff572272014-07-23 13:58:07 -07004480 /**
4481 * Restores a pending widget.
4482 *
4483 * @param appWidgetId The app widget id
4484 * @param cellInfo The position on screen where to create the widget.
4485 */
4486 private void completeRestoreAppWidget(final int appWidgetId) {
4487 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4488 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4489 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4490 return;
4491 }
4492
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004493 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004494 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4495
4496 mWorkspace.reinflateWidgetsIfNecessary();
4497 LauncherModel.updateItemInDatabase(this, info);
4498 }
4499
Adam Cohen1462de32012-07-24 22:34:36 -07004500 public void onPageBoundSynchronously(int page) {
4501 mSynchronouslyBoundPages.add(page);
4502 }
4503
Joe Onorato9c1289c2009-08-17 11:03:03 -04004504 /**
4505 * Callback saying that there aren't any more items to bind.
4506 *
4507 * Implementation of the method from LauncherModel.Callbacks.
4508 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004509 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004510 Runnable r = new Runnable() {
4511 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004512 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004513 }
4514 };
4515 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004516 return;
4517 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004518 if (mSavedState != null) {
4519 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004520 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004521 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004522 mSavedState = null;
4523 }
4524
Adam Cohen1462de32012-07-24 22:34:36 -07004525 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004526
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004527 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004528 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004529
4530 // If we received the result of any pending adds while the loader was running (e.g. the
4531 // widget configuration forced an orientation change), process them now.
4532 if (sPendingAddItem != null) {
4533 final long screenId = completeAdd(sPendingAddItem);
4534
4535 // TODO: this moves the user to the page where the pending item was added. Ideally,
4536 // the screen would be guaranteed to exist after bind, and the page would be set through
4537 // the workspace restore process.
4538 mWorkspace.post(new Runnable() {
4539 @Override
4540 public void run() {
4541 mWorkspace.snapToScreenId(screenId);
4542 }
4543 });
4544 sPendingAddItem = null;
4545 }
4546
Sunny Goyale755d462014-07-22 13:48:29 -07004547 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004548
4549 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004550 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004551 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004552 }
4553
Adam Cohen3ed316a2014-07-23 18:21:20 -07004554 private void sendLoadingCompleteBroadcastIfNecessary() {
4555 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4556 String permission =
4557 getResources().getString(R.string.receive_first_load_broadcast_permission);
4558 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4559 sendBroadcast(intent, permission);
4560 SharedPreferences.Editor editor = mSharedPrefs.edit();
4561 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4562 editor.apply();
4563 }
4564 }
4565
Winson Chunga0b7e862013-09-05 16:03:15 -07004566 public boolean isAllAppsButtonRank(int rank) {
4567 if (mHotseat != null) {
4568 return mHotseat.isAllAppsButtonRank(rank);
4569 }
4570 return false;
4571 }
4572
Winson Chunga2413752012-04-03 14:22:34 -07004573 private boolean canRunNewAppsAnimation() {
4574 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4575 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4576 }
4577
Winson Chungc9168342013-06-26 14:54:55 -07004578 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4579 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4580 PropertyValuesHolder.ofFloat("alpha", 1f),
4581 PropertyValuesHolder.ofFloat("scaleX", 1f),
4582 PropertyValuesHolder.ofFloat("scaleY", 1f));
4583 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4584 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4585 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4586 return bounceAnim;
4587 }
4588
Adam Cohen27772732013-11-11 14:00:56 +00004589 public boolean useVerticalBarLayout() {
4590 return LauncherAppState.getInstance().getDynamicGrid().
4591 getDeviceProfile().isVerticalBarLayout();
4592 }
4593
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004594 protected Rect getSearchBarBounds() {
4595 return LauncherAppState.getInstance().getDynamicGrid().
4596 getDeviceProfile().getSearchBarBounds();
4597 }
4598
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004599 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004600 if (mSearchDropTargetBar == null) {
4601 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004602 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004603 if (mQsb != null) {
4604 mSearchDropTargetBar.removeView(mQsb);
4605 mQsb = null;
4606 }
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08004607 getOrCreateQsbBar();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004608 }
4609
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004610 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004611 * Add the icons for all apps.
4612 *
4613 * Implementation of the method from LauncherModel.Callbacks.
4614 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004615 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004616 if (mAppsCustomizeContent != null) {
4617 mAppsCustomizeContent.setApps(apps);
4618 mAppsCustomizeContent.onPackagesUpdated(
4619 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -07004620 }
Adam Cohen9211d422014-10-07 18:14:53 -07004621 if (mLauncherCallbacks != null) {
4622 mLauncherCallbacks.bindAllApplications(apps);
4623 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004624 }
4625
4626 /**
4627 * A package was updated.
4628 *
4629 * Implementation of the method from LauncherModel.Callbacks.
4630 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004631 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004632 Runnable r = new Runnable() {
4633 public void run() {
4634 bindAppsUpdated(apps);
4635 }
4636 };
4637 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004638 return;
4639 }
4640
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004641 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004642 mAppsCustomizeContent.updateApps(apps);
4643 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004644 }
4645
Sunny Goyal4390ace2014-10-13 11:33:11 -07004646 @Override
4647 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4648 Runnable r = new Runnable() {
4649 public void run() {
4650 bindWidgetsRestored(widgets);
4651 }
4652 };
4653 if (waitUntilResume(r)) {
4654 return;
4655 }
4656 mWorkspace.widgetsRestored(widgets);
4657 }
4658
Joe Onorato9c1289c2009-08-17 11:03:03 -04004659 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004660 * Some shortcuts were updated in the background.
4661 *
4662 * Implementation of the method from LauncherModel.Callbacks.
4663 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004664 @Override
4665 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4666 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004667 Runnable r = new Runnable() {
4668 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004669 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004670 }
4671 };
4672 if (waitUntilResume(r)) {
4673 return;
4674 }
4675
Sunny Goyal4390ace2014-10-13 11:33:11 -07004676 if (!updated.isEmpty()) {
4677 mWorkspace.updateShortcuts(updated);
4678 }
4679
4680 if (!removed.isEmpty()) {
4681 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4682 for (ShortcutInfo si : removed) {
4683 removedComponents.add(si.getTargetComponent());
4684 }
4685 mWorkspace.removeItemsByComponentName(removedComponents, user);
4686 // Notify the drag controller
4687 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004688 }
4689 }
4690
4691 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004692 * Update the state of a package, typically related to install state.
4693 *
4694 * Implementation of the method from LauncherModel.Callbacks.
4695 */
Sunny Goyale755d462014-07-22 13:48:29 -07004696 @Override
4697 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004698 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004699 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004700 }
4701 }
4702
4703 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004704 * Update the label and icon of all the icons in a package
4705 *
4706 * Implementation of the method from LauncherModel.Callbacks.
4707 */
4708 @Override
4709 public void updatePackageBadge(String packageName) {
4710 if (mWorkspace != null) {
4711 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4712 }
4713 }
4714
4715 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004716 * A package was uninstalled. We take both the super set of packageNames
4717 * in addition to specific applications to remove, the reason being that
4718 * this can be called when a package is updated as well. In that scenario,
4719 * we only remove specific components from the workspace, where as
4720 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004721 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004722 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004723 * Implementation of the method from LauncherModel.Callbacks.
4724 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004725 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004726 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004727 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004728 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004729 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004730 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004731 }
Winson Chungd64d1762013-08-20 14:37:16 -07004732 };
4733 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004734 return;
4735 }
4736
Sunny Goyal1a745e82014-10-02 15:58:31 -07004737 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004738 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4739 for (AppInfo info : appInfos) {
4740 removedComponents.add(info.componentName);
4741 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004742 if (!packageNames.isEmpty()) {
4743 mWorkspace.removeItemsByPackageName(packageNames, user);
4744 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004745 if (!removedComponents.isEmpty()) {
4746 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004747 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004748 // Notify the drag controller
4749 mDragController.onAppsRemoved(packageNames, removedComponents);
4750
Sunny Goyal1a745e82014-10-02 15:58:31 -07004751 } else {
4752 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004753 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004754
Winson Chungdf95eb12013-10-16 14:57:07 -07004755 // Update AllApps
Sunny Goyalc9acdd52015-02-26 12:34:42 -08004756 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004757 mAppsCustomizeContent.removeApps(appInfos);
4758 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004759 }
Joe Onoratobe386092009-11-17 17:32:16 -08004760
4761 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004762 * A number of packages were updated.
4763 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004764 private ArrayList<Object> mWidgetsAndShortcuts;
4765 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004766 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004767 bindPackagesUpdated(mWidgetsAndShortcuts);
4768 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004769 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004770 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004771 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4772 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4773 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004774 return;
4775 }
4776
Winson Chung64359a52013-07-08 17:17:08 -07004777 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004778 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004779 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004780 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004781 }
4782
Winson Chung400438b2011-01-16 17:53:48 -08004783 private int mapConfigurationOriActivityInfoOri(int configOri) {
4784 final Display d = getWindowManager().getDefaultDisplay();
4785 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4786 switch (d.getRotation()) {
4787 case Surface.ROTATION_0:
4788 case Surface.ROTATION_180:
4789 // We are currently in the same basic orientation as the natural orientation
4790 naturalOri = configOri;
4791 break;
4792 case Surface.ROTATION_90:
4793 case Surface.ROTATION_270:
4794 // We are currently in the other basic orientation to the natural orientation
4795 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4796 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4797 break;
4798 }
4799
4800 int[] oriMap = {
4801 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4802 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4803 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4804 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4805 };
4806 // Since the map starts at portrait, we need to offset if this device's natural orientation
4807 // is landscape.
4808 int indexOffset = 0;
4809 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4810 indexOffset = 1;
4811 }
4812 return oriMap[(d.getRotation() + indexOffset) % 4];
4813 }
Adam Cohen446e9402011-09-15 18:21:21 -07004814
Winson Chung4b919f82012-05-01 10:44:08 -07004815 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004816 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004817 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4818 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4819 .getConfiguration().orientation));
4820 } else {
4821 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4822 }
Winson Chung4b919f82012-05-01 10:44:08 -07004823 }
4824 }
4825 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004826 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004827 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004828 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004829 } else {
4830 mHandler.postDelayed(new Runnable() {
4831 public void run() {
4832 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4833 }
4834 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004835 }
Winson Chung4b919f82012-05-01 10:44:08 -07004836 }
Winson Chung400438b2011-01-16 17:53:48 -08004837 }
4838
Winson Chunge43a1e72014-01-15 10:33:02 -08004839 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004840 if (mLauncherCallbacks != null) {
4841 return mLauncherCallbacks.isLauncherPreinstalled();
4842 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004843 PackageManager pm = getPackageManager();
4844 try {
4845 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4846 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4847 return true;
4848 } else {
4849 return false;
4850 }
4851 } catch (NameNotFoundException e) {
4852 e.printStackTrace();
4853 return false;
4854 }
4855 }
4856
Adam Cohenea90f832014-05-21 15:03:34 -07004857 /**
4858 * This method indicates whether or not we should suggest default wallpaper dimensions
4859 * when our wallpaper cropper was not yet used to set a wallpaper.
4860 */
4861 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004862 if (mLauncherCallbacks != null) {
4863 return mLauncherCallbacks.overrideWallpaperDimensions();
4864 }
Adam Cohenea90f832014-05-21 15:03:34 -07004865 return true;
4866 }
4867
Adam Cohen432609a2014-03-13 17:03:22 -07004868 /**
4869 * To be overridden by subclasses to indicate that there is an activity to launch
4870 * before showing the standard launcher experience.
4871 */
4872 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004873 if (mLauncherCallbacks != null) {
4874 return mLauncherCallbacks.hasFirstRunActivity();
4875 }
Adam Cohen432609a2014-03-13 17:03:22 -07004876 return false;
4877 }
4878
4879 /**
4880 * To be overridden by subclasses to launch any first run activity
4881 */
4882 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004883 if (mLauncherCallbacks != null) {
4884 return mLauncherCallbacks.getFirstRunActivity();
4885 }
Adam Cohen432609a2014-03-13 17:03:22 -07004886 return null;
4887 }
4888
Winson Chunga6945242014-01-08 14:04:34 -08004889 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004890 return !ActivityManager.isRunningInTestHarness() &&
4891 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004892 }
4893
Adam Cohen4a9423d2014-03-28 14:22:31 -07004894 protected boolean hasRunFirstRunActivity() {
4895 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4896 }
4897
Adam Cohen432609a2014-03-13 17:03:22 -07004898 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004899 if (shouldRunFirstRunActivity() &&
4900 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004901 Intent firstRunIntent = getFirstRunActivity();
4902 if (firstRunIntent != null) {
4903 startActivity(firstRunIntent);
4904 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004905 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004906 }
4907 }
Adam Cohen432609a2014-03-13 17:03:22 -07004908 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004909 }
4910
4911 private void markFirstRunActivityShown() {
4912 SharedPreferences.Editor editor = mSharedPrefs.edit();
4913 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4914 editor.apply();
4915 }
4916
Adam Cohen432609a2014-03-13 17:03:22 -07004917 /**
4918 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4919 * screen that must be displayed and dismissed.
4920 */
4921 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004922 if (mLauncherCallbacks != null) {
4923 return mLauncherCallbacks.hasDismissableIntroScreen();
4924 }
Adam Cohen432609a2014-03-13 17:03:22 -07004925 return false;
4926 }
4927
4928 /**
4929 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4930 */
4931 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004932 if (mLauncherCallbacks != null) {
4933 return mLauncherCallbacks.getIntroScreen();
4934 }
Adam Cohen432609a2014-03-13 17:03:22 -07004935 return null;
4936 }
4937
4938 /**
4939 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4940 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4941 */
4942 private boolean shouldShowIntroScreen() {
4943 return hasDismissableIntroScreen() &&
4944 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4945 }
4946
4947 protected void showIntroScreen() {
4948 View introScreen = getIntroScreen();
4949 changeWallpaperVisiblity(false);
4950 if (introScreen != null) {
4951 mDragLayer.showOverlayView(introScreen);
4952 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004953 if (mLauncherOverlayContainer != null) {
4954 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4955 }
Adam Cohen432609a2014-03-13 17:03:22 -07004956 }
4957
4958 public void dismissIntroScreen() {
4959 markIntroScreenDismissed();
4960 if (showFirstRunActivity()) {
4961 // We delay hiding the intro view until the first run activity is showing. This
4962 // avoids a blip.
4963 mWorkspace.postDelayed(new Runnable() {
4964 @Override
4965 public void run() {
4966 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004967 if (mLauncherOverlayContainer != null) {
4968 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4969 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004970 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004971 }
4972 }, ACTIVITY_START_DELAY);
4973 } else {
4974 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004975 if (mLauncherOverlayContainer != null) {
4976 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4977 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004978 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004979 }
4980 changeWallpaperVisiblity(true);
4981 }
4982
4983 private void markIntroScreenDismissed() {
4984 SharedPreferences.Editor editor = mSharedPrefs.edit();
4985 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4986 editor.apply();
4987 }
4988
Sunny Goyal88d60f12014-09-08 03:47:29 -07004989 private void showFirstRunClings() {
4990 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4991 // on the device, then we always show the first run cling experience (or if there is no
4992 // launcher2). Otherwise, we prompt the user upon started for migration
4993 LauncherClings launcherClings = new LauncherClings(this);
4994 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4995 if (mModel.canMigrateFromOldLauncherDb(this)) {
4996 launcherClings.showMigrationCling();
4997 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004998 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004999 }
5000 }
5001 }
5002
Winson Chunga6945242014-01-08 14:04:34 -08005003 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005004 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5005 if (mHotseat != null) mHotseat.setAlpha(1f);
5006 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5007 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005008 }
5009
5010 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005011 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5012 if (mHotseat != null) mHotseat.setAlpha(0f);
5013 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5014 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005015 }
5016
Mathew Inwood72fbec12013-11-19 15:45:07 +00005017 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005018 // Called from search suggestion, not supported in other profiles.
5019 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5020 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5021 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5022 myUser);
5023 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005024 return null;
5025 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08005026 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005027 }
5028
5029 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5030 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005031 // Called from search suggestion, not supported in other profiles.
5032 return createShortcutDragInfo(shortcutIntent, caption, icon,
5033 UserHandleCompat.myUserHandle());
5034 }
5035
5036 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5037 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005038 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005039 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005040 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005041 }
5042
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005043 protected void moveWorkspaceToDefaultScreen() {
5044 mWorkspace.moveToDefaultScreen(false);
5045 }
5046
Mathew Inwood72fbec12013-11-19 15:45:07 +00005047 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5048 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005049 mWorkspace.onExternalDragStartedWithItem(dragView);
5050 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005051 }
5052
Anjali Koppalf5d29132014-02-28 13:33:27 -08005053 @Override
5054 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005055 if (mLauncherCallbacks != null) {
5056 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5057 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005058 }
5059
Winson Chung80baf5a2010-08-09 16:03:15 -07005060 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005061 * Prints out out state for debugging.
5062 */
5063 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005064 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005065 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005066 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5067 Log.d(TAG, "mRestoring=" + mRestoring);
5068 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5069 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005070 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005071 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005072
Winson Chung785d2eb2011-04-14 16:08:02 -07005073 if (mAppsCustomizeContent != null) {
5074 mAppsCustomizeContent.dumpState();
5075 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005076 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005077 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005078
5079 @Override
5080 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5081 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005082 synchronized (sDumpLogs) {
5083 writer.println(" ");
5084 writer.println("Debug logs: ");
5085 for (int i = 0; i < sDumpLogs.size(); i++) {
5086 writer.println(" " + sDumpLogs.get(i));
5087 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005088 }
Adam Cohen9211d422014-10-07 18:14:53 -07005089 if (mLauncherCallbacks != null) {
5090 mLauncherCallbacks.dump(prefix, fd, writer, args);
5091 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005092 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005093
5094 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005095 if (DEBUG_DUMP_LOG) {
5096 synchronized (sDumpLogs) {
5097 Log.d(TAG, "");
5098 Log.d(TAG, "*********************");
5099 Log.d(TAG, "Launcher debug logs: ");
5100 for (int i = 0; i < sDumpLogs.size(); i++) {
5101 Log.d(TAG, " " + sDumpLogs.get(i));
5102 }
5103 Log.d(TAG, "*********************");
5104 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005105 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005106 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005107 }
5108
5109 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005110 addDumpLog(tag, log, null, debugLog);
5111 }
5112
5113 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005114 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005115 if (e != null) {
5116 Log.d(tag, log, e);
5117 } else {
5118 Log.d(tag, log);
5119 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005120 }
Winson Chungede41292013-09-19 16:27:36 -07005121 if (DEBUG_DUMP_LOG) {
5122 sDateStamp.setTime(System.currentTimeMillis());
5123 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005124 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5125 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005126 }
Winson Chungede41292013-09-19 16:27:36 -07005127 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005128 }
5129
Adam Cohen59400422014-03-05 18:07:04 -08005130 public static CustomAppWidget getCustomAppWidget(String name) {
5131 return sCustomAppWidgets.get(name);
5132 }
5133
5134 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
5135 return sCustomAppWidgets;
5136 }
5137
Winson Chungede41292013-09-19 16:27:36 -07005138 public void dumpLogsToLocalData() {
5139 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005140 new AsyncTask<Void, Void, Void>() {
5141 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005142 boolean success = false;
5143 sDateStamp.setTime(sRunStart);
5144 String FILENAME = sDateStamp.getMonth() + "-"
5145 + sDateStamp.getDay() + "_"
5146 + sDateStamp.getHours() + "-"
5147 + sDateStamp.getMinutes() + "_"
5148 + sDateStamp.getSeconds() + ".txt";
5149
5150 FileOutputStream fos = null;
5151 File outFile = null;
5152 try {
5153 outFile = new File(getFilesDir(), FILENAME);
5154 outFile.createNewFile();
5155 fos = new FileOutputStream(outFile);
5156 } catch (Exception e) {
5157 e.printStackTrace();
5158 }
5159 if (fos != null) {
5160 PrintWriter writer = new PrintWriter(fos);
5161
5162 writer.println(" ");
5163 writer.println("Debug logs: ");
5164 synchronized (sDumpLogs) {
5165 for (int i = 0; i < sDumpLogs.size(); i++) {
5166 writer.println(" " + sDumpLogs.get(i));
5167 }
5168 }
5169 writer.close();
5170 }
5171 try {
5172 if (fos != null) {
5173 fos.close();
5174 success = true;
5175 }
5176 } catch (IOException e) {
5177 e.printStackTrace();
5178 }
Michael Jurka43467462013-11-14 12:03:58 +01005179 return null;
Winson Chungede41292013-09-19 16:27:36 -07005180 }
Michael Jurka43467462013-11-14 12:03:58 +01005181 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005182 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005183 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005184}
Michael Jurka2763be32011-02-24 11:19:57 -08005185
Michael Jurkaabded662011-03-04 12:06:57 -08005186interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005187 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005188 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005189 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005190 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005191 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005192}
Dan Sandlerd5024042014-01-09 15:01:33 -05005193
5194interface DebugIntents {
5195 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5196 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005197}