blob: 7e2ae4ede9f94e4db6b795c7f9371e3a9f67d8fe [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Adam Cohen2854d252014-08-27 16:04:07 -070025import android.animation.TimeInterpolator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000027import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070029import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070053import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.Bitmap;
55import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070056import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen2854d252014-08-27 16:04:07 -070085import android.view.ViewAnimationUtils;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070087import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070089import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080091import android.view.accessibility.AccessibilityEvent;
Adam Cohen9bfdb762014-07-21 17:44:06 -070092import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Advanceable;
96import android.widget.FrameLayout;
97import android.widget.ImageView;
Adam Cohen96d30a12013-07-16 18:13:21 -070098import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070099
Sunny Goyal651077b2014-06-30 14:15:31 -0700100import com.android.launcher3.DropTarget.DragObject;
101import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherActivityInfoCompat;
104import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700105import com.android.launcher3.compat.PackageInstallerCompat;
106import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100107import com.android.launcher3.compat.UserHandleCompat;
108import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700109
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.io.DataInputStream;
111import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700113import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700118import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700120import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700121import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700122import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700123import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700124import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000125import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127/**
128 * Default launcher application.
129 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100130public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700131 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700132 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800133 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700134 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Daniel Sandlerf061f822013-06-27 13:59:36 -0400136 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400137 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700138 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700140 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700141
Dan Sandlerd5024042014-01-09 15:01:33 -0500142 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700148 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Michael Jurka8b805b12012-04-18 14:23:14 -0700150 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700151 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700152
Mathew Inwood876a8462013-06-14 14:12:41 +0100153 /**
154 * IntentStarter uses request codes starting with this. This must be greater than all activity
155 * request codes used internally.
156 */
157 protected static final int REQUEST_LAST = 100;
158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
160
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800161 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Michael Jurka0a457bf2012-11-19 14:05:05 -0800163 // To turn on these properties, type
164 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800165 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800166 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Winson Chung2672ff92012-05-04 16:22:30 -0700168 // The Intent extra that defines whether to ignore the launch animation
169 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400170 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 // Type: int
173 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700174 // Type: int
175 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700177 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
178 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
180 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: boolean
185 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
186 // Type: long
187 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700188 // Type: int
189 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
192 // Type: parcelable
193 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000194 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: int[]
197 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
Adam Cohen432609a2014-03-13 17:03:22 -0700199 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800200 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
201
Adam Cohen3ed316a2014-07-23 18:21:20 -0700202 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
203 static final String ACTION_FIRST_LOAD_COMPLETE =
204 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
205
Adam Cohen39a06042013-07-19 14:30:12 -0700206 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700207 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700208
Sunny Goyal594d76d2014-11-06 10:12:54 -0800209 private static final String QSB_WIDGET_ID = "qsb_widget_id";
210 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
211
Winson Chunga6945242014-01-08 14:04:34 -0800212 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
213
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700214 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700215 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700216 private State mState = State.WORKSPACE;
217 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700218
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700219 private boolean mIsSafeModeEnabled;
220
Adam Cohenc2d6e892014-10-16 09:49:24 -0700221 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
222 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700223 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700224
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700226 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
227 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700228 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000230 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
231 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
232
Winson Chunga2413752012-04-03 14:22:34 -0700233 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700234 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
235 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700236 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700237 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700238
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800239 private final BroadcastReceiver mCloseSystemDialogsReceiver
240 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800241 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 private LayoutInflater mInflater;
244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700246 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800247 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800248 private DragLayer mDragLayer;
249 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700250 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700251
Sunny Goyalffe83f12014-08-14 17:39:34 -0700252 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700253 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700254
Michael Jurkac9d95c52011-08-29 14:03:34 -0700255 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800256 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800257 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700258
Michael Jurka0280c3b2010-09-17 15:00:07 -0700259 private int[] mTmpAddItemCellCoordinates = new int[2];
260
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 private FolderInfo mFolderInfo;
262
Winson Chung3d503fb2011-07-13 17:25:49 -0700263 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800264 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700265
Michael Jurka838a4ca2011-02-07 13:33:06 -0800266 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700267
Winson Chungc51db6a2011-10-05 11:44:49 -0700268 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700269 private AppsCustomizeTabHost mAppsCustomizeTabHost;
270 private AppsCustomizePagedView mAppsCustomizeContent;
271 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800272 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700275 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
276 // scroll issues (because the workspace may not have been measured yet) and extra work.
277 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
278 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279
280 private SpannableStringBuilder mDefaultKeySsb = null;
281
Joe Onorato9c1289c2009-08-17 11:03:03 -0400282 private boolean mWorkspaceLoading = true;
283
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800284 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 private boolean mRestoring;
286 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700287 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288
Michael Jurka1e2f4652013-07-08 18:03:46 -0700289 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700290 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
291
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 private Bundle mSavedInstanceState;
293
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800295 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800296 private boolean mUserPresent = true;
297 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700298 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800299 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400300
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700301 private static LocaleConfiguration sLocaleConfiguration = null;
302
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700303 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700304
Adam Cohen61f560d2013-09-30 15:58:20 -0700305 private View.OnTouchListener mHapticFeedbackTouchListener;
306
Adam Cohen80e6beb2015-02-13 16:14:33 -0800307 public static final int BUILD_LAYER = 0;
308 public static final int BUILD_AND_SET_LAYER = 1;
309
Adam Cohended9f8d2010-11-03 13:25:16 -0700310 // Related to the auto-advancing of widgets
311 private final int ADVANCE_MSG = 1;
312 private final int mAdvanceInterval = 20000;
313 private final int mAdvanceStagger = 250;
314 private long mAutoAdvanceSentTime;
315 private long mAutoAdvanceTimeLeft = -1;
316 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
317 new HashMap<View, AppWidgetProviderInfo>();
318
Winson Chung400438b2011-01-16 17:53:48 -0800319 // Determines how long to wait after a rotation before restoring the screen orientation to
320 // match the sensor state.
321 private final int mRestoreScreenOrientationDelay = 500;
322
Craig Mautner360310b2012-10-26 15:13:08 -0700323 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700324
Adam Cohen1462de32012-07-24 22:34:36 -0700325 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700326 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700327
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700328 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700329 static Date sDateStamp = new Date();
330 static DateFormat sDateFormat =
331 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
332 static long sRunStart = System.currentTimeMillis();
333 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700334
Winson Chung46353de2012-02-16 14:05:10 -0800335 // We only want to get the SharedPreferences once since it does an FS stat each time we get
336 // it from the context.
337 private SharedPreferences mSharedPrefs;
338
Winson Chungf0c6ae02012-03-21 16:10:31 -0700339 // Holds the page that we need to animate to, and the icon views that we need to animate up
340 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700341 private ImageView mFolderIconImageView;
342 private Bitmap mFolderIconBitmap;
343 private Canvas mFolderIconCanvas;
344 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700345
Michael Jurkaddd62e92011-02-16 17:49:14 -0800346 private BubbleTextView mWaitingForResume;
347
Adam Cohen59400422014-03-05 18:07:04 -0800348 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
349 new HashMap<String, CustomAppWidget>();
350
351 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
352 static {
353 if (ENABLE_CUSTOM_WIDGET_TEST) {
354 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
355 }
356 }
357
Michael Jurkac1f5d262011-09-30 19:32:27 -0700358 private Runnable mBuildLayersRunnable = new Runnable() {
359 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700360 if (mWorkspace != null) {
361 mWorkspace.buildPageHardwareLayers();
362 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700363 }
364 };
365
Adam Cohendb364c32014-05-20 14:23:40 -0700366 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800367
368 private static class PendingAddArguments {
369 int requestCode;
370 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700371 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700372 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800373 int cellX;
374 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700375 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800376 }
377
Daniel Sandlerff02d492013-08-05 02:12:05 -0400378 private Stats mStats;
379
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700380 FocusIndicatorView mFocusHandler;
381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 @Override
383 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700384 if (DEBUG_STRICT_MODE) {
385 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
386 .detectDiskReads()
387 .detectDiskWrites()
388 .detectNetwork() // or .detectAll() for all detectable problems
389 .penaltyLog()
390 .build());
391 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
392 .detectLeakedSqlLiteObjects()
393 .detectLeakedClosableObjects()
394 .penaltyLog()
395 .penaltyDeath()
396 .build());
397 }
398
Adam Cohen9211d422014-10-07 18:14:53 -0700399 if (mLauncherCallbacks != null) {
400 mLauncherCallbacks.preOnCreate();
401 }
402
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400404
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400405 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400406 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700407 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700408
Winson Chung5f8afe62013-08-12 16:19:28 -0700409 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700410 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700411
412 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400413 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700414 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700415 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800416 mModel = app.setLauncher(this);
417 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700418 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400419 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700421
Daniel Sandlerff02d492013-08-05 02:12:05 -0400422 mStats = new Stats(this);
423
Sunny Goyalffe83f12014-08-14 17:39:34 -0700424 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700425
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700426 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
427 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700428
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700429 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
430 // this also ensures that any synchronous binding below doesn't re-trigger another
431 // LauncherModel load.
432 mPaused = false;
433
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200435 android.os.Debug.startMethodTracing(
436 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 }
438
439 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700441
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100443 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800445 registerContentObservers();
446
Joe Onorato7c312c12009-08-13 21:36:53 -0700447 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 mSavedState = savedInstanceState;
450 restoreState(mSavedState);
451
452 if (PROFILE_STARTUP) {
453 android.os.Debug.stopMethodTracing();
454 }
455
456 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700457 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700458 // If the user leaves launcher, then we should just load items asynchronously when
459 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500460 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 } else {
462 // We only load the page synchronously if the user rotates (or triggers a
463 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800464 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 }
467
468 // For handling default keys
469 mDefaultKeySsb = new SpannableStringBuilder();
470 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800471
472 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
473 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800474
Adam Cohenf9426d52012-06-04 17:26:21 -0700475 // On large interfaces, we want the screen to auto-rotate based on the current orientation
476 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700477
Adam Cohen9211d422014-10-07 18:14:53 -0700478 if (mLauncherCallbacks != null) {
479 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700480 if (mLauncherCallbacks.hasLauncherOverlay()) {
481 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700482 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
483 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
484 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700485 mWorkspace.setLauncherOverlay(mLauncherOverlay);
486 }
Adam Cohen9211d422014-10-07 18:14:53 -0700487 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700488
489 if (shouldShowIntroScreen()) {
490 showIntroScreen();
491 } else {
492 showFirstRunActivity();
493 showFirstRunClings();
494 }
Adam Cohen9211d422014-10-07 18:14:53 -0700495 }
496
497 private LauncherCallbacks mLauncherCallbacks;
498
499 public void onPostCreate(Bundle savedInstanceState) {
500 super.onPostCreate(savedInstanceState);
501 if (mLauncherCallbacks != null) {
502 mLauncherCallbacks.onPostCreate(savedInstanceState);
503 }
504 }
505
506 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
507 mLauncherCallbacks = callbacks;
508 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700509 }
510
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700511 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700512 public void onLauncherProviderChange() {
513 if (mLauncherCallbacks != null) {
514 mLauncherCallbacks.onLauncherProviderChange();
515 }
516 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700517
Adam Cohen9211d422014-10-07 18:14:53 -0700518 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700519 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700520 if (mLauncherCallbacks != null) {
521 return mLauncherCallbacks.hasCustomContentToLeft();
522 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700523 return false;
524 }
525
Dave Hawkeya8881582013-09-17 15:55:33 -0600526 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500527 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600528 * {@link #addToCustomContentPage}. This will only be invoked if
529 * {@link #hasCustomContentToLeft()} is {@code true}.
530 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500531 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700532 if (mLauncherCallbacks != null) {
533 mLauncherCallbacks.populateCustomContentContainer();
534 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600535 }
536
537 /**
538 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
539 * ensure the custom content page is added or removed if necessary.
540 */
541 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600542 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600543 // Not bound yet, wait for bindScreens to be called.
544 return;
545 }
546
547 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
548 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500549 mWorkspace.createCustomContentContainer();
550 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600551 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
552 mWorkspace.removeCustomContentPage();
553 }
554 }
555
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800556 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700557 if (sLocaleConfiguration == null) {
558 new AsyncTask<Void, Void, LocaleConfiguration>() {
559 @Override
560 protected LocaleConfiguration doInBackground(Void... unused) {
561 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
562 readConfiguration(Launcher.this, localeConfiguration);
563 return localeConfiguration;
564 }
565
566 @Override
567 protected void onPostExecute(LocaleConfiguration result) {
568 sLocaleConfiguration = result;
569 checkForLocaleChange(); // recursive, but now with a locale configuration
570 }
571 }.execute();
572 return;
573 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700574
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 final Configuration configuration = getResources().getConfiguration();
576
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700577 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 final String locale = configuration.locale.toString();
579
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700580 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 final int mcc = configuration.mcc;
582
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700583 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 final int mnc = configuration.mnc;
585
Romain Guy84f296c2009-11-04 15:00:44 -0800586 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587
Romain Guy84f296c2009-11-04 15:00:44 -0800588 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700589 sLocaleConfiguration.locale = locale;
590 sLocaleConfiguration.mcc = mcc;
591 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700592
Joe Onorato0589f0f2010-02-08 13:44:00 -0800593 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700594
595 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100596 new AsyncTask<Void, Void, Void>() {
597 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700598 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100599 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700600 }
Michael Jurka43467462013-11-14 12:03:58 +0100601 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700602 }
603 }
604
605 private static class LocaleConfiguration {
606 public String locale;
607 public int mcc = -1;
608 public int mnc = -1;
609 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700610
Romain Guy98d01652009-06-30 16:21:04 -0700611 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
612 DataInputStream in = null;
613 try {
Helena Josol28db2802014-10-09 17:04:09 +0100614 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700615 configuration.locale = in.readUTF();
616 configuration.mcc = in.readInt();
617 configuration.mnc = in.readInt();
618 } catch (FileNotFoundException e) {
619 // Ignore
620 } catch (IOException e) {
621 // Ignore
622 } finally {
623 if (in != null) {
624 try {
625 in.close();
626 } catch (IOException e) {
627 // Ignore
628 }
629 }
630 }
631 }
632
633 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
634 DataOutputStream out = null;
635 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100636 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100637 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700638 out.writeUTF(configuration.locale);
639 out.writeInt(configuration.mcc);
640 out.writeInt(configuration.mnc);
641 out.flush();
642 } catch (FileNotFoundException e) {
643 // Ignore
644 } catch (IOException e) {
645 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100646 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700647 } finally {
648 if (out != null) {
649 try {
650 out.close();
651 } catch (IOException e) {
652 // Ignore
653 }
654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 }
656 }
657
Anton Hanssona2f665f2013-09-26 12:18:32 +0100658 public Stats getStats() {
659 return mStats;
660 }
661
Bjorn Bringertc459e522013-06-07 19:36:01 +0100662 public LayoutInflater getInflater() {
663 return mInflater;
664 }
665
Winson Chung36a62fe2012-05-06 18:04:42 -0700666 boolean isDraggingEnabled() {
667 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
668 // that is subsequently removed from the workspace in startBinding().
669 return !mModel.isLoadingWorkspace();
670 }
671
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800672 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000673 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100674 if (Build.VERSION.SDK_INT >= 17) {
675 return View.generateViewId();
676 } else {
677 // View.generateViewId() is not available. The following fallback logic is a copy
678 // of its implementation.
679 for (;;) {
680 final int result = sNextGeneratedId.get();
681 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
682 int newValue = result + 1;
683 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
684 if (sNextGeneratedId.compareAndSet(result, newValue)) {
685 return result;
686 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000687 }
688 }
689 }
690
691 public int getViewIdForItem(ItemInfo info) {
692 // This cast is safe given the > 2B range for int.
693 int itemId = (int) info.id;
694 if (mItemIdToViewId.containsKey(itemId)) {
695 return mItemIdToViewId.get(itemId);
696 }
697 int viewId = generateViewId();
698 mItemIdToViewId.put(itemId, viewId);
699 return viewId;
700 }
701
702 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700703 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
704 * a configuration step, this allows the proper animations to run after other transitions.
705 */
Adam Cohendb364c32014-05-20 14:23:40 -0700706 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700707 long screenId = args.screenId;
708 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
709 // When the screen id represents an actual screen (as opposed to a rank) we make sure
710 // that the drop page actually exists.
711 screenId = ensurePendingDropLayoutExists(args.screenId);
712 }
Adam Cohendb364c32014-05-20 14:23:40 -0700713
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800714 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800715 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700716 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700717 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700718 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800719 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700720 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700721 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700722 case REQUEST_RECONFIGURE_APPWIDGET:
723 completeRestoreAppWidget(args.appWidgetId);
724 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800725 }
Michael Jurka27614d22012-04-02 06:40:22 -0700726 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
727 // if you turned the screen off and then back while in All Apps, Launcher would not
728 // return to the workspace. Clearing mAddInfo.container here fixes this issue
729 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700730 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800731 }
732
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800733 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700734 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700735 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700736 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700737 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800738 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700739
Adam Cohenad4e15c2013-10-17 16:21:35 -0700740 Runnable exitSpringLoaded = new Runnable() {
741 @Override
742 public void run() {
743 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
744 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
745 }
746 };
747
Michael Jurka8b805b12012-04-18 14:23:14 -0700748 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700749 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700750 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
751 if (resultCode == RESULT_CANCELED) {
752 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700753 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700754 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700755 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800756 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700757 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700758 }
759 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200760 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
761 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
762 mWorkspace.exitOverviewMode(false);
763 }
764 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200766
Adam Cohened66b2b2012-01-23 17:28:51 -0800767 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
768 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700769
Adam Cohendb364c32014-05-20 14:23:40 -0700770 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800771 // We have special handling for widgets
772 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800773 final int appWidgetId;
774 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
775 : -1;
776 if (widgetId < 0) {
777 appWidgetId = pendingAddWidgetId;
778 } else {
779 appWidgetId = widgetId;
780 }
781
Adam Cohenad4e15c2013-10-17 16:21:35 -0700782 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800783 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700784 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
785 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700786 result = RESULT_CANCELED;
787 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700788 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700789 @Override
790 public void run() {
791 exitSpringLoadedDragModeDelayed(false, 0, null);
792 }
793 };
Adam Cohendb364c32014-05-20 14:23:40 -0700794 if (workspaceLocked) {
795 // No need to remove the empty screen if we're mid-binding, as the
796 // the bind will not add the empty screen.
797 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
798 } else {
799 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
800 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
801 }
Winson Chung5aaab772012-04-27 15:24:02 -0700802 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700803 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700804 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
805 // When the screen id represents an actual screen (as opposed to a rank)
806 // we make sure that the drop page actually exists.
807 mPendingAddInfo.screenId =
808 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
809 }
Adam Cohendb364c32014-05-20 14:23:40 -0700810 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
811
812 dropLayout.setDropPending(true);
813 final Runnable onComplete = new Runnable() {
814 @Override
815 public void run() {
816 completeTwoStageWidgetDrop(resultCode, appWidgetId);
817 dropLayout.setDropPending(false);
818 }
819 };
820 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
821 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
822 } else {
823 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
824 mPendingAddInfo);
825 sPendingAddItem = args;
826 }
Winson Chung5aaab772012-04-27 15:24:02 -0700827 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800828 return;
829 }
830
Sunny Goyalff572272014-07-23 13:58:07 -0700831 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
832 if (resultCode == RESULT_OK) {
833 // Update the widget view.
834 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
835 pendingAddWidgetId, mPendingAddInfo);
836 if (workspaceLocked) {
837 sPendingAddItem = args;
838 } else {
839 completeAdd(args);
840 }
841 }
842 // Leave the widget in the pending state if the user canceled the configure.
843 return;
844 }
845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 // The pattern used here is that a user PICKs a specific application,
847 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700848
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
850 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700851 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700852 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
853 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800854 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700855 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800856 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700857 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700858 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
859 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 }
Adam Cohen00074722013-10-11 17:46:09 -0700861 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700862 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700863 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800865 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800866
867 }
868
869 @Override
870 protected void onActivityResult(
871 final int requestCode, final int resultCode, final Intent data) {
872 handleActivityResult(requestCode, resultCode, data);
873 if (mLauncherCallbacks != null) {
874 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
875 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800876 }
877
Adam Cohendb364c32014-05-20 14:23:40 -0700878 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
879 appWidgetId, ItemInfo info) {
880 PendingAddArguments args = new PendingAddArguments();
881 args.requestCode = requestCode;
882 args.intent = data;
883 args.container = info.container;
884 args.screenId = info.screenId;
885 args.cellX = info.cellX;
886 args.cellY = info.cellY;
887 args.appWidgetId = appWidgetId;
888 return args;
889 }
890
891 /**
892 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
893 *
894 * @param screenId the screen id to check
895 * @return the new screen, or screenId if it exists
896 */
897 private long ensurePendingDropLayoutExists(long screenId) {
898 CellLayout dropLayout =
899 (CellLayout) mWorkspace.getScreenWithId(screenId);
900 if (dropLayout == null) {
901 // it's possible that the add screen was removed because it was
902 // empty and a re-bind occurred
903 mWorkspace.addExtraEmptyScreen();
904 return mWorkspace.commitExtraEmptyScreen();
905 } else {
906 return screenId;
907 }
908 }
909
Adam Cohened66b2b2012-01-23 17:28:51 -0800910 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700911 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700912 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 Runnable onCompleteRunnable = null;
914 int animationType = 0;
915
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700916 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 if (resultCode == RESULT_OK) {
918 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
919 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700920 mPendingAddWidgetInfo);
921 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800922 onCompleteRunnable = new Runnable() {
923 @Override
924 public void run() {
925 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700926 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700927 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
928 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 }
930 };
931 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800932 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700935 if (mDragLayer.getAnimatedView() != null) {
936 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
937 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
938 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700939 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700940 // The animated view may be null in the case of a rotation during widget configuration
941 onCompleteRunnable.run();
942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 }
944
945 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700946 protected void onStop() {
947 super.onStop();
948 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700949
950 if (mLauncherCallbacks != null) {
951 mLauncherCallbacks.onStop();
952 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700953 }
954
955 @Override
956 protected void onStart() {
957 super.onStart();
958 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700959
960 if (mLauncherCallbacks != null) {
961 mLauncherCallbacks.onStart();
962 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700963 }
964
965 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200967 long startTime = 0;
968 if (DEBUG_RESUME_TIME) {
969 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400970 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200971 }
Adam Cohen9211d422014-10-07 18:14:53 -0700972
973 if (mLauncherCallbacks != null) {
974 mLauncherCallbacks.preOnResume();
975 }
976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700978
Winson Chung4a2afa32012-07-19 14:53:05 -0700979 // Restore the previous launcher state
980 if (mOnResumeState == State.WORKSPACE) {
981 showWorkspace(false);
982 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800983 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700984 }
985 mOnResumeState = State.NONE;
986
Craig Mautner360310b2012-10-26 15:13:08 -0700987 // Background was set to gradient in onPause(), restore to black if in all apps.
988 setWorkspaceBackground(mState == State.WORKSPACE);
989
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800990 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700991 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700992 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500993 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700995 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700997 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200998 // We might have postponed some bind calls until onResume (see waitUntilResume) --
999 // execute them here
1000 long startTimeCallbacks = 0;
1001 if (DEBUG_RESUME_TIME) {
1002 startTimeCallbacks = System.currentTimeMillis();
1003 }
1004
1005 if (mAppsCustomizeContent != null) {
1006 mAppsCustomizeContent.setBulkBind(true);
1007 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001008 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1009 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001010 }
1011 if (mAppsCustomizeContent != null) {
1012 mAppsCustomizeContent.setBulkBind(false);
1013 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001014 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 if (DEBUG_RESUME_TIME) {
1016 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1017 (System.currentTimeMillis() - startTimeCallbacks));
1018 }
Michael Jurka447bf842013-05-15 14:52:15 +02001019 }
Michael Jurka54554252013-08-01 12:52:23 +02001020 if (mOnResumeCallbacks.size() > 0) {
1021 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1022 mOnResumeCallbacks.get(i).run();
1023 }
1024 mOnResumeCallbacks.clear();
1025 }
Winson Chunge4e50662012-01-23 14:45:13 -08001026
1027 // Reset the pressed state of icons that were locked in the press state while activities
1028 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001029 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001030 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001031 mWaitingForResume.setStayPressed(false);
1032 }
Winson Chung82963d52013-10-09 11:20:57 -07001033
Adam Cohen06dff352012-06-01 17:17:08 -07001034 // It is possible that widgets can receive updates while launcher is not in the foreground.
1035 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1036 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1037 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001038 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001039
Winson Chung780fe592013-09-26 14:48:44 -07001040 // Process any items that were added while Launcher was away.
1041 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1042
Michael Jurka447bf842013-05-15 14:52:15 +02001043 if (DEBUG_RESUME_TIME) {
1044 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1045 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001046
1047 if (mWorkspace.getCustomContentCallbacks() != null) {
1048 // If we are resuming and the custom content is the current page, we call onShow().
1049 // It is also poassible that onShow will instead be called slightly after first layout
1050 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1051 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001052 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001053 }
1054 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001055 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001056 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001057
1058 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001059
1060 if (mLauncherCallbacks != null) {
1061 mLauncherCallbacks.onResume();
1062 }
Adam Cohen06dff352012-06-01 17:17:08 -07001063 }
1064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001066 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001067 // Ensure that items added to Launcher are queued until Launcher returns
1068 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001069 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001070
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001071 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001072 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001073 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001074 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001075
1076 // We call onHide() aggressively. The custom content callbacks should be able to
1077 // debounce excess onHide calls.
1078 if (mWorkspace.getCustomContentCallbacks() != null) {
1079 mWorkspace.getCustomContentCallbacks().onHide();
1080 }
Romain Guycbb89e42009-06-08 15:52:54 -07001081
Adam Cohen9211d422014-10-07 18:14:53 -07001082 if (mLauncherCallbacks != null) {
1083 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001084 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001085 }
1086
1087 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001088 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1089 // by a onResume or by scrolling otherwise.
1090 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001091
1092 // Custom content is completely hidden
1093 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001094
1095 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1096 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001097
1098 // Indicates whether the user is allowed to scroll away from the custom content.
1099 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001100 }
1101
Adam Cohenc2d6e892014-10-16 09:49:24 -07001102 public interface LauncherOverlay {
1103
1104 /**
1105 * Touch interaction leading to overscroll has begun
1106 */
1107 public void onScrollInteractionBegin();
1108
1109 /**
1110 * Touch interaction related to overscroll has ended
1111 */
1112 public void onScrollInteractionEnd();
1113
1114 /**
1115 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1116 * screen (or in the case of RTL, the rightmost screen).
1117 */
1118 public void onScrollChange(int progress, boolean rtl);
1119
1120 /**
1121 * Screen has stopped scrolling
1122 */
1123 public void onScrollSettled();
1124
1125 /**
1126 * This method can be called by the Launcher in order to force the LauncherOverlay
1127 * to exit fully immersive mode.
1128 */
1129 public void forceExitFullImmersion();
1130 }
1131
1132 public interface LauncherOverlayCallbacks {
1133 /**
1134 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1135 * however it doesn't modify any state within the launcher.
1136 */
1137 public boolean canEnterFullImmersion();
1138
1139 /**
1140 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1141 * eg. by occupying the full screen and handling all touch events.
1142 *
1143 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1144 * case, Launcher will modify any necessary state and assumes the overlay is
1145 * handling all interaction. If false, the LauncherOverlay should cancel any
1146 *
1147 */
1148 public boolean enterFullImmersion();
1149
1150 /**
1151 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1152 * full control over UI and state.
1153 */
1154 public void exitFullImmersion();
1155 }
1156
1157 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1158
1159 @Override
1160 public boolean canEnterFullImmersion() {
1161 return mState == State.WORKSPACE;
1162 }
1163
1164 @Override
1165 public boolean enterFullImmersion() {
1166 if (mState == State.WORKSPACE) {
1167 // When fully immersed, disregard any touches which fall through.
1168 mDragLayer.setBlockTouch(true);
1169 return true;
1170 }
1171 return false;
1172 }
1173
1174 @Override
1175 public void exitFullImmersion() {
1176 mDragLayer.setBlockTouch(false);
1177 }
1178 }
1179
Jorim Jaggid017f882014-01-14 17:08:48 -08001180 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001181 if (mLauncherCallbacks != null) {
1182 return mLauncherCallbacks.hasSettings();
1183 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001184 return false;
1185 }
1186
Adam Cohenbffe7452013-07-22 18:21:45 -07001187
Adam Cohen9211d422014-10-07 18:14:53 -07001188 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001189 CustomContentCallbacks callbacks, String description) {
1190 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001191 }
1192
Adam Cohenedb40762013-07-18 16:45:45 -07001193 // The custom content needs to offset its content to account for the QSB
1194 public int getTopOffsetForCustomContent() {
1195 return mWorkspace.getPaddingTop();
1196 }
1197
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001198 @Override
1199 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001201 if (mModel.isCurrentCallbacks(this)) {
1202 mModel.stopLoader();
1203 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001204 if (mAppsCustomizeContent != null) {
1205 mAppsCustomizeContent.surrender();
1206 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001207 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001208 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001209
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001210 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001211 @Override
1212 public void onWindowFocusChanged(boolean hasFocus) {
1213 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001214 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001215
1216 if (mLauncherCallbacks != null) {
1217 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1218 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001219 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 private boolean acceptFilter() {
1222 final InputMethodManager inputManager = (InputMethodManager)
1223 getSystemService(Context.INPUT_METHOD_SERVICE);
1224 return !inputManager.isFullscreenMode();
1225 }
1226
1227 @Override
1228 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001229 final int uniChar = event.getUnicodeChar();
1230 final boolean handled = super.onKeyDown(keyCode, event);
1231 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1232 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1234 keyCode, event);
1235 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001236 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001237 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001238 // showSearchDialog()
1239 // If there are multiple keystrokes before the search dialog takes focus,
1240 // onSearchRequested() will be called for every keystroke,
1241 // but it is idempotent, so it's fine.
1242 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
1244 }
1245
Joe Onorato8a9625e2010-01-28 15:55:35 -08001246 // Eat the long press event so the keyboard doesn't come up.
1247 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1248 return true;
1249 }
1250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 return handled;
1252 }
1253
Karl Rosaen138a0412009-04-23 19:00:21 -07001254 private String getTypedText() {
1255 return mDefaultKeySsb.toString();
1256 }
1257
1258 private void clearTypedText() {
1259 mDefaultKeySsb.clear();
1260 mDefaultKeySsb.clearSpans();
1261 Selection.setSelection(mDefaultKeySsb, 0);
1262 }
1263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001265 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1266 * State
1267 */
1268 private static State intToState(int stateOrdinal) {
1269 State state = State.WORKSPACE;
1270 final State[] stateValues = State.values();
1271 for (int i = 0; i < stateValues.length; i++) {
1272 if (stateValues[i].ordinal() == stateOrdinal) {
1273 state = stateValues[i];
1274 break;
1275 }
1276 }
1277 return state;
1278 }
1279
1280 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 * Restores the previous state, if it exists.
1282 *
1283 * @param savedState The previous state.
1284 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001285 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 private void restoreState(Bundle savedState) {
1287 if (savedState == null) {
1288 return;
1289 }
1290
Michael Jurka883f55b2010-10-21 15:47:14 -07001291 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001292 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001293 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001294 }
1295
Adam Cohen21cd0022013-10-09 18:57:02 -07001296 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1297 PagedView.INVALID_RESTORE_PAGE);
1298 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001299 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 }
1301
Winson Chung3d503fb2011-07-13 17:25:49 -07001302 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001303 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001304
Winson Chung3d503fb2011-07-13 17:25:49 -07001305 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1306 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001307 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001308 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1309 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001310 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1311 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1312 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001313 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001314 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 mRestoring = true;
1316 }
1317
1318 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1319 if (renameFolder) {
1320 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001321 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 mRestoring = true;
1323 }
Winson Chunga12a2502010-12-20 14:41:35 -08001324
Winson Chung785d2eb2011-04-14 16:08:02 -07001325 // Restore the AppsCustomize tab
1326 if (mAppsCustomizeTabHost != null) {
1327 String curTab = savedState.getString("apps_customize_currentTab");
1328 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001329 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001330 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001331 mAppsCustomizeContent.loadAssociatedPages(
1332 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001333 }
1334
Winson Chung5afbf7b2011-07-25 11:53:08 -07001335 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1336 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001337 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001338 mItemIdToViewId = (HashMap<Integer, Integer>)
1339 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 }
1341
1342 /**
1343 * Finds all the views we need and configure them properly.
1344 */
1345 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001346 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001347
Craig Mautner360310b2012-10-26 15:13:08 -07001348 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001349 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001350 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1351 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001352 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001353 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001354
John Spurlock77e1f472013-09-11 10:09:51 -04001355 mLauncherView.setSystemUiVisibility(
1356 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001357 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358
Winson Chung4c98d922011-05-31 16:50:48 -07001359 // Setup the drag layer
1360 mDragLayer.setup(this, dragController);
1361
Winson Chung3d503fb2011-07-13 17:25:49 -07001362 // Setup the hotseat
1363 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1364 if (mHotseat != null) {
1365 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001366 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001367 }
1368
Jorim Jaggid017f882014-01-14 17:08:48 -08001369 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001370 View widgetButton = findViewById(R.id.widget_button);
1371 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001372 @Override
1373 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001374 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001375 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001376 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001377 }
1378 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001379 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1380
1381 View wallpaperButton = findViewById(R.id.wallpaper_button);
1382 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001383 @Override
1384 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001385 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001386 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001387 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001388 }
1389 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001390 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1391
1392 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001393 if (hasSettings()) {
1394 settingsButton.setOnClickListener(new OnClickListener() {
1395 @Override
1396 public void onClick(View arg0) {
1397 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001398 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001399 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001400 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001401 });
1402 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1403 } else {
1404 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001405 }
1406
Adam Cohendbdff6b2013-09-18 19:09:15 -07001407 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001408
Winson Chung4c98d922011-05-31 16:50:48 -07001409 // Setup the workspace
1410 mWorkspace.setHapticFeedbackEnabled(false);
1411 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001412 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001413 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001414
Winson Chungf0ea4d32011-06-06 14:27:16 -07001415 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001416 mSearchDropTargetBar = (SearchDropTargetBar)
1417 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001418
Winson Chungf0ea4d32011-06-06 14:27:16 -07001419 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001420 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001421 mAppsCustomizeContent = (AppsCustomizePagedView)
1422 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1423 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001424
Winson Chung3d503fb2011-07-13 17:25:49 -07001425 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001426 dragController.setDragScoller(mWorkspace);
1427 dragController.setScrollView(mDragLayer);
1428 dragController.setMoveTarget(mWorkspace);
1429 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001430 if (mSearchDropTargetBar != null) {
1431 mSearchDropTargetBar.setup(this, dragController);
Sunny Goyal594d76d2014-11-06 10:12:54 -08001432 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001433 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001434
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001435 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001436 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001437 mWeightWatcher = new WeightWatcher(this);
1438 mWeightWatcher.setAlpha(0.5f);
1439 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001440 new FrameLayout.LayoutParams(
1441 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001442 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001443 Gravity.BOTTOM)
1444 );
Adam Cohen39a06042013-07-19 14:30:12 -07001445
1446 boolean show = shouldShowWeightWatcher();
1447 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001448 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 }
1450
1451 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001452 * Sets the all apps button. This method is called from {@link Hotseat}.
1453 */
1454 public void setAllAppsButton(View allAppsButton) {
1455 mAllAppsButton = allAppsButton;
1456 }
1457
1458 public View getAllAppsButton() {
1459 return mAllAppsButton;
1460 }
1461
1462 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 * Creates a view representing a shortcut.
1464 *
1465 * @param info The data structure describing the shortcut.
1466 *
1467 * @return A View inflated from R.layout.application.
1468 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001469 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001471 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473
1474 /**
1475 * Creates a view representing a shortcut inflated from the specified resource.
1476 *
1477 * @param layoutResId The id of the XML layout used to create the shortcut.
1478 * @param parent The group the shortcut belongs to.
1479 * @param info The data structure describing the shortcut.
1480 *
1481 * @return A View inflated from layoutResId.
1482 */
Adam Cohen59400422014-03-05 18:07:04 -08001483 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001484 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001485 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001487 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 return favorite;
1489 }
1490
1491 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 * Add a shortcut to the workspace.
1493 *
1494 * @param data The intent describing the shortcut.
1495 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001497 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001498 int cellY) {
1499 int[] cellXY = mTmpAddItemCellCoordinates;
1500 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001501 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001502
Michael Jurkad3ef3062010-11-23 16:23:58 -08001503 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001504
Sunny Goyal2350bc92014-10-14 16:42:54 -07001505 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001506 if (info == null) {
1507 return;
1508 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001509 final View view = createShortcut(info);
1510
Adam Cohenfbba09b2011-07-18 21:43:05 -07001511 // First we check if we already know the exact location where we want to add this item.
1512 if (cellX >= 0 && cellY >= 0) {
1513 cellXY[0] = cellX;
1514 cellXY[1] = cellY;
1515 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001516
1517 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001518 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001519 true, null,null)) {
1520 return;
1521 }
1522 DragObject dragObject = new DragObject();
1523 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001524 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1525 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001526 return;
1527 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001528 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001529 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001530 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001531 foundCellSpan = (result != null);
1532 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001533 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001534 }
1535
1536 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001537 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001538 return;
1539 }
1540
Adam Cohendcd297f2013-06-18 13:13:40 -07001541 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542
1543 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001545 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
1547 }
1548
Adam Cohen2f093b62012-04-30 18:59:53 -07001549 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1550 int minHeight) {
1551 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001552 // We want to account for the extra amount of padding that we are adding to the widget
1553 // to ensure that it gets the full amount of space that it has requested
1554 int requiredWidth = minWidth + padding.left + padding.right;
1555 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001556 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001557 }
1558
Adam Cohen2f093b62012-04-30 18:59:53 -07001559 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1560 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001561 }
1562
Adam Cohen2f093b62012-04-30 18:59:53 -07001563 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1564 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001565 }
1566
Adam Cohen2f093b62012-04-30 18:59:53 -07001567 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1568 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001569 }
1570
Adam Cohen2f093b62012-04-30 18:59:53 -07001571 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1572 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001573 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001574 }
1575
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001577 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001579 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 */
Adam Cohen59400422014-03-05 18:07:04 -08001581 private void completeAddAppWidget(int appWidgetId, long container, long screenId,
1582 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1583
1584 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001585 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001586 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1587 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001588 }
Romain Guycbb89e42009-06-08 15:52:54 -07001589
Adam Cohen59400422014-03-05 18:07:04 -08001590 if (appWidgetInfo.isCustomWidget) {
1591 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001592 }
1593
Adam Cohen59400422014-03-05 18:07:04 -08001594 LauncherAppWidgetInfo launcherInfo;
1595 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1596 launcherInfo.spanX = info.spanX;
1597 launcherInfo.spanY = info.spanY;
1598 launcherInfo.minSpanX = info.minSpanX;
1599 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001600 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001601
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001603 container, screenId, info.cellX, info.cellY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604
1605 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001606 if (hostView == null) {
1607 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001608 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1609 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001610 } else {
1611 // The AppWidgetHostView has already been inflated and instantiated
1612 launcherInfo.hostView = hostView;
1613 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001615 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001616 launcherInfo.notifyWidgetSizeChanged(this);
1617
Adam Cohen59400422014-03-05 18:07:04 -08001618 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1619 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001620
1621 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001623 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 }
Romain Guycbb89e42009-06-08 15:52:54 -07001625
Adam Cohended9f8d2010-11-03 13:25:16 -07001626 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1627 @Override
1628 public void onReceive(Context context, Intent intent) {
1629 final String action = intent.getAction();
1630 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1631 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001632 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001633 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001634
Winson Chungc100e8e2011-08-09 16:02:43 -07001635 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001636 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001637 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001638 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001639 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001640 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1641 mUserPresent = true;
1642 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001643 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1644 mModel.resetLoadedState(false, true);
1645 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1646 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1647 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1648 mModel.resetLoadedState(false, true);
1649 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1650 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1651 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001652 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1653 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1654 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001655 }
1656 }
1657 };
1658
1659 @Override
1660 public void onAttachedToWindow() {
1661 super.onAttachedToWindow();
1662
1663 // Listen for broadcasts related to user-presence
1664 final IntentFilter filter = new IntentFilter();
1665 filter.addAction(Intent.ACTION_SCREEN_OFF);
1666 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001667 // For handling managed profiles
1668 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1669 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001670 if (ENABLE_DEBUG_INTENTS) {
1671 filter.addAction(DebugIntents.DELETE_DATABASE);
1672 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1673 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001674 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001675 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001676 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001677 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001678 mVisible = true;
1679 }
1680
Adam Cohen0b395352014-06-09 22:54:36 +00001681 /**
1682 * Sets up transparent navigation and status bars in LMP.
1683 * This method is a no-op for other platform versions.
1684 */
Sunny Goyald0091012015-01-20 15:55:34 -08001685 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001686 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001687 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001688 Window window = getWindow();
1689 window.getAttributes().systemUiVisibility |=
1690 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1691 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1692 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1693 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1694 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1695 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1696 window.setStatusBarColor(Color.TRANSPARENT);
1697 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001698 }
1699 }
1700
Adam Cohended9f8d2010-11-03 13:25:16 -07001701 @Override
1702 public void onDetachedFromWindow() {
1703 super.onDetachedFromWindow();
1704 mVisible = false;
1705
Adam Cohend113e0c2010-11-11 10:48:05 -08001706 if (mAttached) {
1707 unregisterReceiver(mReceiver);
1708 mAttached = false;
1709 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001710 updateRunning();
1711 }
1712
1713 public void onWindowVisibilityChanged(int visibility) {
1714 mVisible = visibility == View.VISIBLE;
1715 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001716 // The following code used to be in onResume, but it turns out onResume is called when
1717 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1718 // is a more appropriate event to handle
1719 if (mVisible) {
1720 mAppsCustomizeTabHost.onWindowVisible();
1721 if (!mWorkspaceLoading) {
1722 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001723 // We want to let Launcher draw itself at least once before we force it to build
1724 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001725 // apps is nice and speedy.
1726 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001727 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001728 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001729 if (mStarted) return;
1730 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001731 // We delay the layer building a bit in order to give
1732 // other message processing a time to run. In particular
1733 // this avoids a delay in hiding the IME if it was
1734 // currently shown, because doing that may involve
1735 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001736 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001737 final ViewTreeObserver.OnDrawListener listener = this;
1738 mWorkspace.post(new Runnable() {
1739 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001740 if (mWorkspace != null &&
1741 mWorkspace.getViewTreeObserver() != null) {
1742 mWorkspace.getViewTreeObserver().
1743 removeOnDrawListener(listener);
1744 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001745 }
1746 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001747 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001748 }
1749 });
1750 }
1751 clearTypedText();
1752 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001753 }
1754
1755 private void sendAdvanceMessage(long delay) {
1756 mHandler.removeMessages(ADVANCE_MSG);
1757 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1758 mHandler.sendMessageDelayed(msg, delay);
1759 mAutoAdvanceSentTime = System.currentTimeMillis();
1760 }
1761
1762 private void updateRunning() {
1763 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1764 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1765 mAutoAdvanceRunning = autoAdvanceRunning;
1766 if (autoAdvanceRunning) {
1767 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1768 sendAdvanceMessage(delay);
1769 } else {
1770 if (!mWidgetsToAdvance.isEmpty()) {
1771 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1772 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1773 }
1774 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001775 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001776 }
1777 }
1778 }
1779
1780 private final Handler mHandler = new Handler() {
1781 @Override
1782 public void handleMessage(Message msg) {
1783 if (msg.what == ADVANCE_MSG) {
1784 int i = 0;
1785 for (View key: mWidgetsToAdvance.keySet()) {
1786 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1787 final int delay = mAdvanceStagger * i;
1788 if (v instanceof Advanceable) {
1789 postDelayed(new Runnable() {
1790 public void run() {
1791 ((Advanceable) v).advance();
1792 }
1793 }, delay);
1794 }
1795 i++;
1796 }
1797 sendAdvanceMessage(mAdvanceInterval);
1798 }
1799 }
1800 };
1801
1802 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001803 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001804 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1805 if (v instanceof Advanceable) {
1806 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001807 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001808 updateRunning();
1809 }
1810 }
1811
1812 void removeWidgetToAutoAdvance(View hostView) {
1813 if (mWidgetsToAdvance.containsKey(hostView)) {
1814 mWidgetsToAdvance.remove(hostView);
1815 updateRunning();
1816 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001817 }
1818
Joe Onorato9c1289c2009-08-17 11:03:03 -04001819 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001820 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001821 launcherInfo.hostView = null;
1822 }
1823
Winson Chung93eef082012-03-23 15:59:27 -07001824 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1825 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1826 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001827 }
1828
Adam Cohen59400422014-03-05 18:07:04 -08001829 public ArrayList<AppInfo> getAllAppsList() {
1830 return mAppsCustomizeContent.getApps();
1831 }
1832
Winson Chunga6945242014-01-08 14:04:34 -08001833 public DragLayer getDragLayer() {
1834 return mDragLayer;
1835 }
1836
1837 public Workspace getWorkspace() {
1838 return mWorkspace;
1839 }
1840
1841 public Hotseat getHotseat() {
1842 return mHotseat;
1843 }
1844
Jorim Jaggid017f882014-01-14 17:08:48 -08001845 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001846 return mOverviewPanel;
1847 }
1848
1849 public SearchDropTargetBar getSearchBar() {
1850 return mSearchDropTargetBar;
1851 }
1852
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001853 public LauncherAppWidgetHost getAppWidgetHost() {
1854 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 }
Romain Guycbb89e42009-06-08 15:52:54 -07001856
Winson Chunga9abd0e2010-10-27 17:18:37 -07001857 public LauncherModel getModel() {
1858 return mModel;
1859 }
1860
Winson Chunga6945242014-01-08 14:04:34 -08001861 protected SharedPreferences getSharedPrefs() {
1862 return mSharedPrefs;
1863 }
1864
Bjorn Bringertc459e522013-06-07 19:36:01 +01001865 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001866 getWindow().closeAllPanels();
1867
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001868 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001869 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001870 }
1871
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 @Override
1873 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001874 long startTime = 0;
1875 if (DEBUG_RESUME_TIME) {
1876 startTime = System.currentTimeMillis();
1877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 super.onNewIntent(intent);
1879
1880 // Close the menu
1881 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001882 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001883 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001884
Adam Cohened307df2013-10-02 09:37:31 -07001885 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1886 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1887 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001888
Adam Cohen6fecd412013-10-02 17:41:50 -07001889 if (mWorkspace == null) {
1890 // Can be cases where mWorkspace is null, this prevents a NPE
1891 return;
1892 }
1893 Folder openFolder = mWorkspace.getOpenFolder();
1894 // In all these cases, only animate if we're already on home
1895 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001896
1897 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1898 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001899 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001900 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001901 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001902 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001903
Adam Cohen6fecd412013-10-02 17:41:50 -07001904 closeFolder();
1905 exitSpringLoadedDragMode();
1906
1907 // If we are already on home, then just animate back to the workspace,
1908 // otherwise, just wait until onResume to set the state back to Workspace
1909 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001910 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001911 } else {
1912 mOnResumeState = State.WORKSPACE;
1913 }
1914
1915 final View v = getWindow().peekDecorView();
1916 if (v != null && v.getWindowToken() != null) {
1917 InputMethodManager imm = (InputMethodManager)getSystemService(
1918 INPUT_METHOD_SERVICE);
1919 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1920 }
1921
1922 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001923 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001924 mAppsCustomizeTabHost.reset();
1925 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001926
Adam Cohen9211d422014-10-07 18:14:53 -07001927 if (mLauncherCallbacks != null) {
1928 mLauncherCallbacks.onHomeIntent();
1929 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 }
Adam Cohened307df2013-10-02 09:37:31 -07001931
Michael Jurka447bf842013-05-15 14:52:15 +02001932 if (DEBUG_RESUME_TIME) {
1933 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1934 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935
Adam Cohen9211d422014-10-07 18:14:53 -07001936 if (mLauncherCallbacks != null) {
1937 mLauncherCallbacks.onNewIntent(intent);
1938 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001939 }
1940
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001942 public void onRestoreInstanceState(Bundle state) {
1943 super.onRestoreInstanceState(state);
1944 for (int page: mSynchronouslyBoundPages) {
1945 mWorkspace.restoreInstanceStateForChild(page);
1946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 }
1948
1949 @Override
1950 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001951 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001952 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1953 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001954 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001955 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956
Michael Jurka883f55b2010-10-21 15:47:14 -07001957 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001958 // We close any open folder since it will not be re-opened, and we need to make sure
1959 // this state is reflected.
1960 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961
Adam Cohendcd297f2013-06-18 13:13:40 -07001962 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001963 mWaitingForResult) {
1964 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001965 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001966 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1967 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001968 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1969 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1970 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001971 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
1974 if (mFolderInfo != null && mWaitingForResult) {
1975 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1976 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1977 }
Michael Jurka946ad472010-07-09 18:05:18 -07001978
Winson Chung785d2eb2011-04-14 16:08:02 -07001979 // Save the current AppsCustomize tab
1980 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001981 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1982 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001983 if (currentTabTag != null) {
1984 outState.putString("apps_customize_currentTab", currentTabTag);
1985 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001986 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1987 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001988 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001989 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07001990
1991 if (mLauncherCallbacks != null) {
1992 mLauncherCallbacks.onSaveInstanceState(outState);
1993 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 }
1995
1996 @Override
1997 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001999
Winson Chunge7a03942011-08-05 15:05:12 -07002000 // Remove all pending runnables
2001 mHandler.removeMessages(ADVANCE_MSG);
2002 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002003 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002004
Winson Chungcd2b0142011-06-08 16:02:26 -07002005 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002006 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002007
2008 // It's possible to receive onDestroy after a new Launcher activity has
2009 // been created. In this case, don't interfere with the new Launcher.
2010 if (mModel.isCurrentCallbacks(this)) {
2011 mModel.stopLoader();
2012 app.setLauncher(null);
2013 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002016 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002018 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002020 mAppWidgetHost = null;
2021
2022 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023
2024 TextKeyListener.getInstance().release();
2025
Winson Chung81b52252012-08-27 15:34:29 -07002026 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2027 // to prevent leaking Launcher activities on orientation change.
2028 if (mModel != null) {
2029 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2030 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002031
2032 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002033 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002034
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002035 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002036 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002037 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002038 mWorkspace = null;
2039 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002040
Michael Jurka2ecf9952012-06-18 12:52:28 -07002041 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002042
2043 if (mLauncherCallbacks != null) {
2044 mLauncherCallbacks.onDestroy();
2045 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 }
2047
Adam Cohena9cf38f2011-05-02 15:36:58 -07002048 public DragController getDragController() {
2049 return mDragController;
2050 }
2051
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 @Override
2053 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002054 if (requestCode >= 0) {
2055 setWaitingForResult(true);
2056 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 super.startActivityForResult(intent, requestCode);
2058 }
2059
Winson Chung70d72102011-08-12 11:24:35 -07002060 /**
2061 * Indicates that we want global search for this activity by setting the globalSearch
2062 * argument for {@link #startSearch} to true.
2063 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002065 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002067
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002068 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002069
Karl Rosaen138a0412009-04-23 19:00:21 -07002070 if (initialQuery == null) {
2071 // Use any text typed in the launcher as the initial query
2072 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 if (appSearchData == null) {
2075 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002076 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 }
Winson Chungadf0c182012-08-23 14:59:07 -07002078 Rect sourceBounds = new Rect();
2079 if (mSearchDropTargetBar != null) {
2080 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2081 }
Romain Guycbb89e42009-06-08 15:52:54 -07002082
Ian Parkinson047036e2014-09-01 15:40:53 +01002083 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002084 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002085 if (clearTextImmediately) {
2086 clearTypedText();
2087 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002088 }
2089
Ian Parkinson047036e2014-09-01 15:40:53 +01002090 /**
2091 * Start a text search.
2092 *
2093 * @return {@code true} if the search will start immediately, so any further keypresses
2094 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2095 * to buffer keypresses.
2096 */
2097 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002098 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002099 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2100 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2101 sourceBounds);
2102 }
2103
Michael Jurkaa33411c2012-06-14 16:18:21 -07002104 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002105 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002106 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002107 }
2108
2109 /**
2110 * Starts the global search activity. This code is a copied from SearchManager
2111 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002112 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002113 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002114 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002115 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2116 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2117 if (globalSearchActivity == null) {
2118 Log.w(TAG, "No global search activity found.");
2119 return;
2120 }
2121 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2122 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2123 intent.setComponent(globalSearchActivity);
2124 // Make sure that we have a Bundle to put source in
2125 if (appSearchData == null) {
2126 appSearchData = new Bundle();
2127 } else {
2128 appSearchData = new Bundle(appSearchData);
2129 }
Adam Cohen9211d422014-10-07 18:14:53 -07002130 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002131 if (!appSearchData.containsKey("source")) {
2132 appSearchData.putString("source", getPackageName());
2133 }
2134 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2135 if (!TextUtils.isEmpty(initialQuery)) {
2136 intent.putExtra(SearchManager.QUERY, initialQuery);
2137 }
2138 if (selectInitialQuery) {
2139 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2140 }
2141 intent.setSourceBounds(sourceBounds);
2142 try {
2143 startActivity(intent);
2144 } catch (ActivityNotFoundException ex) {
2145 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2146 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 }
2148
Yura4f93ec62014-02-04 14:15:21 +00002149 public boolean isOnCustomContent() {
2150 return mWorkspace.isOnOrMovingToCustomContent();
2151 }
2152
Winson Chung70d72102011-08-12 11:24:35 -07002153 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002154 public boolean onPrepareOptionsMenu(Menu menu) {
2155 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002156 if (!isOnCustomContent()) {
2157 // Close any open folders
2158 closeFolder();
2159 // Stop resizing any widgets
2160 mWorkspace.exitWidgetResizeMode();
2161 if (!mWorkspace.isInOverviewMode()) {
2162 // Show the overview mode
2163 showOverviewMode(true);
2164 } else {
2165 showWorkspace(true);
2166 }
Winson Chunge0298742014-01-17 12:03:00 -08002167 }
Adam Cohen9211d422014-10-07 18:14:53 -07002168 if (mLauncherCallbacks != null) {
2169 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2170 }
2171
Michael Jurkab94f3f82013-09-11 18:08:54 +02002172 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002175 @Override
2176 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002177 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002178 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002179 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002180 }
2181
Joe Onorato9c1289c2009-08-17 11:03:03 -04002182 public boolean isWorkspaceLocked() {
2183 return mWorkspaceLoading || mWaitingForResult;
2184 }
2185
Adam Cohen517a7f52014-03-01 12:12:59 -08002186 public boolean isWorkspaceLoading() {
2187 return mWorkspaceLoading;
2188 }
2189
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002190 private void setWorkspaceLoading(boolean value) {
2191 boolean isLocked = isWorkspaceLocked();
2192 mWorkspaceLoading = value;
2193 if (isLocked != isWorkspaceLocked()) {
2194 onWorkspaceLockedChanged();
2195 }
2196 }
2197
2198 private void setWaitingForResult(boolean value) {
2199 boolean isLocked = isWorkspaceLocked();
2200 mWaitingForResult = value;
2201 if (isLocked != isWorkspaceLocked()) {
2202 onWorkspaceLockedChanged();
2203 }
2204 }
2205
Adam Cohen9211d422014-10-07 18:14:53 -07002206 protected void onWorkspaceLockedChanged() {
2207 if (mLauncherCallbacks != null) {
2208 mLauncherCallbacks.onWorkspaceLockedChanged();
2209 }
2210 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002211
Michael Jurka0280c3b2010-09-17 15:00:07 -07002212 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002213 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002214 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002215 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2216 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002217 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002218 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002219 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002220
Sunny Goyale6b63a32015-01-16 16:14:29 -08002221 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002222 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002223 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2224 }
2225
Sunny Goyale6b63a32015-01-16 16:14:29 -08002226 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002227 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2228 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002229 if (appWidgetInfo.configure != null) {
2230 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002231 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002232
Bjorn Bringert7984c942009-12-09 15:38:25 +00002233 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002234 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2235 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002238 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002239 Runnable onComplete = new Runnable() {
2240 @Override
2241 public void run() {
2242 // Exit spring loaded mode if necessary after adding the widget
2243 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2244 null);
2245 }
2246 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002247 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002248 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002249 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 }
2251 }
Romain Guycbb89e42009-06-08 15:52:54 -07002252
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002253 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002254 // Close any folders that may be open.
2255 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002256 mWorkspace.moveToCustomContentScreen(animate);
2257 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002258
2259 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2260 int[] cell, int spanX, int spanY) {
2261 switch (info.itemType) {
2262 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2263 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2264 int span[] = new int[2];
2265 span[0] = spanX;
2266 span[1] = spanY;
2267 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2268 container, screenId, cell, span);
2269 break;
2270 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2271 processShortcutFromDrop(info.componentName, container, screenId, cell);
2272 break;
2273 default:
2274 throw new IllegalStateException("Unknown item type: " + info.itemType);
2275 }
2276 }
2277
Adam Cohenfbba09b2011-07-18 21:43:05 -07002278 /**
2279 * Process a shortcut drop.
2280 *
2281 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002282 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002283 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002285 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2286 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002287 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002288 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002289 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002290 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002291
2292 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002293 mPendingAddInfo.cellX = cell[0];
2294 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002295 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002296
2297 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2298 createShortcutIntent.setComponent(componentName);
2299 processShortcut(createShortcutIntent);
2300 }
2301
Adam Cohenfbba09b2011-07-18 21:43:05 -07002302 /**
2303 * Process a widget drop.
2304 *
2305 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002306 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002307 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002308 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002309 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2310 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002311 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002312 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002313 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002314 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002315 mPendingAddInfo.minSpanX = info.minSpanX;
2316 mPendingAddInfo.minSpanY = info.minSpanY;
2317
Adam Cohenfbba09b2011-07-18 21:43:05 -07002318 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002319 mPendingAddInfo.cellX = cell[0];
2320 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002321 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002322 if (span != null) {
2323 mPendingAddInfo.spanX = span[0];
2324 mPendingAddInfo.spanY = span[1];
2325 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002326
Adam Cohened66b2b2012-01-23 17:28:51 -08002327 AppWidgetHostView hostView = info.boundWidget;
2328 int appWidgetId;
2329 if (hostView != null) {
2330 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002331 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002332 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002333 // In this case, we either need to start an activity to get permission to bind
2334 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002335 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002336 Bundle options = info.bindOptions;
2337
Sunny Goyalffe83f12014-08-14 17:39:34 -07002338 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2339 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002340 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002341 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002342 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002343 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002344 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2345 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2346 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002347 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2348 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002349 // TODO: we need to make sure that this accounts for the options bundle.
2350 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002351 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2352 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002353 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002354 }
2355
Joe Onoratodeb98af2010-02-19 14:59:39 -08002356 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002357 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 }
2359
Winson Chung24ab2f12010-09-16 14:10:47 -07002360 void processWallpaper(Intent intent) {
2361 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2362 }
2363
Adam Cohendcd297f2013-06-18 13:13:40 -07002364 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002365 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002366 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 folderInfo.title = getText(R.string.folder_name);
2368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002370 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002371 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002372 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002373
2374 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002375 FolderIcon newFolder =
2376 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002377 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002378 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002379 // Force measure the new folder icon
2380 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2381 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002382 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 }
Romain Guycbb89e42009-06-08 15:52:54 -07002384
Joe Onorato9c1289c2009-08-17 11:03:03 -04002385 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002386 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002387 }
2388
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002389 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002390 if (mLauncherCallbacks != null) {
2391 return mLauncherCallbacks.getWallpaperPickerComponent();
2392 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002393 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002394 }
2395
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002396 /**
2397 * Registers various content observers. The current implementation registers
2398 * only a favorites observer to keep track of the favorites applications.
2399 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002400 private void registerContentObservers() {
2401 ContentResolver resolver = getContentResolver();
2402 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2403 true, mWidgetObserver);
2404 }
2405
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 @Override
2407 public boolean dispatchKeyEvent(KeyEvent event) {
2408 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2409 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002411 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002412 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002413 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002414 dumpState();
2415 return true;
2416 }
2417 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002418 }
2419 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2420 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002421 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 return true;
2423 }
2424 }
2425
2426 return super.dispatchKeyEvent(event);
2427 }
2428
Joe Onorato88ec0992009-11-19 13:16:06 -08002429 @Override
2430 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002431 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2432 return;
2433 }
2434
Winson Chungc93e5ae2012-07-23 20:48:26 -07002435 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002436 if (mAppsCustomizeContent.getContentType() ==
2437 AppsCustomizePagedView.ContentType.Applications) {
2438 showWorkspace(true);
2439 } else {
2440 showOverviewMode(true);
2441 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002442 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002443 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002444 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002445 Folder openFolder = mWorkspace.getOpenFolder();
2446 if (openFolder.isEditingName()) {
2447 openFolder.dismissEditingName();
2448 } else {
2449 closeFolder();
2450 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002451 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002452 mWorkspace.exitWidgetResizeMode();
2453
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002454 // Back button is a no-op here, but give at least some feedback for the button press
2455 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002456 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002457 }
2458
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002460 * Re-listen when widgets are reset.
2461 */
2462 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002463 if (mAppWidgetHost != null) {
2464 mAppWidgetHost.startListening();
2465 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002466 }
2467
2468 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002469 * Launches the intent referred by the clicked shortcut.
2470 *
2471 * @param v The view representing the clicked shortcut.
2472 */
2473 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002474 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2475 // view has detached (it's possible for this to happen if the view is removed mid touch).
2476 if (v.getWindowToken() == null) {
2477 return;
2478 }
2479
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002480 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002481 return;
2482 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002483
Adam Cohen1697b792013-09-17 19:08:21 -07002484 if (v instanceof Workspace) {
2485 if (mWorkspace.isInOverviewMode()) {
2486 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002487 }
2488 return;
2489 }
2490
2491 if (v instanceof CellLayout) {
2492 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002493 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002494 }
2495 }
2496
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002498 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002499 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002501 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002502 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002503 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002504 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002505 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002506 } else if (tag instanceof AppInfo) {
2507 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002508 } else if (tag instanceof LauncherAppWidgetInfo) {
2509 if (v instanceof PendingAppWidgetHostView) {
2510 onClickPendingWidget((PendingAppWidgetHostView) v);
2511 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002512 }
2513 }
2514
Sunny Goyal508da152014-08-14 10:53:27 -07002515 public void onClickPagedViewIcon(View v) {
2516 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002517 if (mLauncherCallbacks != null) {
2518 mLauncherCallbacks.onClickPagedViewIcon(v);
2519 }
Sunny Goyal508da152014-08-14 10:53:27 -07002520 }
2521
Michael Jurka0e260592010-06-30 17:07:39 -07002522 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002523 return false;
2524 }
2525
Michael Jurkaaf442092010-06-10 17:01:57 -07002526 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002527 * Event handler for the app widget view which has not fully restored.
2528 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002529 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002530 if (mIsSafeModeEnabled) {
2531 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2532 return;
2533 }
2534
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002535 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002536 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002537 int widgetId = info.appWidgetId;
2538 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2539 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002540 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2541 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002542 mPendingAddInfo.copyFrom(info);
2543 mPendingAddWidgetId = widgetId;
2544
Sunny Goyalffe83f12014-08-14 17:39:34 -07002545 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2546 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002547 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002548 } else if (info.installProgress < 0) {
2549 // The install has not been queued
2550 final String packageName = info.providerName.getPackageName();
2551 showBrokenAppInstallDialog(packageName,
2552 new DialogInterface.OnClickListener() {
2553 public void onClick(DialogInterface dialog, int id) {
2554 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2555 }
2556 });
2557 } else {
2558 // Download has started.
2559 final String packageName = info.providerName.getPackageName();
2560 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002561 }
2562 }
2563
2564 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002565 * Event handler for the "grid" button that appears on the home screen, which
2566 * enters all apps mode.
2567 *
2568 * @param v The view that was clicked.
2569 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002570 protected void onClickAllAppsButton(View v) {
2571 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2572 if (isAllAppsVisible()) {
2573 showWorkspace(true);
2574 } else {
2575 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2576 }
Adam Cohen9211d422014-10-07 18:14:53 -07002577 if (mLauncherCallbacks != null) {
2578 mLauncherCallbacks.onClickAllAppsButton(v);
2579 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002580 }
2581
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002582 private void showBrokenAppInstallDialog(final String packageName,
2583 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002584 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002585 .setTitle(R.string.abandoned_promises_title)
2586 .setMessage(R.string.abandoned_promise_explanation)
2587 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2588 .setNeutralButton(R.string.abandoned_clean_this,
2589 new DialogInterface.OnClickListener() {
2590 public void onClick(DialogInterface dialog, int id) {
2591 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2592 mWorkspace.removeAbandonedPromise(packageName, user);
2593 }
2594 })
2595 .create().show();
2596 return;
2597 }
2598
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002599 /**
2600 * Event handler for an app shortcut click.
2601 *
2602 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2603 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002604 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002605 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2606 Object tag = v.getTag();
2607 if (!(tag instanceof ShortcutInfo)) {
2608 throw new IllegalArgumentException("Input must be a Shortcut");
2609 }
2610
2611 // Open shortcut
2612 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002613
2614 if (shortcut.isDisabled != 0) {
2615 int error = R.string.activity_not_available;
2616 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2617 error = R.string.safemode_shortcut_error;
2618 }
2619 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2620 return;
2621 }
2622
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002623 final Intent intent = shortcut.intent;
2624
2625 // Check for special shortcuts
2626 if (intent.getComponent() != null) {
2627 final String shortcutClass = intent.getComponent().getClassName();
2628
2629 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2630 MemoryDumpActivity.startDump(this);
2631 return;
2632 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2633 toggleShowWeightWatcher();
2634 return;
2635 }
2636 }
2637
Chris Wren40c5ed32014-06-24 18:24:23 -04002638 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002639 if ((v instanceof BubbleTextView)
2640 && shortcut.isPromise()
2641 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002642 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002643 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002644 new DialogInterface.OnClickListener() {
2645 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002646 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002647 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002648 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 return;
2650 }
2651
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002652 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002653 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002654
2655 if (mLauncherCallbacks != null) {
2656 mLauncherCallbacks.onClickAppShortcut(v);
2657 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002658 }
2659
Sunny Goyal508da152014-08-14 10:53:27 -07002660 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002661 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002662 final ShortcutInfo shortcut;
2663 final Intent intent;
2664 if (tag instanceof ShortcutInfo) {
2665 shortcut = (ShortcutInfo) tag;
2666 intent = shortcut.intent;
2667 int[] pos = new int[2];
2668 v.getLocationOnScreen(pos);
2669 intent.setSourceBounds(new Rect(pos[0], pos[1],
2670 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002671
Sunny Goyal508da152014-08-14 10:53:27 -07002672 } else if (tag instanceof AppInfo) {
2673 shortcut = null;
2674 intent = ((AppInfo) tag).intent;
2675 } else {
2676 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2677 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002678
2679 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002680 mStats.recordLaunch(intent, shortcut);
2681
2682 if (success && v instanceof BubbleTextView) {
2683 mWaitingForResume = (BubbleTextView) v;
2684 mWaitingForResume.setStayPressed(true);
2685 }
2686 }
2687
2688 /**
2689 * Event handler for a folder icon click.
2690 *
2691 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2692 */
2693 protected void onClickFolderIcon(View v) {
2694 if (LOGD) Log.d(TAG, "onClickFolder");
2695 if (!(v instanceof FolderIcon)){
2696 throw new IllegalArgumentException("Input must be a FolderIcon");
2697 }
2698
2699 FolderIcon folderIcon = (FolderIcon) v;
2700 final FolderInfo info = folderIcon.getFolderInfo();
2701 Folder openFolder = mWorkspace.getFolderForTag(info);
2702
2703 // If the folder info reports that the associated folder is open, then verify that
2704 // it is actually opened. There have been a few instances where this gets out of sync.
2705 if (info.opened && openFolder == null) {
2706 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2707 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2708 info.opened = false;
2709 }
2710
2711 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2712 // Close any open folder
2713 closeFolder();
2714 // Open the requested folder
2715 openFolder(folderIcon);
2716 } else {
2717 // Find the open folder...
2718 int folderScreen;
2719 if (openFolder != null) {
2720 folderScreen = mWorkspace.getPageForView(openFolder);
2721 // .. and close it
2722 closeFolder(openFolder);
2723 if (folderScreen != mWorkspace.getCurrentPage()) {
2724 // Close any folder open on the current screen
2725 closeFolder();
2726 // Pull the folder onto this screen
2727 openFolder(folderIcon);
2728 }
2729 }
2730 }
Adam Cohen9211d422014-10-07 18:14:53 -07002731
2732 if (mLauncherCallbacks != null) {
2733 mLauncherCallbacks.onClickFolderIcon(v);
2734 }
Michael Jurka5130e402011-10-13 04:55:35 -07002735 }
2736
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002737 /**
2738 * Event handler for the (Add) Widgets button that appears after a long press
2739 * on the home screen.
2740 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002741 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002742 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002743 if (mIsSafeModeEnabled) {
2744 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2745 } else {
2746 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2747 if (mLauncherCallbacks != null) {
2748 mLauncherCallbacks.onClickAddWidgetButton(view);
2749 }
Adam Cohen9211d422014-10-07 18:14:53 -07002750 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002751 }
2752
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002753 /**
2754 * Event handler for the wallpaper picker button that appears after a long press
2755 * on the home screen.
2756 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002757 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002758 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2759 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2760 pickWallpaper.setComponent(getWallpaperPickerComponent());
2761 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002762
2763 if (mLauncherCallbacks != null) {
2764 mLauncherCallbacks.onClickWallpaperPicker(v);
2765 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002766 }
2767
2768 /**
2769 * Event handler for a click on the settings button that appears after a long press
2770 * on the home screen.
2771 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002772 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002773 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002774 if (mLauncherCallbacks != null) {
2775 mLauncherCallbacks.onClickSettingsButton(v);
2776 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002777 }
2778
Michael Jurka5130e402011-10-13 04:55:35 -07002779 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002780 // Provide the same haptic feedback that the system offers for virtual keys.
2781 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002782 }
2783
Adam Cohen61f560d2013-09-30 15:58:20 -07002784 public void performHapticFeedbackOnTouchDown(View v) {
2785 // Provide the same haptic feedback that the system offers for virtual keys.
2786 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2787 }
2788
2789 public View.OnTouchListener getHapticFeedbackTouchListener() {
2790 if (mHapticFeedbackTouchListener == null) {
2791 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2792 @Override
2793 public boolean onTouch(View v, MotionEvent event) {
2794 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2795 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2796 }
2797 return false;
2798 }
2799 };
2800 }
2801 return mHapticFeedbackTouchListener;
2802 }
2803
Adam Cohen9211d422014-10-07 18:14:53 -07002804 public void onDragStarted(View view) {
2805 if (isOnCustomContent()) {
2806 // Custom content screen doesn't participate in drag and drop. If on custom
2807 // content screen, move to default.
2808 moveWorkspaceToDefaultScreen();
2809 }
2810
2811 if (mLauncherCallbacks != null) {
2812 mLauncherCallbacks.onDragStarted(view);
2813 }
2814 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002815
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002816 /**
2817 * Called when the user stops interacting with the launcher.
2818 * This implies that the user is now on the homescreen and is not doing housekeeping.
2819 */
Adam Cohen9211d422014-10-07 18:14:53 -07002820 protected void onInteractionEnd() {
2821 if (mLauncherCallbacks != null) {
2822 mLauncherCallbacks.onInteractionEnd();
2823 }
2824 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002825
2826 /**
2827 * Called when the user starts interacting with the launcher.
2828 * The possible interactions are:
2829 * - open all apps
2830 * - reorder an app shortcut, or a widget
2831 * - open the overview mode.
2832 * This is a good time to stop doing things that only make sense
2833 * when the user is on the homescreen and not doing housekeeping.
2834 */
Adam Cohen9211d422014-10-07 18:14:53 -07002835 protected void onInteractionBegin() {
2836 if (mLauncherCallbacks != null) {
2837 mLauncherCallbacks.onInteractionBegin();
2838 }
2839 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002840
Kenny Guyf07af7b2014-07-31 11:39:16 +01002841 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002842 try {
2843 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002844 launcherApps.showAppDetailsForProfile(componentName, user);
2845 } catch (SecurityException e) {
2846 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2847 Log.e(TAG, "Launcher does not have permission to launch settings");
2848 } catch (ActivityNotFoundException e) {
2849 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2850 Log.e(TAG, "Unable to launch settings");
2851 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002852 }
2853
Michael Jurka1e2f4652013-07-08 18:03:46 -07002854 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002855 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2856 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002857 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002858 // System applications cannot be installed. For now, show a toast explaining that.
2859 // We may give them the option of disabling apps this way.
2860 int messageId = R.string.uninstall_system_app_text;
2861 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002862 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002863 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002864 String packageName = componentName.getPackageName();
2865 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002866 Intent intent = new Intent(
2867 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002868 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2869 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002870 if (user != null) {
2871 user.addToIntent(intent, Intent.EXTRA_USER);
2872 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002873 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002874 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002875 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002876 }
2877
Michael Jurka86a720e2012-05-09 11:23:23 -07002878 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002879 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002880 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002881 // Only launch using the new animation if the shortcut has not opted out (this is a
2882 // private contract between launcher and may be ignored in the future).
2883 boolean useLaunchAnimation = (v != null) &&
2884 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002885 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2886 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002887
Kenny Guy1317e2d2014-05-08 18:52:50 +01002888 UserHandleCompat user = null;
2889 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2890 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2891 user = userManager.getUserForSerialNumber(serialNumber);
2892 }
2893
2894 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002895 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002896 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002897 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2898 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002899 optsBundle = opts.toBundle();
2900 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002901
2902 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2903 // Could be launching some bookkeeping activity
2904 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002905 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002906 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002907 launcherApps.startActivityForProfile(intent.getComponent(), user,
2908 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002909 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002910 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002911 } catch (SecurityException e) {
2912 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002913 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002914 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002915 "or use the exported attribute for this activity. "
2916 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002917 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002918 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002919 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002920
Michael Jurka86a720e2012-05-09 11:23:23 -07002921 boolean startActivitySafely(View v, Intent intent, Object tag) {
2922 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002923 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2924 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2925 return false;
2926 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002927 try {
2928 success = startActivity(v, intent, tag);
2929 } catch (ActivityNotFoundException e) {
2930 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2931 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2932 }
2933 return success;
2934 }
2935
Adam Cohen268c4752012-06-06 17:47:33 -07002936 /**
2937 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2938 * in the DragLayer in the exact absolute location of the original FolderIcon.
2939 */
2940 private void copyFolderIconToImage(FolderIcon fi) {
2941 final int width = fi.getMeasuredWidth();
2942 final int height = fi.getMeasuredHeight();
2943
2944 // Lazy load ImageView, Bitmap and Canvas
2945 if (mFolderIconImageView == null) {
2946 mFolderIconImageView = new ImageView(this);
2947 }
2948 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2949 mFolderIconBitmap.getHeight() != height) {
2950 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2951 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2952 }
2953
2954 DragLayer.LayoutParams lp;
2955 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2956 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2957 } else {
2958 lp = new DragLayer.LayoutParams(width, height);
2959 }
2960
Adam Cohen307fe232012-08-16 17:55:58 -07002961 // The layout from which the folder is being opened may be scaled, adjust the starting
2962 // view size by this scale factor.
2963 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002964 lp.customPosition = true;
2965 lp.x = mRectForFolderAnimation.left;
2966 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002967 lp.width = (int) (scale * width);
2968 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002969
2970 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2971 fi.draw(mFolderIconCanvas);
2972 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002973 if (fi.getFolder() != null) {
2974 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2975 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002976 }
Adam Cohen268c4752012-06-06 17:47:33 -07002977 // Just in case this image view is still in the drag layer from a previous animation,
2978 // we remove it and re-add it.
2979 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2980 mDragLayer.removeView(mFolderIconImageView);
2981 }
2982 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002983 if (fi.getFolder() != null) {
2984 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002985 }
Adam Cohen268c4752012-06-06 17:47:33 -07002986 }
2987
Adam Cohen2801caf2011-05-13 20:57:39 -07002988 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002989 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002990 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2991 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2992 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2993
Adam Cohenc51934b2011-07-26 21:07:43 -07002994 FolderInfo info = (FolderInfo) fi.getTag();
2995 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2996 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002997 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2998 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002999 }
3000
Adam Cohen268c4752012-06-06 17:47:33 -07003001 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3002 copyFolderIconToImage(fi);
3003 fi.setVisibility(View.INVISIBLE);
3004
Michael Jurka2ecf9952012-06-18 12:52:28 -07003005 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003006 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003007 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003008 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3009 }
3010 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003011 oa.start();
3012 }
3013
Adam Cohen268c4752012-06-06 17:47:33 -07003014 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003015 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003016 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3017 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3018 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3019
Adam Cohen268c4752012-06-06 17:47:33 -07003020 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003021
Adam Cohen268c4752012-06-06 17:47:33 -07003022 // We remove and re-draw the FolderIcon in-case it has changed
3023 mDragLayer.removeView(mFolderIconImageView);
3024 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003025 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003026 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003027 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003028 oa.addListener(new AnimatorListenerAdapter() {
3029 @Override
3030 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003031 if (cl != null) {
3032 cl.clearFolderLeaveBehind();
3033 // Remove the ImageView copy of the FolderIcon and make the original visible.
3034 mDragLayer.removeView(mFolderIconImageView);
3035 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003036 }
3037 }
3038 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003039 oa.start();
3040 }
3041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003043 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003044 * is animated relative to the specified View. If the View is null, no animation
3045 * is played.
3046 *
3047 * @param folderInfo The FolderInfo describing the folder to open.
3048 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003049 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003050 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003051 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003052
Adam Cohena9cf38f2011-05-02 15:36:58 -07003053 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003054
Adam Cohen4554ee12011-08-03 16:13:21 -07003055 // Just verify that the folder hasn't already been added to the DragLayer.
3056 // There was a one-off crash where the folder had a parent already.
3057 if (folder.getParent() == null) {
3058 mDragLayer.addView(folder);
3059 mDragController.addDropTarget((DropTarget) folder);
3060 } else {
3061 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3062 folder.getParent() + ").");
3063 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003064 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003065 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003066
3067 // Notify the accessibility manager that this folder "window" has appeared and occluded
3068 // the workspace items
3069 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3070 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003071 }
3072
3073 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003074 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003075 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003076 if (folder.isEditingName()) {
3077 folder.dismissEditingName();
3078 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003079 closeFolder(folder);
3080 }
3081 }
3082
3083 void closeFolder(Folder folder) {
3084 folder.getInfo().opened = false;
3085
3086 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3087 if (parent != null) {
3088 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3089 shrinkAndFadeInFolderIcon(fi);
3090 }
3091 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003092
3093 // Notify the accessibility manager that this folder "window" has disappeard and no
3094 // longer occludeds the workspace items
3095 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003096 }
3097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003098 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003099 if (!isDraggingEnabled()) return false;
3100 if (isWorkspaceLocked()) return false;
3101 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102
Adam Cohen1697b792013-09-17 19:08:21 -07003103 if (v instanceof Workspace) {
3104 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003105 if (mWorkspace.enterOverviewMode()) {
3106 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3107 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3108 return true;
3109 } else {
3110 return false;
3111 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003112 } else {
3113 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003114 }
Adam Cohen1697b792013-09-17 19:08:21 -07003115 }
3116
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003117 CellLayout.CellInfo longClickCellInfo = null;
3118 View itemUnderLongClick = null;
3119 if (v.getTag() instanceof ItemInfo) {
3120 ItemInfo info = (ItemInfo) v.getTag();
3121 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3122 itemUnderLongClick = longClickCellInfo.cell;
3123 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124 }
3125
Winson Chung3d503fb2011-07-13 17:25:49 -07003126 // The hotseat touch handling does not go through Workspace, and we always allow long press
3127 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003128 final boolean inHotseat = isHotseatLayout(v);
3129 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003130 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003131 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003132 // User long pressed on empty space
3133 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3134 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003135 if (mWorkspace.isInOverviewMode()) {
3136 mWorkspace.startReordering(v);
3137 } else {
3138 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003139 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003140 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003141 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3142 mHotseat.getOrderInHotseat(
3143 longClickCellInfo.cellX,
3144 longClickCellInfo.cellY));
3145 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003146 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003147 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003148 }
3149 }
3150 }
3151 return true;
3152 }
3153
Winson Chung3d503fb2011-07-13 17:25:49 -07003154 boolean isHotseatLayout(View layout) {
3155 return mHotseat != null && layout != null &&
3156 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3157 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003158
Winson Chung3d503fb2011-07-13 17:25:49 -07003159 /**
3160 * Returns the CellLayout of the specified container at the specified screen.
3161 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003162 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003163 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3164 if (mHotseat != null) {
3165 return mHotseat.getLayout();
3166 } else {
3167 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003168 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003169 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003170 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003171 }
3172 }
3173
Daniel Sandler843e8602010-06-07 14:59:01 -04003174 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003175 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003176 }
3177
Craig Mautner360310b2012-10-26 15:13:08 -07003178 private void setWorkspaceBackground(boolean workspace) {
3179 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003180 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003181 }
3182
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003183 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003184 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3185 int curflags = getWindow().getAttributes().flags
3186 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3187 if (wpflags != curflags) {
3188 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3189 }
Craig Mautner360310b2012-10-26 15:13:08 -07003190 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003191 }
3192
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003193 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3194 if (v instanceof LauncherTransitionable) {
3195 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3196 }
3197 }
3198
Michael Jurkabed61d22012-02-14 22:51:29 -08003199 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3200 if (v instanceof LauncherTransitionable) {
3201 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3202 }
Winson Chung70442722012-02-10 15:43:22 -08003203
3204 // Update the workspace transition step as well
3205 dispatchOnLauncherTransitionStep(v, 0f);
3206 }
3207
3208 private void dispatchOnLauncherTransitionStep(View v, float t) {
3209 if (v instanceof LauncherTransitionable) {
3210 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3211 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003212 }
3213
3214 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3215 if (v instanceof LauncherTransitionable) {
3216 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3217 }
Winson Chung70442722012-02-10 15:43:22 -08003218
3219 // Update the workspace transition step as well
3220 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003221 }
3222
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003223 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003224 * Things to test when changing the following seven functions.
3225 * - Home from workspace
3226 * - from center screen
3227 * - from other screens
3228 * - Home from all apps
3229 * - from center screen
3230 * - from other screens
3231 * - Back from all apps
3232 * - from center screen
3233 * - from other screens
3234 * - Launch app from workspace and quit
3235 * - with back
3236 * - with home
3237 * - Launch app from all apps and quit
3238 * - with back
3239 * - with home
3240 * - Go to a screen that's not the default, then all
3241 * apps, and launch and app, and go back
3242 * - with back
3243 * -with home
3244 * - On workspace, long press power and go back
3245 * - with back
3246 * - with home
3247 * - On all apps, long press power and go back
3248 * - with back
3249 * - with home
3250 * - On workspace, power off
3251 * - On all apps, power off
3252 * - Launch an app and turn off the screen while in that app
3253 * - Go back with home key
3254 * - Go back with back key TODO: make this not go to workspace
3255 * - From all apps
3256 * - From workspace
3257 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3258 * - From all apps
3259 * - From the center workspace
3260 * - From another workspace
3261 */
3262
3263 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003264 * Zoom the camera out from the workspace to reveal 'toView'.
3265 * Assumes that the view to show is anchored at either the very top or very bottom
3266 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003267 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003268 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003269 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3270 showAppsCustomizeHelper(animated, springLoaded, contentType);
3271 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003272
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003273 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chungc58497e2013-09-03 17:48:37 -07003274 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3275 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003276 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003277 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003278 mStateAnimation.cancel();
3279 mStateAnimation = null;
3280 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003281
Kenny Guyd794a3f2014-09-16 15:17:58 +01003282 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003283
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003284 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003285
Adam Cohen6c5891a2014-07-09 23:53:15 -07003286 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003287 final int itemsAlphaStagger =
3288 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003289
Michael Jurkabed61d22012-02-14 22:51:29 -08003290 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003291 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003292
Adam Cohen80e6beb2015-02-13 16:14:33 -08003293 final HashMap<View, Integer> layerViews = new HashMap<View, Integer>();
Adam Cohen2854d252014-08-27 16:04:07 -07003294
Adam Cohen6c5891a2014-07-09 23:53:15 -07003295 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3296 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003297 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003298 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003299 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003300 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3301 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003302 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3303 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003304
Adam Cohena38dc902014-09-07 17:48:55 +02003305 // If for some reason our views aren't initialized, don't animate
3306 boolean initialized = getAllAppsButton() != null;
3307
3308 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003309 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003310 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3311 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003312
Adam Cohen9bfdb762014-07-21 17:44:06 -07003313 final View page = content.getPageAt(content.getCurrentPage());
3314 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003315
Adam Cohen63f1ec02014-08-12 09:23:13 -07003316 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3317 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003318 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3319 } else {
3320 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3321 }
3322
Adam Cohen9bfdb762014-07-21 17:44:06 -07003323 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003324 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003325 revealView.setVisibility(View.VISIBLE);
3326 // We need to hide this view as the animation start will be posted.
3327 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003328
Adam Cohen9bfdb762014-07-21 17:44:06 -07003329 int width = revealView.getMeasuredWidth();
3330 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003331 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003332
Adam Cohen63f1ec02014-08-12 09:23:13 -07003333 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003334 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003335
Adam Cohen63f1ec02014-08-12 09:23:13 -07003336 // Get the y delta between the center of the page and the center of the all apps button
3337 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3338 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003339
Adam Cohen2854d252014-08-27 16:04:07 -07003340 float alpha = 0;
3341 float xDrift = 0;
3342 float yDrift = 0;
3343 if (material) {
3344 alpha = isWidgetTray ? 0.3f : 1f;
3345 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3346 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3347 } else {
3348 yDrift = 2 * height / 3;
3349 xDrift = 0;
3350 }
3351 final float initAlpha = alpha;
3352
Adam Cohen80e6beb2015-02-13 16:14:33 -08003353 layerViews.put(revealView, BUILD_AND_SET_LAYER);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003354 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003355 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003356 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003357 PropertyValuesHolder panelDriftX =
3358 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003359
Adam Cohen2854d252014-08-27 16:04:07 -07003360 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3361 panelAlpha, panelDriftY, panelDriftX);
3362
Adam Cohen9bfdb762014-07-21 17:44:06 -07003363 panelAlphaAndDrift.setDuration(revealDuration);
3364 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003365
Adam Cohen9bfdb762014-07-21 17:44:06 -07003366 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003367
Adam Cohen4e243a22014-08-10 18:30:55 -07003368 if (page != null) {
3369 page.setVisibility(View.VISIBLE);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003370 layerViews.put(page, BUILD_AND_SET_LAYER);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003371
Adam Cohen2854d252014-08-27 16:04:07 -07003372 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3373 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003374 pageDrift.setDuration(revealDuration);
3375 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003376 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003377 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003378
Adam Cohen63f1ec02014-08-12 09:23:13 -07003379 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003380 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003381 itemsAlpha.setDuration(revealDuration);
3382 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3383 itemsAlpha.setStartDelay(itemsAlphaStagger);
3384 mStateAnimation.play(itemsAlpha);
3385 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003386
Adam Cohen4e243a22014-08-10 18:30:55 -07003387 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3388 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003389 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003390 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003391 indicatorsAlpha.setDuration(revealDuration);
3392 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003393
Adam Cohen9bfdb762014-07-21 17:44:06 -07003394 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003395 final View allApps = getAllAppsButton();
3396 int allAppsButtonSize = LauncherAppState.getInstance().
3397 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3398 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003399 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003400 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003401 reveal.setDuration(revealDuration);
3402 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003403
3404 reveal.addListener(new AnimatorListenerAdapter() {
3405 public void onAnimationStart(Animator animation) {
3406 if (!isWidgetTray) {
3407 allApps.setVisibility(View.INVISIBLE);
3408 }
3409 }
3410 public void onAnimationEnd(Animator animation) {
3411 if (!isWidgetTray) {
3412 allApps.setVisibility(View.VISIBLE);
3413 }
3414 }
3415 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003416 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003417 }
Michael Jurka1899a362011-11-03 13:50:45 -07003418
Adam Cohen9bfdb762014-07-21 17:44:06 -07003419 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3420 @Override
3421 public void onAnimationEnd(Animator animation) {
3422 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3423 dispatchOnLauncherTransitionEnd(toView, animated, false);
3424
3425 revealView.setVisibility(View.INVISIBLE);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003426
3427 for (View v : layerViews.keySet()) {
3428 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3429 v.setLayerType(View.LAYER_TYPE_NONE, null);
3430 }
Adam Cohen4e243a22014-08-10 18:30:55 -07003431 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003432 content.setPageBackgroundsVisible(true);
3433
3434 // Hide the search bar
3435 if (mSearchDropTargetBar != null) {
3436 mSearchDropTargetBar.hideSearchBar(false);
3437 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003438
3439 // This can hold unnecessary references to views.
3440 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003441 }
3442
Adam Cohen9bfdb762014-07-21 17:44:06 -07003443 });
3444
Adam Cohen6c5891a2014-07-09 23:53:15 -07003445 if (workspaceAnim != null) {
3446 mStateAnimation.play(workspaceAnim);
3447 }
Adam Cohen2854d252014-08-27 16:04:07 -07003448
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003449 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3450 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003451 final AnimatorSet stateAnimation = mStateAnimation;
3452 final Runnable startAnimRunnable = new Runnable() {
3453 public void run() {
3454 // Check that mStateAnimation hasn't changed while
3455 // we waited for a layout/draw pass
3456 if (mStateAnimation != stateAnimation)
3457 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003458 dispatchOnLauncherTransitionStart(fromView, animated, false);
3459 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003460
3461 revealView.setAlpha(initAlpha);
Adam Cohen80e6beb2015-02-13 16:14:33 -08003462
3463 for (View v : layerViews.keySet()) {
3464 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3465 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3466 }
3467 }
3468
Kenny Guyd794a3f2014-09-16 15:17:58 +01003469 if (Utilities.isLmpOrAbove()) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003470 for (View v : layerViews.keySet()) {
3471 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen2854d252014-08-27 16:04:07 -07003472 }
3473 }
3474 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003475 }
3476 };
Adam Cohen2854d252014-08-27 16:04:07 -07003477 toView.bringToFront();
3478 toView.setVisibility(View.VISIBLE);
3479 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003480 } else {
3481 toView.setTranslationX(0.0f);
3482 toView.setTranslationY(0.0f);
3483 toView.setScaleX(1.0f);
3484 toView.setScaleY(1.0f);
3485 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003486 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003487
Daniel Sandlere4f98912013-06-25 15:13:26 -04003488 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003489 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003490 if (mSearchDropTargetBar != null) {
3491 mSearchDropTargetBar.hideSearchBar(false);
3492 }
Michael Jurkaabded662011-03-04 12:06:57 -08003493 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003494 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003495 dispatchOnLauncherTransitionStart(fromView, animated, false);
3496 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003497 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003498 dispatchOnLauncherTransitionStart(toView, animated, false);
3499 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003500 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003501 }
3502
3503 /**
3504 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003505 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003506 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003507 */
Adam Cohened307df2013-10-02 09:37:31 -07003508 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003509 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003510
Michael Jurkab3e22d92011-10-31 15:58:33 -07003511 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003512 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003513 mStateAnimation.cancel();
3514 mStateAnimation = null;
3515 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003516
Kenny Guyd794a3f2014-09-16 15:17:58 +01003517 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003518 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003519
Adam Cohen63f1ec02014-08-12 09:23:13 -07003520 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003521 final int itemsAlphaStagger =
3522 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003523
Winson Chungf0ea4d32011-06-06 14:27:16 -07003524 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003525 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003526 Animator workspaceAnim = null;
Adam Cohen80e6beb2015-02-13 16:14:33 -08003527 final HashMap<View, Integer> layerViews = new HashMap<View, Integer>();
Adam Cohen2854d252014-08-27 16:04:07 -07003528
Adam Cohened307df2013-10-02 09:37:31 -07003529 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003530 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003531 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003532 } else if (toState == Workspace.State.SPRING_LOADED ||
3533 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003534 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003535 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003536 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003537
Adam Cohena38dc902014-09-07 17:48:55 +02003538 // If for some reason our views aren't initialized, don't animate
3539 boolean initialized = getAllAppsButton() != null;
3540
3541 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003542 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003543 if (workspaceAnim != null) {
3544 mStateAnimation.play(workspaceAnim);
3545 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003546
Adam Cohen9bfdb762014-07-21 17:44:06 -07003547 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3548 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003549
Adam Cohen9bfdb762014-07-21 17:44:06 -07003550 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003551
3552 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3553 int count = content.getChildCount();
3554 for (int i = 0; i < count; i++) {
3555 View child = content.getChildAt(i);
3556 if (child != page) {
3557 child.setVisibility(View.INVISIBLE);
3558 }
3559 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003560 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003561
Adam Cohen2854d252014-08-27 16:04:07 -07003562 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3563 // don't perform all these no-op animations. In particularly, this was causing
3564 // the all-apps button to pop in and out.
3565 if (fromView.getVisibility() == View.VISIBLE) {
3566 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3567 final boolean isWidgetTray =
3568 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003569
Adam Cohen2854d252014-08-27 16:04:07 -07003570 if (isWidgetTray) {
3571 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3572 } else {
3573 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003574 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003575
Adam Cohen2854d252014-08-27 16:04:07 -07003576 int width = revealView.getMeasuredWidth();
3577 int height = revealView.getMeasuredHeight();
3578 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3579
3580 // Hide the real page background, and swap in the fake one
3581 revealView.setVisibility(View.VISIBLE);
3582 content.setPageBackgroundsVisible(false);
3583
3584 final View allAppsButton = getAllAppsButton();
3585 revealView.setTranslationY(0);
3586 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3587 allAppsButton, null);
3588
3589 float xDrift = 0;
3590 float yDrift = 0;
3591 if (material) {
3592 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3593 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3594 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003595 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003596 xDrift = 0;
3597 }
3598
Adam Cohen80e6beb2015-02-13 16:14:33 -08003599 layerViews.put(revealView, BUILD_AND_SET_LAYER);
Adam Cohen2854d252014-08-27 16:04:07 -07003600 TimeInterpolator decelerateInterpolator = material ?
3601 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003602 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003603
3604 // The vertical motion of the apps panel should be delayed by one frame
3605 // from the conceal animation in order to give the right feel. We correpsondingly
3606 // shorten the duration so that the slide and conceal end at the same time.
3607 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3608 0, yDrift);
3609 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3610 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3611 panelDriftY.setInterpolator(decelerateInterpolator);
3612 mStateAnimation.play(panelDriftY);
3613
3614 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3615 0, xDrift);
3616 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3617 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3618 panelDriftX.setInterpolator(decelerateInterpolator);
3619 mStateAnimation.play(panelDriftX);
3620
3621 if (isWidgetTray || !material) {
3622 float finalAlpha = material ? 0.4f : 0f;
3623 revealView.setAlpha(1f);
3624 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3625 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003626 panelAlpha.setDuration(material ? revealDuration : 150);
3627 panelAlpha.setInterpolator(decelerateInterpolator);
3628 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003629 mStateAnimation.play(panelAlpha);
3630 }
3631
3632 if (page != null) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003633 layerViews.put(page, BUILD_AND_SET_LAYER);
Adam Cohen2854d252014-08-27 16:04:07 -07003634
3635 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3636 0, yDrift);
3637 page.setTranslationY(0);
3638 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3639 pageDrift.setInterpolator(decelerateInterpolator);
3640 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3641 mStateAnimation.play(pageDrift);
3642
3643 page.setAlpha(1f);
3644 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3645 itemsAlpha.setDuration(100);
3646 itemsAlpha.setInterpolator(decelerateInterpolator);
3647 mStateAnimation.play(itemsAlpha);
3648 }
3649
3650 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3651 pageIndicators.setAlpha(1f);
3652 ObjectAnimator indicatorsAlpha =
3653 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3654 indicatorsAlpha.setDuration(revealDuration);
3655 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3656 mStateAnimation.play(indicatorsAlpha);
3657
3658 width = revealView.getMeasuredWidth();
3659
3660 if (material) {
3661 if (!isWidgetTray) {
3662 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003663 }
Adam Cohen2854d252014-08-27 16:04:07 -07003664 int allAppsButtonSize = LauncherAppState.getInstance().
3665 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3666 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3667 Animator reveal =
3668 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3669 height / 2, revealRadius, finalRadius);
3670 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3671 reveal.setDuration(revealDuration);
3672 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003673
Adam Cohen2854d252014-08-27 16:04:07 -07003674 reveal.addListener(new AnimatorListenerAdapter() {
3675 public void onAnimationEnd(Animator animation) {
3676 revealView.setVisibility(View.INVISIBLE);
3677 if (!isWidgetTray) {
3678 allAppsButton.setVisibility(View.VISIBLE);
3679 }
3680 }
3681 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003682
Adam Cohen2854d252014-08-27 16:04:07 -07003683 mStateAnimation.play(reveal);
3684 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003685
Adam Cohen2854d252014-08-27 16:04:07 -07003686 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3687 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3688 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003689 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003690
3691 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003692 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003693 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003694 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003695 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3696 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003697 if (onCompleteRunnable != null) {
3698 onCompleteRunnable.run();
3699 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003700
Adam Cohen80e6beb2015-02-13 16:14:33 -08003701 for (View v : layerViews.keySet()) {
3702 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3703 v.setLayerType(View.LAYER_TYPE_NONE, null);
3704 }
Adam Cohen4e243a22014-08-10 18:30:55 -07003705 }
Adam Cohen80e6beb2015-02-13 16:14:33 -08003706
Adam Cohen9bfdb762014-07-21 17:44:06 -07003707 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003708 // Unhide side pages
3709 int count = content.getChildCount();
3710 for (int i = 0; i < count; i++) {
3711 View child = content.getChildAt(i);
3712 child.setVisibility(View.VISIBLE);
3713 }
3714
3715 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003716 if (page != null) {
3717 page.setTranslationX(0);
3718 page.setTranslationY(0);
3719 page.setAlpha(1);
3720 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003721 content.setCurrentPage(content.getNextPage());
3722
Chet Haasebc2f0822012-10-26 17:59:53 -07003723 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003724
3725 // This can hold unnecessary references to views.
3726 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003727 }
3728 });
3729
Adam Cohen2854d252014-08-27 16:04:07 -07003730 final AnimatorSet stateAnimation = mStateAnimation;
3731 final Runnable startAnimRunnable = new Runnable() {
3732 public void run() {
3733 // Check that mStateAnimation hasn't changed while
3734 // we waited for a layout/draw pass
3735 if (mStateAnimation != stateAnimation)
3736 return;
3737 dispatchOnLauncherTransitionStart(fromView, animated, false);
3738 dispatchOnLauncherTransitionStart(toView, animated, false);
3739
Adam Cohen80e6beb2015-02-13 16:14:33 -08003740 for (View v : layerViews.keySet()) {
3741 if (layerViews.get(v) == BUILD_AND_SET_LAYER) {
3742 v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3743 }
3744 }
3745
Kenny Guyd794a3f2014-09-16 15:17:58 +01003746 if (Utilities.isLmpOrAbove()) {
Adam Cohen80e6beb2015-02-13 16:14:33 -08003747 for (View v : layerViews.keySet()) {
3748 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen2854d252014-08-27 16:04:07 -07003749 }
3750 }
3751 mStateAnimation.start();
3752 }
3753 };
3754 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003755 } else {
3756 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003757 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003758 dispatchOnLauncherTransitionStart(fromView, animated, true);
3759 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003760 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003761 dispatchOnLauncherTransitionStart(toView, animated, true);
3762 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003763 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003764 }
3765
Michael Jurkae326f182011-11-21 14:05:46 -08003766 @Override
3767 public void onTrimMemory(int level) {
3768 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003769 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3770 // The widget preview db can result in holding onto over
3771 // 3MB of memory for caching which isn't necessary.
3772 SQLiteDatabase.releaseMemory();
3773
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003774 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003775 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003776 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003777 }
Michael Jurkae326f182011-11-21 14:05:46 -08003778 }
3779 }
3780
Adam Cohened307df2013-10-02 09:37:31 -07003781 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003782 showWorkspace(animated, null);
3783 }
3784
Adam Cohened307df2013-10-02 09:37:31 -07003785 protected void showWorkspace() {
3786 showWorkspace(true);
3787 }
3788
Adam Cohened66b2b2012-01-23 17:28:51 -08003789 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003790 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003791 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003792 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003793 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003794
Winson Chungc7d2b602012-05-16 17:02:20 -07003795 // Show the search bar (only animate if we were showing the drop target bar in spring
3796 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003797 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003798 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003799 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003800
Michael Jurkab3e22d92011-10-31 15:58:33 -07003801 // Set focus to the AppsCustomize button
3802 if (mAllAppsButton != null) {
3803 mAllAppsButton.requestFocus();
3804 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003805 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003806
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003807 // Change the state *after* we've called all the transition code
3808 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003809
Winson Chung5fb63472011-02-02 17:03:37 -08003810 // Resume the auto-advance of widgets
3811 mUserPresent = true;
3812 updateRunning();
3813
alanv1d4fde62012-10-17 13:15:47 -07003814 // Send an accessibility event to announce the context change
3815 getWindow().getDecorView()
3816 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003817
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003818 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003819 }
3820
Adam Cohened307df2013-10-02 09:37:31 -07003821 void showOverviewMode(boolean animated) {
3822 mWorkspace.setVisibility(View.VISIBLE);
3823 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3824 mState = State.WORKSPACE;
3825 onWorkspaceShown(animated);
3826 }
3827
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003828 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003829 }
3830
Winson Chung82963d52013-10-09 11:20:57 -07003831 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3832 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003833 if (mState != State.WORKSPACE) return;
3834
Winson Chung82963d52013-10-09 11:20:57 -07003835 if (resetPageToZero) {
3836 mAppsCustomizeTabHost.reset();
3837 }
Winson Chungc58497e2013-09-03 17:48:37 -07003838 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003839 mAppsCustomizeTabHost.post(new Runnable() {
3840 @Override
3841 public void run() {
3842 // We post this in-case the all apps view isn't yet constructed.
3843 mAppsCustomizeTabHost.requestFocus();
3844 }
3845 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003846
Michael Jurkab3e22d92011-10-31 15:58:33 -07003847 // Change the state *after* we've called all the transition code
3848 mState = State.APPS_CUSTOMIZE;
3849
3850 // Pause the auto-advance of widgets until we are out of AllApps
3851 mUserPresent = false;
3852 updateRunning();
3853 closeFolder();
3854
3855 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003856 getWindow().getDecorView()
3857 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003858 }
3859
Adam Cohen7777d962011-08-18 18:58:38 -07003860 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003861 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003862 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003863 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003864 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003865 }
Adam Cohen7777d962011-08-18 18:58:38 -07003866
Adam Cohenad4e15c2013-10-17 16:21:35 -07003867 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003868 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003869 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3870
Winson Chunge7a03942011-08-05 15:05:12 -07003871 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003872 @Override
3873 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003874 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003875 // Before we show workspace, hide all apps again because
3876 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3877 // clean up our state transition functions
3878 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003879 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003880 } else {
3881 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003882 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003883 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003884 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003885 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003886
Michael Jurkad3ef3062010-11-23 16:23:58 -08003887 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003888 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003889 final boolean animated = true;
3890 final boolean springLoaded = true;
3891 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003892 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003893 }
3894 // Otherwise, we are not in spring loaded mode, so don't do anything.
3895 }
3896
Michael Jurkab3e22d92011-10-31 15:58:33 -07003897 void lockAllApps() {
3898 // TODO
3899 }
3900
3901 void unlockAllApps() {
3902 // TODO
3903 }
3904
Sunny Goyal594d76d2014-11-06 10:12:54 -08003905 protected void disableVoiceButtonProxy(boolean disable) {
3906 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003907 }
3908
Cristina Stancu476493b2013-08-07 17:20:14 +01003909 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003910 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3911 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003912 }
3913
Adam Cohen24ce0b32014-01-14 16:18:14 -08003914 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003915 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3916 if (searchProvider == null) {
3917 return null;
3918 }
3919
3920 Bundle opts = new Bundle();
3921 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
3922 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
3923
3924 SharedPreferences sp = getSharedPreferences(
3925 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3926 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003927 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003928 if (!searchProvider.provider.flattenToString().equals(
3929 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003930 || (widgetInfo == null)
3931 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003932 // A valid widget is not already bound.
3933 if (widgetId > -1) {
3934 mAppWidgetHost.deleteAppWidgetId(widgetId);
3935 widgetId = -1;
3936 }
3937
3938 // Try to bind a new widget
3939 widgetId = mAppWidgetHost.allocateAppWidgetId();
3940
3941 if (!AppWidgetManagerCompat.getInstance(this)
3942 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3943 mAppWidgetHost.deleteAppWidgetId(widgetId);
3944 widgetId = -1;
3945 }
3946
3947 sp.edit()
3948 .putInt(QSB_WIDGET_ID, widgetId)
3949 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3950 .commit();
3951 }
3952
3953 if (widgetId != -1) {
3954 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3955 mQsb.updateAppWidgetOptions(opts);
3956 mQsb.setPadding(0, 0, 0, 0);
3957 mSearchDropTargetBar.addView(mQsb);
3958 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003959 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003960 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003961 }
3962
Michael Jurkad7c28052012-04-27 15:43:36 -07003963 @Override
3964 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003965 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003966 final List<CharSequence> text = event.getText();
3967 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003968 // Populate event with a fake title based on the current state.
3969 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07003970 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07003971 } else {
3972 text.add(getString(R.string.all_apps_home_button_label));
3973 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003974 return result;
3975 }
3976
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003977 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003978 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003979 */
3980 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3981 @Override
3982 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003983 closeSystemDialogs();
3984 }
3985 }
3986
3987 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003988 * Receives notifications whenever the appwidgets are reset.
3989 */
3990 private class AppWidgetResetObserver extends ContentObserver {
3991 public AppWidgetResetObserver() {
3992 super(new Handler());
3993 }
3994
3995 @Override
3996 public void onChange(boolean selfChange) {
3997 onAppWidgetReset();
3998 }
3999 }
4000
4001 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004002 * If the activity is currently paused, signal that we need to run the passed Runnable
4003 * in onResume.
4004 *
4005 * This needs to be called from incoming places where resources might have been loaded
4006 * while we are paused. That is becaues the Configuration might be wrong
4007 * when we're not running, and if it comes back to what it was when we
4008 * were paused, we are not restarted.
4009 *
4010 * Implementation of the method from LauncherModel.Callbacks.
4011 *
4012 * @return true if we are currently paused. The caller might be able to
4013 * skip some work in that case since we will come back again.
4014 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004015 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004016 if (mPaused) {
4017 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004018 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004019 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004020 }
4021 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004022 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004023 return true;
4024 } else {
4025 return false;
4026 }
4027 }
4028
Michael Jurkac402cd92013-05-20 15:49:32 +02004029 private boolean waitUntilResume(Runnable run) {
4030 return waitUntilResume(run, false);
4031 }
4032
Michael Jurka1e2f4652013-07-08 18:03:46 -07004033 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004034 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004035 }
4036
Michael Jurka7607c2f2013-04-03 14:33:19 -07004037 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004038 * If the activity is currently paused, signal that we need to re-run the loader
4039 * in onResume.
4040 *
4041 * This needs to be called from incoming places where resources might have been loaded
4042 * while we are paused. That is becaues the Configuration might be wrong
4043 * when we're not running, and if it comes back to what it was when we
4044 * were paused, we are not restarted.
4045 *
4046 * Implementation of the method from LauncherModel.Callbacks.
4047 *
4048 * @return true if we are currently paused. The caller might be able to
4049 * skip some work in that case since we will come back again.
4050 */
4051 public boolean setLoadOnResume() {
4052 if (mPaused) {
4053 Log.i(TAG, "setLoadOnResume");
4054 mOnResumeNeedsLoad = true;
4055 return true;
4056 } else {
4057 return false;
4058 }
4059 }
4060
4061 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004062 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004063 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004064 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004065 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004066 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004067 } else {
4068 return SCREEN_COUNT / 2;
4069 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004070 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004071
Joe Onorato9c1289c2009-08-17 11:03:03 -04004072 /**
4073 * Refreshes the shortcuts shown on the workspace.
4074 *
4075 * Implementation of the method from LauncherModel.Callbacks.
4076 */
4077 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004078 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004079
Michael Jurka7607c2f2013-04-03 14:33:19 -07004080 // If we're starting binding all over again, clear any bind calls we'd postponed in
4081 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4082 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004083 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004084
Winson Chungd64d1762013-08-20 14:37:16 -07004085 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004086 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004087 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004088
Michael Jurka05bf6442011-11-30 20:28:53 -08004089 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004090 if (mHotseat != null) {
4091 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004092 }
4093 }
4094
Adam Cohendcd297f2013-06-18 13:13:40 -07004095 @Override
4096 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004097 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004098
Adam Cohen5084cba2013-09-03 12:01:16 -07004099 // If there are no screens, we need to have an empty screen
4100 if (orderedScreenIds.size() == 0) {
4101 mWorkspace.addExtraEmptyScreen();
4102 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004103
4104 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004105 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004106 if (hasCustomContentToLeft()) {
4107 mWorkspace.createCustomContentContainer();
4108 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004109 }
Winson Chung64359a52013-07-08 17:17:08 -07004110 }
4111
4112 @Override
4113 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004114 // Log to disk
4115 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4116 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4117 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004118 int count = orderedScreenIds.size();
4119 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004120 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004121 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004122 }
4123
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08004124 @Override
4125 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
4126 final long screenId, final int[] cell, final int spanX, final int spanY) {
4127 showWorkspace(true, new Runnable() {
4128
4129 @Override
4130 public void run() {
4131 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
4132 addPendingItem(info, container, screenId, cell, spanX, spanY);
4133 }
4134 });
4135 }
4136
Adam Cohen39a06042013-07-19 14:30:12 -07004137 private boolean shouldShowWeightWatcher() {
4138 String spKey = LauncherAppState.getSharedPreferencesKey();
4139 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004140 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004141
4142 return show;
4143 }
4144
4145 private void toggleShowWeightWatcher() {
4146 String spKey = LauncherAppState.getSharedPreferencesKey();
4147 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4148 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4149
4150 show = !show;
4151
4152 SharedPreferences.Editor editor = sp.edit();
4153 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4154 editor.commit();
4155
4156 if (mWeightWatcher != null) {
4157 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4158 }
4159 }
4160
Winson Chungd64d1762013-08-20 14:37:16 -07004161 public void bindAppsAdded(final ArrayList<Long> newScreens,
4162 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004163 final ArrayList<ItemInfo> addAnimated,
4164 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004165 Runnable r = new Runnable() {
4166 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004167 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004168 }
4169 };
4170 if (waitUntilResume(r)) {
4171 return;
4172 }
4173
Winson Chungd64d1762013-08-20 14:37:16 -07004174 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004175 if (newScreens != null) {
4176 bindAddScreens(newScreens);
4177 }
Winson Chungd64d1762013-08-20 14:37:16 -07004178
4179 // We add the items without animation on non-visible pages, and with
4180 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004181 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004182 bindItems(addNotAnimated, 0,
4183 addNotAnimated.size(), false);
4184 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004185 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004186 bindItems(addAnimated, 0,
4187 addAnimated.size(), true);
4188 }
Winson Chungc58497e2013-09-03 17:48:37 -07004189
Winson Chung87412982013-10-03 18:34:14 -07004190 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004191 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004192
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004193 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004194 addedApps != null && mAppsCustomizeContent != null) {
4195 mAppsCustomizeContent.addApps(addedApps);
4196 }
Winson Chungd64d1762013-08-20 14:37:16 -07004197 }
4198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004199 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004200 * Bind the items start-end from the list.
4201 *
4202 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004203 */
Winson Chung64359a52013-07-08 17:17:08 -07004204 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4205 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004206 Runnable r = new Runnable() {
4207 public void run() {
4208 bindItems(shortcuts, start, end, forceAnimateIcons);
4209 }
4210 };
4211 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004212 return;
4213 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004214
Winson Chungf0c6ae02012-03-21 16:10:31 -07004215 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004216 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4217 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004218 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004219 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004220 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004221 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004222 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004223
4224 // Short circuit if we are loading dock items for a configuration which has no dock
4225 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4226 mHotseat == null) {
4227 continue;
4228 }
4229
Joe Onorato9c1289c2009-08-17 11:03:03 -04004230 switch (item.itemType) {
4231 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4232 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004233 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004234 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004235
Winson Chung64359a52013-07-08 17:17:08 -07004236 /*
4237 * TODO: FIX collision case
4238 */
Winson Chungce376632013-07-11 16:12:41 -07004239 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4240 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4241 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004242 View v = cl.getChildAt(item.cellX, item.cellY);
4243 Object tag = v.getTag();
4244 String desc = "Collision while binding workspace item: " + item
4245 + ". Collides with " + tag;
4246 if (LauncherAppState.isDogfoodBuild()) {
4247 throw (new RuntimeException(desc));
4248 } else {
4249 Log.d(TAG, desc);
4250 }
Winson Chungce376632013-07-11 16:12:41 -07004251 }
Winson Chung64359a52013-07-08 17:17:08 -07004252 }
4253
Adam Cohendcd297f2013-06-18 13:13:40 -07004254 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4255 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004256 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004257 // Animate all the applications up now
4258 shortcut.setAlpha(0f);
4259 shortcut.setScaleX(0f);
4260 shortcut.setScaleY(0f);
4261 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004262 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004263 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004264 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004265 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004266 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004267 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004268 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004269 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4270 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004271 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004272 default:
4273 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004275 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004276
Winson Chung997a9232013-07-24 15:33:46 -07004277 if (animateIcons) {
4278 // Animate to the correct page
4279 if (newShortcutsScreenId > -1) {
4280 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004281 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004282 final Runnable startBounceAnimRunnable = new Runnable() {
4283 public void run() {
4284 anim.playTogether(bounceAnims);
4285 anim.start();
4286 }
4287 };
Winson Chung997a9232013-07-24 15:33:46 -07004288 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004289 // We post the animation slightly delayed to prevent slowdowns
4290 // when we are loading right after we return to launcher.
4291 mWorkspace.postDelayed(new Runnable() {
4292 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004293 if (mWorkspace != null) {
4294 mWorkspace.snapToPage(newScreenIndex);
4295 mWorkspace.postDelayed(startBounceAnimRunnable,
4296 NEW_APPS_ANIMATION_DELAY);
4297 }
Winson Chung94d67682013-09-25 16:29:40 -07004298 }
4299 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004300 } else {
4301 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004302 }
4303 }
Winson Chung64359a52013-07-08 17:17:08 -07004304 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004305 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004306 }
4307
Joe Onorato9c1289c2009-08-17 11:03:03 -04004308 /**
4309 * Implementation of the method from LauncherModel.Callbacks.
4310 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004311 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004312 Runnable r = new Runnable() {
4313 public void run() {
4314 bindFolders(folders);
4315 }
4316 };
4317 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004318 return;
4319 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004320 sFolders.clear();
4321 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004322 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004323
4324 /**
4325 * Add the views for a widget to the workspace.
4326 *
4327 * Implementation of the method from LauncherModel.Callbacks.
4328 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004329 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004330 Runnable r = new Runnable() {
4331 public void run() {
4332 bindAppWidget(item);
4333 }
4334 };
4335 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004336 return;
4337 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004338
Daniel Sandler843e8602010-06-07 14:59:01 -04004339 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4340 if (DEBUG_WIDGETS) {
4341 Log.d(TAG, "bindAppWidget: " + item);
4342 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004343 final Workspace workspace = mWorkspace;
4344
Adam Cohen59400422014-03-05 18:07:04 -08004345 LauncherAppWidgetProviderInfo appWidgetInfo =
4346 LauncherModel.getProviderInfo(this, item.providerName);
4347
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004348 if (!mIsSafeModeEnabled
4349 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4350 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004351 if (appWidgetInfo == null) {
4352 if (DEBUG_WIDGETS) {
4353 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4354 + " belongs to component " + item.providerName
4355 + ", as the povider is null");
4356 }
4357 LauncherModel.deleteItemFromDatabase(this, item);
4358 return;
4359 }
4360 // Note: This assumes that the id remap broadcast is received before this step.
4361 // If that is not the case, the id remap will be ignored and user may see the
4362 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004363 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004364 pendingInfo.spanX = item.spanX;
4365 pendingInfo.spanY = item.spanY;
4366 pendingInfo.minSpanX = item.minSpanX;
4367 pendingInfo.minSpanY = item.minSpanY;
4368 Bundle options =
4369 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4370
Sunny Goyalff572272014-07-23 13:58:07 -07004371 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004372 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4373 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004374
4375 // TODO consider showing a permission dialog when the widget is clicked.
4376 if (!success) {
4377 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4378 if (DEBUG_WIDGETS) {
4379 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4380 + " belongs to component " + item.providerName
4381 + ", as the launcher is unable to bing a new widget id");
4382 }
4383 LauncherModel.deleteItemFromDatabase(this, item);
4384 return;
4385 }
4386
4387 item.appWidgetId = newWidgetId;
4388
4389 // If the widget has a configure activity, it is still needs to set it up, otherwise
4390 // the widget is ready to go.
4391 item.restoreStatus = (appWidgetInfo.configure == null)
4392 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4393 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4394
4395 LauncherModel.updateItemInDatabase(this, item);
4396 }
4397
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004398 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004399 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004400 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004401 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4402 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004403 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004404
Sunny Goyal651077b2014-06-30 14:15:31 -07004405 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4406 } else {
4407 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004408 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4409 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004410 view.updateIcon(mIconCache);
4411 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004412 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004413 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004414 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004415
Joe Onorato9c1289c2009-08-17 11:03:03 -04004416 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004417 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004418
Adam Cohendcd297f2013-06-18 13:13:40 -07004419 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004420 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004421 if (!item.isCustomWidget()) {
4422 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4423 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004424
Joe Onorato9c1289c2009-08-17 11:03:03 -04004425 workspace.requestLayout();
4426
Daniel Sandler843e8602010-06-07 14:59:01 -04004427 if (DEBUG_WIDGETS) {
4428 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4429 + (SystemClock.uptimeMillis()-start) + "ms");
4430 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004431 }
4432
Sunny Goyalff572272014-07-23 13:58:07 -07004433 /**
4434 * Restores a pending widget.
4435 *
4436 * @param appWidgetId The app widget id
4437 * @param cellInfo The position on screen where to create the widget.
4438 */
4439 private void completeRestoreAppWidget(final int appWidgetId) {
4440 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4441 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4442 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4443 return;
4444 }
4445
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004446 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004447 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4448
4449 mWorkspace.reinflateWidgetsIfNecessary();
4450 LauncherModel.updateItemInDatabase(this, info);
4451 }
4452
Adam Cohen1462de32012-07-24 22:34:36 -07004453 public void onPageBoundSynchronously(int page) {
4454 mSynchronouslyBoundPages.add(page);
4455 }
4456
Joe Onorato9c1289c2009-08-17 11:03:03 -04004457 /**
4458 * Callback saying that there aren't any more items to bind.
4459 *
4460 * Implementation of the method from LauncherModel.Callbacks.
4461 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004462 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004463 Runnable r = new Runnable() {
4464 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004465 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004466 }
4467 };
4468 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004469 return;
4470 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004471 if (mSavedState != null) {
4472 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004473 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004474 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004475 mSavedState = null;
4476 }
4477
Adam Cohen1462de32012-07-24 22:34:36 -07004478 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004479
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004480 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004481 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004482
4483 // If we received the result of any pending adds while the loader was running (e.g. the
4484 // widget configuration forced an orientation change), process them now.
4485 if (sPendingAddItem != null) {
4486 final long screenId = completeAdd(sPendingAddItem);
4487
4488 // TODO: this moves the user to the page where the pending item was added. Ideally,
4489 // the screen would be guaranteed to exist after bind, and the page would be set through
4490 // the workspace restore process.
4491 mWorkspace.post(new Runnable() {
4492 @Override
4493 public void run() {
4494 mWorkspace.snapToScreenId(screenId);
4495 }
4496 });
4497 sPendingAddItem = null;
4498 }
4499
Sunny Goyale755d462014-07-22 13:48:29 -07004500 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004501
4502 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004503 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004504 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004505 }
4506
Adam Cohen3ed316a2014-07-23 18:21:20 -07004507 private void sendLoadingCompleteBroadcastIfNecessary() {
4508 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4509 String permission =
4510 getResources().getString(R.string.receive_first_load_broadcast_permission);
4511 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4512 sendBroadcast(intent, permission);
4513 SharedPreferences.Editor editor = mSharedPrefs.edit();
4514 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4515 editor.apply();
4516 }
4517 }
4518
Winson Chunga0b7e862013-09-05 16:03:15 -07004519 public boolean isAllAppsButtonRank(int rank) {
4520 if (mHotseat != null) {
4521 return mHotseat.isAllAppsButtonRank(rank);
4522 }
4523 return false;
4524 }
4525
Winson Chunga2413752012-04-03 14:22:34 -07004526 private boolean canRunNewAppsAnimation() {
4527 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4528 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4529 }
4530
Winson Chungc9168342013-06-26 14:54:55 -07004531 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4532 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4533 PropertyValuesHolder.ofFloat("alpha", 1f),
4534 PropertyValuesHolder.ofFloat("scaleX", 1f),
4535 PropertyValuesHolder.ofFloat("scaleY", 1f));
4536 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4537 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4538 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4539 return bounceAnim;
4540 }
4541
Adam Cohen27772732013-11-11 14:00:56 +00004542 public boolean useVerticalBarLayout() {
4543 return LauncherAppState.getInstance().getDynamicGrid().
4544 getDeviceProfile().isVerticalBarLayout();
4545 }
4546
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004547 protected Rect getSearchBarBounds() {
4548 return LauncherAppState.getInstance().getDynamicGrid().
4549 getDeviceProfile().getSearchBarBounds();
4550 }
4551
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004552 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004553 if (mSearchDropTargetBar == null) {
4554 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004555 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004556 if (mQsb != null) {
4557 mSearchDropTargetBar.removeView(mQsb);
4558 mQsb = null;
4559 }
4560 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004561 }
4562
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004563 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004564 * Add the icons for all apps.
4565 *
4566 * Implementation of the method from LauncherModel.Callbacks.
4567 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004568 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004569 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004570 if (mAppsCustomizeContent != null) {
4571 mAppsCustomizeContent.onPackagesUpdated(
4572 LauncherModel.getSortedWidgetsAndShortcuts(this));
4573 }
Winson Chungc58497e2013-09-03 17:48:37 -07004574 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004575 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004576 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004577 mAppsCustomizeContent.onPackagesUpdated(
4578 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004579 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004580 }
Adam Cohen9211d422014-10-07 18:14:53 -07004581 if (mLauncherCallbacks != null) {
4582 mLauncherCallbacks.bindAllApplications(apps);
4583 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004584 }
4585
4586 /**
4587 * A package was updated.
4588 *
4589 * Implementation of the method from LauncherModel.Callbacks.
4590 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004591 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004592 Runnable r = new Runnable() {
4593 public void run() {
4594 bindAppsUpdated(apps);
4595 }
4596 };
4597 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004598 return;
4599 }
4600
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004601 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004602 mAppsCustomizeContent != null) {
4603 mAppsCustomizeContent.updateApps(apps);
4604 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004605 }
4606
Sunny Goyal4390ace2014-10-13 11:33:11 -07004607 @Override
4608 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4609 Runnable r = new Runnable() {
4610 public void run() {
4611 bindWidgetsRestored(widgets);
4612 }
4613 };
4614 if (waitUntilResume(r)) {
4615 return;
4616 }
4617 mWorkspace.widgetsRestored(widgets);
4618 }
4619
Joe Onorato9c1289c2009-08-17 11:03:03 -04004620 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004621 * Some shortcuts were updated in the background.
4622 *
4623 * Implementation of the method from LauncherModel.Callbacks.
4624 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004625 @Override
4626 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4627 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004628 Runnable r = new Runnable() {
4629 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004630 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004631 }
4632 };
4633 if (waitUntilResume(r)) {
4634 return;
4635 }
4636
Sunny Goyal4390ace2014-10-13 11:33:11 -07004637 if (!updated.isEmpty()) {
4638 mWorkspace.updateShortcuts(updated);
4639 }
4640
4641 if (!removed.isEmpty()) {
4642 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4643 for (ShortcutInfo si : removed) {
4644 removedComponents.add(si.getTargetComponent());
4645 }
4646 mWorkspace.removeItemsByComponentName(removedComponents, user);
4647 // Notify the drag controller
4648 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004649 }
4650 }
4651
4652 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004653 * Update the state of a package, typically related to install state.
4654 *
4655 * Implementation of the method from LauncherModel.Callbacks.
4656 */
Sunny Goyale755d462014-07-22 13:48:29 -07004657 @Override
4658 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004659 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004660 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004661 }
4662 }
4663
4664 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004665 * Update the label and icon of all the icons in a package
4666 *
4667 * Implementation of the method from LauncherModel.Callbacks.
4668 */
4669 @Override
4670 public void updatePackageBadge(String packageName) {
4671 if (mWorkspace != null) {
4672 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4673 }
4674 }
4675
4676 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004677 * A package was uninstalled. We take both the super set of packageNames
4678 * in addition to specific applications to remove, the reason being that
4679 * this can be called when a package is updated as well. In that scenario,
4680 * we only remove specific components from the workspace, where as
4681 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004682 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004683 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004684 * Implementation of the method from LauncherModel.Callbacks.
4685 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004686 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004687 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004688 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004689 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004690 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004691 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004692 }
Winson Chungd64d1762013-08-20 14:37:16 -07004693 };
4694 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004695 return;
4696 }
4697
Sunny Goyal1a745e82014-10-02 15:58:31 -07004698 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004699 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4700 for (AppInfo info : appInfos) {
4701 removedComponents.add(info.componentName);
4702 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004703 if (!packageNames.isEmpty()) {
4704 mWorkspace.removeItemsByPackageName(packageNames, user);
4705 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004706 if (!removedComponents.isEmpty()) {
4707 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004708 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004709 // Notify the drag controller
4710 mDragController.onAppsRemoved(packageNames, removedComponents);
4711
Sunny Goyal1a745e82014-10-02 15:58:31 -07004712 } else {
4713 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004714 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004715
Winson Chungdf95eb12013-10-16 14:57:07 -07004716 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004717 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004718 mAppsCustomizeContent != null) {
4719 mAppsCustomizeContent.removeApps(appInfos);
4720 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004721 }
Joe Onoratobe386092009-11-17 17:32:16 -08004722
4723 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004724 * A number of packages were updated.
4725 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004726 private ArrayList<Object> mWidgetsAndShortcuts;
4727 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004728 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004729 bindPackagesUpdated(mWidgetsAndShortcuts);
4730 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004731 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004732 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004733 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4734 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4735 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004736 return;
4737 }
4738
Winson Chung64359a52013-07-08 17:17:08 -07004739 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004740 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004741 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004742 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004743 }
4744
Winson Chung400438b2011-01-16 17:53:48 -08004745 private int mapConfigurationOriActivityInfoOri(int configOri) {
4746 final Display d = getWindowManager().getDefaultDisplay();
4747 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4748 switch (d.getRotation()) {
4749 case Surface.ROTATION_0:
4750 case Surface.ROTATION_180:
4751 // We are currently in the same basic orientation as the natural orientation
4752 naturalOri = configOri;
4753 break;
4754 case Surface.ROTATION_90:
4755 case Surface.ROTATION_270:
4756 // We are currently in the other basic orientation to the natural orientation
4757 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4758 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4759 break;
4760 }
4761
4762 int[] oriMap = {
4763 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4764 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4765 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4766 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4767 };
4768 // Since the map starts at portrait, we need to offset if this device's natural orientation
4769 // is landscape.
4770 int indexOffset = 0;
4771 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4772 indexOffset = 1;
4773 }
4774 return oriMap[(d.getRotation() + indexOffset) % 4];
4775 }
Adam Cohen446e9402011-09-15 18:21:21 -07004776
Winson Chung4b919f82012-05-01 10:44:08 -07004777 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004778 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004779 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4780 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4781 .getConfiguration().orientation));
4782 } else {
4783 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4784 }
Winson Chung4b919f82012-05-01 10:44:08 -07004785 }
4786 }
4787 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004788 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004789 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004790 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004791 } else {
4792 mHandler.postDelayed(new Runnable() {
4793 public void run() {
4794 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4795 }
4796 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004797 }
Winson Chung4b919f82012-05-01 10:44:08 -07004798 }
Winson Chung400438b2011-01-16 17:53:48 -08004799 }
4800
Winson Chunge43a1e72014-01-15 10:33:02 -08004801 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004802 if (mLauncherCallbacks != null) {
4803 return mLauncherCallbacks.isLauncherPreinstalled();
4804 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004805 PackageManager pm = getPackageManager();
4806 try {
4807 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4808 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4809 return true;
4810 } else {
4811 return false;
4812 }
4813 } catch (NameNotFoundException e) {
4814 e.printStackTrace();
4815 return false;
4816 }
4817 }
4818
Adam Cohenea90f832014-05-21 15:03:34 -07004819 /**
4820 * This method indicates whether or not we should suggest default wallpaper dimensions
4821 * when our wallpaper cropper was not yet used to set a wallpaper.
4822 */
4823 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004824 if (mLauncherCallbacks != null) {
4825 return mLauncherCallbacks.overrideWallpaperDimensions();
4826 }
Adam Cohenea90f832014-05-21 15:03:34 -07004827 return true;
4828 }
4829
Adam Cohen432609a2014-03-13 17:03:22 -07004830 /**
4831 * To be overridden by subclasses to indicate that there is an activity to launch
4832 * before showing the standard launcher experience.
4833 */
4834 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004835 if (mLauncherCallbacks != null) {
4836 return mLauncherCallbacks.hasFirstRunActivity();
4837 }
Adam Cohen432609a2014-03-13 17:03:22 -07004838 return false;
4839 }
4840
4841 /**
4842 * To be overridden by subclasses to launch any first run activity
4843 */
4844 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004845 if (mLauncherCallbacks != null) {
4846 return mLauncherCallbacks.getFirstRunActivity();
4847 }
Adam Cohen432609a2014-03-13 17:03:22 -07004848 return null;
4849 }
4850
Winson Chunga6945242014-01-08 14:04:34 -08004851 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004852 return !ActivityManager.isRunningInTestHarness() &&
4853 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004854 }
4855
Adam Cohen4a9423d2014-03-28 14:22:31 -07004856 protected boolean hasRunFirstRunActivity() {
4857 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4858 }
4859
Adam Cohen432609a2014-03-13 17:03:22 -07004860 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004861 if (shouldRunFirstRunActivity() &&
4862 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004863 Intent firstRunIntent = getFirstRunActivity();
4864 if (firstRunIntent != null) {
4865 startActivity(firstRunIntent);
4866 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004867 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004868 }
4869 }
Adam Cohen432609a2014-03-13 17:03:22 -07004870 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004871 }
4872
4873 private void markFirstRunActivityShown() {
4874 SharedPreferences.Editor editor = mSharedPrefs.edit();
4875 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4876 editor.apply();
4877 }
4878
Adam Cohen432609a2014-03-13 17:03:22 -07004879 /**
4880 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4881 * screen that must be displayed and dismissed.
4882 */
4883 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004884 if (mLauncherCallbacks != null) {
4885 return mLauncherCallbacks.hasDismissableIntroScreen();
4886 }
Adam Cohen432609a2014-03-13 17:03:22 -07004887 return false;
4888 }
4889
4890 /**
4891 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4892 */
4893 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004894 if (mLauncherCallbacks != null) {
4895 return mLauncherCallbacks.getIntroScreen();
4896 }
Adam Cohen432609a2014-03-13 17:03:22 -07004897 return null;
4898 }
4899
4900 /**
4901 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4902 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4903 */
4904 private boolean shouldShowIntroScreen() {
4905 return hasDismissableIntroScreen() &&
4906 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4907 }
4908
4909 protected void showIntroScreen() {
4910 View introScreen = getIntroScreen();
4911 changeWallpaperVisiblity(false);
4912 if (introScreen != null) {
4913 mDragLayer.showOverlayView(introScreen);
4914 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004915 if (mLauncherOverlayContainer != null) {
4916 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4917 }
Adam Cohen432609a2014-03-13 17:03:22 -07004918 }
4919
4920 public void dismissIntroScreen() {
4921 markIntroScreenDismissed();
4922 if (showFirstRunActivity()) {
4923 // We delay hiding the intro view until the first run activity is showing. This
4924 // avoids a blip.
4925 mWorkspace.postDelayed(new Runnable() {
4926 @Override
4927 public void run() {
4928 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004929 if (mLauncherOverlayContainer != null) {
4930 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4931 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004932 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004933 }
4934 }, ACTIVITY_START_DELAY);
4935 } else {
4936 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004937 if (mLauncherOverlayContainer != null) {
4938 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4939 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004940 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004941 }
4942 changeWallpaperVisiblity(true);
4943 }
4944
4945 private void markIntroScreenDismissed() {
4946 SharedPreferences.Editor editor = mSharedPrefs.edit();
4947 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4948 editor.apply();
4949 }
4950
Sunny Goyal88d60f12014-09-08 03:47:29 -07004951 private void showFirstRunClings() {
4952 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4953 // on the device, then we always show the first run cling experience (or if there is no
4954 // launcher2). Otherwise, we prompt the user upon started for migration
4955 LauncherClings launcherClings = new LauncherClings(this);
4956 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4957 if (mModel.canMigrateFromOldLauncherDb(this)) {
4958 launcherClings.showMigrationCling();
4959 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004960 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004961 }
4962 }
4963 }
4964
Winson Chunga6945242014-01-08 14:04:34 -08004965 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004966 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4967 if (mHotseat != null) mHotseat.setAlpha(1f);
4968 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4969 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004970 }
4971
4972 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004973 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4974 if (mHotseat != null) mHotseat.setAlpha(0f);
4975 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4976 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004977 }
4978
Mathew Inwood72fbec12013-11-19 15:45:07 +00004979 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004980 // Called from search suggestion, not supported in other profiles.
4981 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4982 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4983 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4984 myUser);
4985 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004986 return null;
4987 }
Kenny Guyed131872014-04-30 03:02:21 +01004988 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004989 }
4990
4991 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4992 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004993 // Called from search suggestion, not supported in other profiles.
4994 return createShortcutDragInfo(shortcutIntent, caption, icon,
4995 UserHandleCompat.myUserHandle());
4996 }
4997
4998 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4999 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005000 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005001 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005002 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005003 }
5004
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005005 protected void moveWorkspaceToDefaultScreen() {
5006 mWorkspace.moveToDefaultScreen(false);
5007 }
5008
Mathew Inwood72fbec12013-11-19 15:45:07 +00005009 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5010 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005011 mWorkspace.onExternalDragStartedWithItem(dragView);
5012 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005013 }
5014
Anjali Koppalf5d29132014-02-28 13:33:27 -08005015 @Override
5016 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005017 if (mLauncherCallbacks != null) {
5018 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5019 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005020 }
5021
Winson Chung80baf5a2010-08-09 16:03:15 -07005022 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005023 * Prints out out state for debugging.
5024 */
5025 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005026 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005027 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005028 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5029 Log.d(TAG, "mRestoring=" + mRestoring);
5030 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5031 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005032 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005033 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005034
Winson Chung785d2eb2011-04-14 16:08:02 -07005035 if (mAppsCustomizeContent != null) {
5036 mAppsCustomizeContent.dumpState();
5037 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005038 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005039 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005040
5041 @Override
5042 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5043 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005044 synchronized (sDumpLogs) {
5045 writer.println(" ");
5046 writer.println("Debug logs: ");
5047 for (int i = 0; i < sDumpLogs.size(); i++) {
5048 writer.println(" " + sDumpLogs.get(i));
5049 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005050 }
Adam Cohen9211d422014-10-07 18:14:53 -07005051 if (mLauncherCallbacks != null) {
5052 mLauncherCallbacks.dump(prefix, fd, writer, args);
5053 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005054 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005055
5056 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005057 if (DEBUG_DUMP_LOG) {
5058 synchronized (sDumpLogs) {
5059 Log.d(TAG, "");
5060 Log.d(TAG, "*********************");
5061 Log.d(TAG, "Launcher debug logs: ");
5062 for (int i = 0; i < sDumpLogs.size(); i++) {
5063 Log.d(TAG, " " + sDumpLogs.get(i));
5064 }
5065 Log.d(TAG, "*********************");
5066 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005067 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005068 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005069 }
5070
5071 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005072 addDumpLog(tag, log, null, debugLog);
5073 }
5074
5075 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005076 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005077 if (e != null) {
5078 Log.d(tag, log, e);
5079 } else {
5080 Log.d(tag, log);
5081 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005082 }
Winson Chungede41292013-09-19 16:27:36 -07005083 if (DEBUG_DUMP_LOG) {
5084 sDateStamp.setTime(System.currentTimeMillis());
5085 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005086 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5087 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005088 }
Winson Chungede41292013-09-19 16:27:36 -07005089 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005090 }
5091
Adam Cohen59400422014-03-05 18:07:04 -08005092 public static CustomAppWidget getCustomAppWidget(String name) {
5093 return sCustomAppWidgets.get(name);
5094 }
5095
5096 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
5097 return sCustomAppWidgets;
5098 }
5099
Winson Chungede41292013-09-19 16:27:36 -07005100 public void dumpLogsToLocalData() {
5101 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005102 new AsyncTask<Void, Void, Void>() {
5103 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005104 boolean success = false;
5105 sDateStamp.setTime(sRunStart);
5106 String FILENAME = sDateStamp.getMonth() + "-"
5107 + sDateStamp.getDay() + "_"
5108 + sDateStamp.getHours() + "-"
5109 + sDateStamp.getMinutes() + "_"
5110 + sDateStamp.getSeconds() + ".txt";
5111
5112 FileOutputStream fos = null;
5113 File outFile = null;
5114 try {
5115 outFile = new File(getFilesDir(), FILENAME);
5116 outFile.createNewFile();
5117 fos = new FileOutputStream(outFile);
5118 } catch (Exception e) {
5119 e.printStackTrace();
5120 }
5121 if (fos != null) {
5122 PrintWriter writer = new PrintWriter(fos);
5123
5124 writer.println(" ");
5125 writer.println("Debug logs: ");
5126 synchronized (sDumpLogs) {
5127 for (int i = 0; i < sDumpLogs.size(); i++) {
5128 writer.println(" " + sDumpLogs.get(i));
5129 }
5130 }
5131 writer.close();
5132 }
5133 try {
5134 if (fos != null) {
5135 fos.close();
5136 success = true;
5137 }
5138 } catch (IOException e) {
5139 e.printStackTrace();
5140 }
Michael Jurka43467462013-11-14 12:03:58 +01005141 return null;
Winson Chungede41292013-09-19 16:27:36 -07005142 }
Michael Jurka43467462013-11-14 12:03:58 +01005143 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005144 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005145 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005146}
Michael Jurka2763be32011-02-24 11:19:57 -08005147
Michael Jurkaabded662011-03-04 12:06:57 -08005148interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005149 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005150 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005151 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005152 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005153 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005154}
Dan Sandlerd5024042014-01-09 15:01:33 -05005155
5156interface DebugIntents {
5157 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5158 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005159}