blob: a7e32d31abfb65bedb92ebab9966f620dd143e72 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Adam Cohen2854d252014-08-27 16:04:07 -070025import android.animation.TimeInterpolator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000027import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070029import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070053import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.Bitmap;
55import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070056import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen2854d252014-08-27 16:04:07 -070085import android.view.ViewAnimationUtils;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070087import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070089import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080091import android.view.accessibility.AccessibilityEvent;
Adam Cohen9bfdb762014-07-21 17:44:06 -070092import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Advanceable;
96import android.widget.FrameLayout;
97import android.widget.ImageView;
Adam Cohen96d30a12013-07-16 18:13:21 -070098import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070099
Sunny Goyal651077b2014-06-30 14:15:31 -0700100import com.android.launcher3.DropTarget.DragObject;
101import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherActivityInfoCompat;
104import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700105import com.android.launcher3.compat.PackageInstallerCompat;
106import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100107import com.android.launcher3.compat.UserHandleCompat;
108import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700109
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.io.DataInputStream;
111import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700113import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000118import java.lang.reflect.Field;
119import 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 Project31dd5032009-03-03 19:32:27 -0800149 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700151 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Michael Jurka8b805b12012-04-18 14:23:14 -0700153 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700154 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700155
Mathew Inwood876a8462013-06-14 14:12:41 +0100156 /**
157 * IntentStarter uses request codes starting with this. This must be greater than all activity
158 * request codes used internally.
159 */
160 protected static final int REQUEST_LAST = 100;
161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
163
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800164 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
Michael Jurka0a457bf2012-11-19 14:05:05 -0800166 // To turn on these properties, type
167 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800169 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Winson Chung2672ff92012-05-04 16:22:30 -0700171 // The Intent extra that defines whether to ignore the launch animation
172 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400173 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
176 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700177 // Type: int
178 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700180 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
181 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
183 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700184 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700186 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: boolean
188 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
189 // Type: long
190 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700191 // Type: int
192 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
193 // Type: int
194 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
195 // Type: parcelable
196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000197 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800198 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000199 // Type: int[]
200 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Adam Cohen432609a2014-03-13 17:03:22 -0700202 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800203 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
204
Adam Cohen3ed316a2014-07-23 18:21:20 -0700205 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
206 static final String ACTION_FIRST_LOAD_COMPLETE =
207 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
208
Adam Cohen39a06042013-07-19 14:30:12 -0700209 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700210 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700211
Sunny Goyal594d76d2014-11-06 10:12:54 -0800212 private static final String QSB_WIDGET_ID = "qsb_widget_id";
213 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
214
Winson Chunga6945242014-01-08 14:04:34 -0800215 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
216
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700217 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700218 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700219 private State mState = State.WORKSPACE;
220 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700221
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700222 private boolean mIsSafeModeEnabled;
223
Adam Cohenc2d6e892014-10-16 09:49:24 -0700224 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
225 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700226 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700227
Joe Onorato9c1289c2009-08-17 11:03:03 -0400228 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700229 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
230 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700231 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private static final Object sLock = new Object();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000235 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
236 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
237
Winson Chunga2413752012-04-03 14:22:34 -0700238 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700239 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
240 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700241 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700242 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700243
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800244 private final BroadcastReceiver mCloseSystemDialogsReceiver
245 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800246 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private LayoutInflater mInflater;
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700251 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800252 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800253 private DragLayer mDragLayer;
254 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700255 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700256
Sunny Goyalffe83f12014-08-14 17:39:34 -0700257 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700258 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700259
Michael Jurkac9d95c52011-08-29 14:03:34 -0700260 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800261 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800262 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700263
Michael Jurka0280c3b2010-09-17 15:00:07 -0700264 private int[] mTmpAddItemCellCoordinates = new int[2];
265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 private FolderInfo mFolderInfo;
267
Winson Chung3d503fb2011-07-13 17:25:49 -0700268 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800269 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700270
Michael Jurka838a4ca2011-02-07 13:33:06 -0800271 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700272
Winson Chungc51db6a2011-10-05 11:44:49 -0700273 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700274 private AppsCustomizeTabHost mAppsCustomizeTabHost;
275 private AppsCustomizePagedView mAppsCustomizeContent;
276 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800277 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700280 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
281 // scroll issues (because the workspace may not have been measured yet) and extra work.
282 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
283 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284
285 private SpannableStringBuilder mDefaultKeySsb = null;
286
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287 private boolean mWorkspaceLoading = true;
288
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800289 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 private boolean mRestoring;
291 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700292 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293
Michael Jurka1e2f4652013-07-08 18:03:46 -0700294 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700295 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
296
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 private Bundle mSavedInstanceState;
298
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800300 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800301 private boolean mUserPresent = true;
302 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700303 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700306 private static LocaleConfiguration sLocaleConfiguration = null;
307
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700308 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700309
Adam Cohen61f560d2013-09-30 15:58:20 -0700310 private View.OnTouchListener mHapticFeedbackTouchListener;
311
Adam Cohen80e6beb2015-02-13 16:14:33 -0800312 public static final int BUILD_LAYER = 0;
313 public static final int BUILD_AND_SET_LAYER = 1;
314
Adam Cohended9f8d2010-11-03 13:25:16 -0700315 // Related to the auto-advancing of widgets
316 private final int ADVANCE_MSG = 1;
317 private final int mAdvanceInterval = 20000;
318 private final int mAdvanceStagger = 250;
319 private long mAutoAdvanceSentTime;
320 private long mAutoAdvanceTimeLeft = -1;
321 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
322 new HashMap<View, AppWidgetProviderInfo>();
323
Winson Chung400438b2011-01-16 17:53:48 -0800324 // Determines how long to wait after a rotation before restoring the screen orientation to
325 // match the sensor state.
326 private final int mRestoreScreenOrientationDelay = 500;
327
Craig Mautner360310b2012-10-26 15:13:08 -0700328 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700329
Adam Cohen1462de32012-07-24 22:34:36 -0700330 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700331 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700332
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700333 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700334 static Date sDateStamp = new Date();
335 static DateFormat sDateFormat =
336 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
337 static long sRunStart = System.currentTimeMillis();
338 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700339
Winson Chung46353de2012-02-16 14:05:10 -0800340 // We only want to get the SharedPreferences once since it does an FS stat each time we get
341 // it from the context.
342 private SharedPreferences mSharedPrefs;
343
Adam Cohene25af792013-06-06 23:08:25 -0700344 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
345
Winson Chungf0c6ae02012-03-21 16:10:31 -0700346 // Holds the page that we need to animate to, and the icon views that we need to animate up
347 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700348 private ImageView mFolderIconImageView;
349 private Bitmap mFolderIconBitmap;
350 private Canvas mFolderIconCanvas;
351 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700352
Michael Jurkaddd62e92011-02-16 17:49:14 -0800353 private BubbleTextView mWaitingForResume;
354
Adam Cohen59400422014-03-05 18:07:04 -0800355 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
356 new HashMap<String, CustomAppWidget>();
357
358 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
359 static {
360 if (ENABLE_CUSTOM_WIDGET_TEST) {
361 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
362 }
363 }
364
Michael Jurkac1f5d262011-09-30 19:32:27 -0700365 private Runnable mBuildLayersRunnable = new Runnable() {
366 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700367 if (mWorkspace != null) {
368 mWorkspace.buildPageHardwareLayers();
369 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700370 }
371 };
372
Adam Cohendb364c32014-05-20 14:23:40 -0700373 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800374
375 private static class PendingAddArguments {
376 int requestCode;
377 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700378 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700379 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800380 int cellX;
381 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700382 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800383 }
384
Daniel Sandlerff02d492013-08-05 02:12:05 -0400385 private Stats mStats;
386
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700387 FocusIndicatorView mFocusHandler;
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 @Override
390 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700391 if (DEBUG_STRICT_MODE) {
392 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
393 .detectDiskReads()
394 .detectDiskWrites()
395 .detectNetwork() // or .detectAll() for all detectable problems
396 .penaltyLog()
397 .build());
398 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
399 .detectLeakedSqlLiteObjects()
400 .detectLeakedClosableObjects()
401 .penaltyLog()
402 .penaltyDeath()
403 .build());
404 }
405
Adam Cohen9211d422014-10-07 18:14:53 -0700406 if (mLauncherCallbacks != null) {
407 mLauncherCallbacks.preOnCreate();
408 }
409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400411
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400412 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400413 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700414 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700415
Winson Chung5f8afe62013-08-12 16:19:28 -0700416 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700417 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700418
419 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400420 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700421 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700422 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800423 mModel = app.setLauncher(this);
424 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700425 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400426 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700428
Daniel Sandlerff02d492013-08-05 02:12:05 -0400429 mStats = new Stats(this);
430
Sunny Goyalffe83f12014-08-14 17:39:34 -0700431 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700432
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700433 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
434 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700435
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700436 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
437 // this also ensures that any synchronous binding below doesn't re-trigger another
438 // LauncherModel load.
439 mPaused = false;
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200442 android.os.Debug.startMethodTracing(
443 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 }
445
446 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100450 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800452 registerContentObservers();
453
Joe Onorato7c312c12009-08-13 21:36:53 -0700454 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700455
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 mSavedState = savedInstanceState;
457 restoreState(mSavedState);
458
459 if (PROFILE_STARTUP) {
460 android.os.Debug.stopMethodTracing();
461 }
462
463 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700464 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 // If the user leaves launcher, then we should just load items asynchronously when
466 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500467 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700468 } else {
469 // We only load the page synchronously if the user rotates (or triggers a
470 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800471 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700472 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 }
474
475 // For handling default keys
476 mDefaultKeySsb = new SpannableStringBuilder();
477 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800478
479 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
480 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800481
Adam Cohenf9426d52012-06-04 17:26:21 -0700482 // On large interfaces, we want the screen to auto-rotate based on the current orientation
483 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700484
Adam Cohen9211d422014-10-07 18:14:53 -0700485 if (mLauncherCallbacks != null) {
486 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700487 if (mLauncherCallbacks.hasLauncherOverlay()) {
488 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700489 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
490 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
491 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700492 mWorkspace.setLauncherOverlay(mLauncherOverlay);
493 }
Adam Cohen9211d422014-10-07 18:14:53 -0700494 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700495
496 if (shouldShowIntroScreen()) {
497 showIntroScreen();
498 } else {
499 showFirstRunActivity();
500 showFirstRunClings();
501 }
Adam Cohen9211d422014-10-07 18:14:53 -0700502 }
503
504 private LauncherCallbacks mLauncherCallbacks;
505
506 public void onPostCreate(Bundle savedInstanceState) {
507 super.onPostCreate(savedInstanceState);
508 if (mLauncherCallbacks != null) {
509 mLauncherCallbacks.onPostCreate(savedInstanceState);
510 }
511 }
512
513 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
514 mLauncherCallbacks = callbacks;
515 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700516 }
517
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700518 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700519 public void onLauncherProviderChange() {
520 if (mLauncherCallbacks != null) {
521 mLauncherCallbacks.onLauncherProviderChange();
522 }
523 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700524
Adam Cohen9211d422014-10-07 18:14:53 -0700525 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700526 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700527 if (mLauncherCallbacks != null) {
528 return mLauncherCallbacks.hasCustomContentToLeft();
529 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700530 return false;
531 }
532
Dave Hawkeya8881582013-09-17 15:55:33 -0600533 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500534 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600535 * {@link #addToCustomContentPage}. This will only be invoked if
536 * {@link #hasCustomContentToLeft()} is {@code true}.
537 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500538 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700539 if (mLauncherCallbacks != null) {
540 mLauncherCallbacks.populateCustomContentContainer();
541 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600542 }
543
544 /**
545 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
546 * ensure the custom content page is added or removed if necessary.
547 */
548 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600549 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600550 // Not bound yet, wait for bindScreens to be called.
551 return;
552 }
553
554 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
555 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500556 mWorkspace.createCustomContentContainer();
557 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600558 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
559 mWorkspace.removeCustomContentPage();
560 }
561 }
562
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700564 if (sLocaleConfiguration == null) {
565 new AsyncTask<Void, Void, LocaleConfiguration>() {
566 @Override
567 protected LocaleConfiguration doInBackground(Void... unused) {
568 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
569 readConfiguration(Launcher.this, localeConfiguration);
570 return localeConfiguration;
571 }
572
573 @Override
574 protected void onPostExecute(LocaleConfiguration result) {
575 sLocaleConfiguration = result;
576 checkForLocaleChange(); // recursive, but now with a locale configuration
577 }
578 }.execute();
579 return;
580 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700581
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 final Configuration configuration = getResources().getConfiguration();
583
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700584 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 final String locale = configuration.locale.toString();
586
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700587 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 final int mcc = configuration.mcc;
589
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700590 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 final int mnc = configuration.mnc;
592
Romain Guy84f296c2009-11-04 15:00:44 -0800593 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594
Romain Guy84f296c2009-11-04 15:00:44 -0800595 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700596 sLocaleConfiguration.locale = locale;
597 sLocaleConfiguration.mcc = mcc;
598 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700599
Joe Onorato0589f0f2010-02-08 13:44:00 -0800600 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700601
602 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100603 new AsyncTask<Void, Void, Void>() {
604 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700605 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100606 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700607 }
Michael Jurka43467462013-11-14 12:03:58 +0100608 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700609 }
610 }
611
612 private static class LocaleConfiguration {
613 public String locale;
614 public int mcc = -1;
615 public int mnc = -1;
616 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700617
Romain Guy98d01652009-06-30 16:21:04 -0700618 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
619 DataInputStream in = null;
620 try {
Helena Josol28db2802014-10-09 17:04:09 +0100621 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700622 configuration.locale = in.readUTF();
623 configuration.mcc = in.readInt();
624 configuration.mnc = in.readInt();
625 } catch (FileNotFoundException e) {
626 // Ignore
627 } catch (IOException e) {
628 // Ignore
629 } finally {
630 if (in != null) {
631 try {
632 in.close();
633 } catch (IOException e) {
634 // Ignore
635 }
636 }
637 }
638 }
639
640 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
641 DataOutputStream out = null;
642 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100643 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100644 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700645 out.writeUTF(configuration.locale);
646 out.writeInt(configuration.mcc);
647 out.writeInt(configuration.mnc);
648 out.flush();
649 } catch (FileNotFoundException e) {
650 // Ignore
651 } catch (IOException e) {
652 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100653 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700654 } finally {
655 if (out != null) {
656 try {
657 out.close();
658 } catch (IOException e) {
659 // Ignore
660 }
661 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
663 }
664
Anton Hanssona2f665f2013-09-26 12:18:32 +0100665 public Stats getStats() {
666 return mStats;
667 }
668
Bjorn Bringertc459e522013-06-07 19:36:01 +0100669 public LayoutInflater getInflater() {
670 return mInflater;
671 }
672
Winson Chung36a62fe2012-05-06 18:04:42 -0700673 boolean isDraggingEnabled() {
674 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
675 // that is subsequently removed from the workspace in startBinding().
676 return !mModel.isLoadingWorkspace();
677 }
678
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800679 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000680 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100681 if (Build.VERSION.SDK_INT >= 17) {
682 return View.generateViewId();
683 } else {
684 // View.generateViewId() is not available. The following fallback logic is a copy
685 // of its implementation.
686 for (;;) {
687 final int result = sNextGeneratedId.get();
688 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
689 int newValue = result + 1;
690 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
691 if (sNextGeneratedId.compareAndSet(result, newValue)) {
692 return result;
693 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000694 }
695 }
696 }
697
698 public int getViewIdForItem(ItemInfo info) {
699 // This cast is safe given the > 2B range for int.
700 int itemId = (int) info.id;
701 if (mItemIdToViewId.containsKey(itemId)) {
702 return mItemIdToViewId.get(itemId);
703 }
704 int viewId = generateViewId();
705 mItemIdToViewId.put(itemId, viewId);
706 return viewId;
707 }
708
709 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700710 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
711 * a configuration step, this allows the proper animations to run after other transitions.
712 */
Adam Cohendb364c32014-05-20 14:23:40 -0700713 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700714 long screenId = args.screenId;
715 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
716 // When the screen id represents an actual screen (as opposed to a rank) we make sure
717 // that the drop page actually exists.
718 screenId = ensurePendingDropLayoutExists(args.screenId);
719 }
Adam Cohendb364c32014-05-20 14:23:40 -0700720
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800721 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800722 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700723 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700724 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700725 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800726 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700727 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700728 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700729 case REQUEST_RECONFIGURE_APPWIDGET:
730 completeRestoreAppWidget(args.appWidgetId);
731 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800732 }
Michael Jurka27614d22012-04-02 06:40:22 -0700733 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
734 // if you turned the screen off and then back while in All Apps, Launcher would not
735 // return to the workspace. Clearing mAddInfo.container here fixes this issue
736 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700737 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800738 }
739
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800740 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700742 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700743 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700744 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800745 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700746
Adam Cohenad4e15c2013-10-17 16:21:35 -0700747 Runnable exitSpringLoaded = new Runnable() {
748 @Override
749 public void run() {
750 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
751 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
752 }
753 };
754
Michael Jurka8b805b12012-04-18 14:23:14 -0700755 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700757 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
758 if (resultCode == RESULT_CANCELED) {
759 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700760 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700761 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700762 } else if (resultCode == RESULT_OK) {
Sunny Goyal8f9a7872015-01-16 16:14:29 -0800763 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 }
766 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200767 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
768 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
769 mWorkspace.exitOverviewMode(false);
770 }
771 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200773
Adam Cohened66b2b2012-01-23 17:28:51 -0800774 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
775 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700776
Adam Cohendb364c32014-05-20 14:23:40 -0700777 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800778 // We have special handling for widgets
779 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800780 final int appWidgetId;
781 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
782 : -1;
783 if (widgetId < 0) {
784 appWidgetId = pendingAddWidgetId;
785 } else {
786 appWidgetId = widgetId;
787 }
788
Adam Cohenad4e15c2013-10-17 16:21:35 -0700789 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800790 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700791 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
792 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700793 result = RESULT_CANCELED;
794 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700795 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700796 @Override
797 public void run() {
798 exitSpringLoadedDragModeDelayed(false, 0, null);
799 }
800 };
Adam Cohendb364c32014-05-20 14:23:40 -0700801 if (workspaceLocked) {
802 // No need to remove the empty screen if we're mid-binding, as the
803 // the bind will not add the empty screen.
804 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
805 } else {
806 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
807 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
808 }
Winson Chung5aaab772012-04-27 15:24:02 -0700809 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700810 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700811 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
812 // When the screen id represents an actual screen (as opposed to a rank)
813 // we make sure that the drop page actually exists.
814 mPendingAddInfo.screenId =
815 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
816 }
Adam Cohendb364c32014-05-20 14:23:40 -0700817 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
818
819 dropLayout.setDropPending(true);
820 final Runnable onComplete = new Runnable() {
821 @Override
822 public void run() {
823 completeTwoStageWidgetDrop(resultCode, appWidgetId);
824 dropLayout.setDropPending(false);
825 }
826 };
827 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
828 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
829 } else {
830 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
831 mPendingAddInfo);
832 sPendingAddItem = args;
833 }
Winson Chung5aaab772012-04-27 15:24:02 -0700834 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800835 return;
836 }
837
Sunny Goyalff572272014-07-23 13:58:07 -0700838 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
839 if (resultCode == RESULT_OK) {
840 // Update the widget view.
841 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
842 pendingAddWidgetId, mPendingAddInfo);
843 if (workspaceLocked) {
844 sPendingAddItem = args;
845 } else {
846 completeAdd(args);
847 }
848 }
849 // Leave the widget in the pending state if the user canceled the configure.
850 return;
851 }
852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 // The pattern used here is that a user PICKs a specific application,
854 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700855
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
857 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700858 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700859 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
860 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800861 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700862 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800863 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700864 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700865 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
866 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 }
Adam Cohen00074722013-10-11 17:46:09 -0700868 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700869 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700870 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800872 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800873
874 }
875
876 @Override
877 protected void onActivityResult(
878 final int requestCode, final int resultCode, final Intent data) {
879 handleActivityResult(requestCode, resultCode, data);
880 if (mLauncherCallbacks != null) {
881 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
882 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800883 }
884
Adam Cohendb364c32014-05-20 14:23:40 -0700885 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
886 appWidgetId, ItemInfo info) {
887 PendingAddArguments args = new PendingAddArguments();
888 args.requestCode = requestCode;
889 args.intent = data;
890 args.container = info.container;
891 args.screenId = info.screenId;
892 args.cellX = info.cellX;
893 args.cellY = info.cellY;
894 args.appWidgetId = appWidgetId;
895 return args;
896 }
897
898 /**
899 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
900 *
901 * @param screenId the screen id to check
902 * @return the new screen, or screenId if it exists
903 */
904 private long ensurePendingDropLayoutExists(long screenId) {
905 CellLayout dropLayout =
906 (CellLayout) mWorkspace.getScreenWithId(screenId);
907 if (dropLayout == null) {
908 // it's possible that the add screen was removed because it was
909 // empty and a re-bind occurred
910 mWorkspace.addExtraEmptyScreen();
911 return mWorkspace.commitExtraEmptyScreen();
912 } else {
913 return screenId;
914 }
915 }
916
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700918 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700919 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800920 Runnable onCompleteRunnable = null;
921 int animationType = 0;
922
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700923 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800924 if (resultCode == RESULT_OK) {
925 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
926 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700927 mPendingAddWidgetInfo);
928 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 onCompleteRunnable = new Runnable() {
930 @Override
931 public void run() {
932 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700933 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700934 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
935 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800936 }
937 };
938 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800939 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800940 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700942 if (mDragLayer.getAnimatedView() != null) {
943 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
944 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
945 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700946 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700947 // The animated view may be null in the case of a rotation during widget configuration
948 onCompleteRunnable.run();
949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 }
951
952 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700953 protected void onStop() {
954 super.onStop();
955 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700956
957 if (mLauncherCallbacks != null) {
958 mLauncherCallbacks.onStop();
959 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700960 }
961
962 @Override
963 protected void onStart() {
964 super.onStart();
965 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700966
967 if (mLauncherCallbacks != null) {
968 mLauncherCallbacks.onStart();
969 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700970 }
971
972 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200974 long startTime = 0;
975 if (DEBUG_RESUME_TIME) {
976 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400977 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200978 }
Adam Cohen9211d422014-10-07 18:14:53 -0700979
980 if (mLauncherCallbacks != null) {
981 mLauncherCallbacks.preOnResume();
982 }
983
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700985
Winson Chung4a2afa32012-07-19 14:53:05 -0700986 // Restore the previous launcher state
987 if (mOnResumeState == State.WORKSPACE) {
988 showWorkspace(false);
989 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800990 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700991 }
992 mOnResumeState = State.NONE;
993
Craig Mautner360310b2012-10-26 15:13:08 -0700994 // Background was set to gradient in onPause(), restore to black if in all apps.
995 setWorkspaceBackground(mState == State.WORKSPACE);
996
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800997 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700998 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700999 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001000 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001001 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001002 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001004 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001005 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1006 // execute them here
1007 long startTimeCallbacks = 0;
1008 if (DEBUG_RESUME_TIME) {
1009 startTimeCallbacks = System.currentTimeMillis();
1010 }
1011
1012 if (mAppsCustomizeContent != null) {
1013 mAppsCustomizeContent.setBulkBind(true);
1014 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001015 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1016 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001017 }
1018 if (mAppsCustomizeContent != null) {
1019 mAppsCustomizeContent.setBulkBind(false);
1020 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001021 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001022 if (DEBUG_RESUME_TIME) {
1023 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1024 (System.currentTimeMillis() - startTimeCallbacks));
1025 }
Michael Jurka447bf842013-05-15 14:52:15 +02001026 }
Michael Jurka54554252013-08-01 12:52:23 +02001027 if (mOnResumeCallbacks.size() > 0) {
1028 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1029 mOnResumeCallbacks.get(i).run();
1030 }
1031 mOnResumeCallbacks.clear();
1032 }
Winson Chunge4e50662012-01-23 14:45:13 -08001033
1034 // Reset the pressed state of icons that were locked in the press state while activities
1035 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001036 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001037 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001038 mWaitingForResume.setStayPressed(false);
1039 }
Winson Chung82963d52013-10-09 11:20:57 -07001040
Adam Cohen06dff352012-06-01 17:17:08 -07001041 // It is possible that widgets can receive updates while launcher is not in the foreground.
1042 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1043 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1044 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001045 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001046
Winson Chung780fe592013-09-26 14:48:44 -07001047 // Process any items that were added while Launcher was away.
1048 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1049
Michael Jurka447bf842013-05-15 14:52:15 +02001050 if (DEBUG_RESUME_TIME) {
1051 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1052 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001053
1054 if (mWorkspace.getCustomContentCallbacks() != null) {
1055 // If we are resuming and the custom content is the current page, we call onShow().
1056 // It is also poassible that onShow will instead be called slightly after first layout
1057 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1058 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001059 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001060 }
1061 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001062 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001063 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001064
1065 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001066
1067 if (mLauncherCallbacks != null) {
1068 mLauncherCallbacks.onResume();
1069 }
Adam Cohen06dff352012-06-01 17:17:08 -07001070 }
1071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001073 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001074 // Ensure that items added to Launcher are queued until Launcher returns
1075 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001076 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001077
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001079 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001080 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001081 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001082
1083 // We call onHide() aggressively. The custom content callbacks should be able to
1084 // debounce excess onHide calls.
1085 if (mWorkspace.getCustomContentCallbacks() != null) {
1086 mWorkspace.getCustomContentCallbacks().onHide();
1087 }
Romain Guycbb89e42009-06-08 15:52:54 -07001088
Adam Cohen9211d422014-10-07 18:14:53 -07001089 if (mLauncherCallbacks != null) {
1090 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001091 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001092 }
1093
1094 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001095 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1096 // by a onResume or by scrolling otherwise.
1097 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001098
1099 // Custom content is completely hidden
1100 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001101
1102 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1103 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001104
1105 // Indicates whether the user is allowed to scroll away from the custom content.
1106 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001107 }
1108
Adam Cohenc2d6e892014-10-16 09:49:24 -07001109 public interface LauncherOverlay {
1110
1111 /**
1112 * Touch interaction leading to overscroll has begun
1113 */
1114 public void onScrollInteractionBegin();
1115
1116 /**
1117 * Touch interaction related to overscroll has ended
1118 */
1119 public void onScrollInteractionEnd();
1120
1121 /**
1122 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1123 * screen (or in the case of RTL, the rightmost screen).
1124 */
1125 public void onScrollChange(int progress, boolean rtl);
1126
1127 /**
1128 * Screen has stopped scrolling
1129 */
1130 public void onScrollSettled();
1131
1132 /**
1133 * This method can be called by the Launcher in order to force the LauncherOverlay
1134 * to exit fully immersive mode.
1135 */
1136 public void forceExitFullImmersion();
1137 }
1138
1139 public interface LauncherOverlayCallbacks {
1140 /**
1141 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1142 * however it doesn't modify any state within the launcher.
1143 */
1144 public boolean canEnterFullImmersion();
1145
1146 /**
1147 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1148 * eg. by occupying the full screen and handling all touch events.
1149 *
1150 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1151 * case, Launcher will modify any necessary state and assumes the overlay is
1152 * handling all interaction. If false, the LauncherOverlay should cancel any
1153 *
1154 */
1155 public boolean enterFullImmersion();
1156
1157 /**
1158 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1159 * full control over UI and state.
1160 */
1161 public void exitFullImmersion();
1162 }
1163
1164 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1165
1166 @Override
1167 public boolean canEnterFullImmersion() {
1168 return mState == State.WORKSPACE;
1169 }
1170
1171 @Override
1172 public boolean enterFullImmersion() {
1173 if (mState == State.WORKSPACE) {
1174 // When fully immersed, disregard any touches which fall through.
1175 mDragLayer.setBlockTouch(true);
1176 return true;
1177 }
1178 return false;
1179 }
1180
1181 @Override
1182 public void exitFullImmersion() {
1183 mDragLayer.setBlockTouch(false);
1184 }
1185 }
1186
Jorim Jaggid017f882014-01-14 17:08:48 -08001187 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001188 if (mLauncherCallbacks != null) {
1189 return mLauncherCallbacks.hasSettings();
1190 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001191 return false;
1192 }
1193
Adam Cohenbffe7452013-07-22 18:21:45 -07001194
Adam Cohen9211d422014-10-07 18:14:53 -07001195 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001196 CustomContentCallbacks callbacks, String description) {
1197 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001198 }
1199
Adam Cohenedb40762013-07-18 16:45:45 -07001200 // The custom content needs to offset its content to account for the QSB
1201 public int getTopOffsetForCustomContent() {
1202 return mWorkspace.getPaddingTop();
1203 }
1204
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001205 @Override
1206 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001208 if (mModel.isCurrentCallbacks(this)) {
1209 mModel.stopLoader();
1210 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001211 if (mAppsCustomizeContent != null) {
1212 mAppsCustomizeContent.surrender();
1213 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001214 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001215 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001216
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001217 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001218 @Override
1219 public void onWindowFocusChanged(boolean hasFocus) {
1220 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001221 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001222
1223 if (mLauncherCallbacks != null) {
1224 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1225 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001226 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 private boolean acceptFilter() {
1229 final InputMethodManager inputManager = (InputMethodManager)
1230 getSystemService(Context.INPUT_METHOD_SERVICE);
1231 return !inputManager.isFullscreenMode();
1232 }
1233
1234 @Override
1235 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001236 final int uniChar = event.getUnicodeChar();
1237 final boolean handled = super.onKeyDown(keyCode, event);
1238 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1239 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1241 keyCode, event);
1242 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001243 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001244 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001245 // showSearchDialog()
1246 // If there are multiple keystrokes before the search dialog takes focus,
1247 // onSearchRequested() will be called for every keystroke,
1248 // but it is idempotent, so it's fine.
1249 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 }
1251 }
1252
Joe Onorato8a9625e2010-01-28 15:55:35 -08001253 // Eat the long press event so the keyboard doesn't come up.
1254 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1255 return true;
1256 }
1257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 return handled;
1259 }
1260
Karl Rosaen138a0412009-04-23 19:00:21 -07001261 private String getTypedText() {
1262 return mDefaultKeySsb.toString();
1263 }
1264
1265 private void clearTypedText() {
1266 mDefaultKeySsb.clear();
1267 mDefaultKeySsb.clearSpans();
1268 Selection.setSelection(mDefaultKeySsb, 0);
1269 }
1270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001272 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1273 * State
1274 */
1275 private static State intToState(int stateOrdinal) {
1276 State state = State.WORKSPACE;
1277 final State[] stateValues = State.values();
1278 for (int i = 0; i < stateValues.length; i++) {
1279 if (stateValues[i].ordinal() == stateOrdinal) {
1280 state = stateValues[i];
1281 break;
1282 }
1283 }
1284 return state;
1285 }
1286
1287 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 * Restores the previous state, if it exists.
1289 *
1290 * @param savedState The previous state.
1291 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001292 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 private void restoreState(Bundle savedState) {
1294 if (savedState == null) {
1295 return;
1296 }
1297
Michael Jurka883f55b2010-10-21 15:47:14 -07001298 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001299 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001300 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001301 }
1302
Adam Cohen21cd0022013-10-09 18:57:02 -07001303 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1304 PagedView.INVALID_RESTORE_PAGE);
1305 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001306 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308
Winson Chung3d503fb2011-07-13 17:25:49 -07001309 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001310 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001311
Winson Chung3d503fb2011-07-13 17:25:49 -07001312 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1313 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001314 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001315 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1316 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001317 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1318 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1319 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001320 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001321 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 mRestoring = true;
1323 }
1324
1325 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1326 if (renameFolder) {
1327 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001328 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 mRestoring = true;
1330 }
Winson Chunga12a2502010-12-20 14:41:35 -08001331
Winson Chung785d2eb2011-04-14 16:08:02 -07001332 // Restore the AppsCustomize tab
1333 if (mAppsCustomizeTabHost != null) {
1334 String curTab = savedState.getString("apps_customize_currentTab");
1335 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001336 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001337 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001338 mAppsCustomizeContent.loadAssociatedPages(
1339 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001340 }
1341
Winson Chung5afbf7b2011-07-25 11:53:08 -07001342 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1343 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001344 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001345 mItemIdToViewId = (HashMap<Integer, Integer>)
1346 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 }
1348
1349 /**
1350 * Finds all the views we need and configure them properly.
1351 */
1352 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001353 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001354
Craig Mautner360310b2012-10-26 15:13:08 -07001355 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001356 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001357 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1358 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001359 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001360 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001361
John Spurlock77e1f472013-09-11 10:09:51 -04001362 mLauncherView.setSystemUiVisibility(
1363 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001364 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365
Winson Chung4c98d922011-05-31 16:50:48 -07001366 // Setup the drag layer
1367 mDragLayer.setup(this, dragController);
1368
Winson Chung3d503fb2011-07-13 17:25:49 -07001369 // Setup the hotseat
1370 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1371 if (mHotseat != null) {
1372 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001373 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001374 }
1375
Jorim Jaggid017f882014-01-14 17:08:48 -08001376 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001377 View widgetButton = findViewById(R.id.widget_button);
1378 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001379 @Override
1380 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001381 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001382 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001383 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001384 }
1385 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001386 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1387
1388 View wallpaperButton = findViewById(R.id.wallpaper_button);
1389 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001390 @Override
1391 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001392 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001393 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001394 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001395 }
1396 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001397 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1398
1399 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001400 if (hasSettings()) {
1401 settingsButton.setOnClickListener(new OnClickListener() {
1402 @Override
1403 public void onClick(View arg0) {
1404 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001405 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001406 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001407 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001408 });
1409 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1410 } else {
1411 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001412 }
1413
Adam Cohendbdff6b2013-09-18 19:09:15 -07001414 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001415
Winson Chung4c98d922011-05-31 16:50:48 -07001416 // Setup the workspace
1417 mWorkspace.setHapticFeedbackEnabled(false);
1418 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001419 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001420 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001421
Winson Chungf0ea4d32011-06-06 14:27:16 -07001422 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001423 mSearchDropTargetBar = (SearchDropTargetBar)
1424 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001425
Winson Chungf0ea4d32011-06-06 14:27:16 -07001426 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001427 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001428 mAppsCustomizeContent = (AppsCustomizePagedView)
1429 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1430 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001431
Winson Chung3d503fb2011-07-13 17:25:49 -07001432 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001433 dragController.setDragScoller(mWorkspace);
1434 dragController.setScrollView(mDragLayer);
1435 dragController.setMoveTarget(mWorkspace);
1436 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001437 if (mSearchDropTargetBar != null) {
1438 mSearchDropTargetBar.setup(this, dragController);
Sunny Goyal594d76d2014-11-06 10:12:54 -08001439 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001440 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001441
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001442 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001443 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001444 mWeightWatcher = new WeightWatcher(this);
1445 mWeightWatcher.setAlpha(0.5f);
1446 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001447 new FrameLayout.LayoutParams(
1448 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001449 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001450 Gravity.BOTTOM)
1451 );
Adam Cohen39a06042013-07-19 14:30:12 -07001452
1453 boolean show = shouldShowWeightWatcher();
1454 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001455 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 }
1457
1458 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001459 * Sets the all apps button. This method is called from {@link Hotseat}.
1460 */
1461 public void setAllAppsButton(View allAppsButton) {
1462 mAllAppsButton = allAppsButton;
1463 }
1464
1465 public View getAllAppsButton() {
1466 return mAllAppsButton;
1467 }
1468
1469 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 * Creates a view representing a shortcut.
1471 *
1472 * @param info The data structure describing the shortcut.
1473 *
1474 * @return A View inflated from R.layout.application.
1475 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001476 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001478 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
1480
1481 /**
1482 * Creates a view representing a shortcut inflated from the specified resource.
1483 *
1484 * @param layoutResId The id of the XML layout used to create the shortcut.
1485 * @param parent The group the shortcut belongs to.
1486 * @param info The data structure describing the shortcut.
1487 *
1488 * @return A View inflated from layoutResId.
1489 */
Adam Cohen59400422014-03-05 18:07:04 -08001490 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001491 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001492 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001494 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 return favorite;
1496 }
1497
1498 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 * Add a shortcut to the workspace.
1500 *
1501 * @param data The intent describing the shortcut.
1502 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001504 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001505 int cellY) {
1506 int[] cellXY = mTmpAddItemCellCoordinates;
1507 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001508 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001509
Michael Jurkad3ef3062010-11-23 16:23:58 -08001510 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001511
Sunny Goyal2350bc92014-10-14 16:42:54 -07001512 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001513 if (info == null) {
1514 return;
1515 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001516 final View view = createShortcut(info);
1517
Adam Cohenfbba09b2011-07-18 21:43:05 -07001518 // First we check if we already know the exact location where we want to add this item.
1519 if (cellX >= 0 && cellY >= 0) {
1520 cellXY[0] = cellX;
1521 cellXY[1] = cellY;
1522 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001523
1524 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001525 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001526 true, null,null)) {
1527 return;
1528 }
1529 DragObject dragObject = new DragObject();
1530 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001531 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1532 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001533 return;
1534 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001535 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001536 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001537 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001538 foundCellSpan = (result != null);
1539 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001540 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001541 }
1542
1543 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001544 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001545 return;
1546 }
1547
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
1550 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001551 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001552 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
1554 }
1555
Adam Cohen2f093b62012-04-30 18:59:53 -07001556 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1557 int minHeight) {
1558 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001559 // We want to account for the extra amount of padding that we are adding to the widget
1560 // to ensure that it gets the full amount of space that it has requested
1561 int requiredWidth = minWidth + padding.left + padding.right;
1562 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001563 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001564 }
1565
Adam Cohen2f093b62012-04-30 18:59:53 -07001566 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1567 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001568 }
1569
Adam Cohen2f093b62012-04-30 18:59:53 -07001570 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1571 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001572 }
1573
Adam Cohen2f093b62012-04-30 18:59:53 -07001574 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1575 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001576 }
1577
Adam Cohen2f093b62012-04-30 18:59:53 -07001578 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1579 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001580 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001581 }
1582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001584 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001586 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 */
Adam Cohen59400422014-03-05 18:07:04 -08001588 private void completeAddAppWidget(int appWidgetId, long container, long screenId,
1589 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1590
1591 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001592 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001593 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1594 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001595 }
Romain Guycbb89e42009-06-08 15:52:54 -07001596
Adam Cohen59400422014-03-05 18:07:04 -08001597 if (appWidgetInfo.isCustomWidget) {
1598 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001599 }
1600
Adam Cohen59400422014-03-05 18:07:04 -08001601 LauncherAppWidgetInfo launcherInfo;
1602 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1603 launcherInfo.spanX = info.spanX;
1604 launcherInfo.spanY = info.spanY;
1605 launcherInfo.minSpanX = info.minSpanX;
1606 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001607 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001608
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001610 container, screenId, info.cellX, info.cellY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611
1612 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001613 if (hostView == null) {
1614 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001615 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1616 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001617 } else {
1618 // The AppWidgetHostView has already been inflated and instantiated
1619 launcherInfo.hostView = hostView;
1620 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001622 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001623 launcherInfo.notifyWidgetSizeChanged(this);
1624
Adam Cohen59400422014-03-05 18:07:04 -08001625 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1626 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001627
1628 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001630 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 }
Romain Guycbb89e42009-06-08 15:52:54 -07001632
Adam Cohended9f8d2010-11-03 13:25:16 -07001633 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1634 @Override
1635 public void onReceive(Context context, Intent intent) {
1636 final String action = intent.getAction();
1637 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1638 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001639 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001640 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001641
Winson Chungc100e8e2011-08-09 16:02:43 -07001642 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001643 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001644 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001645 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001646 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001647 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1648 mUserPresent = true;
1649 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001650 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1651 mModel.resetLoadedState(false, true);
1652 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1653 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1654 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1655 mModel.resetLoadedState(false, true);
1656 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1657 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1658 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001659 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1660 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1661 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001662 }
1663 }
1664 };
1665
1666 @Override
1667 public void onAttachedToWindow() {
1668 super.onAttachedToWindow();
1669
1670 // Listen for broadcasts related to user-presence
1671 final IntentFilter filter = new IntentFilter();
1672 filter.addAction(Intent.ACTION_SCREEN_OFF);
1673 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001674 // For handling managed profiles
1675 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1676 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001677 if (ENABLE_DEBUG_INTENTS) {
1678 filter.addAction(DebugIntents.DELETE_DATABASE);
1679 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1680 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001681 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001682 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001683 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001684 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001685 mVisible = true;
1686 }
1687
Adam Cohen0b395352014-06-09 22:54:36 +00001688 /**
1689 * Sets up transparent navigation and status bars in LMP.
1690 * This method is a no-op for other platform versions.
1691 */
1692 @TargetApi(19)
1693 private void setupTransparentSystemBarsForLmp() {
1694 // TODO(sansid): use the APIs directly when compiling against L sdk.
1695 // Currently we use reflection to access the flags and the API to set the transparency
1696 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001697 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001698 try {
1699 getWindow().getAttributes().systemUiVisibility |=
1700 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1701 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1702 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1703 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1704 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1705 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1706 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1707 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1708
1709 Method setStatusBarColorMethod =
1710 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1711 Method setNavigationBarColorMethod =
1712 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1713 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1714 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1715 } catch (NoSuchFieldException e) {
1716 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1717 } catch (NoSuchMethodException ex) {
1718 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1719 } catch (IllegalAccessException e) {
1720 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1721 } catch (IllegalArgumentException e) {
1722 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1723 } catch (InvocationTargetException e) {
1724 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1725 } finally {}
1726 }
1727 }
1728
Adam Cohended9f8d2010-11-03 13:25:16 -07001729 @Override
1730 public void onDetachedFromWindow() {
1731 super.onDetachedFromWindow();
1732 mVisible = false;
1733
Adam Cohend113e0c2010-11-11 10:48:05 -08001734 if (mAttached) {
1735 unregisterReceiver(mReceiver);
1736 mAttached = false;
1737 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001738 updateRunning();
1739 }
1740
1741 public void onWindowVisibilityChanged(int visibility) {
1742 mVisible = visibility == View.VISIBLE;
1743 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001744 // The following code used to be in onResume, but it turns out onResume is called when
1745 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1746 // is a more appropriate event to handle
1747 if (mVisible) {
1748 mAppsCustomizeTabHost.onWindowVisible();
1749 if (!mWorkspaceLoading) {
1750 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001751 // We want to let Launcher draw itself at least once before we force it to build
1752 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001753 // apps is nice and speedy.
1754 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001755 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001756 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001757 if (mStarted) return;
1758 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001759 // We delay the layer building a bit in order to give
1760 // other message processing a time to run. In particular
1761 // this avoids a delay in hiding the IME if it was
1762 // currently shown, because doing that may involve
1763 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001764 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001765 final ViewTreeObserver.OnDrawListener listener = this;
1766 mWorkspace.post(new Runnable() {
1767 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001768 if (mWorkspace != null &&
1769 mWorkspace.getViewTreeObserver() != null) {
1770 mWorkspace.getViewTreeObserver().
1771 removeOnDrawListener(listener);
1772 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001773 }
1774 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001775 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001776 }
1777 });
1778 }
1779 clearTypedText();
1780 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001781 }
1782
1783 private void sendAdvanceMessage(long delay) {
1784 mHandler.removeMessages(ADVANCE_MSG);
1785 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1786 mHandler.sendMessageDelayed(msg, delay);
1787 mAutoAdvanceSentTime = System.currentTimeMillis();
1788 }
1789
1790 private void updateRunning() {
1791 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1792 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1793 mAutoAdvanceRunning = autoAdvanceRunning;
1794 if (autoAdvanceRunning) {
1795 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1796 sendAdvanceMessage(delay);
1797 } else {
1798 if (!mWidgetsToAdvance.isEmpty()) {
1799 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1800 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1801 }
1802 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001803 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001804 }
1805 }
1806 }
1807
1808 private final Handler mHandler = new Handler() {
1809 @Override
1810 public void handleMessage(Message msg) {
1811 if (msg.what == ADVANCE_MSG) {
1812 int i = 0;
1813 for (View key: mWidgetsToAdvance.keySet()) {
1814 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1815 final int delay = mAdvanceStagger * i;
1816 if (v instanceof Advanceable) {
1817 postDelayed(new Runnable() {
1818 public void run() {
1819 ((Advanceable) v).advance();
1820 }
1821 }, delay);
1822 }
1823 i++;
1824 }
1825 sendAdvanceMessage(mAdvanceInterval);
1826 }
1827 }
1828 };
1829
1830 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001831 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001832 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1833 if (v instanceof Advanceable) {
1834 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001835 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001836 updateRunning();
1837 }
1838 }
1839
1840 void removeWidgetToAutoAdvance(View hostView) {
1841 if (mWidgetsToAdvance.containsKey(hostView)) {
1842 mWidgetsToAdvance.remove(hostView);
1843 updateRunning();
1844 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001845 }
1846
Joe Onorato9c1289c2009-08-17 11:03:03 -04001847 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001848 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849 launcherInfo.hostView = null;
1850 }
1851
Winson Chung93eef082012-03-23 15:59:27 -07001852 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1853 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1854 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001855 }
1856
Adam Cohen59400422014-03-05 18:07:04 -08001857 public ArrayList<AppInfo> getAllAppsList() {
1858 return mAppsCustomizeContent.getApps();
1859 }
1860
Winson Chunga6945242014-01-08 14:04:34 -08001861 public DragLayer getDragLayer() {
1862 return mDragLayer;
1863 }
1864
1865 public Workspace getWorkspace() {
1866 return mWorkspace;
1867 }
1868
1869 public Hotseat getHotseat() {
1870 return mHotseat;
1871 }
1872
Jorim Jaggid017f882014-01-14 17:08:48 -08001873 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001874 return mOverviewPanel;
1875 }
1876
1877 public SearchDropTargetBar getSearchBar() {
1878 return mSearchDropTargetBar;
1879 }
1880
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001881 public LauncherAppWidgetHost getAppWidgetHost() {
1882 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 }
Romain Guycbb89e42009-06-08 15:52:54 -07001884
Winson Chunga9abd0e2010-10-27 17:18:37 -07001885 public LauncherModel getModel() {
1886 return mModel;
1887 }
1888
Winson Chunga6945242014-01-08 14:04:34 -08001889 protected SharedPreferences getSharedPrefs() {
1890 return mSharedPrefs;
1891 }
1892
Bjorn Bringertc459e522013-06-07 19:36:01 +01001893 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001894 getWindow().closeAllPanels();
1895
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001896 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001897 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001898 }
1899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 @Override
1901 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001902 long startTime = 0;
1903 if (DEBUG_RESUME_TIME) {
1904 startTime = System.currentTimeMillis();
1905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 super.onNewIntent(intent);
1907
1908 // Close the menu
1909 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001910 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001911 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001912
Adam Cohened307df2013-10-02 09:37:31 -07001913 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1914 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1915 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001916
Adam Cohen6fecd412013-10-02 17:41:50 -07001917 if (mWorkspace == null) {
1918 // Can be cases where mWorkspace is null, this prevents a NPE
1919 return;
1920 }
1921 Folder openFolder = mWorkspace.getOpenFolder();
1922 // In all these cases, only animate if we're already on home
1923 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001924
1925 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1926 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001927 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001928 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001929 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001930 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001931
Adam Cohen6fecd412013-10-02 17:41:50 -07001932 closeFolder();
1933 exitSpringLoadedDragMode();
1934
1935 // If we are already on home, then just animate back to the workspace,
1936 // otherwise, just wait until onResume to set the state back to Workspace
1937 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001938 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001939 } else {
1940 mOnResumeState = State.WORKSPACE;
1941 }
1942
1943 final View v = getWindow().peekDecorView();
1944 if (v != null && v.getWindowToken() != null) {
1945 InputMethodManager imm = (InputMethodManager)getSystemService(
1946 INPUT_METHOD_SERVICE);
1947 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1948 }
1949
1950 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001951 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001952 mAppsCustomizeTabHost.reset();
1953 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001954
Adam Cohen9211d422014-10-07 18:14:53 -07001955 if (mLauncherCallbacks != null) {
1956 mLauncherCallbacks.onHomeIntent();
1957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
Adam Cohened307df2013-10-02 09:37:31 -07001959
Michael Jurka447bf842013-05-15 14:52:15 +02001960 if (DEBUG_RESUME_TIME) {
1961 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963
Adam Cohen9211d422014-10-07 18:14:53 -07001964 if (mLauncherCallbacks != null) {
1965 mLauncherCallbacks.onNewIntent(intent);
1966 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001967 }
1968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001970 public void onRestoreInstanceState(Bundle state) {
1971 super.onRestoreInstanceState(state);
1972 for (int page: mSynchronouslyBoundPages) {
1973 mWorkspace.restoreInstanceStateForChild(page);
1974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 }
1976
1977 @Override
1978 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001979 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001980 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1981 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001982 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001983 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984
Michael Jurka883f55b2010-10-21 15:47:14 -07001985 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001986 // We close any open folder since it will not be re-opened, and we need to make sure
1987 // this state is reflected.
1988 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989
Adam Cohendcd297f2013-06-18 13:13:40 -07001990 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 mWaitingForResult) {
1992 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001993 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001994 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1995 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001996 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1997 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1998 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001999 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 }
2001
2002 if (mFolderInfo != null && mWaitingForResult) {
2003 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2004 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2005 }
Michael Jurka946ad472010-07-09 18:05:18 -07002006
Winson Chung785d2eb2011-04-14 16:08:02 -07002007 // Save the current AppsCustomize tab
2008 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08002009 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2010 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002011 if (currentTabTag != null) {
2012 outState.putString("apps_customize_currentTab", currentTabTag);
2013 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002014 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2015 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002016 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002017 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002018
2019 if (mLauncherCallbacks != null) {
2020 mLauncherCallbacks.onSaveInstanceState(outState);
2021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
2023
2024 @Override
2025 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002027
Winson Chunge7a03942011-08-05 15:05:12 -07002028 // Remove all pending runnables
2029 mHandler.removeMessages(ADVANCE_MSG);
2030 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002031 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002032
Winson Chungcd2b0142011-06-08 16:02:26 -07002033 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002034 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002035
2036 // It's possible to receive onDestroy after a new Launcher activity has
2037 // been created. In this case, don't interfere with the new Launcher.
2038 if (mModel.isCurrentCallbacks(this)) {
2039 mModel.stopLoader();
2040 app.setLauncher(null);
2041 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002044 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002046 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002048 mAppWidgetHost = null;
2049
2050 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051
2052 TextKeyListener.getInstance().release();
2053
Winson Chung81b52252012-08-27 15:34:29 -07002054 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2055 // to prevent leaking Launcher activities on orientation change.
2056 if (mModel != null) {
2057 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2058 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002059
2060 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002061 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002062
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002063 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002064 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002065 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002066 mWorkspace = null;
2067 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002068
Michael Jurka2ecf9952012-06-18 12:52:28 -07002069 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002070
2071 if (mLauncherCallbacks != null) {
2072 mLauncherCallbacks.onDestroy();
2073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
2075
Adam Cohena9cf38f2011-05-02 15:36:58 -07002076 public DragController getDragController() {
2077 return mDragController;
2078 }
2079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 @Override
2081 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002082 if (requestCode >= 0) {
2083 setWaitingForResult(true);
2084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 super.startActivityForResult(intent, requestCode);
2086 }
2087
Winson Chung70d72102011-08-12 11:24:35 -07002088 /**
2089 * Indicates that we want global search for this activity by setting the globalSearch
2090 * argument for {@link #startSearch} to true.
2091 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002093 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002095
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002096 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002097
Karl Rosaen138a0412009-04-23 19:00:21 -07002098 if (initialQuery == null) {
2099 // Use any text typed in the launcher as the initial query
2100 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002101 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 if (appSearchData == null) {
2103 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002104 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 }
Winson Chungadf0c182012-08-23 14:59:07 -07002106 Rect sourceBounds = new Rect();
2107 if (mSearchDropTargetBar != null) {
2108 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2109 }
Romain Guycbb89e42009-06-08 15:52:54 -07002110
Ian Parkinson047036e2014-09-01 15:40:53 +01002111 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002112 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002113 if (clearTextImmediately) {
2114 clearTypedText();
2115 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002116 }
2117
Ian Parkinson047036e2014-09-01 15:40:53 +01002118 /**
2119 * Start a text search.
2120 *
2121 * @return {@code true} if the search will start immediately, so any further keypresses
2122 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2123 * to buffer keypresses.
2124 */
2125 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002126 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002127 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2128 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2129 sourceBounds);
2130 }
2131
Michael Jurkaa33411c2012-06-14 16:18:21 -07002132 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002133 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002134 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002135 }
2136
2137 /**
2138 * Starts the global search activity. This code is a copied from SearchManager
2139 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002140 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002141 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002142 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002143 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2144 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2145 if (globalSearchActivity == null) {
2146 Log.w(TAG, "No global search activity found.");
2147 return;
2148 }
2149 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2150 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2151 intent.setComponent(globalSearchActivity);
2152 // Make sure that we have a Bundle to put source in
2153 if (appSearchData == null) {
2154 appSearchData = new Bundle();
2155 } else {
2156 appSearchData = new Bundle(appSearchData);
2157 }
Adam Cohen9211d422014-10-07 18:14:53 -07002158 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002159 if (!appSearchData.containsKey("source")) {
2160 appSearchData.putString("source", getPackageName());
2161 }
2162 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2163 if (!TextUtils.isEmpty(initialQuery)) {
2164 intent.putExtra(SearchManager.QUERY, initialQuery);
2165 }
2166 if (selectInitialQuery) {
2167 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2168 }
2169 intent.setSourceBounds(sourceBounds);
2170 try {
2171 startActivity(intent);
2172 } catch (ActivityNotFoundException ex) {
2173 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 }
2176
Yura4f93ec62014-02-04 14:15:21 +00002177 public boolean isOnCustomContent() {
2178 return mWorkspace.isOnOrMovingToCustomContent();
2179 }
2180
Winson Chung70d72102011-08-12 11:24:35 -07002181 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002182 public boolean onPrepareOptionsMenu(Menu menu) {
2183 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002184 if (!isOnCustomContent()) {
2185 // Close any open folders
2186 closeFolder();
2187 // Stop resizing any widgets
2188 mWorkspace.exitWidgetResizeMode();
2189 if (!mWorkspace.isInOverviewMode()) {
2190 // Show the overview mode
2191 showOverviewMode(true);
2192 } else {
2193 showWorkspace(true);
2194 }
Winson Chunge0298742014-01-17 12:03:00 -08002195 }
Adam Cohen9211d422014-10-07 18:14:53 -07002196 if (mLauncherCallbacks != null) {
2197 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2198 }
2199
Michael Jurkab94f3f82013-09-11 18:08:54 +02002200 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002201 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002203 @Override
2204 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002205 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002206 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002207 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002208 }
2209
Joe Onorato9c1289c2009-08-17 11:03:03 -04002210 public boolean isWorkspaceLocked() {
2211 return mWorkspaceLoading || mWaitingForResult;
2212 }
2213
Adam Cohen517a7f52014-03-01 12:12:59 -08002214 public boolean isWorkspaceLoading() {
2215 return mWorkspaceLoading;
2216 }
2217
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002218 private void setWorkspaceLoading(boolean value) {
2219 boolean isLocked = isWorkspaceLocked();
2220 mWorkspaceLoading = value;
2221 if (isLocked != isWorkspaceLocked()) {
2222 onWorkspaceLockedChanged();
2223 }
2224 }
2225
2226 private void setWaitingForResult(boolean value) {
2227 boolean isLocked = isWorkspaceLocked();
2228 mWaitingForResult = value;
2229 if (isLocked != isWorkspaceLocked()) {
2230 onWorkspaceLockedChanged();
2231 }
2232 }
2233
Adam Cohen9211d422014-10-07 18:14:53 -07002234 protected void onWorkspaceLockedChanged() {
2235 if (mLauncherCallbacks != null) {
2236 mLauncherCallbacks.onWorkspaceLockedChanged();
2237 }
2238 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002239
Michael Jurka0280c3b2010-09-17 15:00:07 -07002240 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002241 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002242 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002243 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2244 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002245 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002246 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002247 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002248
Sunny Goyal8f9a7872015-01-16 16:14:29 -08002249 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002250 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002251 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2252 }
2253
Sunny Goyal8f9a7872015-01-16 16:14:29 -08002254 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002255 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2256 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002257 if (appWidgetInfo.configure != null) {
2258 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002259 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002260
Bjorn Bringert7984c942009-12-09 15:38:25 +00002261 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002262 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2263 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002266 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002267 Runnable onComplete = new Runnable() {
2268 @Override
2269 public void run() {
2270 // Exit spring loaded mode if necessary after adding the widget
2271 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2272 null);
2273 }
2274 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002275 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002276 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002277 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 }
2279 }
Romain Guycbb89e42009-06-08 15:52:54 -07002280
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002281 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002282 // Close any folders that may be open.
2283 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002284 mWorkspace.moveToCustomContentScreen(animate);
2285 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002286
2287 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2288 int[] cell, int spanX, int spanY) {
2289 switch (info.itemType) {
2290 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2291 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2292 int span[] = new int[2];
2293 span[0] = spanX;
2294 span[1] = spanY;
2295 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2296 container, screenId, cell, span);
2297 break;
2298 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2299 processShortcutFromDrop(info.componentName, container, screenId, cell);
2300 break;
2301 default:
2302 throw new IllegalStateException("Unknown item type: " + info.itemType);
2303 }
2304 }
2305
Adam Cohenfbba09b2011-07-18 21:43:05 -07002306 /**
2307 * Process a shortcut drop.
2308 *
2309 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002310 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002311 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002312 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002313 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2314 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002315 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002316 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002317 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002318 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002319
2320 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002321 mPendingAddInfo.cellX = cell[0];
2322 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002323 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002324
2325 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2326 createShortcutIntent.setComponent(componentName);
2327 processShortcut(createShortcutIntent);
2328 }
2329
Adam Cohenfbba09b2011-07-18 21:43:05 -07002330 /**
2331 * Process a widget drop.
2332 *
2333 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002334 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002335 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002336 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002337 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2338 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002339 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002341 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002342 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002343 mPendingAddInfo.minSpanX = info.minSpanX;
2344 mPendingAddInfo.minSpanY = info.minSpanY;
2345
Adam Cohenfbba09b2011-07-18 21:43:05 -07002346 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002347 mPendingAddInfo.cellX = cell[0];
2348 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002349 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002350 if (span != null) {
2351 mPendingAddInfo.spanX = span[0];
2352 mPendingAddInfo.spanY = span[1];
2353 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002354
Adam Cohened66b2b2012-01-23 17:28:51 -08002355 AppWidgetHostView hostView = info.boundWidget;
2356 int appWidgetId;
2357 if (hostView != null) {
2358 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002359 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002360 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002361 // In this case, we either need to start an activity to get permission to bind
2362 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002363 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002364 Bundle options = info.bindOptions;
2365
Sunny Goyalffe83f12014-08-14 17:39:34 -07002366 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2367 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002368 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002369 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002370 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002371 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002372 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2373 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2374 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002375 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2376 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002377 // TODO: we need to make sure that this accounts for the options bundle.
2378 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002379 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2380 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002381 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002382 }
2383
Joe Onoratodeb98af2010-02-19 14:59:39 -08002384 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002385 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 }
2387
Winson Chung24ab2f12010-09-16 14:10:47 -07002388 void processWallpaper(Intent intent) {
2389 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2390 }
2391
Adam Cohendcd297f2013-06-18 13:13:40 -07002392 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002393 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002394 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002395 folderInfo.title = getText(R.string.folder_name);
2396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002398 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002399 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002400 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401
2402 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002403 FolderIcon newFolder =
2404 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002405 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002406 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002407 // Force measure the new folder icon
2408 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2409 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002410 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 }
Romain Guycbb89e42009-06-08 15:52:54 -07002412
Joe Onorato9c1289c2009-08-17 11:03:03 -04002413 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002414 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002415 }
2416
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002417 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002418 if (mLauncherCallbacks != null) {
2419 return mLauncherCallbacks.getWallpaperPickerComponent();
2420 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002421 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002422 }
2423
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002424 /**
2425 * Registers various content observers. The current implementation registers
2426 * only a favorites observer to keep track of the favorites applications.
2427 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002428 private void registerContentObservers() {
2429 ContentResolver resolver = getContentResolver();
2430 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2431 true, mWidgetObserver);
2432 }
2433
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 @Override
2435 public boolean dispatchKeyEvent(KeyEvent event) {
2436 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2437 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002439 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002440 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002441 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002442 dumpState();
2443 return true;
2444 }
2445 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002446 }
2447 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2448 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002449 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 return true;
2451 }
2452 }
2453
2454 return super.dispatchKeyEvent(event);
2455 }
2456
Joe Onorato88ec0992009-11-19 13:16:06 -08002457 @Override
2458 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002459 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2460 return;
2461 }
2462
Winson Chungc93e5ae2012-07-23 20:48:26 -07002463 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002464 if (mAppsCustomizeContent.getContentType() ==
2465 AppsCustomizePagedView.ContentType.Applications) {
2466 showWorkspace(true);
2467 } else {
2468 showOverviewMode(true);
2469 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002470 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002471 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002472 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002473 Folder openFolder = mWorkspace.getOpenFolder();
2474 if (openFolder.isEditingName()) {
2475 openFolder.dismissEditingName();
2476 } else {
2477 closeFolder();
2478 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002479 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002480 mWorkspace.exitWidgetResizeMode();
2481
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002482 // Back button is a no-op here, but give at least some feedback for the button press
2483 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002484 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002485 }
2486
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002488 * Re-listen when widgets are reset.
2489 */
2490 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002491 if (mAppWidgetHost != null) {
2492 mAppWidgetHost.startListening();
2493 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002494 }
2495
2496 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 * Launches the intent referred by the clicked shortcut.
2498 *
2499 * @param v The view representing the clicked shortcut.
2500 */
2501 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002502 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2503 // view has detached (it's possible for this to happen if the view is removed mid touch).
2504 if (v.getWindowToken() == null) {
2505 return;
2506 }
2507
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002508 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002509 return;
2510 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002511
Adam Cohen1697b792013-09-17 19:08:21 -07002512 if (v instanceof Workspace) {
2513 if (mWorkspace.isInOverviewMode()) {
2514 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002515 }
2516 return;
2517 }
2518
2519 if (v instanceof CellLayout) {
2520 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002521 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002522 }
2523 }
2524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002526 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002527 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002529 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002530 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002531 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002532 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002533 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002534 } else if (tag instanceof AppInfo) {
2535 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002536 } else if (tag instanceof LauncherAppWidgetInfo) {
2537 if (v instanceof PendingAppWidgetHostView) {
2538 onClickPendingWidget((PendingAppWidgetHostView) v);
2539 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 }
2541 }
2542
Sunny Goyal508da152014-08-14 10:53:27 -07002543 public void onClickPagedViewIcon(View v) {
2544 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002545 if (mLauncherCallbacks != null) {
2546 mLauncherCallbacks.onClickPagedViewIcon(v);
2547 }
Sunny Goyal508da152014-08-14 10:53:27 -07002548 }
2549
Michael Jurka0e260592010-06-30 17:07:39 -07002550 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002551 return false;
2552 }
2553
Michael Jurkaaf442092010-06-10 17:01:57 -07002554 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002555 * Event handler for the app widget view which has not fully restored.
2556 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002557 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002558 if (mIsSafeModeEnabled) {
2559 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2560 return;
2561 }
2562
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002563 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002564 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002565 int widgetId = info.appWidgetId;
2566 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2567 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002568 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2569 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002570 mPendingAddInfo.copyFrom(info);
2571 mPendingAddWidgetId = widgetId;
2572
Sunny Goyalffe83f12014-08-14 17:39:34 -07002573 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2574 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002575 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002576 } else if (info.installProgress < 0) {
2577 // The install has not been queued
2578 final String packageName = info.providerName.getPackageName();
2579 showBrokenAppInstallDialog(packageName,
2580 new DialogInterface.OnClickListener() {
2581 public void onClick(DialogInterface dialog, int id) {
2582 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2583 }
2584 });
2585 } else {
2586 // Download has started.
2587 final String packageName = info.providerName.getPackageName();
2588 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002589 }
2590 }
2591
2592 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002593 * Event handler for the "grid" button that appears on the home screen, which
2594 * enters all apps mode.
2595 *
2596 * @param v The view that was clicked.
2597 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002598 protected void onClickAllAppsButton(View v) {
2599 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2600 if (isAllAppsVisible()) {
2601 showWorkspace(true);
2602 } else {
2603 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2604 }
Adam Cohen9211d422014-10-07 18:14:53 -07002605 if (mLauncherCallbacks != null) {
2606 mLauncherCallbacks.onClickAllAppsButton(v);
2607 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002608 }
2609
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002610 private void showBrokenAppInstallDialog(final String packageName,
2611 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002612 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002613 .setTitle(R.string.abandoned_promises_title)
2614 .setMessage(R.string.abandoned_promise_explanation)
2615 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2616 .setNeutralButton(R.string.abandoned_clean_this,
2617 new DialogInterface.OnClickListener() {
2618 public void onClick(DialogInterface dialog, int id) {
2619 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2620 mWorkspace.removeAbandonedPromise(packageName, user);
2621 }
2622 })
2623 .create().show();
2624 return;
2625 }
2626
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002627 /**
2628 * Event handler for an app shortcut click.
2629 *
2630 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2631 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002632 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002633 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2634 Object tag = v.getTag();
2635 if (!(tag instanceof ShortcutInfo)) {
2636 throw new IllegalArgumentException("Input must be a Shortcut");
2637 }
2638
2639 // Open shortcut
2640 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002641
2642 if (shortcut.isDisabled != 0) {
2643 int error = R.string.activity_not_available;
2644 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2645 error = R.string.safemode_shortcut_error;
2646 }
2647 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2648 return;
2649 }
2650
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002651 final Intent intent = shortcut.intent;
2652
2653 // Check for special shortcuts
2654 if (intent.getComponent() != null) {
2655 final String shortcutClass = intent.getComponent().getClassName();
2656
2657 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2658 MemoryDumpActivity.startDump(this);
2659 return;
2660 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2661 toggleShowWeightWatcher();
2662 return;
2663 }
2664 }
2665
Chris Wren40c5ed32014-06-24 18:24:23 -04002666 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002667 if ((v instanceof BubbleTextView)
2668 && shortcut.isPromise()
2669 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002670 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002671 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002672 new DialogInterface.OnClickListener() {
2673 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002674 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002675 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002676 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002677 return;
2678 }
2679
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002680 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002681 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002682
2683 if (mLauncherCallbacks != null) {
2684 mLauncherCallbacks.onClickAppShortcut(v);
2685 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 }
2687
Sunny Goyal508da152014-08-14 10:53:27 -07002688 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002689 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002690 final ShortcutInfo shortcut;
2691 final Intent intent;
2692 if (tag instanceof ShortcutInfo) {
2693 shortcut = (ShortcutInfo) tag;
2694 intent = shortcut.intent;
2695 int[] pos = new int[2];
2696 v.getLocationOnScreen(pos);
2697 intent.setSourceBounds(new Rect(pos[0], pos[1],
2698 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002699
Sunny Goyal508da152014-08-14 10:53:27 -07002700 } else if (tag instanceof AppInfo) {
2701 shortcut = null;
2702 intent = ((AppInfo) tag).intent;
2703 } else {
2704 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2705 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002706
2707 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002708 mStats.recordLaunch(intent, shortcut);
2709
2710 if (success && v instanceof BubbleTextView) {
2711 mWaitingForResume = (BubbleTextView) v;
2712 mWaitingForResume.setStayPressed(true);
2713 }
2714 }
2715
2716 /**
2717 * Event handler for a folder icon click.
2718 *
2719 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2720 */
2721 protected void onClickFolderIcon(View v) {
2722 if (LOGD) Log.d(TAG, "onClickFolder");
2723 if (!(v instanceof FolderIcon)){
2724 throw new IllegalArgumentException("Input must be a FolderIcon");
2725 }
2726
2727 FolderIcon folderIcon = (FolderIcon) v;
2728 final FolderInfo info = folderIcon.getFolderInfo();
2729 Folder openFolder = mWorkspace.getFolderForTag(info);
2730
2731 // If the folder info reports that the associated folder is open, then verify that
2732 // it is actually opened. There have been a few instances where this gets out of sync.
2733 if (info.opened && openFolder == null) {
2734 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2735 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2736 info.opened = false;
2737 }
2738
2739 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2740 // Close any open folder
2741 closeFolder();
2742 // Open the requested folder
2743 openFolder(folderIcon);
2744 } else {
2745 // Find the open folder...
2746 int folderScreen;
2747 if (openFolder != null) {
2748 folderScreen = mWorkspace.getPageForView(openFolder);
2749 // .. and close it
2750 closeFolder(openFolder);
2751 if (folderScreen != mWorkspace.getCurrentPage()) {
2752 // Close any folder open on the current screen
2753 closeFolder();
2754 // Pull the folder onto this screen
2755 openFolder(folderIcon);
2756 }
2757 }
2758 }
Adam Cohen9211d422014-10-07 18:14:53 -07002759
2760 if (mLauncherCallbacks != null) {
2761 mLauncherCallbacks.onClickFolderIcon(v);
2762 }
Michael Jurka5130e402011-10-13 04:55:35 -07002763 }
2764
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002765 /**
2766 * Event handler for the (Add) Widgets button that appears after a long press
2767 * on the home screen.
2768 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002769 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002770 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002771 if (mIsSafeModeEnabled) {
2772 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2773 } else {
2774 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2775 if (mLauncherCallbacks != null) {
2776 mLauncherCallbacks.onClickAddWidgetButton(view);
2777 }
Adam Cohen9211d422014-10-07 18:14:53 -07002778 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002779 }
2780
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002781 /**
2782 * Event handler for the wallpaper picker button that appears after a long press
2783 * on the home screen.
2784 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002785 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002786 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2787 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2788 pickWallpaper.setComponent(getWallpaperPickerComponent());
2789 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002790
2791 if (mLauncherCallbacks != null) {
2792 mLauncherCallbacks.onClickWallpaperPicker(v);
2793 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002794 }
2795
2796 /**
2797 * Event handler for a click on the settings button that appears after a long press
2798 * on the home screen.
2799 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002800 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002801 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002802 if (mLauncherCallbacks != null) {
2803 mLauncherCallbacks.onClickSettingsButton(v);
2804 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002805 }
2806
Michael Jurka5130e402011-10-13 04:55:35 -07002807 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002808 // Provide the same haptic feedback that the system offers for virtual keys.
2809 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002810 }
2811
Adam Cohen61f560d2013-09-30 15:58:20 -07002812 public void performHapticFeedbackOnTouchDown(View v) {
2813 // Provide the same haptic feedback that the system offers for virtual keys.
2814 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2815 }
2816
2817 public View.OnTouchListener getHapticFeedbackTouchListener() {
2818 if (mHapticFeedbackTouchListener == null) {
2819 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2820 @Override
2821 public boolean onTouch(View v, MotionEvent event) {
2822 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2823 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2824 }
2825 return false;
2826 }
2827 };
2828 }
2829 return mHapticFeedbackTouchListener;
2830 }
2831
Adam Cohen9211d422014-10-07 18:14:53 -07002832 public void onDragStarted(View view) {
2833 if (isOnCustomContent()) {
2834 // Custom content screen doesn't participate in drag and drop. If on custom
2835 // content screen, move to default.
2836 moveWorkspaceToDefaultScreen();
2837 }
2838
2839 if (mLauncherCallbacks != null) {
2840 mLauncherCallbacks.onDragStarted(view);
2841 }
2842 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002843
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002844 /**
2845 * Called when the user stops interacting with the launcher.
2846 * This implies that the user is now on the homescreen and is not doing housekeeping.
2847 */
Adam Cohen9211d422014-10-07 18:14:53 -07002848 protected void onInteractionEnd() {
2849 if (mLauncherCallbacks != null) {
2850 mLauncherCallbacks.onInteractionEnd();
2851 }
2852 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002853
2854 /**
2855 * Called when the user starts interacting with the launcher.
2856 * The possible interactions are:
2857 * - open all apps
2858 * - reorder an app shortcut, or a widget
2859 * - open the overview mode.
2860 * This is a good time to stop doing things that only make sense
2861 * when the user is on the homescreen and not doing housekeeping.
2862 */
Adam Cohen9211d422014-10-07 18:14:53 -07002863 protected void onInteractionBegin() {
2864 if (mLauncherCallbacks != null) {
2865 mLauncherCallbacks.onInteractionBegin();
2866 }
2867 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002868
Kenny Guyf07af7b2014-07-31 11:39:16 +01002869 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002870 try {
2871 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002872 launcherApps.showAppDetailsForProfile(componentName, user);
2873 } catch (SecurityException e) {
2874 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2875 Log.e(TAG, "Launcher does not have permission to launch settings");
2876 } catch (ActivityNotFoundException e) {
2877 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2878 Log.e(TAG, "Unable to launch settings");
2879 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002880 }
2881
Michael Jurka1e2f4652013-07-08 18:03:46 -07002882 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002883 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2884 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002885 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002886 // System applications cannot be installed. For now, show a toast explaining that.
2887 // We may give them the option of disabling apps this way.
2888 int messageId = R.string.uninstall_system_app_text;
2889 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002890 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002891 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002892 String packageName = componentName.getPackageName();
2893 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002894 Intent intent = new Intent(
2895 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002896 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2897 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002898 if (user != null) {
2899 user.addToIntent(intent, Intent.EXTRA_USER);
2900 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002901 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002902 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002903 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002904 }
2905
Michael Jurka86a720e2012-05-09 11:23:23 -07002906 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002907 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002908 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002909 // Only launch using the new animation if the shortcut has not opted out (this is a
2910 // private contract between launcher and may be ignored in the future).
2911 boolean useLaunchAnimation = (v != null) &&
2912 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002913 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2914 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002915
Kenny Guy1317e2d2014-05-08 18:52:50 +01002916 UserHandleCompat user = null;
2917 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2918 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2919 user = userManager.getUserForSerialNumber(serialNumber);
2920 }
2921
2922 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002923 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002924 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002925 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2926 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002927 optsBundle = opts.toBundle();
2928 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002929
2930 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2931 // Could be launching some bookkeeping activity
2932 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002933 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002934 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002935 launcherApps.startActivityForProfile(intent.getComponent(), user,
2936 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002937 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002938 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939 } catch (SecurityException e) {
2940 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002941 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002942 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002943 "or use the exported attribute for this activity. "
2944 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002945 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002946 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002947 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002948
Michael Jurka86a720e2012-05-09 11:23:23 -07002949 boolean startActivitySafely(View v, Intent intent, Object tag) {
2950 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002951 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2952 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2953 return false;
2954 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002955 try {
2956 success = startActivity(v, intent, tag);
2957 } catch (ActivityNotFoundException e) {
2958 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2959 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2960 }
2961 return success;
2962 }
2963
Adam Cohen268c4752012-06-06 17:47:33 -07002964 /**
2965 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2966 * in the DragLayer in the exact absolute location of the original FolderIcon.
2967 */
2968 private void copyFolderIconToImage(FolderIcon fi) {
2969 final int width = fi.getMeasuredWidth();
2970 final int height = fi.getMeasuredHeight();
2971
2972 // Lazy load ImageView, Bitmap and Canvas
2973 if (mFolderIconImageView == null) {
2974 mFolderIconImageView = new ImageView(this);
2975 }
2976 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2977 mFolderIconBitmap.getHeight() != height) {
2978 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2979 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2980 }
2981
2982 DragLayer.LayoutParams lp;
2983 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2984 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2985 } else {
2986 lp = new DragLayer.LayoutParams(width, height);
2987 }
2988
Adam Cohen307fe232012-08-16 17:55:58 -07002989 // The layout from which the folder is being opened may be scaled, adjust the starting
2990 // view size by this scale factor.
2991 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002992 lp.customPosition = true;
2993 lp.x = mRectForFolderAnimation.left;
2994 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002995 lp.width = (int) (scale * width);
2996 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002997
2998 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2999 fi.draw(mFolderIconCanvas);
3000 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003001 if (fi.getFolder() != null) {
3002 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3003 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003004 }
Adam Cohen268c4752012-06-06 17:47:33 -07003005 // Just in case this image view is still in the drag layer from a previous animation,
3006 // we remove it and re-add it.
3007 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3008 mDragLayer.removeView(mFolderIconImageView);
3009 }
3010 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003011 if (fi.getFolder() != null) {
3012 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003013 }
Adam Cohen268c4752012-06-06 17:47:33 -07003014 }
3015
Adam Cohen2801caf2011-05-13 20:57:39 -07003016 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003017 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003018 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3019 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3020 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3021
Adam Cohenc51934b2011-07-26 21:07:43 -07003022 FolderInfo info = (FolderInfo) fi.getTag();
3023 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3024 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003025 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3026 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003027 }
3028
Adam Cohen268c4752012-06-06 17:47:33 -07003029 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3030 copyFolderIconToImage(fi);
3031 fi.setVisibility(View.INVISIBLE);
3032
Michael Jurka2ecf9952012-06-18 12:52:28 -07003033 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003034 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003035 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003036 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3037 }
3038 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003039 oa.start();
3040 }
3041
Adam Cohen268c4752012-06-06 17:47:33 -07003042 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003043 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003044 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3045 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3046 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3047
Adam Cohen268c4752012-06-06 17:47:33 -07003048 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003049
Adam Cohen268c4752012-06-06 17:47:33 -07003050 // We remove and re-draw the FolderIcon in-case it has changed
3051 mDragLayer.removeView(mFolderIconImageView);
3052 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003053 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003054 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003055 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003056 oa.addListener(new AnimatorListenerAdapter() {
3057 @Override
3058 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003059 if (cl != null) {
3060 cl.clearFolderLeaveBehind();
3061 // Remove the ImageView copy of the FolderIcon and make the original visible.
3062 mDragLayer.removeView(mFolderIconImageView);
3063 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003064 }
3065 }
3066 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003067 oa.start();
3068 }
3069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003070 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003071 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003072 * is animated relative to the specified View. If the View is null, no animation
3073 * is played.
3074 *
3075 * @param folderInfo The FolderInfo describing the folder to open.
3076 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003077 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003078 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003079 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003080
Adam Cohena9cf38f2011-05-02 15:36:58 -07003081 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082
Adam Cohen4554ee12011-08-03 16:13:21 -07003083 // Just verify that the folder hasn't already been added to the DragLayer.
3084 // There was a one-off crash where the folder had a parent already.
3085 if (folder.getParent() == null) {
3086 mDragLayer.addView(folder);
3087 mDragController.addDropTarget((DropTarget) folder);
3088 } else {
3089 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3090 folder.getParent() + ").");
3091 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003092 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003093 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003094
3095 // Notify the accessibility manager that this folder "window" has appeared and occluded
3096 // the workspace items
3097 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3098 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003099 }
3100
3101 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003102 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003103 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003104 if (folder.isEditingName()) {
3105 folder.dismissEditingName();
3106 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003107 closeFolder(folder);
3108 }
3109 }
3110
3111 void closeFolder(Folder folder) {
3112 folder.getInfo().opened = false;
3113
3114 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3115 if (parent != null) {
3116 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3117 shrinkAndFadeInFolderIcon(fi);
3118 }
3119 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003120
3121 // Notify the accessibility manager that this folder "window" has disappeard and no
3122 // longer occludeds the workspace items
3123 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124 }
3125
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003126 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003127 if (!isDraggingEnabled()) return false;
3128 if (isWorkspaceLocked()) return false;
3129 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003130
Adam Cohen1697b792013-09-17 19:08:21 -07003131 if (v instanceof Workspace) {
3132 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003133 if (mWorkspace.enterOverviewMode()) {
3134 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3135 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3136 return true;
3137 } else {
3138 return false;
3139 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003140 } else {
3141 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003142 }
Adam Cohen1697b792013-09-17 19:08:21 -07003143 }
3144
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003145 CellLayout.CellInfo longClickCellInfo = null;
3146 View itemUnderLongClick = null;
3147 if (v.getTag() instanceof ItemInfo) {
3148 ItemInfo info = (ItemInfo) v.getTag();
3149 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3150 itemUnderLongClick = longClickCellInfo.cell;
3151 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003152 }
3153
Winson Chung3d503fb2011-07-13 17:25:49 -07003154 // The hotseat touch handling does not go through Workspace, and we always allow long press
3155 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003156 final boolean inHotseat = isHotseatLayout(v);
3157 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003158 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003159 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003160 // User long pressed on empty space
3161 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3162 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003163 if (mWorkspace.isInOverviewMode()) {
3164 mWorkspace.startReordering(v);
3165 } else {
3166 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003168 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003169 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3170 mHotseat.getOrderInHotseat(
3171 longClickCellInfo.cellX,
3172 longClickCellInfo.cellY));
3173 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003175 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003176 }
3177 }
3178 }
3179 return true;
3180 }
3181
Winson Chung3d503fb2011-07-13 17:25:49 -07003182 boolean isHotseatLayout(View layout) {
3183 return mHotseat != null && layout != null &&
3184 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3185 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003186
Winson Chung3d503fb2011-07-13 17:25:49 -07003187 /**
3188 * Returns the CellLayout of the specified container at the specified screen.
3189 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003190 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003191 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3192 if (mHotseat != null) {
3193 return mHotseat.getLayout();
3194 } else {
3195 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003196 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003197 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003198 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003199 }
3200 }
3201
Daniel Sandler843e8602010-06-07 14:59:01 -04003202 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003203 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003204 }
3205
Craig Mautner360310b2012-10-26 15:13:08 -07003206 private void setWorkspaceBackground(boolean workspace) {
3207 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003208 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003209 }
3210
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003211 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003212 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3213 int curflags = getWindow().getAttributes().flags
3214 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3215 if (wpflags != curflags) {
3216 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3217 }
Craig Mautner360310b2012-10-26 15:13:08 -07003218 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003219 }
3220
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003221 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3222 if (v instanceof LauncherTransitionable) {
3223 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3224 }
3225 }
3226
Michael Jurkabed61d22012-02-14 22:51:29 -08003227 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3228 if (v instanceof LauncherTransitionable) {
3229 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3230 }
Winson Chung70442722012-02-10 15:43:22 -08003231
3232 // Update the workspace transition step as well
3233 dispatchOnLauncherTransitionStep(v, 0f);
3234 }
3235
3236 private void dispatchOnLauncherTransitionStep(View v, float t) {
3237 if (v instanceof LauncherTransitionable) {
3238 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3239 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003240 }
3241
3242 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3243 if (v instanceof LauncherTransitionable) {
3244 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3245 }
Winson Chung70442722012-02-10 15:43:22 -08003246
3247 // Update the workspace transition step as well
3248 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003249 }
3250
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003251 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003252 * Things to test when changing the following seven functions.
3253 * - Home from workspace
3254 * - from center screen
3255 * - from other screens
3256 * - Home from all apps
3257 * - from center screen
3258 * - from other screens
3259 * - Back from all apps
3260 * - from center screen
3261 * - from other screens
3262 * - Launch app from workspace and quit
3263 * - with back
3264 * - with home
3265 * - Launch app from all apps and quit
3266 * - with back
3267 * - with home
3268 * - Go to a screen that's not the default, then all
3269 * apps, and launch and app, and go back
3270 * - with back
3271 * -with home
3272 * - On workspace, long press power and go back
3273 * - with back
3274 * - with home
3275 * - On all apps, long press power and go back
3276 * - with back
3277 * - with home
3278 * - On workspace, power off
3279 * - On all apps, power off
3280 * - Launch an app and turn off the screen while in that app
3281 * - Go back with home key
3282 * - Go back with back key TODO: make this not go to workspace
3283 * - From all apps
3284 * - From workspace
3285 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3286 * - From all apps
3287 * - From the center workspace
3288 * - From another workspace
3289 */
3290
3291 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003292 * Zoom the camera out from the workspace to reveal 'toView'.
3293 * Assumes that the view to show is anchored at either the very top or very bottom
3294 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003295 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003296 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003297 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3298 showAppsCustomizeHelper(animated, springLoaded, contentType);
3299 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003300
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003301 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chungc58497e2013-09-03 17:48:37 -07003302 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3303 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003304 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003305 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003306 mStateAnimation.cancel();
3307 mStateAnimation = null;
3308 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003309
Kenny Guyd794a3f2014-09-16 15:17:58 +01003310 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003311
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003312 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003313
Adam Cohen6c5891a2014-07-09 23:53:15 -07003314 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003315 final int itemsAlphaStagger =
3316 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003317
Michael Jurkabed61d22012-02-14 22:51:29 -08003318 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003319 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003320
Adam Cohen80e6beb2015-02-13 16:14:33 -08003321 final HashMap<View, Integer> layerViews = new HashMap<View, Integer>();
Adam Cohen2854d252014-08-27 16:04:07 -07003322
Adam Cohen6c5891a2014-07-09 23:53:15 -07003323 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3324 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003325 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003326 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003327 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003328 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3329 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003330 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3331 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003332
Adam Cohena38dc902014-09-07 17:48:55 +02003333 // If for some reason our views aren't initialized, don't animate
3334 boolean initialized = getAllAppsButton() != null;
3335
3336 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003337 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003338 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3339 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003340
Adam Cohen9bfdb762014-07-21 17:44:06 -07003341 final View page = content.getPageAt(content.getCurrentPage());
3342 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003343
Adam Cohen63f1ec02014-08-12 09:23:13 -07003344 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3345 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003346 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3347 } else {
3348 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3349 }
3350
Adam Cohen9bfdb762014-07-21 17:44:06 -07003351 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003352 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003353 revealView.setVisibility(View.VISIBLE);
3354 // We need to hide this view as the animation start will be posted.
3355 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003356
Adam Cohen9bfdb762014-07-21 17:44:06 -07003357 int width = revealView.getMeasuredWidth();
3358 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003359 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003360
Adam Cohen63f1ec02014-08-12 09:23:13 -07003361 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003362 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003363
Adam Cohen63f1ec02014-08-12 09:23:13 -07003364 // Get the y delta between the center of the page and the center of the all apps button
3365 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3366 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003367
Adam Cohen2854d252014-08-27 16:04:07 -07003368 float alpha = 0;
3369 float xDrift = 0;
3370 float yDrift = 0;
3371 if (material) {
3372 alpha = isWidgetTray ? 0.3f : 1f;
3373 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3374 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3375 } else {
3376 yDrift = 2 * height / 3;
3377 xDrift = 0;
3378 }
3379 final float initAlpha = alpha;
3380
Adam Cohen80e6beb2015-02-13 16:14:33 -08003381 layerViews.put(revealView, BUILD_AND_SET_LAYER);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003382 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003383 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003384 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003385 PropertyValuesHolder panelDriftX =
3386 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003387
Adam Cohen2854d252014-08-27 16:04:07 -07003388 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3389 panelAlpha, panelDriftY, panelDriftX);
3390
Adam Cohen9bfdb762014-07-21 17:44:06 -07003391 panelAlphaAndDrift.setDuration(revealDuration);
3392 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003393
Adam Cohen9bfdb762014-07-21 17:44:06 -07003394 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003395
Adam Cohen4e243a22014-08-10 18:30:55 -07003396 if (page != null) {
3397 page.setVisibility(View.VISIBLE);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003398 layerViews.put(page, BUILD_AND_SET_LAYER);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003399
Adam Cohen2854d252014-08-27 16:04:07 -07003400 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3401 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003402 pageDrift.setDuration(revealDuration);
3403 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003404 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003405 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003406
Adam Cohen63f1ec02014-08-12 09:23:13 -07003407 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003408 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003409 itemsAlpha.setDuration(revealDuration);
3410 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3411 itemsAlpha.setStartDelay(itemsAlphaStagger);
3412 mStateAnimation.play(itemsAlpha);
3413 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003414
Adam Cohen4e243a22014-08-10 18:30:55 -07003415 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3416 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003417 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003418 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003419 indicatorsAlpha.setDuration(revealDuration);
3420 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003421
Adam Cohen9bfdb762014-07-21 17:44:06 -07003422 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003423 final View allApps = getAllAppsButton();
3424 int allAppsButtonSize = LauncherAppState.getInstance().
3425 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3426 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003427 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003428 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003429 reveal.setDuration(revealDuration);
3430 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003431
3432 reveal.addListener(new AnimatorListenerAdapter() {
3433 public void onAnimationStart(Animator animation) {
3434 if (!isWidgetTray) {
3435 allApps.setVisibility(View.INVISIBLE);
3436 }
3437 }
3438 public void onAnimationEnd(Animator animation) {
3439 if (!isWidgetTray) {
3440 allApps.setVisibility(View.VISIBLE);
3441 }
3442 }
3443 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003444 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003445 }
Michael Jurka1899a362011-11-03 13:50:45 -07003446
Adam Cohen9bfdb762014-07-21 17:44:06 -07003447 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3448 @Override
3449 public void onAnimationEnd(Animator animation) {
3450 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3451 dispatchOnLauncherTransitionEnd(toView, animated, false);
3452
3453 revealView.setVisibility(View.INVISIBLE);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003454
3455 for (View v : layerViews.keySet()) {
3456 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3457 v.setLayerType(View.LAYER_TYPE_NONE, null);
3458 }
Adam Cohen4e243a22014-08-10 18:30:55 -07003459 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003460 content.setPageBackgroundsVisible(true);
3461
3462 // Hide the search bar
3463 if (mSearchDropTargetBar != null) {
3464 mSearchDropTargetBar.hideSearchBar(false);
3465 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003466
3467 // This can hold unnecessary references to views.
3468 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003469 }
3470
Adam Cohen9bfdb762014-07-21 17:44:06 -07003471 });
3472
Adam Cohen6c5891a2014-07-09 23:53:15 -07003473 if (workspaceAnim != null) {
3474 mStateAnimation.play(workspaceAnim);
3475 }
Adam Cohen2854d252014-08-27 16:04:07 -07003476
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003477 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3478 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003479 final AnimatorSet stateAnimation = mStateAnimation;
3480 final Runnable startAnimRunnable = new Runnable() {
3481 public void run() {
3482 // Check that mStateAnimation hasn't changed while
3483 // we waited for a layout/draw pass
3484 if (mStateAnimation != stateAnimation)
3485 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003486 dispatchOnLauncherTransitionStart(fromView, animated, false);
3487 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003488
3489 revealView.setAlpha(initAlpha);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003490
3491 for (View v : layerViews.keySet()) {
3492 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3493 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3494 }
3495 }
3496
Kenny Guyd794a3f2014-09-16 15:17:58 +01003497 if (Utilities.isLmpOrAbove()) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003498 for (View v : layerViews.keySet()) {
3499 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen2854d252014-08-27 16:04:07 -07003500 }
3501 }
3502 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003503 }
3504 };
Adam Cohen2854d252014-08-27 16:04:07 -07003505 toView.bringToFront();
3506 toView.setVisibility(View.VISIBLE);
3507 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003508 } else {
3509 toView.setTranslationX(0.0f);
3510 toView.setTranslationY(0.0f);
3511 toView.setScaleX(1.0f);
3512 toView.setScaleY(1.0f);
3513 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003514 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003515
Daniel Sandlere4f98912013-06-25 15:13:26 -04003516 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003517 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003518 if (mSearchDropTargetBar != null) {
3519 mSearchDropTargetBar.hideSearchBar(false);
3520 }
Michael Jurkaabded662011-03-04 12:06:57 -08003521 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003522 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003523 dispatchOnLauncherTransitionStart(fromView, animated, false);
3524 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003525 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003526 dispatchOnLauncherTransitionStart(toView, animated, false);
3527 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003528 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003529 }
3530
3531 /**
3532 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003533 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003534 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003535 */
Adam Cohened307df2013-10-02 09:37:31 -07003536 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003537 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003538
Michael Jurkab3e22d92011-10-31 15:58:33 -07003539 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003540 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003541 mStateAnimation.cancel();
3542 mStateAnimation = null;
3543 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003544
Kenny Guyd794a3f2014-09-16 15:17:58 +01003545 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003546 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003547
Adam Cohen63f1ec02014-08-12 09:23:13 -07003548 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003549 final int itemsAlphaStagger =
3550 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003551
Winson Chungf0ea4d32011-06-06 14:27:16 -07003552 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003553 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003554 Animator workspaceAnim = null;
Adam Cohen80e6beb2015-02-13 16:14:33 -08003555 final HashMap<View, Integer> layerViews = new HashMap<View, Integer>();
Adam Cohen2854d252014-08-27 16:04:07 -07003556
Adam Cohened307df2013-10-02 09:37:31 -07003557 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003558 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003559 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003560 } else if (toState == Workspace.State.SPRING_LOADED ||
3561 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003562 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003563 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003564 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003565
Adam Cohena38dc902014-09-07 17:48:55 +02003566 // If for some reason our views aren't initialized, don't animate
3567 boolean initialized = getAllAppsButton() != null;
3568
3569 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003570 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003571 if (workspaceAnim != null) {
3572 mStateAnimation.play(workspaceAnim);
3573 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003574
Adam Cohen9bfdb762014-07-21 17:44:06 -07003575 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3576 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003577
Adam Cohen9bfdb762014-07-21 17:44:06 -07003578 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003579
3580 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3581 int count = content.getChildCount();
3582 for (int i = 0; i < count; i++) {
3583 View child = content.getChildAt(i);
3584 if (child != page) {
3585 child.setVisibility(View.INVISIBLE);
3586 }
3587 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003588 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003589
Adam Cohen2854d252014-08-27 16:04:07 -07003590 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3591 // don't perform all these no-op animations. In particularly, this was causing
3592 // the all-apps button to pop in and out.
3593 if (fromView.getVisibility() == View.VISIBLE) {
3594 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3595 final boolean isWidgetTray =
3596 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003597
Adam Cohen2854d252014-08-27 16:04:07 -07003598 if (isWidgetTray) {
3599 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3600 } else {
3601 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003602 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003603
Adam Cohen2854d252014-08-27 16:04:07 -07003604 int width = revealView.getMeasuredWidth();
3605 int height = revealView.getMeasuredHeight();
3606 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3607
3608 // Hide the real page background, and swap in the fake one
3609 revealView.setVisibility(View.VISIBLE);
3610 content.setPageBackgroundsVisible(false);
3611
3612 final View allAppsButton = getAllAppsButton();
3613 revealView.setTranslationY(0);
3614 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3615 allAppsButton, null);
3616
3617 float xDrift = 0;
3618 float yDrift = 0;
3619 if (material) {
3620 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3621 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3622 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003623 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003624 xDrift = 0;
3625 }
3626
Adam Cohen80e6beb2015-02-13 16:14:33 -08003627 layerViews.put(revealView, BUILD_AND_SET_LAYER);
Adam Cohen2854d252014-08-27 16:04:07 -07003628 TimeInterpolator decelerateInterpolator = material ?
3629 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003630 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003631
3632 // The vertical motion of the apps panel should be delayed by one frame
3633 // from the conceal animation in order to give the right feel. We correpsondingly
3634 // shorten the duration so that the slide and conceal end at the same time.
3635 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3636 0, yDrift);
3637 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3638 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3639 panelDriftY.setInterpolator(decelerateInterpolator);
3640 mStateAnimation.play(panelDriftY);
3641
3642 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3643 0, xDrift);
3644 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3645 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3646 panelDriftX.setInterpolator(decelerateInterpolator);
3647 mStateAnimation.play(panelDriftX);
3648
3649 if (isWidgetTray || !material) {
3650 float finalAlpha = material ? 0.4f : 0f;
3651 revealView.setAlpha(1f);
3652 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3653 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003654 panelAlpha.setDuration(material ? revealDuration : 150);
3655 panelAlpha.setInterpolator(decelerateInterpolator);
3656 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003657 mStateAnimation.play(panelAlpha);
3658 }
3659
3660 if (page != null) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003661 layerViews.put(page, BUILD_AND_SET_LAYER);
Adam Cohen2854d252014-08-27 16:04:07 -07003662
3663 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3664 0, yDrift);
3665 page.setTranslationY(0);
3666 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3667 pageDrift.setInterpolator(decelerateInterpolator);
3668 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3669 mStateAnimation.play(pageDrift);
3670
3671 page.setAlpha(1f);
3672 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3673 itemsAlpha.setDuration(100);
3674 itemsAlpha.setInterpolator(decelerateInterpolator);
3675 mStateAnimation.play(itemsAlpha);
3676 }
3677
3678 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3679 pageIndicators.setAlpha(1f);
3680 ObjectAnimator indicatorsAlpha =
3681 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3682 indicatorsAlpha.setDuration(revealDuration);
3683 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3684 mStateAnimation.play(indicatorsAlpha);
3685
3686 width = revealView.getMeasuredWidth();
3687
3688 if (material) {
3689 if (!isWidgetTray) {
3690 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003691 }
Adam Cohen2854d252014-08-27 16:04:07 -07003692 int allAppsButtonSize = LauncherAppState.getInstance().
3693 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3694 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3695 Animator reveal =
3696 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3697 height / 2, revealRadius, finalRadius);
3698 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3699 reveal.setDuration(revealDuration);
3700 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003701
Adam Cohen2854d252014-08-27 16:04:07 -07003702 reveal.addListener(new AnimatorListenerAdapter() {
3703 public void onAnimationEnd(Animator animation) {
3704 revealView.setVisibility(View.INVISIBLE);
3705 if (!isWidgetTray) {
3706 allAppsButton.setVisibility(View.VISIBLE);
3707 }
3708 }
3709 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003710
Adam Cohen2854d252014-08-27 16:04:07 -07003711 mStateAnimation.play(reveal);
3712 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003713
Adam Cohen2854d252014-08-27 16:04:07 -07003714 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3715 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3716 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003717 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003718
3719 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003720 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003721 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003722 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003723 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3724 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003725 if (onCompleteRunnable != null) {
3726 onCompleteRunnable.run();
3727 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003728
Adam Cohen80e6beb2015-02-13 16:14:33 -08003729 for (View v : layerViews.keySet()) {
3730 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3731 v.setLayerType(View.LAYER_TYPE_NONE, null);
3732 }
Adam Cohen4e243a22014-08-10 18:30:55 -07003733 }
Adam Cohen80e6beb2015-02-13 16:14:33 -08003734
Adam Cohen9bfdb762014-07-21 17:44:06 -07003735 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003736 // Unhide side pages
3737 int count = content.getChildCount();
3738 for (int i = 0; i < count; i++) {
3739 View child = content.getChildAt(i);
3740 child.setVisibility(View.VISIBLE);
3741 }
3742
3743 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003744 if (page != null) {
3745 page.setTranslationX(0);
3746 page.setTranslationY(0);
3747 page.setAlpha(1);
3748 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003749 content.setCurrentPage(content.getNextPage());
3750
Chet Haasebc2f0822012-10-26 17:59:53 -07003751 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003752
3753 // This can hold unnecessary references to views.
3754 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003755 }
3756 });
3757
Adam Cohen2854d252014-08-27 16:04:07 -07003758 final AnimatorSet stateAnimation = mStateAnimation;
3759 final Runnable startAnimRunnable = new Runnable() {
3760 public void run() {
3761 // Check that mStateAnimation hasn't changed while
3762 // we waited for a layout/draw pass
3763 if (mStateAnimation != stateAnimation)
3764 return;
3765 dispatchOnLauncherTransitionStart(fromView, animated, false);
3766 dispatchOnLauncherTransitionStart(toView, animated, false);
3767
Adam Cohen80e6beb2015-02-13 16:14:33 -08003768 for (View v : layerViews.keySet()) {
3769 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3770 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3771 }
3772 }
3773
Kenny Guyd794a3f2014-09-16 15:17:58 +01003774 if (Utilities.isLmpOrAbove()) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003775 for (View v : layerViews.keySet()) {
3776 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen2854d252014-08-27 16:04:07 -07003777 }
3778 }
3779 mStateAnimation.start();
3780 }
3781 };
3782 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003783 } else {
3784 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003785 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003786 dispatchOnLauncherTransitionStart(fromView, animated, true);
3787 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003788 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003789 dispatchOnLauncherTransitionStart(toView, animated, true);
3790 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003791 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003792 }
3793
Michael Jurkae326f182011-11-21 14:05:46 -08003794 @Override
3795 public void onTrimMemory(int level) {
3796 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003797 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3798 // The widget preview db can result in holding onto over
3799 // 3MB of memory for caching which isn't necessary.
3800 SQLiteDatabase.releaseMemory();
3801
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003802 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003803 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003804 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003805 }
Michael Jurkae326f182011-11-21 14:05:46 -08003806 }
3807 }
3808
Adam Cohened307df2013-10-02 09:37:31 -07003809 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003810 showWorkspace(animated, null);
3811 }
3812
Adam Cohened307df2013-10-02 09:37:31 -07003813 protected void showWorkspace() {
3814 showWorkspace(true);
3815 }
3816
Adam Cohened66b2b2012-01-23 17:28:51 -08003817 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003818 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003819 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003820 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003821 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003822
Winson Chungc7d2b602012-05-16 17:02:20 -07003823 // Show the search bar (only animate if we were showing the drop target bar in spring
3824 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003825 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003826 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003827 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003828
Michael Jurkab3e22d92011-10-31 15:58:33 -07003829 // Set focus to the AppsCustomize button
3830 if (mAllAppsButton != null) {
3831 mAllAppsButton.requestFocus();
3832 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003833 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003834
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003835 // Change the state *after* we've called all the transition code
3836 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003837
Winson Chung5fb63472011-02-02 17:03:37 -08003838 // Resume the auto-advance of widgets
3839 mUserPresent = true;
3840 updateRunning();
3841
alanv1d4fde62012-10-17 13:15:47 -07003842 // Send an accessibility event to announce the context change
3843 getWindow().getDecorView()
3844 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003845
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003846 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003847 }
3848
Adam Cohened307df2013-10-02 09:37:31 -07003849 void showOverviewMode(boolean animated) {
3850 mWorkspace.setVisibility(View.VISIBLE);
3851 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3852 mState = State.WORKSPACE;
3853 onWorkspaceShown(animated);
3854 }
3855
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003856 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003857 }
3858
Winson Chung82963d52013-10-09 11:20:57 -07003859 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3860 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003861 if (mState != State.WORKSPACE) return;
3862
Winson Chung82963d52013-10-09 11:20:57 -07003863 if (resetPageToZero) {
3864 mAppsCustomizeTabHost.reset();
3865 }
Winson Chungc58497e2013-09-03 17:48:37 -07003866 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003867 mAppsCustomizeTabHost.post(new Runnable() {
3868 @Override
3869 public void run() {
3870 // We post this in-case the all apps view isn't yet constructed.
3871 mAppsCustomizeTabHost.requestFocus();
3872 }
3873 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003874
Michael Jurkab3e22d92011-10-31 15:58:33 -07003875 // Change the state *after* we've called all the transition code
3876 mState = State.APPS_CUSTOMIZE;
3877
3878 // Pause the auto-advance of widgets until we are out of AllApps
3879 mUserPresent = false;
3880 updateRunning();
3881 closeFolder();
3882
3883 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003884 getWindow().getDecorView()
3885 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003886 }
3887
Adam Cohen7777d962011-08-18 18:58:38 -07003888 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003889 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003890 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003891 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003892 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003893 }
Adam Cohen7777d962011-08-18 18:58:38 -07003894
Adam Cohenad4e15c2013-10-17 16:21:35 -07003895 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003896 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003897 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3898
Winson Chunge7a03942011-08-05 15:05:12 -07003899 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003900 @Override
3901 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003902 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003903 // Before we show workspace, hide all apps again because
3904 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3905 // clean up our state transition functions
3906 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003907 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003908 } else {
3909 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003910 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003911 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003912 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003913 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003914
Michael Jurkad3ef3062010-11-23 16:23:58 -08003915 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003916 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003917 final boolean animated = true;
3918 final boolean springLoaded = true;
3919 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003920 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003921 }
3922 // Otherwise, we are not in spring loaded mode, so don't do anything.
3923 }
3924
Michael Jurkab3e22d92011-10-31 15:58:33 -07003925 void lockAllApps() {
3926 // TODO
3927 }
3928
3929 void unlockAllApps() {
3930 // TODO
3931 }
3932
Sunny Goyal594d76d2014-11-06 10:12:54 -08003933 protected void disableVoiceButtonProxy(boolean disable) {
3934 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003935 }
3936
Cristina Stancu476493b2013-08-07 17:20:14 +01003937 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003938 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3939 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003940 }
3941
Adam Cohen24ce0b32014-01-14 16:18:14 -08003942 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003943 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3944 if (searchProvider == null) {
3945 return null;
3946 }
3947
3948 Bundle opts = new Bundle();
3949 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
3950 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
3951
3952 SharedPreferences sp = getSharedPreferences(
3953 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3954 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003955 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003956 if (!searchProvider.provider.flattenToString().equals(
3957 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003958 || (widgetInfo == null)
3959 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003960 // A valid widget is not already bound.
3961 if (widgetId > -1) {
3962 mAppWidgetHost.deleteAppWidgetId(widgetId);
3963 widgetId = -1;
3964 }
3965
3966 // Try to bind a new widget
3967 widgetId = mAppWidgetHost.allocateAppWidgetId();
3968
3969 if (!AppWidgetManagerCompat.getInstance(this)
3970 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3971 mAppWidgetHost.deleteAppWidgetId(widgetId);
3972 widgetId = -1;
3973 }
3974
3975 sp.edit()
3976 .putInt(QSB_WIDGET_ID, widgetId)
3977 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3978 .commit();
3979 }
3980
3981 if (widgetId != -1) {
3982 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3983 mQsb.updateAppWidgetOptions(opts);
3984 mQsb.setPadding(0, 0, 0, 0);
3985 mSearchDropTargetBar.addView(mQsb);
3986 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003987 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003988 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003989 }
3990
Michael Jurkad7c28052012-04-27 15:43:36 -07003991 @Override
3992 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003993 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003994 final List<CharSequence> text = event.getText();
3995 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003996 // Populate event with a fake title based on the current state.
3997 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07003998 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07003999 } else {
4000 text.add(getString(R.string.all_apps_home_button_label));
4001 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004002 return result;
4003 }
4004
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004005 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004006 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004007 */
4008 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4009 @Override
4010 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004011 closeSystemDialogs();
4012 }
4013 }
4014
4015 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004016 * Receives notifications whenever the appwidgets are reset.
4017 */
4018 private class AppWidgetResetObserver extends ContentObserver {
4019 public AppWidgetResetObserver() {
4020 super(new Handler());
4021 }
4022
4023 @Override
4024 public void onChange(boolean selfChange) {
4025 onAppWidgetReset();
4026 }
4027 }
4028
4029 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004030 * If the activity is currently paused, signal that we need to run the passed Runnable
4031 * in onResume.
4032 *
4033 * This needs to be called from incoming places where resources might have been loaded
4034 * while we are paused. That is becaues the Configuration might be wrong
4035 * when we're not running, and if it comes back to what it was when we
4036 * were paused, we are not restarted.
4037 *
4038 * Implementation of the method from LauncherModel.Callbacks.
4039 *
4040 * @return true if we are currently paused. The caller might be able to
4041 * skip some work in that case since we will come back again.
4042 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004043 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004044 if (mPaused) {
4045 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004046 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004047 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004048 }
4049 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004050 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004051 return true;
4052 } else {
4053 return false;
4054 }
4055 }
4056
Michael Jurkac402cd92013-05-20 15:49:32 +02004057 private boolean waitUntilResume(Runnable run) {
4058 return waitUntilResume(run, false);
4059 }
4060
Michael Jurka1e2f4652013-07-08 18:03:46 -07004061 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004062 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004063 }
4064
Michael Jurka7607c2f2013-04-03 14:33:19 -07004065 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004066 * If the activity is currently paused, signal that we need to re-run the loader
4067 * in onResume.
4068 *
4069 * This needs to be called from incoming places where resources might have been loaded
4070 * while we are paused. That is becaues the Configuration might be wrong
4071 * when we're not running, and if it comes back to what it was when we
4072 * were paused, we are not restarted.
4073 *
4074 * Implementation of the method from LauncherModel.Callbacks.
4075 *
4076 * @return true if we are currently paused. The caller might be able to
4077 * skip some work in that case since we will come back again.
4078 */
4079 public boolean setLoadOnResume() {
4080 if (mPaused) {
4081 Log.i(TAG, "setLoadOnResume");
4082 mOnResumeNeedsLoad = true;
4083 return true;
4084 } else {
4085 return false;
4086 }
4087 }
4088
4089 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004090 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004091 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004092 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004093 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004094 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004095 } else {
4096 return SCREEN_COUNT / 2;
4097 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004098 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004099
Joe Onorato9c1289c2009-08-17 11:03:03 -04004100 /**
4101 * Refreshes the shortcuts shown on the workspace.
4102 *
4103 * Implementation of the method from LauncherModel.Callbacks.
4104 */
4105 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004106 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004107
Michael Jurka7607c2f2013-04-03 14:33:19 -07004108 // If we're starting binding all over again, clear any bind calls we'd postponed in
4109 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4110 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004111 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004112
Winson Chungd64d1762013-08-20 14:37:16 -07004113 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004114 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004115 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004116
Michael Jurka05bf6442011-11-30 20:28:53 -08004117 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004118 if (mHotseat != null) {
4119 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004120 }
4121 }
4122
Adam Cohendcd297f2013-06-18 13:13:40 -07004123 @Override
4124 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004125 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004126
Adam Cohen5084cba2013-09-03 12:01:16 -07004127 // If there are no screens, we need to have an empty screen
4128 if (orderedScreenIds.size() == 0) {
4129 mWorkspace.addExtraEmptyScreen();
4130 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004131
4132 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004133 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004134 if (hasCustomContentToLeft()) {
4135 mWorkspace.createCustomContentContainer();
4136 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004137 }
Winson Chung64359a52013-07-08 17:17:08 -07004138 }
4139
4140 @Override
4141 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004142 // Log to disk
4143 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4144 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4145 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004146 int count = orderedScreenIds.size();
4147 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004148 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004149 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004150 }
4151
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08004152 @Override
4153 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
4154 final long screenId, final int[] cell, final int spanX, final int spanY) {
4155 showWorkspace(true, new Runnable() {
4156
4157 @Override
4158 public void run() {
4159 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
4160 addPendingItem(info, container, screenId, cell, spanX, spanY);
4161 }
4162 });
4163 }
4164
Adam Cohen39a06042013-07-19 14:30:12 -07004165 private boolean shouldShowWeightWatcher() {
4166 String spKey = LauncherAppState.getSharedPreferencesKey();
4167 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004168 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004169
4170 return show;
4171 }
4172
4173 private void toggleShowWeightWatcher() {
4174 String spKey = LauncherAppState.getSharedPreferencesKey();
4175 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4176 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4177
4178 show = !show;
4179
4180 SharedPreferences.Editor editor = sp.edit();
4181 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4182 editor.commit();
4183
4184 if (mWeightWatcher != null) {
4185 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4186 }
4187 }
4188
Winson Chungd64d1762013-08-20 14:37:16 -07004189 public void bindAppsAdded(final ArrayList<Long> newScreens,
4190 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004191 final ArrayList<ItemInfo> addAnimated,
4192 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004193 Runnable r = new Runnable() {
4194 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004195 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004196 }
4197 };
4198 if (waitUntilResume(r)) {
4199 return;
4200 }
4201
Winson Chungd64d1762013-08-20 14:37:16 -07004202 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004203 if (newScreens != null) {
4204 bindAddScreens(newScreens);
4205 }
Winson Chungd64d1762013-08-20 14:37:16 -07004206
4207 // We add the items without animation on non-visible pages, and with
4208 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004209 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004210 bindItems(addNotAnimated, 0,
4211 addNotAnimated.size(), false);
4212 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004213 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004214 bindItems(addAnimated, 0,
4215 addAnimated.size(), true);
4216 }
Winson Chungc58497e2013-09-03 17:48:37 -07004217
Winson Chung87412982013-10-03 18:34:14 -07004218 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004219 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004220
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004221 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004222 addedApps != null && mAppsCustomizeContent != null) {
4223 mAppsCustomizeContent.addApps(addedApps);
4224 }
Winson Chungd64d1762013-08-20 14:37:16 -07004225 }
4226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004227 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004228 * Bind the items start-end from the list.
4229 *
4230 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004231 */
Winson Chung64359a52013-07-08 17:17:08 -07004232 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4233 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004234 Runnable r = new Runnable() {
4235 public void run() {
4236 bindItems(shortcuts, start, end, forceAnimateIcons);
4237 }
4238 };
4239 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004240 return;
4241 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004242
Winson Chungf0c6ae02012-03-21 16:10:31 -07004243 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004244 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4245 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004246 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004247 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004248 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004249 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004250 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004251
4252 // Short circuit if we are loading dock items for a configuration which has no dock
4253 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4254 mHotseat == null) {
4255 continue;
4256 }
4257
Joe Onorato9c1289c2009-08-17 11:03:03 -04004258 switch (item.itemType) {
4259 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4260 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004261 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004262 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004263
Winson Chung64359a52013-07-08 17:17:08 -07004264 /*
4265 * TODO: FIX collision case
4266 */
Winson Chungce376632013-07-11 16:12:41 -07004267 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4268 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4269 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004270 View v = cl.getChildAt(item.cellX, item.cellY);
4271 Object tag = v.getTag();
4272 String desc = "Collision while binding workspace item: " + item
4273 + ". Collides with " + tag;
4274 if (LauncherAppState.isDogfoodBuild()) {
4275 throw (new RuntimeException(desc));
4276 } else {
4277 Log.d(TAG, desc);
4278 }
Winson Chungce376632013-07-11 16:12:41 -07004279 }
Winson Chung64359a52013-07-08 17:17:08 -07004280 }
4281
Adam Cohendcd297f2013-06-18 13:13:40 -07004282 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4283 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004284 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004285 // Animate all the applications up now
4286 shortcut.setAlpha(0f);
4287 shortcut.setScaleX(0f);
4288 shortcut.setScaleY(0f);
4289 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004290 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004291 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004292 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004293 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004294 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004295 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004296 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004297 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4298 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004299 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004300 default:
4301 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004302 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004303 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004304
Winson Chung997a9232013-07-24 15:33:46 -07004305 if (animateIcons) {
4306 // Animate to the correct page
4307 if (newShortcutsScreenId > -1) {
4308 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004309 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004310 final Runnable startBounceAnimRunnable = new Runnable() {
4311 public void run() {
4312 anim.playTogether(bounceAnims);
4313 anim.start();
4314 }
4315 };
Winson Chung997a9232013-07-24 15:33:46 -07004316 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004317 // We post the animation slightly delayed to prevent slowdowns
4318 // when we are loading right after we return to launcher.
4319 mWorkspace.postDelayed(new Runnable() {
4320 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004321 if (mWorkspace != null) {
4322 mWorkspace.snapToPage(newScreenIndex);
4323 mWorkspace.postDelayed(startBounceAnimRunnable,
4324 NEW_APPS_ANIMATION_DELAY);
4325 }
Winson Chung94d67682013-09-25 16:29:40 -07004326 }
4327 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004328 } else {
4329 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004330 }
4331 }
Winson Chung64359a52013-07-08 17:17:08 -07004332 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004333 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004334 }
4335
Joe Onorato9c1289c2009-08-17 11:03:03 -04004336 /**
4337 * Implementation of the method from LauncherModel.Callbacks.
4338 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004339 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004340 Runnable r = new Runnable() {
4341 public void run() {
4342 bindFolders(folders);
4343 }
4344 };
4345 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004346 return;
4347 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004348 sFolders.clear();
4349 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004350 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004351
4352 /**
4353 * Add the views for a widget to the workspace.
4354 *
4355 * Implementation of the method from LauncherModel.Callbacks.
4356 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004357 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004358 Runnable r = new Runnable() {
4359 public void run() {
4360 bindAppWidget(item);
4361 }
4362 };
4363 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004364 return;
4365 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004366
Daniel Sandler843e8602010-06-07 14:59:01 -04004367 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4368 if (DEBUG_WIDGETS) {
4369 Log.d(TAG, "bindAppWidget: " + item);
4370 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004371 final Workspace workspace = mWorkspace;
4372
Adam Cohen59400422014-03-05 18:07:04 -08004373 LauncherAppWidgetProviderInfo appWidgetInfo =
4374 LauncherModel.getProviderInfo(this, item.providerName);
4375
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004376 if (!mIsSafeModeEnabled
4377 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4378 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004379 if (appWidgetInfo == null) {
4380 if (DEBUG_WIDGETS) {
4381 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4382 + " belongs to component " + item.providerName
4383 + ", as the povider is null");
4384 }
4385 LauncherModel.deleteItemFromDatabase(this, item);
4386 return;
4387 }
4388 // Note: This assumes that the id remap broadcast is received before this step.
4389 // If that is not the case, the id remap will be ignored and user may see the
4390 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004391 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004392 pendingInfo.spanX = item.spanX;
4393 pendingInfo.spanY = item.spanY;
4394 pendingInfo.minSpanX = item.minSpanX;
4395 pendingInfo.minSpanY = item.minSpanY;
4396 Bundle options =
4397 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4398
Sunny Goyalff572272014-07-23 13:58:07 -07004399 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004400 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4401 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004402
4403 // TODO consider showing a permission dialog when the widget is clicked.
4404 if (!success) {
4405 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4406 if (DEBUG_WIDGETS) {
4407 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4408 + " belongs to component " + item.providerName
4409 + ", as the launcher is unable to bing a new widget id");
4410 }
4411 LauncherModel.deleteItemFromDatabase(this, item);
4412 return;
4413 }
4414
4415 item.appWidgetId = newWidgetId;
4416
4417 // If the widget has a configure activity, it is still needs to set it up, otherwise
4418 // the widget is ready to go.
4419 item.restoreStatus = (appWidgetInfo.configure == null)
4420 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4421 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4422
4423 LauncherModel.updateItemInDatabase(this, item);
4424 }
4425
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004426 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004427 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004428 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004429 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4430 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004431 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004432
Sunny Goyal651077b2014-06-30 14:15:31 -07004433 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4434 } else {
4435 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004436 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4437 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004438 view.updateIcon(mIconCache);
4439 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004440 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004441 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004442 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004443
Joe Onorato9c1289c2009-08-17 11:03:03 -04004444 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004445 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004446
Adam Cohendcd297f2013-06-18 13:13:40 -07004447 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004448 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004449 if (!item.isCustomWidget()) {
4450 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4451 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004452
Joe Onorato9c1289c2009-08-17 11:03:03 -04004453 workspace.requestLayout();
4454
Daniel Sandler843e8602010-06-07 14:59:01 -04004455 if (DEBUG_WIDGETS) {
4456 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4457 + (SystemClock.uptimeMillis()-start) + "ms");
4458 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004459 }
4460
Sunny Goyalff572272014-07-23 13:58:07 -07004461 /**
4462 * Restores a pending widget.
4463 *
4464 * @param appWidgetId The app widget id
4465 * @param cellInfo The position on screen where to create the widget.
4466 */
4467 private void completeRestoreAppWidget(final int appWidgetId) {
4468 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4469 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4470 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4471 return;
4472 }
4473
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004474 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004475 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4476
4477 mWorkspace.reinflateWidgetsIfNecessary();
4478 LauncherModel.updateItemInDatabase(this, info);
4479 }
4480
Adam Cohen1462de32012-07-24 22:34:36 -07004481 public void onPageBoundSynchronously(int page) {
4482 mSynchronouslyBoundPages.add(page);
4483 }
4484
Joe Onorato9c1289c2009-08-17 11:03:03 -04004485 /**
4486 * Callback saying that there aren't any more items to bind.
4487 *
4488 * Implementation of the method from LauncherModel.Callbacks.
4489 */
Adam Cohene25af792013-06-06 23:08:25 -07004490 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004491 Runnable r = new Runnable() {
4492 public void run() {
4493 finishBindingItems(upgradePath);
4494 }
4495 };
4496 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004497 return;
4498 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004499 if (mSavedState != null) {
4500 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004501 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004502 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004503 mSavedState = null;
4504 }
4505
Adam Cohen1462de32012-07-24 22:34:36 -07004506 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004507
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004508 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004509 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004510
4511 // If we received the result of any pending adds while the loader was running (e.g. the
4512 // widget configuration forced an orientation change), process them now.
4513 if (sPendingAddItem != null) {
4514 final long screenId = completeAdd(sPendingAddItem);
4515
4516 // TODO: this moves the user to the page where the pending item was added. Ideally,
4517 // the screen would be guaranteed to exist after bind, and the page would be set through
4518 // the workspace restore process.
4519 mWorkspace.post(new Runnable() {
4520 @Override
4521 public void run() {
4522 mWorkspace.snapToScreenId(screenId);
4523 }
4524 });
4525 sPendingAddItem = null;
4526 }
4527
Adam Cohene25af792013-06-06 23:08:25 -07004528 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004529 mWorkspace.getUniqueComponents(true, null);
4530 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004531 }
Sunny Goyale755d462014-07-22 13:48:29 -07004532 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004533
4534 if (mLauncherCallbacks != null) {
4535 mLauncherCallbacks.finishBindingItems(upgradePath);
4536 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004537 }
4538
Adam Cohen3ed316a2014-07-23 18:21:20 -07004539 private void sendLoadingCompleteBroadcastIfNecessary() {
4540 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4541 String permission =
4542 getResources().getString(R.string.receive_first_load_broadcast_permission);
4543 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4544 sendBroadcast(intent, permission);
4545 SharedPreferences.Editor editor = mSharedPrefs.edit();
4546 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4547 editor.apply();
4548 }
4549 }
4550
Winson Chunga0b7e862013-09-05 16:03:15 -07004551 public boolean isAllAppsButtonRank(int rank) {
4552 if (mHotseat != null) {
4553 return mHotseat.isAllAppsButtonRank(rank);
4554 }
4555 return false;
4556 }
4557
Winson Chunga2413752012-04-03 14:22:34 -07004558 private boolean canRunNewAppsAnimation() {
4559 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4560 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4561 }
4562
Winson Chungc9168342013-06-26 14:54:55 -07004563 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4564 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4565 PropertyValuesHolder.ofFloat("alpha", 1f),
4566 PropertyValuesHolder.ofFloat("scaleX", 1f),
4567 PropertyValuesHolder.ofFloat("scaleY", 1f));
4568 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4569 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4570 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4571 return bounceAnim;
4572 }
4573
Adam Cohen27772732013-11-11 14:00:56 +00004574 public boolean useVerticalBarLayout() {
4575 return LauncherAppState.getInstance().getDynamicGrid().
4576 getDeviceProfile().isVerticalBarLayout();
4577 }
4578
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004579 protected Rect getSearchBarBounds() {
4580 return LauncherAppState.getInstance().getDynamicGrid().
4581 getDeviceProfile().getSearchBarBounds();
4582 }
4583
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004584 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004585 if (mSearchDropTargetBar == null) {
4586 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004587 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004588 if (mQsb != null) {
4589 mSearchDropTargetBar.removeView(mQsb);
4590 mQsb = null;
4591 }
4592 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004593 }
4594
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004595 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004596 * Add the icons for all apps.
4597 *
4598 * Implementation of the method from LauncherModel.Callbacks.
4599 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004600 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004601 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004602 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4603 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4604 getHotseat().addAllAppsFolder(mIconCache, apps,
4605 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4606 }
4607 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004608 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004609 if (mAppsCustomizeContent != null) {
4610 mAppsCustomizeContent.onPackagesUpdated(
Hyunyoung Songfe1dcbf2015-03-11 16:36:52 -07004611 LauncherModel.getSortedWidgetsAndShortcuts(this, false /* refresh */));
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004612 }
Winson Chungc58497e2013-09-03 17:48:37 -07004613 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004614 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004615 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004616 mAppsCustomizeContent.onPackagesUpdated(
Hyunyoung Songfe1dcbf2015-03-11 16:36:52 -07004617 LauncherModel.getSortedWidgetsAndShortcuts(this, false /* refresh */));
Winson Chungc58497e2013-09-03 17:48:37 -07004618 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004619 }
Adam Cohen9211d422014-10-07 18:14:53 -07004620 if (mLauncherCallbacks != null) {
4621 mLauncherCallbacks.bindAllApplications(apps);
4622 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004623 }
4624
4625 /**
4626 * A package was updated.
4627 *
4628 * Implementation of the method from LauncherModel.Callbacks.
4629 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004630 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004631 Runnable r = new Runnable() {
4632 public void run() {
4633 bindAppsUpdated(apps);
4634 }
4635 };
4636 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004637 return;
4638 }
4639
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004640 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004641 mAppsCustomizeContent != null) {
4642 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
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004756 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004757 mAppsCustomizeContent != null) {
4758 mAppsCustomizeContent.removeApps(appInfos);
4759 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004760 }
Joe Onoratobe386092009-11-17 17:32:16 -08004761
4762 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004763 * A number of packages were updated.
4764 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004765 private ArrayList<Object> mWidgetsAndShortcuts;
4766 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004767 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004768 bindPackagesUpdated(mWidgetsAndShortcuts);
4769 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004770 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004771 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004772 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4773 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4774 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004775 return;
4776 }
4777
Winson Chung64359a52013-07-08 17:17:08 -07004778 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004779 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004780 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004781 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004782 }
4783
Winson Chung400438b2011-01-16 17:53:48 -08004784 private int mapConfigurationOriActivityInfoOri(int configOri) {
4785 final Display d = getWindowManager().getDefaultDisplay();
4786 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4787 switch (d.getRotation()) {
4788 case Surface.ROTATION_0:
4789 case Surface.ROTATION_180:
4790 // We are currently in the same basic orientation as the natural orientation
4791 naturalOri = configOri;
4792 break;
4793 case Surface.ROTATION_90:
4794 case Surface.ROTATION_270:
4795 // We are currently in the other basic orientation to the natural orientation
4796 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4797 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4798 break;
4799 }
4800
4801 int[] oriMap = {
4802 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4803 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4804 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4805 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4806 };
4807 // Since the map starts at portrait, we need to offset if this device's natural orientation
4808 // is landscape.
4809 int indexOffset = 0;
4810 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4811 indexOffset = 1;
4812 }
4813 return oriMap[(d.getRotation() + indexOffset) % 4];
4814 }
Adam Cohen446e9402011-09-15 18:21:21 -07004815
Winson Chung4b919f82012-05-01 10:44:08 -07004816 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004817 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal7470c812015-02-10 14:28:44 -08004818 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4819 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4820 .getConfiguration().orientation));
4821 } else {
4822 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4823 }
Winson Chung4b919f82012-05-01 10:44:08 -07004824 }
4825 }
4826 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004827 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004828 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004829 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004830 } else {
4831 mHandler.postDelayed(new Runnable() {
4832 public void run() {
4833 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4834 }
4835 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004836 }
Winson Chung4b919f82012-05-01 10:44:08 -07004837 }
Winson Chung400438b2011-01-16 17:53:48 -08004838 }
4839
Winson Chunge43a1e72014-01-15 10:33:02 -08004840 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004841 if (mLauncherCallbacks != null) {
4842 return mLauncherCallbacks.isLauncherPreinstalled();
4843 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004844 PackageManager pm = getPackageManager();
4845 try {
4846 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4847 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4848 return true;
4849 } else {
4850 return false;
4851 }
4852 } catch (NameNotFoundException e) {
4853 e.printStackTrace();
4854 return false;
4855 }
4856 }
4857
Adam Cohenea90f832014-05-21 15:03:34 -07004858 /**
4859 * This method indicates whether or not we should suggest default wallpaper dimensions
4860 * when our wallpaper cropper was not yet used to set a wallpaper.
4861 */
4862 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004863 if (mLauncherCallbacks != null) {
4864 return mLauncherCallbacks.overrideWallpaperDimensions();
4865 }
Adam Cohenea90f832014-05-21 15:03:34 -07004866 return true;
4867 }
4868
Adam Cohen432609a2014-03-13 17:03:22 -07004869 /**
4870 * To be overridden by subclasses to indicate that there is an activity to launch
4871 * before showing the standard launcher experience.
4872 */
4873 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004874 if (mLauncherCallbacks != null) {
4875 return mLauncherCallbacks.hasFirstRunActivity();
4876 }
Adam Cohen432609a2014-03-13 17:03:22 -07004877 return false;
4878 }
4879
4880 /**
4881 * To be overridden by subclasses to launch any first run activity
4882 */
4883 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004884 if (mLauncherCallbacks != null) {
4885 return mLauncherCallbacks.getFirstRunActivity();
4886 }
Adam Cohen432609a2014-03-13 17:03:22 -07004887 return null;
4888 }
4889
Winson Chunga6945242014-01-08 14:04:34 -08004890 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004891 return !ActivityManager.isRunningInTestHarness() &&
4892 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004893 }
4894
Adam Cohen4a9423d2014-03-28 14:22:31 -07004895 protected boolean hasRunFirstRunActivity() {
4896 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4897 }
4898
Adam Cohen432609a2014-03-13 17:03:22 -07004899 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004900 if (shouldRunFirstRunActivity() &&
4901 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004902 Intent firstRunIntent = getFirstRunActivity();
4903 if (firstRunIntent != null) {
4904 startActivity(firstRunIntent);
4905 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004906 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004907 }
4908 }
Adam Cohen432609a2014-03-13 17:03:22 -07004909 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004910 }
4911
4912 private void markFirstRunActivityShown() {
4913 SharedPreferences.Editor editor = mSharedPrefs.edit();
4914 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4915 editor.apply();
4916 }
4917
Adam Cohen432609a2014-03-13 17:03:22 -07004918 /**
4919 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4920 * screen that must be displayed and dismissed.
4921 */
4922 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004923 if (mLauncherCallbacks != null) {
4924 return mLauncherCallbacks.hasDismissableIntroScreen();
4925 }
Adam Cohen432609a2014-03-13 17:03:22 -07004926 return false;
4927 }
4928
4929 /**
4930 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4931 */
4932 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004933 if (mLauncherCallbacks != null) {
4934 return mLauncherCallbacks.getIntroScreen();
4935 }
Adam Cohen432609a2014-03-13 17:03:22 -07004936 return null;
4937 }
4938
4939 /**
4940 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4941 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4942 */
4943 private boolean shouldShowIntroScreen() {
4944 return hasDismissableIntroScreen() &&
4945 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4946 }
4947
4948 protected void showIntroScreen() {
4949 View introScreen = getIntroScreen();
4950 changeWallpaperVisiblity(false);
4951 if (introScreen != null) {
4952 mDragLayer.showOverlayView(introScreen);
4953 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004954 if (mLauncherOverlayContainer != null) {
4955 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4956 }
Adam Cohen432609a2014-03-13 17:03:22 -07004957 }
4958
4959 public void dismissIntroScreen() {
4960 markIntroScreenDismissed();
4961 if (showFirstRunActivity()) {
4962 // We delay hiding the intro view until the first run activity is showing. This
4963 // avoids a blip.
4964 mWorkspace.postDelayed(new Runnable() {
4965 @Override
4966 public void run() {
4967 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004968 if (mLauncherOverlayContainer != null) {
4969 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4970 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004971 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004972 }
4973 }, ACTIVITY_START_DELAY);
4974 } else {
4975 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004976 if (mLauncherOverlayContainer != null) {
4977 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4978 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004979 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004980 }
4981 changeWallpaperVisiblity(true);
4982 }
4983
4984 private void markIntroScreenDismissed() {
4985 SharedPreferences.Editor editor = mSharedPrefs.edit();
4986 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4987 editor.apply();
4988 }
4989
Sunny Goyal88d60f12014-09-08 03:47:29 -07004990 private void showFirstRunClings() {
4991 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4992 // on the device, then we always show the first run cling experience (or if there is no
4993 // launcher2). Otherwise, we prompt the user upon started for migration
4994 LauncherClings launcherClings = new LauncherClings(this);
4995 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4996 if (mModel.canMigrateFromOldLauncherDb(this)) {
4997 launcherClings.showMigrationCling();
4998 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004999 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005000 }
5001 }
5002 }
5003
Winson Chunga6945242014-01-08 14:04:34 -08005004 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005005 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5006 if (mHotseat != null) mHotseat.setAlpha(1f);
5007 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5008 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005009 }
5010
5011 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005012 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5013 if (mHotseat != null) mHotseat.setAlpha(0f);
5014 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5015 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005016 }
5017
Mathew Inwood72fbec12013-11-19 15:45:07 +00005018 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005019 // Called from search suggestion, not supported in other profiles.
5020 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5021 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5022 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5023 myUser);
5024 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005025 return null;
5026 }
Kenny Guyed131872014-04-30 03:02:21 +01005027 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005028 }
5029
5030 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5031 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005032 // Called from search suggestion, not supported in other profiles.
5033 return createShortcutDragInfo(shortcutIntent, caption, icon,
5034 UserHandleCompat.myUserHandle());
5035 }
5036
5037 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5038 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005039 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005040 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005041 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005042 }
5043
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005044 protected void moveWorkspaceToDefaultScreen() {
5045 mWorkspace.moveToDefaultScreen(false);
5046 }
5047
Mathew Inwood72fbec12013-11-19 15:45:07 +00005048 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5049 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005050 mWorkspace.onExternalDragStartedWithItem(dragView);
5051 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005052 }
5053
Anjali Koppalf5d29132014-02-28 13:33:27 -08005054 @Override
5055 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005056 if (mLauncherCallbacks != null) {
5057 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5058 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005059 }
5060
Winson Chung80baf5a2010-08-09 16:03:15 -07005061 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005062 * Prints out out state for debugging.
5063 */
5064 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005065 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005066 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005067 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5068 Log.d(TAG, "mRestoring=" + mRestoring);
5069 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5070 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005071 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005072 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005073
Winson Chung785d2eb2011-04-14 16:08:02 -07005074 if (mAppsCustomizeContent != null) {
5075 mAppsCustomizeContent.dumpState();
5076 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005077 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005078 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005079
5080 @Override
5081 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5082 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005083 synchronized (sDumpLogs) {
5084 writer.println(" ");
5085 writer.println("Debug logs: ");
5086 for (int i = 0; i < sDumpLogs.size(); i++) {
5087 writer.println(" " + sDumpLogs.get(i));
5088 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005089 }
Adam Cohen9211d422014-10-07 18:14:53 -07005090 if (mLauncherCallbacks != null) {
5091 mLauncherCallbacks.dump(prefix, fd, writer, args);
5092 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005093 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005094
5095 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005096 if (DEBUG_DUMP_LOG) {
5097 synchronized (sDumpLogs) {
5098 Log.d(TAG, "");
5099 Log.d(TAG, "*********************");
5100 Log.d(TAG, "Launcher debug logs: ");
5101 for (int i = 0; i < sDumpLogs.size(); i++) {
5102 Log.d(TAG, " " + sDumpLogs.get(i));
5103 }
5104 Log.d(TAG, "*********************");
5105 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005106 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005107 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005108 }
5109
5110 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005111 addDumpLog(tag, log, null, debugLog);
5112 }
5113
5114 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005115 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005116 if (e != null) {
5117 Log.d(tag, log, e);
5118 } else {
5119 Log.d(tag, log);
5120 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005121 }
Winson Chungede41292013-09-19 16:27:36 -07005122 if (DEBUG_DUMP_LOG) {
5123 sDateStamp.setTime(System.currentTimeMillis());
5124 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005125 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5126 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005127 }
Winson Chungede41292013-09-19 16:27:36 -07005128 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005129 }
5130
Adam Cohen59400422014-03-05 18:07:04 -08005131 public static CustomAppWidget getCustomAppWidget(String name) {
5132 return sCustomAppWidgets.get(name);
5133 }
5134
5135 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
5136 return sCustomAppWidgets;
5137 }
5138
Winson Chungede41292013-09-19 16:27:36 -07005139 public void dumpLogsToLocalData() {
5140 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005141 new AsyncTask<Void, Void, Void>() {
5142 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005143 boolean success = false;
5144 sDateStamp.setTime(sRunStart);
5145 String FILENAME = sDateStamp.getMonth() + "-"
5146 + sDateStamp.getDay() + "_"
5147 + sDateStamp.getHours() + "-"
5148 + sDateStamp.getMinutes() + "_"
5149 + sDateStamp.getSeconds() + ".txt";
5150
5151 FileOutputStream fos = null;
5152 File outFile = null;
5153 try {
5154 outFile = new File(getFilesDir(), FILENAME);
5155 outFile.createNewFile();
5156 fos = new FileOutputStream(outFile);
5157 } catch (Exception e) {
5158 e.printStackTrace();
5159 }
5160 if (fos != null) {
5161 PrintWriter writer = new PrintWriter(fos);
5162
5163 writer.println(" ");
5164 writer.println("Debug logs: ");
5165 synchronized (sDumpLogs) {
5166 for (int i = 0; i < sDumpLogs.size(); i++) {
5167 writer.println(" " + sDumpLogs.get(i));
5168 }
5169 }
5170 writer.close();
5171 }
5172 try {
5173 if (fos != null) {
5174 fos.close();
5175 success = true;
5176 }
5177 } catch (IOException e) {
5178 e.printStackTrace();
5179 }
Michael Jurka43467462013-11-14 12:03:58 +01005180 return null;
Winson Chungede41292013-09-19 16:27:36 -07005181 }
Michael Jurka43467462013-11-14 12:03:58 +01005182 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005183 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005184 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005185}
Michael Jurka2763be32011-02-24 11:19:57 -08005186
Michael Jurkaabded662011-03-04 12:06:57 -08005187interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005188 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005189 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005190 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005191 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005192 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005193}
Dan Sandlerd5024042014-01-09 15:01:33 -05005194
5195interface DebugIntents {
5196 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5197 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005198}