blob: adea29e763452e5e81b6976e2b1bd81cbe970dac [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 Cohended9f8d2010-11-03 13:25:16 -0700307 // Related to the auto-advancing of widgets
308 private final int ADVANCE_MSG = 1;
309 private final int mAdvanceInterval = 20000;
310 private final int mAdvanceStagger = 250;
311 private long mAutoAdvanceSentTime;
312 private long mAutoAdvanceTimeLeft = -1;
313 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
314 new HashMap<View, AppWidgetProviderInfo>();
315
Winson Chung400438b2011-01-16 17:53:48 -0800316 // Determines how long to wait after a rotation before restoring the screen orientation to
317 // match the sensor state.
318 private final int mRestoreScreenOrientationDelay = 500;
319
Craig Mautner360310b2012-10-26 15:13:08 -0700320 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700321
Adam Cohen1462de32012-07-24 22:34:36 -0700322 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700323 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700324
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700325 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700326 static Date sDateStamp = new Date();
327 static DateFormat sDateFormat =
328 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
329 static long sRunStart = System.currentTimeMillis();
330 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700331
Winson Chung46353de2012-02-16 14:05:10 -0800332 // We only want to get the SharedPreferences once since it does an FS stat each time we get
333 // it from the context.
334 private SharedPreferences mSharedPrefs;
335
Winson Chungf0c6ae02012-03-21 16:10:31 -0700336 // Holds the page that we need to animate to, and the icon views that we need to animate up
337 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700338 private ImageView mFolderIconImageView;
339 private Bitmap mFolderIconBitmap;
340 private Canvas mFolderIconCanvas;
341 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700342
Michael Jurkaddd62e92011-02-16 17:49:14 -0800343 private BubbleTextView mWaitingForResume;
344
Adam Cohen59400422014-03-05 18:07:04 -0800345 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
346 new HashMap<String, CustomAppWidget>();
347
348 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
349 static {
350 if (ENABLE_CUSTOM_WIDGET_TEST) {
351 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
352 }
353 }
354
Michael Jurkac1f5d262011-09-30 19:32:27 -0700355 private Runnable mBuildLayersRunnable = new Runnable() {
356 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700357 if (mWorkspace != null) {
358 mWorkspace.buildPageHardwareLayers();
359 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700360 }
361 };
362
Adam Cohendb364c32014-05-20 14:23:40 -0700363 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364
365 private static class PendingAddArguments {
366 int requestCode;
367 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700368 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700369 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800370 int cellX;
371 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700372 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800373 }
374
Daniel Sandlerff02d492013-08-05 02:12:05 -0400375 private Stats mStats;
376
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700377 FocusIndicatorView mFocusHandler;
378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 @Override
380 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700381 if (DEBUG_STRICT_MODE) {
382 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
383 .detectDiskReads()
384 .detectDiskWrites()
385 .detectNetwork() // or .detectAll() for all detectable problems
386 .penaltyLog()
387 .build());
388 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
389 .detectLeakedSqlLiteObjects()
390 .detectLeakedClosableObjects()
391 .penaltyLog()
392 .penaltyDeath()
393 .build());
394 }
395
Adam Cohen9211d422014-10-07 18:14:53 -0700396 if (mLauncherCallbacks != null) {
397 mLauncherCallbacks.preOnCreate();
398 }
399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400401
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400402 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400403 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700404 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700405
Winson Chung5f8afe62013-08-12 16:19:28 -0700406 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700407 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700408
409 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400410 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700411 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700412 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800413 mModel = app.setLauncher(this);
414 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700415 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400416 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700418
Daniel Sandlerff02d492013-08-05 02:12:05 -0400419 mStats = new Stats(this);
420
Sunny Goyalffe83f12014-08-14 17:39:34 -0700421 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700422
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700423 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
424 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700426 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
427 // this also ensures that any synchronous binding below doesn't re-trigger another
428 // LauncherModel load.
429 mPaused = false;
430
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200432 android.os.Debug.startMethodTracing(
433 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 }
435
436 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100440 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800442 registerContentObservers();
443
Joe Onorato7c312c12009-08-13 21:36:53 -0700444 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 mSavedState = savedInstanceState;
447 restoreState(mSavedState);
448
449 if (PROFILE_STARTUP) {
450 android.os.Debug.stopMethodTracing();
451 }
452
453 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700454 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700455 // If the user leaves launcher, then we should just load items asynchronously when
456 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500457 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700458 } else {
459 // We only load the page synchronously if the user rotates (or triggers a
460 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800461 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 }
464
465 // For handling default keys
466 mDefaultKeySsb = new SpannableStringBuilder();
467 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800468
469 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
470 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800471
Adam Cohenf9426d52012-06-04 17:26:21 -0700472 // On large interfaces, we want the screen to auto-rotate based on the current orientation
473 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700474
Adam Cohen9211d422014-10-07 18:14:53 -0700475 if (mLauncherCallbacks != null) {
476 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700477 if (mLauncherCallbacks.hasLauncherOverlay()) {
478 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700479 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
480 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
481 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700482 mWorkspace.setLauncherOverlay(mLauncherOverlay);
483 }
Adam Cohen9211d422014-10-07 18:14:53 -0700484 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700485
486 if (shouldShowIntroScreen()) {
487 showIntroScreen();
488 } else {
489 showFirstRunActivity();
490 showFirstRunClings();
491 }
Adam Cohen9211d422014-10-07 18:14:53 -0700492 }
493
494 private LauncherCallbacks mLauncherCallbacks;
495
496 public void onPostCreate(Bundle savedInstanceState) {
497 super.onPostCreate(savedInstanceState);
498 if (mLauncherCallbacks != null) {
499 mLauncherCallbacks.onPostCreate(savedInstanceState);
500 }
501 }
502
503 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
504 mLauncherCallbacks = callbacks;
505 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700506 }
507
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700508 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700509 public void onLauncherProviderChange() {
510 if (mLauncherCallbacks != null) {
511 mLauncherCallbacks.onLauncherProviderChange();
512 }
513 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700514
Adam Cohen9211d422014-10-07 18:14:53 -0700515 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700516 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700517 if (mLauncherCallbacks != null) {
518 return mLauncherCallbacks.hasCustomContentToLeft();
519 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700520 return false;
521 }
522
Dave Hawkeya8881582013-09-17 15:55:33 -0600523 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500524 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600525 * {@link #addToCustomContentPage}. This will only be invoked if
526 * {@link #hasCustomContentToLeft()} is {@code true}.
527 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500528 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700529 if (mLauncherCallbacks != null) {
530 mLauncherCallbacks.populateCustomContentContainer();
531 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600532 }
533
534 /**
535 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
536 * ensure the custom content page is added or removed if necessary.
537 */
538 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600539 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600540 // Not bound yet, wait for bindScreens to be called.
541 return;
542 }
543
544 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
545 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500546 mWorkspace.createCustomContentContainer();
547 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600548 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
549 mWorkspace.removeCustomContentPage();
550 }
551 }
552
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700554 if (sLocaleConfiguration == null) {
555 new AsyncTask<Void, Void, LocaleConfiguration>() {
556 @Override
557 protected LocaleConfiguration doInBackground(Void... unused) {
558 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
559 readConfiguration(Launcher.this, localeConfiguration);
560 return localeConfiguration;
561 }
562
563 @Override
564 protected void onPostExecute(LocaleConfiguration result) {
565 sLocaleConfiguration = result;
566 checkForLocaleChange(); // recursive, but now with a locale configuration
567 }
568 }.execute();
569 return;
570 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700571
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 final Configuration configuration = getResources().getConfiguration();
573
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700574 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 final String locale = configuration.locale.toString();
576
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700577 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 final int mcc = configuration.mcc;
579
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700580 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 final int mnc = configuration.mnc;
582
Romain Guy84f296c2009-11-04 15:00:44 -0800583 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584
Romain Guy84f296c2009-11-04 15:00:44 -0800585 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700586 sLocaleConfiguration.locale = locale;
587 sLocaleConfiguration.mcc = mcc;
588 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700589
Joe Onorato0589f0f2010-02-08 13:44:00 -0800590 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700591
592 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100593 new AsyncTask<Void, Void, Void>() {
594 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700595 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100596 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700597 }
Michael Jurka43467462013-11-14 12:03:58 +0100598 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700599 }
600 }
601
602 private static class LocaleConfiguration {
603 public String locale;
604 public int mcc = -1;
605 public int mnc = -1;
606 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700607
Romain Guy98d01652009-06-30 16:21:04 -0700608 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
609 DataInputStream in = null;
610 try {
Helena Josol28db2802014-10-09 17:04:09 +0100611 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700612 configuration.locale = in.readUTF();
613 configuration.mcc = in.readInt();
614 configuration.mnc = in.readInt();
615 } catch (FileNotFoundException e) {
616 // Ignore
617 } catch (IOException e) {
618 // Ignore
619 } finally {
620 if (in != null) {
621 try {
622 in.close();
623 } catch (IOException e) {
624 // Ignore
625 }
626 }
627 }
628 }
629
630 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
631 DataOutputStream out = null;
632 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100633 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100634 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700635 out.writeUTF(configuration.locale);
636 out.writeInt(configuration.mcc);
637 out.writeInt(configuration.mnc);
638 out.flush();
639 } catch (FileNotFoundException e) {
640 // Ignore
641 } catch (IOException e) {
642 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100643 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700644 } finally {
645 if (out != null) {
646 try {
647 out.close();
648 } catch (IOException e) {
649 // Ignore
650 }
651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
653 }
654
Anton Hanssona2f665f2013-09-26 12:18:32 +0100655 public Stats getStats() {
656 return mStats;
657 }
658
Bjorn Bringertc459e522013-06-07 19:36:01 +0100659 public LayoutInflater getInflater() {
660 return mInflater;
661 }
662
Winson Chung36a62fe2012-05-06 18:04:42 -0700663 boolean isDraggingEnabled() {
664 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
665 // that is subsequently removed from the workspace in startBinding().
666 return !mModel.isLoadingWorkspace();
667 }
668
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800669 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000670 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100671 if (Build.VERSION.SDK_INT >= 17) {
672 return View.generateViewId();
673 } else {
674 // View.generateViewId() is not available. The following fallback logic is a copy
675 // of its implementation.
676 for (;;) {
677 final int result = sNextGeneratedId.get();
678 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
679 int newValue = result + 1;
680 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
681 if (sNextGeneratedId.compareAndSet(result, newValue)) {
682 return result;
683 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000684 }
685 }
686 }
687
688 public int getViewIdForItem(ItemInfo info) {
689 // This cast is safe given the > 2B range for int.
690 int itemId = (int) info.id;
691 if (mItemIdToViewId.containsKey(itemId)) {
692 return mItemIdToViewId.get(itemId);
693 }
694 int viewId = generateViewId();
695 mItemIdToViewId.put(itemId, viewId);
696 return viewId;
697 }
698
699 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700700 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
701 * a configuration step, this allows the proper animations to run after other transitions.
702 */
Adam Cohendb364c32014-05-20 14:23:40 -0700703 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700704 long screenId = args.screenId;
705 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
706 // When the screen id represents an actual screen (as opposed to a rank) we make sure
707 // that the drop page actually exists.
708 screenId = ensurePendingDropLayoutExists(args.screenId);
709 }
Adam Cohendb364c32014-05-20 14:23:40 -0700710
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800711 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800712 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700713 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700714 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700715 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800716 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700717 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700718 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700719 case REQUEST_RECONFIGURE_APPWIDGET:
720 completeRestoreAppWidget(args.appWidgetId);
721 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800722 }
Michael Jurka27614d22012-04-02 06:40:22 -0700723 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
724 // if you turned the screen off and then back while in All Apps, Launcher would not
725 // return to the workspace. Clearing mAddInfo.container here fixes this issue
726 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700727 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800728 }
729
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800730 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700731 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700732 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700733 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700734 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800735 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700736
Adam Cohenad4e15c2013-10-17 16:21:35 -0700737 Runnable exitSpringLoaded = new Runnable() {
738 @Override
739 public void run() {
740 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
741 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
742 }
743 };
744
Michael Jurka8b805b12012-04-18 14:23:14 -0700745 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700746 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700747 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
748 if (resultCode == RESULT_CANCELED) {
749 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700750 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700751 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700752 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800753 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700754 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700755 }
756 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200757 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
758 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
759 mWorkspace.exitOverviewMode(false);
760 }
761 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700762 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200763
Adam Cohened66b2b2012-01-23 17:28:51 -0800764 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
765 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700766
Adam Cohendb364c32014-05-20 14:23:40 -0700767 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800768 // We have special handling for widgets
769 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800770 final int appWidgetId;
771 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
772 : -1;
773 if (widgetId < 0) {
774 appWidgetId = pendingAddWidgetId;
775 } else {
776 appWidgetId = widgetId;
777 }
778
Adam Cohenad4e15c2013-10-17 16:21:35 -0700779 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800780 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700781 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
782 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700783 result = RESULT_CANCELED;
784 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700785 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700786 @Override
787 public void run() {
788 exitSpringLoadedDragModeDelayed(false, 0, null);
789 }
790 };
Adam Cohendb364c32014-05-20 14:23:40 -0700791 if (workspaceLocked) {
792 // No need to remove the empty screen if we're mid-binding, as the
793 // the bind will not add the empty screen.
794 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
795 } else {
796 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
797 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
798 }
Winson Chung5aaab772012-04-27 15:24:02 -0700799 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700800 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700801 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
802 // When the screen id represents an actual screen (as opposed to a rank)
803 // we make sure that the drop page actually exists.
804 mPendingAddInfo.screenId =
805 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
806 }
Adam Cohendb364c32014-05-20 14:23:40 -0700807 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
808
809 dropLayout.setDropPending(true);
810 final Runnable onComplete = new Runnable() {
811 @Override
812 public void run() {
813 completeTwoStageWidgetDrop(resultCode, appWidgetId);
814 dropLayout.setDropPending(false);
815 }
816 };
817 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
818 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
819 } else {
820 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
821 mPendingAddInfo);
822 sPendingAddItem = args;
823 }
Winson Chung5aaab772012-04-27 15:24:02 -0700824 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800825 return;
826 }
827
Sunny Goyalff572272014-07-23 13:58:07 -0700828 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
829 if (resultCode == RESULT_OK) {
830 // Update the widget view.
831 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
832 pendingAddWidgetId, mPendingAddInfo);
833 if (workspaceLocked) {
834 sPendingAddItem = args;
835 } else {
836 completeAdd(args);
837 }
838 }
839 // Leave the widget in the pending state if the user canceled the configure.
840 return;
841 }
842
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 // The pattern used here is that a user PICKs a specific application,
844 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
847 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700848 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700849 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
850 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800851 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700852 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800853 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700854 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700855 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
856 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 }
Adam Cohen00074722013-10-11 17:46:09 -0700858 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700859 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700860 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800862 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800863
864 }
865
866 @Override
867 protected void onActivityResult(
868 final int requestCode, final int resultCode, final Intent data) {
869 handleActivityResult(requestCode, resultCode, data);
870 if (mLauncherCallbacks != null) {
871 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
872 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800873 }
874
Adam Cohendb364c32014-05-20 14:23:40 -0700875 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
876 appWidgetId, ItemInfo info) {
877 PendingAddArguments args = new PendingAddArguments();
878 args.requestCode = requestCode;
879 args.intent = data;
880 args.container = info.container;
881 args.screenId = info.screenId;
882 args.cellX = info.cellX;
883 args.cellY = info.cellY;
884 args.appWidgetId = appWidgetId;
885 return args;
886 }
887
888 /**
889 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
890 *
891 * @param screenId the screen id to check
892 * @return the new screen, or screenId if it exists
893 */
894 private long ensurePendingDropLayoutExists(long screenId) {
895 CellLayout dropLayout =
896 (CellLayout) mWorkspace.getScreenWithId(screenId);
897 if (dropLayout == null) {
898 // it's possible that the add screen was removed because it was
899 // empty and a re-bind occurred
900 mWorkspace.addExtraEmptyScreen();
901 return mWorkspace.commitExtraEmptyScreen();
902 } else {
903 return screenId;
904 }
905 }
906
Adam Cohened66b2b2012-01-23 17:28:51 -0800907 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700908 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700909 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800910 Runnable onCompleteRunnable = null;
911 int animationType = 0;
912
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700913 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 if (resultCode == RESULT_OK) {
915 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
916 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700917 mPendingAddWidgetInfo);
918 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800919 onCompleteRunnable = new Runnable() {
920 @Override
921 public void run() {
922 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700923 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700924 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
925 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 }
927 };
928 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800929 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700932 if (mDragLayer.getAnimatedView() != null) {
933 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
934 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
935 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700936 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700937 // The animated view may be null in the case of a rotation during widget configuration
938 onCompleteRunnable.run();
939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
942 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700943 protected void onStop() {
944 super.onStop();
945 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700946
947 if (mLauncherCallbacks != null) {
948 mLauncherCallbacks.onStop();
949 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700950 }
951
952 @Override
953 protected void onStart() {
954 super.onStart();
955 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700956
957 if (mLauncherCallbacks != null) {
958 mLauncherCallbacks.onStart();
959 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700960 }
961
962 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200964 long startTime = 0;
965 if (DEBUG_RESUME_TIME) {
966 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400967 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200968 }
Adam Cohen9211d422014-10-07 18:14:53 -0700969
970 if (mLauncherCallbacks != null) {
971 mLauncherCallbacks.preOnResume();
972 }
973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700975
Winson Chung4a2afa32012-07-19 14:53:05 -0700976 // Restore the previous launcher state
977 if (mOnResumeState == State.WORKSPACE) {
978 showWorkspace(false);
979 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800980 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700981 }
982 mOnResumeState = State.NONE;
983
Craig Mautner360310b2012-10-26 15:13:08 -0700984 // Background was set to gradient in onPause(), restore to black if in all apps.
985 setWorkspaceBackground(mState == State.WORKSPACE);
986
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800987 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700988 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700989 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500990 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400991 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700992 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700994 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200995 // We might have postponed some bind calls until onResume (see waitUntilResume) --
996 // execute them here
997 long startTimeCallbacks = 0;
998 if (DEBUG_RESUME_TIME) {
999 startTimeCallbacks = System.currentTimeMillis();
1000 }
1001
1002 if (mAppsCustomizeContent != null) {
1003 mAppsCustomizeContent.setBulkBind(true);
1004 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001005 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1006 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001007 }
1008 if (mAppsCustomizeContent != null) {
1009 mAppsCustomizeContent.setBulkBind(false);
1010 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001011 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001012 if (DEBUG_RESUME_TIME) {
1013 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1014 (System.currentTimeMillis() - startTimeCallbacks));
1015 }
Michael Jurka447bf842013-05-15 14:52:15 +02001016 }
Michael Jurka54554252013-08-01 12:52:23 +02001017 if (mOnResumeCallbacks.size() > 0) {
1018 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1019 mOnResumeCallbacks.get(i).run();
1020 }
1021 mOnResumeCallbacks.clear();
1022 }
Winson Chunge4e50662012-01-23 14:45:13 -08001023
1024 // Reset the pressed state of icons that were locked in the press state while activities
1025 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001026 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001027 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001028 mWaitingForResume.setStayPressed(false);
1029 }
Winson Chung82963d52013-10-09 11:20:57 -07001030
Adam Cohen06dff352012-06-01 17:17:08 -07001031 // It is possible that widgets can receive updates while launcher is not in the foreground.
1032 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1033 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1034 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001035 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001036
Winson Chung780fe592013-09-26 14:48:44 -07001037 // Process any items that were added while Launcher was away.
1038 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1039
Michael Jurka447bf842013-05-15 14:52:15 +02001040 if (DEBUG_RESUME_TIME) {
1041 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1042 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001043
1044 if (mWorkspace.getCustomContentCallbacks() != null) {
1045 // If we are resuming and the custom content is the current page, we call onShow().
1046 // It is also poassible that onShow will instead be called slightly after first layout
1047 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1048 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001049 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001050 }
1051 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001052 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001053 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001054
1055 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001056
1057 if (mLauncherCallbacks != null) {
1058 mLauncherCallbacks.onResume();
1059 }
Adam Cohen06dff352012-06-01 17:17:08 -07001060 }
1061
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001063 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001064 // Ensure that items added to Launcher are queued until Launcher returns
1065 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001066 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001067
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001068 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001069 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001070 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001071 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001072
1073 // We call onHide() aggressively. The custom content callbacks should be able to
1074 // debounce excess onHide calls.
1075 if (mWorkspace.getCustomContentCallbacks() != null) {
1076 mWorkspace.getCustomContentCallbacks().onHide();
1077 }
Romain Guycbb89e42009-06-08 15:52:54 -07001078
Adam Cohen9211d422014-10-07 18:14:53 -07001079 if (mLauncherCallbacks != null) {
1080 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001081 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001082 }
1083
1084 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001085 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1086 // by a onResume or by scrolling otherwise.
1087 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001088
1089 // Custom content is completely hidden
1090 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001091
1092 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1093 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001094
1095 // Indicates whether the user is allowed to scroll away from the custom content.
1096 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001097 }
1098
Adam Cohenc2d6e892014-10-16 09:49:24 -07001099 public interface LauncherOverlay {
1100
1101 /**
1102 * Touch interaction leading to overscroll has begun
1103 */
1104 public void onScrollInteractionBegin();
1105
1106 /**
1107 * Touch interaction related to overscroll has ended
1108 */
1109 public void onScrollInteractionEnd();
1110
1111 /**
1112 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1113 * screen (or in the case of RTL, the rightmost screen).
1114 */
1115 public void onScrollChange(int progress, boolean rtl);
1116
1117 /**
1118 * Screen has stopped scrolling
1119 */
1120 public void onScrollSettled();
1121
1122 /**
1123 * This method can be called by the Launcher in order to force the LauncherOverlay
1124 * to exit fully immersive mode.
1125 */
1126 public void forceExitFullImmersion();
1127 }
1128
1129 public interface LauncherOverlayCallbacks {
1130 /**
1131 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1132 * however it doesn't modify any state within the launcher.
1133 */
1134 public boolean canEnterFullImmersion();
1135
1136 /**
1137 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1138 * eg. by occupying the full screen and handling all touch events.
1139 *
1140 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1141 * case, Launcher will modify any necessary state and assumes the overlay is
1142 * handling all interaction. If false, the LauncherOverlay should cancel any
1143 *
1144 */
1145 public boolean enterFullImmersion();
1146
1147 /**
1148 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1149 * full control over UI and state.
1150 */
1151 public void exitFullImmersion();
1152 }
1153
1154 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1155
1156 @Override
1157 public boolean canEnterFullImmersion() {
1158 return mState == State.WORKSPACE;
1159 }
1160
1161 @Override
1162 public boolean enterFullImmersion() {
1163 if (mState == State.WORKSPACE) {
1164 // When fully immersed, disregard any touches which fall through.
1165 mDragLayer.setBlockTouch(true);
1166 return true;
1167 }
1168 return false;
1169 }
1170
1171 @Override
1172 public void exitFullImmersion() {
1173 mDragLayer.setBlockTouch(false);
1174 }
1175 }
1176
Jorim Jaggid017f882014-01-14 17:08:48 -08001177 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001178 if (mLauncherCallbacks != null) {
1179 return mLauncherCallbacks.hasSettings();
1180 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001181 return false;
1182 }
1183
Adam Cohenbffe7452013-07-22 18:21:45 -07001184
Adam Cohen9211d422014-10-07 18:14:53 -07001185 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001186 CustomContentCallbacks callbacks, String description) {
1187 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001188 }
1189
Adam Cohenedb40762013-07-18 16:45:45 -07001190 // The custom content needs to offset its content to account for the QSB
1191 public int getTopOffsetForCustomContent() {
1192 return mWorkspace.getPaddingTop();
1193 }
1194
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001195 @Override
1196 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001197 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001198 if (mModel.isCurrentCallbacks(this)) {
1199 mModel.stopLoader();
1200 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001201 if (mAppsCustomizeContent != null) {
1202 mAppsCustomizeContent.surrender();
1203 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001204 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001205 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001206
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001207 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001208 @Override
1209 public void onWindowFocusChanged(boolean hasFocus) {
1210 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001211 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001212
1213 if (mLauncherCallbacks != null) {
1214 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1215 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001216 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 private boolean acceptFilter() {
1219 final InputMethodManager inputManager = (InputMethodManager)
1220 getSystemService(Context.INPUT_METHOD_SERVICE);
1221 return !inputManager.isFullscreenMode();
1222 }
1223
1224 @Override
1225 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001226 final int uniChar = event.getUnicodeChar();
1227 final boolean handled = super.onKeyDown(keyCode, event);
1228 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1229 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1231 keyCode, event);
1232 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001233 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001234 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001235 // showSearchDialog()
1236 // If there are multiple keystrokes before the search dialog takes focus,
1237 // onSearchRequested() will be called for every keystroke,
1238 // but it is idempotent, so it's fine.
1239 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 }
1241 }
1242
Joe Onorato8a9625e2010-01-28 15:55:35 -08001243 // Eat the long press event so the keyboard doesn't come up.
1244 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1245 return true;
1246 }
1247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 return handled;
1249 }
1250
Karl Rosaen138a0412009-04-23 19:00:21 -07001251 private String getTypedText() {
1252 return mDefaultKeySsb.toString();
1253 }
1254
1255 private void clearTypedText() {
1256 mDefaultKeySsb.clear();
1257 mDefaultKeySsb.clearSpans();
1258 Selection.setSelection(mDefaultKeySsb, 0);
1259 }
1260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001262 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1263 * State
1264 */
1265 private static State intToState(int stateOrdinal) {
1266 State state = State.WORKSPACE;
1267 final State[] stateValues = State.values();
1268 for (int i = 0; i < stateValues.length; i++) {
1269 if (stateValues[i].ordinal() == stateOrdinal) {
1270 state = stateValues[i];
1271 break;
1272 }
1273 }
1274 return state;
1275 }
1276
1277 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 * Restores the previous state, if it exists.
1279 *
1280 * @param savedState The previous state.
1281 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001282 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 private void restoreState(Bundle savedState) {
1284 if (savedState == null) {
1285 return;
1286 }
1287
Michael Jurka883f55b2010-10-21 15:47:14 -07001288 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001289 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001290 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001291 }
1292
Adam Cohen21cd0022013-10-09 18:57:02 -07001293 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1294 PagedView.INVALID_RESTORE_PAGE);
1295 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001296 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
1298
Winson Chung3d503fb2011-07-13 17:25:49 -07001299 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001300 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001301
Winson Chung3d503fb2011-07-13 17:25:49 -07001302 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1303 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001304 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001305 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1306 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001307 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1308 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1309 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001310 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001311 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 mRestoring = true;
1313 }
1314
1315 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1316 if (renameFolder) {
1317 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001318 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 mRestoring = true;
1320 }
Winson Chunga12a2502010-12-20 14:41:35 -08001321
Winson Chung785d2eb2011-04-14 16:08:02 -07001322 // Restore the AppsCustomize tab
1323 if (mAppsCustomizeTabHost != null) {
1324 String curTab = savedState.getString("apps_customize_currentTab");
1325 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001326 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001327 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001328 mAppsCustomizeContent.loadAssociatedPages(
1329 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001330 }
1331
Winson Chung5afbf7b2011-07-25 11:53:08 -07001332 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1333 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001334 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001335 mItemIdToViewId = (HashMap<Integer, Integer>)
1336 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
1339 /**
1340 * Finds all the views we need and configure them properly.
1341 */
1342 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001343 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001344
Craig Mautner360310b2012-10-26 15:13:08 -07001345 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001346 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001347 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1348 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001349 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001350 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001351
John Spurlock77e1f472013-09-11 10:09:51 -04001352 mLauncherView.setSystemUiVisibility(
1353 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001354 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355
Winson Chung4c98d922011-05-31 16:50:48 -07001356 // Setup the drag layer
1357 mDragLayer.setup(this, dragController);
1358
Winson Chung3d503fb2011-07-13 17:25:49 -07001359 // Setup the hotseat
1360 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1361 if (mHotseat != null) {
1362 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001363 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001364 }
1365
Jorim Jaggid017f882014-01-14 17:08:48 -08001366 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001367 View widgetButton = findViewById(R.id.widget_button);
1368 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001369 @Override
1370 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001371 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001372 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001373 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001374 }
1375 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001376 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1377
1378 View wallpaperButton = findViewById(R.id.wallpaper_button);
1379 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001380 @Override
1381 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001382 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001383 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001384 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001385 }
1386 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001387 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1388
1389 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001390 if (hasSettings()) {
1391 settingsButton.setOnClickListener(new OnClickListener() {
1392 @Override
1393 public void onClick(View arg0) {
1394 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001395 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001396 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001397 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001398 });
1399 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1400 } else {
1401 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001402 }
1403
Adam Cohendbdff6b2013-09-18 19:09:15 -07001404 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001405
Winson Chung4c98d922011-05-31 16:50:48 -07001406 // Setup the workspace
1407 mWorkspace.setHapticFeedbackEnabled(false);
1408 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001409 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001410 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001411
Winson Chungf0ea4d32011-06-06 14:27:16 -07001412 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001413 mSearchDropTargetBar = (SearchDropTargetBar)
1414 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001415
Winson Chungf0ea4d32011-06-06 14:27:16 -07001416 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001417 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001418 mAppsCustomizeContent = (AppsCustomizePagedView)
1419 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1420 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001421
Winson Chung3d503fb2011-07-13 17:25:49 -07001422 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001423 dragController.setDragScoller(mWorkspace);
1424 dragController.setScrollView(mDragLayer);
1425 dragController.setMoveTarget(mWorkspace);
1426 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001427 if (mSearchDropTargetBar != null) {
1428 mSearchDropTargetBar.setup(this, dragController);
Sunny Goyal594d76d2014-11-06 10:12:54 -08001429 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001430 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001431
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001432 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001433 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001434 mWeightWatcher = new WeightWatcher(this);
1435 mWeightWatcher.setAlpha(0.5f);
1436 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001437 new FrameLayout.LayoutParams(
1438 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001439 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001440 Gravity.BOTTOM)
1441 );
Adam Cohen39a06042013-07-19 14:30:12 -07001442
1443 boolean show = shouldShowWeightWatcher();
1444 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001445 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 }
1447
1448 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001449 * Sets the all apps button. This method is called from {@link Hotseat}.
1450 */
1451 public void setAllAppsButton(View allAppsButton) {
1452 mAllAppsButton = allAppsButton;
1453 }
1454
1455 public View getAllAppsButton() {
1456 return mAllAppsButton;
1457 }
1458
1459 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 * Creates a view representing a shortcut.
1461 *
1462 * @param info The data structure describing the shortcut.
1463 *
1464 * @return A View inflated from R.layout.application.
1465 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001466 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001468 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 }
1470
1471 /**
1472 * Creates a view representing a shortcut inflated from the specified resource.
1473 *
1474 * @param layoutResId The id of the XML layout used to create the shortcut.
1475 * @param parent The group the shortcut belongs to.
1476 * @param info The data structure describing the shortcut.
1477 *
1478 * @return A View inflated from layoutResId.
1479 */
Adam Cohen59400422014-03-05 18:07:04 -08001480 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001481 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001482 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001484 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 return favorite;
1486 }
1487
1488 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 * Add a shortcut to the workspace.
1490 *
1491 * @param data The intent describing the shortcut.
1492 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001494 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001495 int cellY) {
1496 int[] cellXY = mTmpAddItemCellCoordinates;
1497 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001498 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001499
Michael Jurkad3ef3062010-11-23 16:23:58 -08001500 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001501
Sunny Goyal2350bc92014-10-14 16:42:54 -07001502 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001503 if (info == null) {
1504 return;
1505 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001506 final View view = createShortcut(info);
1507
Adam Cohenfbba09b2011-07-18 21:43:05 -07001508 // First we check if we already know the exact location where we want to add this item.
1509 if (cellX >= 0 && cellY >= 0) {
1510 cellXY[0] = cellX;
1511 cellXY[1] = cellY;
1512 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001513
1514 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001515 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001516 true, null,null)) {
1517 return;
1518 }
1519 DragObject dragObject = new DragObject();
1520 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001521 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1522 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001523 return;
1524 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001525 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001526 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001527 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001528 foundCellSpan = (result != null);
1529 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001530 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001531 }
1532
1533 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001534 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001535 return;
1536 }
1537
Adam Cohendcd297f2013-06-18 13:13:40 -07001538 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539
1540 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001541 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001542 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 }
1544 }
1545
Adam Cohen2f093b62012-04-30 18:59:53 -07001546 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1547 int minHeight) {
1548 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001549 // We want to account for the extra amount of padding that we are adding to the widget
1550 // to ensure that it gets the full amount of space that it has requested
1551 int requiredWidth = minWidth + padding.left + padding.right;
1552 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001553 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001554 }
1555
Adam Cohen2f093b62012-04-30 18:59:53 -07001556 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1557 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001558 }
1559
Adam Cohen2f093b62012-04-30 18:59:53 -07001560 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1561 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001562 }
1563
Adam Cohen2f093b62012-04-30 18:59:53 -07001564 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1565 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001566 }
1567
Adam Cohen2f093b62012-04-30 18:59:53 -07001568 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1569 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001570 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001571 }
1572
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001574 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001576 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 */
Adam Cohen59400422014-03-05 18:07:04 -08001578 private void completeAddAppWidget(int appWidgetId, long container, long screenId,
1579 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1580
1581 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001582 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001583 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1584 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001585 }
Romain Guycbb89e42009-06-08 15:52:54 -07001586
Adam Cohen59400422014-03-05 18:07:04 -08001587 if (appWidgetInfo.isCustomWidget) {
1588 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001589 }
1590
Adam Cohen59400422014-03-05 18:07:04 -08001591 LauncherAppWidgetInfo launcherInfo;
1592 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1593 launcherInfo.spanX = info.spanX;
1594 launcherInfo.spanY = info.spanY;
1595 launcherInfo.minSpanX = info.minSpanX;
1596 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001597 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001600 container, screenId, info.cellX, info.cellY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601
1602 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001603 if (hostView == null) {
1604 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001605 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1606 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001607 } else {
1608 // The AppWidgetHostView has already been inflated and instantiated
1609 launcherInfo.hostView = hostView;
1610 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001612 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001613 launcherInfo.notifyWidgetSizeChanged(this);
1614
Adam Cohen59400422014-03-05 18:07:04 -08001615 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1616 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001617
1618 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001620 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 }
Romain Guycbb89e42009-06-08 15:52:54 -07001622
Adam Cohended9f8d2010-11-03 13:25:16 -07001623 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1624 @Override
1625 public void onReceive(Context context, Intent intent) {
1626 final String action = intent.getAction();
1627 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1628 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001629 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001630 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001631
Winson Chungc100e8e2011-08-09 16:02:43 -07001632 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001633 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001634 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001635 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001636 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001637 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1638 mUserPresent = true;
1639 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001640 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1641 mModel.resetLoadedState(false, true);
1642 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1643 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1644 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1645 mModel.resetLoadedState(false, true);
1646 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1647 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1648 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001649 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1650 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1651 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001652 }
1653 }
1654 };
1655
1656 @Override
1657 public void onAttachedToWindow() {
1658 super.onAttachedToWindow();
1659
1660 // Listen for broadcasts related to user-presence
1661 final IntentFilter filter = new IntentFilter();
1662 filter.addAction(Intent.ACTION_SCREEN_OFF);
1663 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001664 // For handling managed profiles
1665 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1666 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001667 if (ENABLE_DEBUG_INTENTS) {
1668 filter.addAction(DebugIntents.DELETE_DATABASE);
1669 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1670 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001671 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001672 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001673 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001674 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001675 mVisible = true;
1676 }
1677
Adam Cohen0b395352014-06-09 22:54:36 +00001678 /**
1679 * Sets up transparent navigation and status bars in LMP.
1680 * This method is a no-op for other platform versions.
1681 */
Sunny Goyald0091012015-01-20 15:55:34 -08001682 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001683 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001684 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001685 Window window = getWindow();
1686 window.getAttributes().systemUiVisibility |=
1687 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1688 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1689 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1690 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1691 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1692 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1693 window.setStatusBarColor(Color.TRANSPARENT);
1694 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001695 }
1696 }
1697
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 @Override
1699 public void onDetachedFromWindow() {
1700 super.onDetachedFromWindow();
1701 mVisible = false;
1702
Adam Cohend113e0c2010-11-11 10:48:05 -08001703 if (mAttached) {
1704 unregisterReceiver(mReceiver);
1705 mAttached = false;
1706 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001707 updateRunning();
1708 }
1709
1710 public void onWindowVisibilityChanged(int visibility) {
1711 mVisible = visibility == View.VISIBLE;
1712 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001713 // The following code used to be in onResume, but it turns out onResume is called when
1714 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1715 // is a more appropriate event to handle
1716 if (mVisible) {
1717 mAppsCustomizeTabHost.onWindowVisible();
1718 if (!mWorkspaceLoading) {
1719 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001720 // We want to let Launcher draw itself at least once before we force it to build
1721 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001722 // apps is nice and speedy.
1723 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001724 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001725 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001726 if (mStarted) return;
1727 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001728 // We delay the layer building a bit in order to give
1729 // other message processing a time to run. In particular
1730 // this avoids a delay in hiding the IME if it was
1731 // currently shown, because doing that may involve
1732 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001733 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001734 final ViewTreeObserver.OnDrawListener listener = this;
1735 mWorkspace.post(new Runnable() {
1736 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001737 if (mWorkspace != null &&
1738 mWorkspace.getViewTreeObserver() != null) {
1739 mWorkspace.getViewTreeObserver().
1740 removeOnDrawListener(listener);
1741 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001742 }
1743 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001744 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001745 }
1746 });
1747 }
1748 clearTypedText();
1749 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001750 }
1751
1752 private void sendAdvanceMessage(long delay) {
1753 mHandler.removeMessages(ADVANCE_MSG);
1754 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1755 mHandler.sendMessageDelayed(msg, delay);
1756 mAutoAdvanceSentTime = System.currentTimeMillis();
1757 }
1758
1759 private void updateRunning() {
1760 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1761 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1762 mAutoAdvanceRunning = autoAdvanceRunning;
1763 if (autoAdvanceRunning) {
1764 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1765 sendAdvanceMessage(delay);
1766 } else {
1767 if (!mWidgetsToAdvance.isEmpty()) {
1768 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1769 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1770 }
1771 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001772 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001773 }
1774 }
1775 }
1776
1777 private final Handler mHandler = new Handler() {
1778 @Override
1779 public void handleMessage(Message msg) {
1780 if (msg.what == ADVANCE_MSG) {
1781 int i = 0;
1782 for (View key: mWidgetsToAdvance.keySet()) {
1783 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1784 final int delay = mAdvanceStagger * i;
1785 if (v instanceof Advanceable) {
1786 postDelayed(new Runnable() {
1787 public void run() {
1788 ((Advanceable) v).advance();
1789 }
1790 }, delay);
1791 }
1792 i++;
1793 }
1794 sendAdvanceMessage(mAdvanceInterval);
1795 }
1796 }
1797 };
1798
1799 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001800 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001801 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1802 if (v instanceof Advanceable) {
1803 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001804 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001805 updateRunning();
1806 }
1807 }
1808
1809 void removeWidgetToAutoAdvance(View hostView) {
1810 if (mWidgetsToAdvance.containsKey(hostView)) {
1811 mWidgetsToAdvance.remove(hostView);
1812 updateRunning();
1813 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001814 }
1815
Joe Onorato9c1289c2009-08-17 11:03:03 -04001816 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001817 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001818 launcherInfo.hostView = null;
1819 }
1820
Winson Chung93eef082012-03-23 15:59:27 -07001821 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1822 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1823 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001824 }
1825
Adam Cohen59400422014-03-05 18:07:04 -08001826 public ArrayList<AppInfo> getAllAppsList() {
1827 return mAppsCustomizeContent.getApps();
1828 }
1829
Winson Chunga6945242014-01-08 14:04:34 -08001830 public DragLayer getDragLayer() {
1831 return mDragLayer;
1832 }
1833
1834 public Workspace getWorkspace() {
1835 return mWorkspace;
1836 }
1837
1838 public Hotseat getHotseat() {
1839 return mHotseat;
1840 }
1841
Jorim Jaggid017f882014-01-14 17:08:48 -08001842 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001843 return mOverviewPanel;
1844 }
1845
1846 public SearchDropTargetBar getSearchBar() {
1847 return mSearchDropTargetBar;
1848 }
1849
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001850 public LauncherAppWidgetHost getAppWidgetHost() {
1851 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 }
Romain Guycbb89e42009-06-08 15:52:54 -07001853
Winson Chunga9abd0e2010-10-27 17:18:37 -07001854 public LauncherModel getModel() {
1855 return mModel;
1856 }
1857
Winson Chunga6945242014-01-08 14:04:34 -08001858 protected SharedPreferences getSharedPrefs() {
1859 return mSharedPrefs;
1860 }
1861
Bjorn Bringertc459e522013-06-07 19:36:01 +01001862 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001863 getWindow().closeAllPanels();
1864
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001865 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001866 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001867 }
1868
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 @Override
1870 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001871 long startTime = 0;
1872 if (DEBUG_RESUME_TIME) {
1873 startTime = System.currentTimeMillis();
1874 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 super.onNewIntent(intent);
1876
1877 // Close the menu
1878 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001879 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001880 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001881
Adam Cohened307df2013-10-02 09:37:31 -07001882 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1883 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1884 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001885
Adam Cohen6fecd412013-10-02 17:41:50 -07001886 if (mWorkspace == null) {
1887 // Can be cases where mWorkspace is null, this prevents a NPE
1888 return;
1889 }
1890 Folder openFolder = mWorkspace.getOpenFolder();
1891 // In all these cases, only animate if we're already on home
1892 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001893
1894 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1895 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001896 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001897 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001898 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001899 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001900
Adam Cohen6fecd412013-10-02 17:41:50 -07001901 closeFolder();
1902 exitSpringLoadedDragMode();
1903
1904 // If we are already on home, then just animate back to the workspace,
1905 // otherwise, just wait until onResume to set the state back to Workspace
1906 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001907 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001908 } else {
1909 mOnResumeState = State.WORKSPACE;
1910 }
1911
1912 final View v = getWindow().peekDecorView();
1913 if (v != null && v.getWindowToken() != null) {
1914 InputMethodManager imm = (InputMethodManager)getSystemService(
1915 INPUT_METHOD_SERVICE);
1916 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1917 }
1918
1919 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001920 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001921 mAppsCustomizeTabHost.reset();
1922 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001923
Adam Cohen9211d422014-10-07 18:14:53 -07001924 if (mLauncherCallbacks != null) {
1925 mLauncherCallbacks.onHomeIntent();
1926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 }
Adam Cohened307df2013-10-02 09:37:31 -07001928
Michael Jurka447bf842013-05-15 14:52:15 +02001929 if (DEBUG_RESUME_TIME) {
1930 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932
Adam Cohen9211d422014-10-07 18:14:53 -07001933 if (mLauncherCallbacks != null) {
1934 mLauncherCallbacks.onNewIntent(intent);
1935 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001936 }
1937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001939 public void onRestoreInstanceState(Bundle state) {
1940 super.onRestoreInstanceState(state);
1941 for (int page: mSynchronouslyBoundPages) {
1942 mWorkspace.restoreInstanceStateForChild(page);
1943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
1945
1946 @Override
1947 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001948 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001949 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1950 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001951 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001952 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953
Michael Jurka883f55b2010-10-21 15:47:14 -07001954 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001955 // We close any open folder since it will not be re-opened, and we need to make sure
1956 // this state is reflected.
1957 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958
Adam Cohendcd297f2013-06-18 13:13:40 -07001959 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001960 mWaitingForResult) {
1961 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001962 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001963 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1964 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001965 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1966 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1967 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001968 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
1970
1971 if (mFolderInfo != null && mWaitingForResult) {
1972 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1973 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1974 }
Michael Jurka946ad472010-07-09 18:05:18 -07001975
Winson Chung785d2eb2011-04-14 16:08:02 -07001976 // Save the current AppsCustomize tab
1977 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001978 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1979 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001980 if (currentTabTag != null) {
1981 outState.putString("apps_customize_currentTab", currentTabTag);
1982 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001983 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1984 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001985 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001986 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07001987
1988 if (mLauncherCallbacks != null) {
1989 mLauncherCallbacks.onSaveInstanceState(outState);
1990 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 }
1992
1993 @Override
1994 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001996
Winson Chunge7a03942011-08-05 15:05:12 -07001997 // Remove all pending runnables
1998 mHandler.removeMessages(ADVANCE_MSG);
1999 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002000 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002001
Winson Chungcd2b0142011-06-08 16:02:26 -07002002 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002003 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002004
2005 // It's possible to receive onDestroy after a new Launcher activity has
2006 // been created. In this case, don't interfere with the new Launcher.
2007 if (mModel.isCurrentCallbacks(this)) {
2008 mModel.stopLoader();
2009 app.setLauncher(null);
2010 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002013 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002015 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002017 mAppWidgetHost = null;
2018
2019 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020
2021 TextKeyListener.getInstance().release();
2022
Winson Chung81b52252012-08-27 15:34:29 -07002023 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2024 // to prevent leaking Launcher activities on orientation change.
2025 if (mModel != null) {
2026 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2027 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002028
2029 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002030 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002031
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002032 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002033 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002034 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002035 mWorkspace = null;
2036 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002037
Michael Jurka2ecf9952012-06-18 12:52:28 -07002038 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002039
2040 if (mLauncherCallbacks != null) {
2041 mLauncherCallbacks.onDestroy();
2042 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 }
2044
Adam Cohena9cf38f2011-05-02 15:36:58 -07002045 public DragController getDragController() {
2046 return mDragController;
2047 }
2048
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 @Override
2050 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002051 if (requestCode >= 0) {
2052 setWaitingForResult(true);
2053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 super.startActivityForResult(intent, requestCode);
2055 }
2056
Winson Chung70d72102011-08-12 11:24:35 -07002057 /**
2058 * Indicates that we want global search for this activity by setting the globalSearch
2059 * argument for {@link #startSearch} to true.
2060 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002062 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002064
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002065 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002066
Karl Rosaen138a0412009-04-23 19:00:21 -07002067 if (initialQuery == null) {
2068 // Use any text typed in the launcher as the initial query
2069 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 if (appSearchData == null) {
2072 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002073 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
Winson Chungadf0c182012-08-23 14:59:07 -07002075 Rect sourceBounds = new Rect();
2076 if (mSearchDropTargetBar != null) {
2077 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2078 }
Romain Guycbb89e42009-06-08 15:52:54 -07002079
Ian Parkinson047036e2014-09-01 15:40:53 +01002080 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002081 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002082 if (clearTextImmediately) {
2083 clearTypedText();
2084 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002085 }
2086
Ian Parkinson047036e2014-09-01 15:40:53 +01002087 /**
2088 * Start a text search.
2089 *
2090 * @return {@code true} if the search will start immediately, so any further keypresses
2091 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2092 * to buffer keypresses.
2093 */
2094 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002095 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002096 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2097 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2098 sourceBounds);
2099 }
2100
Michael Jurkaa33411c2012-06-14 16:18:21 -07002101 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002102 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002103 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002104 }
2105
2106 /**
2107 * Starts the global search activity. This code is a copied from SearchManager
2108 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002109 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002110 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002111 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002112 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2113 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2114 if (globalSearchActivity == null) {
2115 Log.w(TAG, "No global search activity found.");
2116 return;
2117 }
2118 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2119 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2120 intent.setComponent(globalSearchActivity);
2121 // Make sure that we have a Bundle to put source in
2122 if (appSearchData == null) {
2123 appSearchData = new Bundle();
2124 } else {
2125 appSearchData = new Bundle(appSearchData);
2126 }
Adam Cohen9211d422014-10-07 18:14:53 -07002127 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002128 if (!appSearchData.containsKey("source")) {
2129 appSearchData.putString("source", getPackageName());
2130 }
2131 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2132 if (!TextUtils.isEmpty(initialQuery)) {
2133 intent.putExtra(SearchManager.QUERY, initialQuery);
2134 }
2135 if (selectInitialQuery) {
2136 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2137 }
2138 intent.setSourceBounds(sourceBounds);
2139 try {
2140 startActivity(intent);
2141 } catch (ActivityNotFoundException ex) {
2142 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2143 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 }
2145
Yura4f93ec62014-02-04 14:15:21 +00002146 public boolean isOnCustomContent() {
2147 return mWorkspace.isOnOrMovingToCustomContent();
2148 }
2149
Winson Chung70d72102011-08-12 11:24:35 -07002150 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002151 public boolean onPrepareOptionsMenu(Menu menu) {
2152 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002153 if (!isOnCustomContent()) {
2154 // Close any open folders
2155 closeFolder();
2156 // Stop resizing any widgets
2157 mWorkspace.exitWidgetResizeMode();
2158 if (!mWorkspace.isInOverviewMode()) {
2159 // Show the overview mode
2160 showOverviewMode(true);
2161 } else {
2162 showWorkspace(true);
2163 }
Winson Chunge0298742014-01-17 12:03:00 -08002164 }
Adam Cohen9211d422014-10-07 18:14:53 -07002165 if (mLauncherCallbacks != null) {
2166 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2167 }
2168
Michael Jurkab94f3f82013-09-11 18:08:54 +02002169 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002170 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002172 @Override
2173 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002174 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002175 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002176 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002177 }
2178
Joe Onorato9c1289c2009-08-17 11:03:03 -04002179 public boolean isWorkspaceLocked() {
2180 return mWorkspaceLoading || mWaitingForResult;
2181 }
2182
Adam Cohen517a7f52014-03-01 12:12:59 -08002183 public boolean isWorkspaceLoading() {
2184 return mWorkspaceLoading;
2185 }
2186
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002187 private void setWorkspaceLoading(boolean value) {
2188 boolean isLocked = isWorkspaceLocked();
2189 mWorkspaceLoading = value;
2190 if (isLocked != isWorkspaceLocked()) {
2191 onWorkspaceLockedChanged();
2192 }
2193 }
2194
2195 private void setWaitingForResult(boolean value) {
2196 boolean isLocked = isWorkspaceLocked();
2197 mWaitingForResult = value;
2198 if (isLocked != isWorkspaceLocked()) {
2199 onWorkspaceLockedChanged();
2200 }
2201 }
2202
Adam Cohen9211d422014-10-07 18:14:53 -07002203 protected void onWorkspaceLockedChanged() {
2204 if (mLauncherCallbacks != null) {
2205 mLauncherCallbacks.onWorkspaceLockedChanged();
2206 }
2207 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002208
Michael Jurka0280c3b2010-09-17 15:00:07 -07002209 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002210 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002211 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002212 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2213 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002214 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002215 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002216 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002217
Sunny Goyale6b63a32015-01-16 16:14:29 -08002218 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002219 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002220 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2221 }
2222
Sunny Goyale6b63a32015-01-16 16:14:29 -08002223 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002224 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2225 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002226 if (appWidgetInfo.configure != null) {
2227 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002228 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002229
Bjorn Bringert7984c942009-12-09 15:38:25 +00002230 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002231 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2232 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002235 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002236 Runnable onComplete = new Runnable() {
2237 @Override
2238 public void run() {
2239 // Exit spring loaded mode if necessary after adding the widget
2240 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2241 null);
2242 }
2243 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002245 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002246 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247 }
2248 }
Romain Guycbb89e42009-06-08 15:52:54 -07002249
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002250 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002251 // Close any folders that may be open.
2252 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002253 mWorkspace.moveToCustomContentScreen(animate);
2254 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002255
2256 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2257 int[] cell, int spanX, int spanY) {
2258 switch (info.itemType) {
2259 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2260 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2261 int span[] = new int[2];
2262 span[0] = spanX;
2263 span[1] = spanY;
2264 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2265 container, screenId, cell, span);
2266 break;
2267 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2268 processShortcutFromDrop(info.componentName, container, screenId, cell);
2269 break;
2270 default:
2271 throw new IllegalStateException("Unknown item type: " + info.itemType);
2272 }
2273 }
2274
Adam Cohenfbba09b2011-07-18 21:43:05 -07002275 /**
2276 * Process a shortcut drop.
2277 *
2278 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002279 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002280 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002281 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002282 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2283 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002284 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002285 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002286 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002287 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002288
2289 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002290 mPendingAddInfo.cellX = cell[0];
2291 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002292 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002293
2294 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2295 createShortcutIntent.setComponent(componentName);
2296 processShortcut(createShortcutIntent);
2297 }
2298
Adam Cohenfbba09b2011-07-18 21:43:05 -07002299 /**
2300 * Process a widget drop.
2301 *
2302 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002303 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002304 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002305 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002306 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2307 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002308 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002309 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002310 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002311 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002312 mPendingAddInfo.minSpanX = info.minSpanX;
2313 mPendingAddInfo.minSpanY = info.minSpanY;
2314
Adam Cohenfbba09b2011-07-18 21:43:05 -07002315 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002316 mPendingAddInfo.cellX = cell[0];
2317 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002318 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002319 if (span != null) {
2320 mPendingAddInfo.spanX = span[0];
2321 mPendingAddInfo.spanY = span[1];
2322 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002323
Adam Cohened66b2b2012-01-23 17:28:51 -08002324 AppWidgetHostView hostView = info.boundWidget;
2325 int appWidgetId;
2326 if (hostView != null) {
2327 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002328 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002329 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002330 // In this case, we either need to start an activity to get permission to bind
2331 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002332 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002333 Bundle options = info.bindOptions;
2334
Sunny Goyalffe83f12014-08-14 17:39:34 -07002335 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2336 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002337 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002338 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002339 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002340 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002341 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2342 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2343 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002344 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2345 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002346 // TODO: we need to make sure that this accounts for the options bundle.
2347 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002348 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2349 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002350 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351 }
2352
Joe Onoratodeb98af2010-02-19 14:59:39 -08002353 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002354 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355 }
2356
Winson Chung24ab2f12010-09-16 14:10:47 -07002357 void processWallpaper(Intent intent) {
2358 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2359 }
2360
Adam Cohendcd297f2013-06-18 13:13:40 -07002361 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002362 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002363 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364 folderInfo.title = getText(R.string.folder_name);
2365
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002367 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002368 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002369 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370
2371 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002372 FolderIcon newFolder =
2373 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002374 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002375 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002376 // Force measure the new folder icon
2377 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2378 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002379 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380 }
Romain Guycbb89e42009-06-08 15:52:54 -07002381
Joe Onorato9c1289c2009-08-17 11:03:03 -04002382 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002383 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002384 }
2385
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002386 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002387 if (mLauncherCallbacks != null) {
2388 return mLauncherCallbacks.getWallpaperPickerComponent();
2389 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002390 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002391 }
2392
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002393 /**
2394 * Registers various content observers. The current implementation registers
2395 * only a favorites observer to keep track of the favorites applications.
2396 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002397 private void registerContentObservers() {
2398 ContentResolver resolver = getContentResolver();
2399 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2400 true, mWidgetObserver);
2401 }
2402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 @Override
2404 public boolean dispatchKeyEvent(KeyEvent event) {
2405 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2406 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002408 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002409 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002410 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002411 dumpState();
2412 return true;
2413 }
2414 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002415 }
2416 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2417 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002418 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 return true;
2420 }
2421 }
2422
2423 return super.dispatchKeyEvent(event);
2424 }
2425
Joe Onorato88ec0992009-11-19 13:16:06 -08002426 @Override
2427 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002428 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2429 return;
2430 }
2431
Winson Chungc93e5ae2012-07-23 20:48:26 -07002432 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002433 if (mAppsCustomizeContent.getContentType() ==
2434 AppsCustomizePagedView.ContentType.Applications) {
2435 showWorkspace(true);
2436 } else {
2437 showOverviewMode(true);
2438 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002439 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002440 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002441 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002442 Folder openFolder = mWorkspace.getOpenFolder();
2443 if (openFolder.isEditingName()) {
2444 openFolder.dismissEditingName();
2445 } else {
2446 closeFolder();
2447 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002448 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002449 mWorkspace.exitWidgetResizeMode();
2450
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002451 // Back button is a no-op here, but give at least some feedback for the button press
2452 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002453 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002454 }
2455
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002456 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002457 * Re-listen when widgets are reset.
2458 */
2459 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002460 if (mAppWidgetHost != null) {
2461 mAppWidgetHost.startListening();
2462 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002463 }
2464
2465 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 * Launches the intent referred by the clicked shortcut.
2467 *
2468 * @param v The view representing the clicked shortcut.
2469 */
2470 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002471 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2472 // view has detached (it's possible for this to happen if the view is removed mid touch).
2473 if (v.getWindowToken() == null) {
2474 return;
2475 }
2476
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002477 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002478 return;
2479 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002480
Adam Cohen1697b792013-09-17 19:08:21 -07002481 if (v instanceof Workspace) {
2482 if (mWorkspace.isInOverviewMode()) {
2483 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002484 }
2485 return;
2486 }
2487
2488 if (v instanceof CellLayout) {
2489 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002490 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002491 }
2492 }
2493
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002495 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002496 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002498 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002499 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002500 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002501 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002502 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002503 } else if (tag instanceof AppInfo) {
2504 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002505 } else if (tag instanceof LauncherAppWidgetInfo) {
2506 if (v instanceof PendingAppWidgetHostView) {
2507 onClickPendingWidget((PendingAppWidgetHostView) v);
2508 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002509 }
2510 }
2511
Sunny Goyal508da152014-08-14 10:53:27 -07002512 public void onClickPagedViewIcon(View v) {
2513 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002514 if (mLauncherCallbacks != null) {
2515 mLauncherCallbacks.onClickPagedViewIcon(v);
2516 }
Sunny Goyal508da152014-08-14 10:53:27 -07002517 }
2518
Michael Jurka0e260592010-06-30 17:07:39 -07002519 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002520 return false;
2521 }
2522
Michael Jurkaaf442092010-06-10 17:01:57 -07002523 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002524 * Event handler for the app widget view which has not fully restored.
2525 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002526 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002527 if (mIsSafeModeEnabled) {
2528 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2529 return;
2530 }
2531
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002532 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002533 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002534 int widgetId = info.appWidgetId;
2535 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2536 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002537 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2538 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002539 mPendingAddInfo.copyFrom(info);
2540 mPendingAddWidgetId = widgetId;
2541
Sunny Goyalffe83f12014-08-14 17:39:34 -07002542 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2543 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002544 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002545 } else if (info.installProgress < 0) {
2546 // The install has not been queued
2547 final String packageName = info.providerName.getPackageName();
2548 showBrokenAppInstallDialog(packageName,
2549 new DialogInterface.OnClickListener() {
2550 public void onClick(DialogInterface dialog, int id) {
2551 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2552 }
2553 });
2554 } else {
2555 // Download has started.
2556 final String packageName = info.providerName.getPackageName();
2557 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002558 }
2559 }
2560
2561 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002562 * Event handler for the "grid" button that appears on the home screen, which
2563 * enters all apps mode.
2564 *
2565 * @param v The view that was clicked.
2566 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002567 protected void onClickAllAppsButton(View v) {
2568 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2569 if (isAllAppsVisible()) {
2570 showWorkspace(true);
2571 } else {
2572 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2573 }
Adam Cohen9211d422014-10-07 18:14:53 -07002574 if (mLauncherCallbacks != null) {
2575 mLauncherCallbacks.onClickAllAppsButton(v);
2576 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002577 }
2578
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002579 private void showBrokenAppInstallDialog(final String packageName,
2580 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002581 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002582 .setTitle(R.string.abandoned_promises_title)
2583 .setMessage(R.string.abandoned_promise_explanation)
2584 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2585 .setNeutralButton(R.string.abandoned_clean_this,
2586 new DialogInterface.OnClickListener() {
2587 public void onClick(DialogInterface dialog, int id) {
2588 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2589 mWorkspace.removeAbandonedPromise(packageName, user);
2590 }
2591 })
2592 .create().show();
2593 return;
2594 }
2595
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002596 /**
2597 * Event handler for an app shortcut click.
2598 *
2599 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2600 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002601 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002602 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2603 Object tag = v.getTag();
2604 if (!(tag instanceof ShortcutInfo)) {
2605 throw new IllegalArgumentException("Input must be a Shortcut");
2606 }
2607
2608 // Open shortcut
2609 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002610
2611 if (shortcut.isDisabled != 0) {
2612 int error = R.string.activity_not_available;
2613 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2614 error = R.string.safemode_shortcut_error;
2615 }
2616 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2617 return;
2618 }
2619
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002620 final Intent intent = shortcut.intent;
2621
2622 // Check for special shortcuts
2623 if (intent.getComponent() != null) {
2624 final String shortcutClass = intent.getComponent().getClassName();
2625
2626 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2627 MemoryDumpActivity.startDump(this);
2628 return;
2629 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2630 toggleShowWeightWatcher();
2631 return;
2632 }
2633 }
2634
Chris Wren40c5ed32014-06-24 18:24:23 -04002635 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002636 if ((v instanceof BubbleTextView)
2637 && shortcut.isPromise()
2638 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002639 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002640 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002641 new DialogInterface.OnClickListener() {
2642 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002643 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002644 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002645 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002646 return;
2647 }
2648
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002649 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002650 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002651
2652 if (mLauncherCallbacks != null) {
2653 mLauncherCallbacks.onClickAppShortcut(v);
2654 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002655 }
2656
Sunny Goyal508da152014-08-14 10:53:27 -07002657 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002658 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002659 final ShortcutInfo shortcut;
2660 final Intent intent;
2661 if (tag instanceof ShortcutInfo) {
2662 shortcut = (ShortcutInfo) tag;
2663 intent = shortcut.intent;
2664 int[] pos = new int[2];
2665 v.getLocationOnScreen(pos);
2666 intent.setSourceBounds(new Rect(pos[0], pos[1],
2667 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002668
Sunny Goyal508da152014-08-14 10:53:27 -07002669 } else if (tag instanceof AppInfo) {
2670 shortcut = null;
2671 intent = ((AppInfo) tag).intent;
2672 } else {
2673 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2674 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002675
2676 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002677 mStats.recordLaunch(intent, shortcut);
2678
2679 if (success && v instanceof BubbleTextView) {
2680 mWaitingForResume = (BubbleTextView) v;
2681 mWaitingForResume.setStayPressed(true);
2682 }
2683 }
2684
2685 /**
2686 * Event handler for a folder icon click.
2687 *
2688 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2689 */
2690 protected void onClickFolderIcon(View v) {
2691 if (LOGD) Log.d(TAG, "onClickFolder");
2692 if (!(v instanceof FolderIcon)){
2693 throw new IllegalArgumentException("Input must be a FolderIcon");
2694 }
2695
2696 FolderIcon folderIcon = (FolderIcon) v;
2697 final FolderInfo info = folderIcon.getFolderInfo();
2698 Folder openFolder = mWorkspace.getFolderForTag(info);
2699
2700 // If the folder info reports that the associated folder is open, then verify that
2701 // it is actually opened. There have been a few instances where this gets out of sync.
2702 if (info.opened && openFolder == null) {
2703 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2704 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2705 info.opened = false;
2706 }
2707
2708 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2709 // Close any open folder
2710 closeFolder();
2711 // Open the requested folder
2712 openFolder(folderIcon);
2713 } else {
2714 // Find the open folder...
2715 int folderScreen;
2716 if (openFolder != null) {
2717 folderScreen = mWorkspace.getPageForView(openFolder);
2718 // .. and close it
2719 closeFolder(openFolder);
2720 if (folderScreen != mWorkspace.getCurrentPage()) {
2721 // Close any folder open on the current screen
2722 closeFolder();
2723 // Pull the folder onto this screen
2724 openFolder(folderIcon);
2725 }
2726 }
2727 }
Adam Cohen9211d422014-10-07 18:14:53 -07002728
2729 if (mLauncherCallbacks != null) {
2730 mLauncherCallbacks.onClickFolderIcon(v);
2731 }
Michael Jurka5130e402011-10-13 04:55:35 -07002732 }
2733
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002734 /**
2735 * Event handler for the (Add) Widgets button that appears after a long press
2736 * on the home screen.
2737 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002738 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002739 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002740 if (mIsSafeModeEnabled) {
2741 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2742 } else {
2743 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2744 if (mLauncherCallbacks != null) {
2745 mLauncherCallbacks.onClickAddWidgetButton(view);
2746 }
Adam Cohen9211d422014-10-07 18:14:53 -07002747 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002748 }
2749
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002750 /**
2751 * Event handler for the wallpaper picker button that appears after a long press
2752 * on the home screen.
2753 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002754 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002755 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2756 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2757 pickWallpaper.setComponent(getWallpaperPickerComponent());
2758 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002759
2760 if (mLauncherCallbacks != null) {
2761 mLauncherCallbacks.onClickWallpaperPicker(v);
2762 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002763 }
2764
2765 /**
2766 * Event handler for a click on the settings button that appears after a long press
2767 * on the home screen.
2768 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002769 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002770 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002771 if (mLauncherCallbacks != null) {
2772 mLauncherCallbacks.onClickSettingsButton(v);
2773 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002774 }
2775
Michael Jurka5130e402011-10-13 04:55:35 -07002776 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002777 // Provide the same haptic feedback that the system offers for virtual keys.
2778 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002779 }
2780
Adam Cohen61f560d2013-09-30 15:58:20 -07002781 public void performHapticFeedbackOnTouchDown(View v) {
2782 // Provide the same haptic feedback that the system offers for virtual keys.
2783 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2784 }
2785
2786 public View.OnTouchListener getHapticFeedbackTouchListener() {
2787 if (mHapticFeedbackTouchListener == null) {
2788 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2789 @Override
2790 public boolean onTouch(View v, MotionEvent event) {
2791 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2792 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2793 }
2794 return false;
2795 }
2796 };
2797 }
2798 return mHapticFeedbackTouchListener;
2799 }
2800
Adam Cohen9211d422014-10-07 18:14:53 -07002801 public void onDragStarted(View view) {
2802 if (isOnCustomContent()) {
2803 // Custom content screen doesn't participate in drag and drop. If on custom
2804 // content screen, move to default.
2805 moveWorkspaceToDefaultScreen();
2806 }
2807
2808 if (mLauncherCallbacks != null) {
2809 mLauncherCallbacks.onDragStarted(view);
2810 }
2811 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002812
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002813 /**
2814 * Called when the user stops interacting with the launcher.
2815 * This implies that the user is now on the homescreen and is not doing housekeeping.
2816 */
Adam Cohen9211d422014-10-07 18:14:53 -07002817 protected void onInteractionEnd() {
2818 if (mLauncherCallbacks != null) {
2819 mLauncherCallbacks.onInteractionEnd();
2820 }
2821 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002822
2823 /**
2824 * Called when the user starts interacting with the launcher.
2825 * The possible interactions are:
2826 * - open all apps
2827 * - reorder an app shortcut, or a widget
2828 * - open the overview mode.
2829 * This is a good time to stop doing things that only make sense
2830 * when the user is on the homescreen and not doing housekeeping.
2831 */
Adam Cohen9211d422014-10-07 18:14:53 -07002832 protected void onInteractionBegin() {
2833 if (mLauncherCallbacks != null) {
2834 mLauncherCallbacks.onInteractionBegin();
2835 }
2836 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002837
Kenny Guyf07af7b2014-07-31 11:39:16 +01002838 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002839 try {
2840 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002841 launcherApps.showAppDetailsForProfile(componentName, user);
2842 } catch (SecurityException e) {
2843 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2844 Log.e(TAG, "Launcher does not have permission to launch settings");
2845 } catch (ActivityNotFoundException e) {
2846 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2847 Log.e(TAG, "Unable to launch settings");
2848 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002849 }
2850
Michael Jurka1e2f4652013-07-08 18:03:46 -07002851 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002852 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2853 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002854 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002855 // System applications cannot be installed. For now, show a toast explaining that.
2856 // We may give them the option of disabling apps this way.
2857 int messageId = R.string.uninstall_system_app_text;
2858 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002859 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002860 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002861 String packageName = componentName.getPackageName();
2862 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002863 Intent intent = new Intent(
2864 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002865 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2866 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002867 if (user != null) {
2868 user.addToIntent(intent, Intent.EXTRA_USER);
2869 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002870 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002871 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002872 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002873 }
2874
Michael Jurka86a720e2012-05-09 11:23:23 -07002875 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002876 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002878 // Only launch using the new animation if the shortcut has not opted out (this is a
2879 // private contract between launcher and may be ignored in the future).
2880 boolean useLaunchAnimation = (v != null) &&
2881 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002882 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2883 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002884
Kenny Guy1317e2d2014-05-08 18:52:50 +01002885 UserHandleCompat user = null;
2886 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2887 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2888 user = userManager.getUserForSerialNumber(serialNumber);
2889 }
2890
2891 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002892 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002893 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002894 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2895 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002896 optsBundle = opts.toBundle();
2897 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002898
2899 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2900 // Could be launching some bookkeeping activity
2901 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002902 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002903 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002904 launcherApps.startActivityForProfile(intent.getComponent(), user,
2905 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002906 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002907 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002908 } catch (SecurityException e) {
2909 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002910 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002911 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002912 "or use the exported attribute for this activity. "
2913 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002914 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002915 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002916 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002917
Michael Jurka86a720e2012-05-09 11:23:23 -07002918 boolean startActivitySafely(View v, Intent intent, Object tag) {
2919 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002920 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2921 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2922 return false;
2923 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002924 try {
2925 success = startActivity(v, intent, tag);
2926 } catch (ActivityNotFoundException e) {
2927 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2928 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2929 }
2930 return success;
2931 }
2932
Adam Cohen268c4752012-06-06 17:47:33 -07002933 /**
2934 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2935 * in the DragLayer in the exact absolute location of the original FolderIcon.
2936 */
2937 private void copyFolderIconToImage(FolderIcon fi) {
2938 final int width = fi.getMeasuredWidth();
2939 final int height = fi.getMeasuredHeight();
2940
2941 // Lazy load ImageView, Bitmap and Canvas
2942 if (mFolderIconImageView == null) {
2943 mFolderIconImageView = new ImageView(this);
2944 }
2945 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2946 mFolderIconBitmap.getHeight() != height) {
2947 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2948 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2949 }
2950
2951 DragLayer.LayoutParams lp;
2952 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2953 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2954 } else {
2955 lp = new DragLayer.LayoutParams(width, height);
2956 }
2957
Adam Cohen307fe232012-08-16 17:55:58 -07002958 // The layout from which the folder is being opened may be scaled, adjust the starting
2959 // view size by this scale factor.
2960 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002961 lp.customPosition = true;
2962 lp.x = mRectForFolderAnimation.left;
2963 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002964 lp.width = (int) (scale * width);
2965 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002966
2967 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2968 fi.draw(mFolderIconCanvas);
2969 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002970 if (fi.getFolder() != null) {
2971 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2972 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002973 }
Adam Cohen268c4752012-06-06 17:47:33 -07002974 // Just in case this image view is still in the drag layer from a previous animation,
2975 // we remove it and re-add it.
2976 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2977 mDragLayer.removeView(mFolderIconImageView);
2978 }
2979 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002980 if (fi.getFolder() != null) {
2981 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002982 }
Adam Cohen268c4752012-06-06 17:47:33 -07002983 }
2984
Adam Cohen2801caf2011-05-13 20:57:39 -07002985 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002986 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002987 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2988 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2989 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2990
Adam Cohenc51934b2011-07-26 21:07:43 -07002991 FolderInfo info = (FolderInfo) fi.getTag();
2992 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2993 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002994 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2995 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002996 }
2997
Adam Cohen268c4752012-06-06 17:47:33 -07002998 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2999 copyFolderIconToImage(fi);
3000 fi.setVisibility(View.INVISIBLE);
3001
Michael Jurka2ecf9952012-06-18 12:52:28 -07003002 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003003 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003004 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003005 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3006 }
3007 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003008 oa.start();
3009 }
3010
Adam Cohen268c4752012-06-06 17:47:33 -07003011 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003012 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003013 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3014 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3015 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3016
Adam Cohen268c4752012-06-06 17:47:33 -07003017 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003018
Adam Cohen268c4752012-06-06 17:47:33 -07003019 // We remove and re-draw the FolderIcon in-case it has changed
3020 mDragLayer.removeView(mFolderIconImageView);
3021 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003022 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003023 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003024 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003025 oa.addListener(new AnimatorListenerAdapter() {
3026 @Override
3027 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003028 if (cl != null) {
3029 cl.clearFolderLeaveBehind();
3030 // Remove the ImageView copy of the FolderIcon and make the original visible.
3031 mDragLayer.removeView(mFolderIconImageView);
3032 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003033 }
3034 }
3035 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003036 oa.start();
3037 }
3038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003039 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003040 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041 * is animated relative to the specified View. If the View is null, no animation
3042 * is played.
3043 *
3044 * @param folderInfo The FolderInfo describing the folder to open.
3045 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003046 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003047 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003048 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049
Adam Cohena9cf38f2011-05-02 15:36:58 -07003050 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003051
Adam Cohen4554ee12011-08-03 16:13:21 -07003052 // Just verify that the folder hasn't already been added to the DragLayer.
3053 // There was a one-off crash where the folder had a parent already.
3054 if (folder.getParent() == null) {
3055 mDragLayer.addView(folder);
3056 mDragController.addDropTarget((DropTarget) folder);
3057 } else {
3058 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3059 folder.getParent() + ").");
3060 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003061 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003062 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003063
3064 // Notify the accessibility manager that this folder "window" has appeared and occluded
3065 // the workspace items
3066 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3067 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003068 }
3069
3070 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003071 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003072 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003073 if (folder.isEditingName()) {
3074 folder.dismissEditingName();
3075 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003076 closeFolder(folder);
3077 }
3078 }
3079
3080 void closeFolder(Folder folder) {
3081 folder.getInfo().opened = false;
3082
3083 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3084 if (parent != null) {
3085 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3086 shrinkAndFadeInFolderIcon(fi);
3087 }
3088 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003089
3090 // Notify the accessibility manager that this folder "window" has disappeard and no
3091 // longer occludeds the workspace items
3092 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003093 }
3094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003095 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003096 if (!isDraggingEnabled()) return false;
3097 if (isWorkspaceLocked()) return false;
3098 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099
Adam Cohen1697b792013-09-17 19:08:21 -07003100 if (v instanceof Workspace) {
3101 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003102 if (mWorkspace.enterOverviewMode()) {
3103 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3104 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3105 return true;
3106 } else {
3107 return false;
3108 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003109 } else {
3110 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003111 }
Adam Cohen1697b792013-09-17 19:08:21 -07003112 }
3113
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003114 CellLayout.CellInfo longClickCellInfo = null;
3115 View itemUnderLongClick = null;
3116 if (v.getTag() instanceof ItemInfo) {
3117 ItemInfo info = (ItemInfo) v.getTag();
3118 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3119 itemUnderLongClick = longClickCellInfo.cell;
3120 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003121 }
3122
Winson Chung3d503fb2011-07-13 17:25:49 -07003123 // The hotseat touch handling does not go through Workspace, and we always allow long press
3124 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003125 final boolean inHotseat = isHotseatLayout(v);
3126 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003127 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003128 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003129 // User long pressed on empty space
3130 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3131 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003132 if (mWorkspace.isInOverviewMode()) {
3133 mWorkspace.startReordering(v);
3134 } else {
3135 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003136 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003138 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3139 mHotseat.getOrderInHotseat(
3140 longClickCellInfo.cellX,
3141 longClickCellInfo.cellY));
3142 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003144 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 }
3146 }
3147 }
3148 return true;
3149 }
3150
Winson Chung3d503fb2011-07-13 17:25:49 -07003151 boolean isHotseatLayout(View layout) {
3152 return mHotseat != null && layout != null &&
3153 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3154 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003155
Winson Chung3d503fb2011-07-13 17:25:49 -07003156 /**
3157 * Returns the CellLayout of the specified container at the specified screen.
3158 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003159 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003160 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3161 if (mHotseat != null) {
3162 return mHotseat.getLayout();
3163 } else {
3164 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003165 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003166 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003167 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003168 }
3169 }
3170
Daniel Sandler843e8602010-06-07 14:59:01 -04003171 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003172 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003173 }
3174
Craig Mautner360310b2012-10-26 15:13:08 -07003175 private void setWorkspaceBackground(boolean workspace) {
3176 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003177 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003178 }
3179
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003180 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003181 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3182 int curflags = getWindow().getAttributes().flags
3183 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3184 if (wpflags != curflags) {
3185 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3186 }
Craig Mautner360310b2012-10-26 15:13:08 -07003187 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003188 }
3189
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003190 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3191 if (v instanceof LauncherTransitionable) {
3192 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3193 }
3194 }
3195
Michael Jurkabed61d22012-02-14 22:51:29 -08003196 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3197 if (v instanceof LauncherTransitionable) {
3198 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3199 }
Winson Chung70442722012-02-10 15:43:22 -08003200
3201 // Update the workspace transition step as well
3202 dispatchOnLauncherTransitionStep(v, 0f);
3203 }
3204
3205 private void dispatchOnLauncherTransitionStep(View v, float t) {
3206 if (v instanceof LauncherTransitionable) {
3207 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3208 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003209 }
3210
3211 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3212 if (v instanceof LauncherTransitionable) {
3213 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3214 }
Winson Chung70442722012-02-10 15:43:22 -08003215
3216 // Update the workspace transition step as well
3217 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003218 }
3219
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003220 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003221 * Things to test when changing the following seven functions.
3222 * - Home from workspace
3223 * - from center screen
3224 * - from other screens
3225 * - Home from all apps
3226 * - from center screen
3227 * - from other screens
3228 * - Back from all apps
3229 * - from center screen
3230 * - from other screens
3231 * - Launch app from workspace and quit
3232 * - with back
3233 * - with home
3234 * - Launch app from all apps and quit
3235 * - with back
3236 * - with home
3237 * - Go to a screen that's not the default, then all
3238 * apps, and launch and app, and go back
3239 * - with back
3240 * -with home
3241 * - On workspace, long press power and go back
3242 * - with back
3243 * - with home
3244 * - On all apps, long press power and go back
3245 * - with back
3246 * - with home
3247 * - On workspace, power off
3248 * - On all apps, power off
3249 * - Launch an app and turn off the screen while in that app
3250 * - Go back with home key
3251 * - Go back with back key TODO: make this not go to workspace
3252 * - From all apps
3253 * - From workspace
3254 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3255 * - From all apps
3256 * - From the center workspace
3257 * - From another workspace
3258 */
3259
3260 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003261 * Zoom the camera out from the workspace to reveal 'toView'.
3262 * Assumes that the view to show is anchored at either the very top or very bottom
3263 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003264 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003265 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003266 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3267 showAppsCustomizeHelper(animated, springLoaded, contentType);
3268 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003269
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003270 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chungc58497e2013-09-03 17:48:37 -07003271 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3272 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003273 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003274 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003275 mStateAnimation.cancel();
3276 mStateAnimation = null;
3277 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003278
Kenny Guyd794a3f2014-09-16 15:17:58 +01003279 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003280
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003281 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003282
Adam Cohen6c5891a2014-07-09 23:53:15 -07003283 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003284 final int itemsAlphaStagger =
3285 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003286
Michael Jurkabed61d22012-02-14 22:51:29 -08003287 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003288 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003289
Adam Cohen2854d252014-08-27 16:04:07 -07003290 final ArrayList<View> layerViews = new ArrayList<View>();
3291
Adam Cohen6c5891a2014-07-09 23:53:15 -07003292 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3293 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003294 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003295 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003296 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003297 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3298 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003299 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3300 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003301
Adam Cohena38dc902014-09-07 17:48:55 +02003302 // If for some reason our views aren't initialized, don't animate
3303 boolean initialized = getAllAppsButton() != null;
3304
3305 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003306 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003307 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3308 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003309
Adam Cohen9bfdb762014-07-21 17:44:06 -07003310 final View page = content.getPageAt(content.getCurrentPage());
3311 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003312
Adam Cohen63f1ec02014-08-12 09:23:13 -07003313 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3314 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003315 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3316 } else {
3317 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3318 }
3319
Adam Cohen9bfdb762014-07-21 17:44:06 -07003320 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003321 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003322 revealView.setVisibility(View.VISIBLE);
3323 // We need to hide this view as the animation start will be posted.
3324 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003325
Adam Cohen9bfdb762014-07-21 17:44:06 -07003326 int width = revealView.getMeasuredWidth();
3327 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003328 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003329
Adam Cohen63f1ec02014-08-12 09:23:13 -07003330 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003331 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003332
Adam Cohen63f1ec02014-08-12 09:23:13 -07003333 // Get the y delta between the center of the page and the center of the all apps button
3334 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3335 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003336
Adam Cohen2854d252014-08-27 16:04:07 -07003337 float alpha = 0;
3338 float xDrift = 0;
3339 float yDrift = 0;
3340 if (material) {
3341 alpha = isWidgetTray ? 0.3f : 1f;
3342 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3343 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3344 } else {
3345 yDrift = 2 * height / 3;
3346 xDrift = 0;
3347 }
3348 final float initAlpha = alpha;
3349
Adam Cohen9bfdb762014-07-21 17:44:06 -07003350 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003351 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003352 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003353 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003354 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003355 PropertyValuesHolder panelDriftX =
3356 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003357
Adam Cohen2854d252014-08-27 16:04:07 -07003358 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3359 panelAlpha, panelDriftY, panelDriftX);
3360
Adam Cohen9bfdb762014-07-21 17:44:06 -07003361 panelAlphaAndDrift.setDuration(revealDuration);
3362 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003363
Adam Cohen9bfdb762014-07-21 17:44:06 -07003364 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003365
Adam Cohen4e243a22014-08-10 18:30:55 -07003366 if (page != null) {
3367 page.setVisibility(View.VISIBLE);
3368 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003369 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003370
Adam Cohen2854d252014-08-27 16:04:07 -07003371 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3372 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003373 pageDrift.setDuration(revealDuration);
3374 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003375 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003376 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003377
Adam Cohen63f1ec02014-08-12 09:23:13 -07003378 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003379 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003380 itemsAlpha.setDuration(revealDuration);
3381 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3382 itemsAlpha.setStartDelay(itemsAlphaStagger);
3383 mStateAnimation.play(itemsAlpha);
3384 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003385
Adam Cohen4e243a22014-08-10 18:30:55 -07003386 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3387 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003388 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003389 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003390 indicatorsAlpha.setDuration(revealDuration);
3391 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003392
Adam Cohen9bfdb762014-07-21 17:44:06 -07003393 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003394 final View allApps = getAllAppsButton();
3395 int allAppsButtonSize = LauncherAppState.getInstance().
3396 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3397 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003398 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003399 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003400 reveal.setDuration(revealDuration);
3401 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003402
3403 reveal.addListener(new AnimatorListenerAdapter() {
3404 public void onAnimationStart(Animator animation) {
3405 if (!isWidgetTray) {
3406 allApps.setVisibility(View.INVISIBLE);
3407 }
3408 }
3409 public void onAnimationEnd(Animator animation) {
3410 if (!isWidgetTray) {
3411 allApps.setVisibility(View.VISIBLE);
3412 }
3413 }
3414 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003415 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003416 }
Michael Jurka1899a362011-11-03 13:50:45 -07003417
Adam Cohen9bfdb762014-07-21 17:44:06 -07003418 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3419 @Override
3420 public void onAnimationEnd(Animator animation) {
3421 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3422 dispatchOnLauncherTransitionEnd(toView, animated, false);
3423
3424 revealView.setVisibility(View.INVISIBLE);
3425 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003426 if (page != null) {
3427 page.setLayerType(View.LAYER_TYPE_NONE, null);
3428 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003429 content.setPageBackgroundsVisible(true);
3430
3431 // Hide the search bar
3432 if (mSearchDropTargetBar != null) {
3433 mSearchDropTargetBar.hideSearchBar(false);
3434 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003435
3436 // This can hold unnecessary references to views.
3437 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003438 }
3439
Adam Cohen9bfdb762014-07-21 17:44:06 -07003440 });
3441
Adam Cohen6c5891a2014-07-09 23:53:15 -07003442 if (workspaceAnim != null) {
3443 mStateAnimation.play(workspaceAnim);
3444 }
Adam Cohen2854d252014-08-27 16:04:07 -07003445
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003446 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3447 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003448 final AnimatorSet stateAnimation = mStateAnimation;
3449 final Runnable startAnimRunnable = new Runnable() {
3450 public void run() {
3451 // Check that mStateAnimation hasn't changed while
3452 // we waited for a layout/draw pass
3453 if (mStateAnimation != stateAnimation)
3454 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003455 dispatchOnLauncherTransitionStart(fromView, animated, false);
3456 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003457
3458 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003459 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003460 for (int i = 0; i < layerViews.size(); i++) {
3461 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003462 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003463 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003464 }
Adam Cohen2854d252014-08-27 16:04:07 -07003465 }
3466 }
3467 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003468 }
3469 };
Adam Cohen2854d252014-08-27 16:04:07 -07003470 toView.bringToFront();
3471 toView.setVisibility(View.VISIBLE);
3472 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003473 } else {
3474 toView.setTranslationX(0.0f);
3475 toView.setTranslationY(0.0f);
3476 toView.setScaleX(1.0f);
3477 toView.setScaleY(1.0f);
3478 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003479 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003480
Daniel Sandlere4f98912013-06-25 15:13:26 -04003481 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003482 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003483 if (mSearchDropTargetBar != null) {
3484 mSearchDropTargetBar.hideSearchBar(false);
3485 }
Michael Jurkaabded662011-03-04 12:06:57 -08003486 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003487 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003488 dispatchOnLauncherTransitionStart(fromView, animated, false);
3489 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003490 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003491 dispatchOnLauncherTransitionStart(toView, animated, false);
3492 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003493 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003494 }
3495
3496 /**
3497 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003498 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003499 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003500 */
Adam Cohened307df2013-10-02 09:37:31 -07003501 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003502 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003503
Michael Jurkab3e22d92011-10-31 15:58:33 -07003504 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003505 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003506 mStateAnimation.cancel();
3507 mStateAnimation = null;
3508 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003509
Kenny Guyd794a3f2014-09-16 15:17:58 +01003510 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003511 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003512
Adam Cohen63f1ec02014-08-12 09:23:13 -07003513 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003514 final int itemsAlphaStagger =
3515 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003516
Winson Chungf0ea4d32011-06-06 14:27:16 -07003517 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003518 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003519 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003520 final ArrayList<View> layerViews = new ArrayList<View>();
3521
Adam Cohened307df2013-10-02 09:37:31 -07003522 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003523 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003524 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003525 } else if (toState == Workspace.State.SPRING_LOADED ||
3526 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003527 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003528 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003529 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003530
Adam Cohena38dc902014-09-07 17:48:55 +02003531 // If for some reason our views aren't initialized, don't animate
3532 boolean initialized = getAllAppsButton() != null;
3533
3534 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003535 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003536 if (workspaceAnim != null) {
3537 mStateAnimation.play(workspaceAnim);
3538 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003539
Adam Cohen9bfdb762014-07-21 17:44:06 -07003540 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3541 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003542
Adam Cohen9bfdb762014-07-21 17:44:06 -07003543 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003544
3545 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3546 int count = content.getChildCount();
3547 for (int i = 0; i < count; i++) {
3548 View child = content.getChildAt(i);
3549 if (child != page) {
3550 child.setVisibility(View.INVISIBLE);
3551 }
3552 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003553 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003554
Adam Cohen2854d252014-08-27 16:04:07 -07003555 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3556 // don't perform all these no-op animations. In particularly, this was causing
3557 // the all-apps button to pop in and out.
3558 if (fromView.getVisibility() == View.VISIBLE) {
3559 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3560 final boolean isWidgetTray =
3561 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003562
Adam Cohen2854d252014-08-27 16:04:07 -07003563 if (isWidgetTray) {
3564 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3565 } else {
3566 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003567 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003568
Adam Cohen2854d252014-08-27 16:04:07 -07003569 int width = revealView.getMeasuredWidth();
3570 int height = revealView.getMeasuredHeight();
3571 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3572
3573 // Hide the real page background, and swap in the fake one
3574 revealView.setVisibility(View.VISIBLE);
3575 content.setPageBackgroundsVisible(false);
3576
3577 final View allAppsButton = getAllAppsButton();
3578 revealView.setTranslationY(0);
3579 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3580 allAppsButton, null);
3581
3582 float xDrift = 0;
3583 float yDrift = 0;
3584 if (material) {
3585 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3586 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3587 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003588 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003589 xDrift = 0;
3590 }
3591
3592 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3593 TimeInterpolator decelerateInterpolator = material ?
3594 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003595 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003596
3597 // The vertical motion of the apps panel should be delayed by one frame
3598 // from the conceal animation in order to give the right feel. We correpsondingly
3599 // shorten the duration so that the slide and conceal end at the same time.
3600 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3601 0, yDrift);
3602 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3603 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3604 panelDriftY.setInterpolator(decelerateInterpolator);
3605 mStateAnimation.play(panelDriftY);
3606
3607 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3608 0, xDrift);
3609 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3610 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3611 panelDriftX.setInterpolator(decelerateInterpolator);
3612 mStateAnimation.play(panelDriftX);
3613
3614 if (isWidgetTray || !material) {
3615 float finalAlpha = material ? 0.4f : 0f;
3616 revealView.setAlpha(1f);
3617 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3618 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003619 panelAlpha.setDuration(material ? revealDuration : 150);
3620 panelAlpha.setInterpolator(decelerateInterpolator);
3621 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003622 mStateAnimation.play(panelAlpha);
3623 }
3624
3625 if (page != null) {
3626 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3627
3628 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3629 0, yDrift);
3630 page.setTranslationY(0);
3631 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3632 pageDrift.setInterpolator(decelerateInterpolator);
3633 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3634 mStateAnimation.play(pageDrift);
3635
3636 page.setAlpha(1f);
3637 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3638 itemsAlpha.setDuration(100);
3639 itemsAlpha.setInterpolator(decelerateInterpolator);
3640 mStateAnimation.play(itemsAlpha);
3641 }
3642
3643 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3644 pageIndicators.setAlpha(1f);
3645 ObjectAnimator indicatorsAlpha =
3646 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3647 indicatorsAlpha.setDuration(revealDuration);
3648 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3649 mStateAnimation.play(indicatorsAlpha);
3650
3651 width = revealView.getMeasuredWidth();
3652
3653 if (material) {
3654 if (!isWidgetTray) {
3655 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003656 }
Adam Cohen2854d252014-08-27 16:04:07 -07003657 int allAppsButtonSize = LauncherAppState.getInstance().
3658 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3659 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3660 Animator reveal =
3661 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3662 height / 2, revealRadius, finalRadius);
3663 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3664 reveal.setDuration(revealDuration);
3665 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003666
Adam Cohen2854d252014-08-27 16:04:07 -07003667 reveal.addListener(new AnimatorListenerAdapter() {
3668 public void onAnimationEnd(Animator animation) {
3669 revealView.setVisibility(View.INVISIBLE);
3670 if (!isWidgetTray) {
3671 allAppsButton.setVisibility(View.VISIBLE);
3672 }
3673 }
3674 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003675
Adam Cohen2854d252014-08-27 16:04:07 -07003676 mStateAnimation.play(reveal);
3677 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003678
Adam Cohen2854d252014-08-27 16:04:07 -07003679 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3680 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3681 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003682 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003683
3684 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003685 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003686 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003687 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003688 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3689 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003690 if (onCompleteRunnable != null) {
3691 onCompleteRunnable.run();
3692 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003693
3694 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003695 if (page != null) {
3696 page.setLayerType(View.LAYER_TYPE_NONE, null);
3697 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003698 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003699 // Unhide side pages
3700 int count = content.getChildCount();
3701 for (int i = 0; i < count; i++) {
3702 View child = content.getChildAt(i);
3703 child.setVisibility(View.VISIBLE);
3704 }
3705
3706 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003707 if (page != null) {
3708 page.setTranslationX(0);
3709 page.setTranslationY(0);
3710 page.setAlpha(1);
3711 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003712 content.setCurrentPage(content.getNextPage());
3713
Chet Haasebc2f0822012-10-26 17:59:53 -07003714 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003715
3716 // This can hold unnecessary references to views.
3717 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003718 }
3719 });
3720
Adam Cohen2854d252014-08-27 16:04:07 -07003721 final AnimatorSet stateAnimation = mStateAnimation;
3722 final Runnable startAnimRunnable = new Runnable() {
3723 public void run() {
3724 // Check that mStateAnimation hasn't changed while
3725 // we waited for a layout/draw pass
3726 if (mStateAnimation != stateAnimation)
3727 return;
3728 dispatchOnLauncherTransitionStart(fromView, animated, false);
3729 dispatchOnLauncherTransitionStart(toView, animated, false);
3730
Kenny Guyd794a3f2014-09-16 15:17:58 +01003731 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003732 for (int i = 0; i < layerViews.size(); i++) {
3733 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003734 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003735 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003736 }
Adam Cohen2854d252014-08-27 16:04:07 -07003737 }
3738 }
3739 mStateAnimation.start();
3740 }
3741 };
3742 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003743 } else {
3744 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003745 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003746 dispatchOnLauncherTransitionStart(fromView, animated, true);
3747 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003748 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003749 dispatchOnLauncherTransitionStart(toView, animated, true);
3750 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003751 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003752 }
3753
Michael Jurkae326f182011-11-21 14:05:46 -08003754 @Override
3755 public void onTrimMemory(int level) {
3756 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003757 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3758 // The widget preview db can result in holding onto over
3759 // 3MB of memory for caching which isn't necessary.
3760 SQLiteDatabase.releaseMemory();
3761
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003762 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003763 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003764 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003765 }
Michael Jurkae326f182011-11-21 14:05:46 -08003766 }
3767 }
3768
Adam Cohened307df2013-10-02 09:37:31 -07003769 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003770 showWorkspace(animated, null);
3771 }
3772
Adam Cohened307df2013-10-02 09:37:31 -07003773 protected void showWorkspace() {
3774 showWorkspace(true);
3775 }
3776
Adam Cohened66b2b2012-01-23 17:28:51 -08003777 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003778 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003779 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003780 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003781 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003782
Winson Chungc7d2b602012-05-16 17:02:20 -07003783 // Show the search bar (only animate if we were showing the drop target bar in spring
3784 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003785 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003786 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003787 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003788
Michael Jurkab3e22d92011-10-31 15:58:33 -07003789 // Set focus to the AppsCustomize button
3790 if (mAllAppsButton != null) {
3791 mAllAppsButton.requestFocus();
3792 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003793 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003794
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003795 // Change the state *after* we've called all the transition code
3796 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003797
Winson Chung5fb63472011-02-02 17:03:37 -08003798 // Resume the auto-advance of widgets
3799 mUserPresent = true;
3800 updateRunning();
3801
alanv1d4fde62012-10-17 13:15:47 -07003802 // Send an accessibility event to announce the context change
3803 getWindow().getDecorView()
3804 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003805
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003806 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003807 }
3808
Adam Cohened307df2013-10-02 09:37:31 -07003809 void showOverviewMode(boolean animated) {
3810 mWorkspace.setVisibility(View.VISIBLE);
3811 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3812 mState = State.WORKSPACE;
3813 onWorkspaceShown(animated);
3814 }
3815
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003816 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003817 }
3818
Winson Chung82963d52013-10-09 11:20:57 -07003819 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3820 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003821 if (mState != State.WORKSPACE) return;
3822
Winson Chung82963d52013-10-09 11:20:57 -07003823 if (resetPageToZero) {
3824 mAppsCustomizeTabHost.reset();
3825 }
Winson Chungc58497e2013-09-03 17:48:37 -07003826 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003827 mAppsCustomizeTabHost.post(new Runnable() {
3828 @Override
3829 public void run() {
3830 // We post this in-case the all apps view isn't yet constructed.
3831 mAppsCustomizeTabHost.requestFocus();
3832 }
3833 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003834
Michael Jurkab3e22d92011-10-31 15:58:33 -07003835 // Change the state *after* we've called all the transition code
3836 mState = State.APPS_CUSTOMIZE;
3837
3838 // Pause the auto-advance of widgets until we are out of AllApps
3839 mUserPresent = false;
3840 updateRunning();
3841 closeFolder();
3842
3843 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003844 getWindow().getDecorView()
3845 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003846 }
3847
Adam Cohen7777d962011-08-18 18:58:38 -07003848 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003849 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003850 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003851 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003852 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003853 }
Adam Cohen7777d962011-08-18 18:58:38 -07003854
Adam Cohenad4e15c2013-10-17 16:21:35 -07003855 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003856 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003857 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3858
Winson Chunge7a03942011-08-05 15:05:12 -07003859 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003860 @Override
3861 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003862 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003863 // Before we show workspace, hide all apps again because
3864 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3865 // clean up our state transition functions
3866 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003867 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003868 } else {
3869 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003870 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003871 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003872 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003873 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003874
Michael Jurkad3ef3062010-11-23 16:23:58 -08003875 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003876 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003877 final boolean animated = true;
3878 final boolean springLoaded = true;
3879 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003880 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003881 }
3882 // Otherwise, we are not in spring loaded mode, so don't do anything.
3883 }
3884
Michael Jurkab3e22d92011-10-31 15:58:33 -07003885 void lockAllApps() {
3886 // TODO
3887 }
3888
3889 void unlockAllApps() {
3890 // TODO
3891 }
3892
Sunny Goyal594d76d2014-11-06 10:12:54 -08003893 protected void disableVoiceButtonProxy(boolean disable) {
3894 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003895 }
3896
Cristina Stancu476493b2013-08-07 17:20:14 +01003897 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003898 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3899 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003900 }
3901
Adam Cohen24ce0b32014-01-14 16:18:14 -08003902 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003903 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3904 if (searchProvider == null) {
3905 return null;
3906 }
3907
3908 Bundle opts = new Bundle();
3909 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
3910 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
3911
3912 SharedPreferences sp = getSharedPreferences(
3913 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3914 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003915 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003916 if (!searchProvider.provider.flattenToString().equals(
3917 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003918 || (widgetInfo == null)
3919 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003920 // A valid widget is not already bound.
3921 if (widgetId > -1) {
3922 mAppWidgetHost.deleteAppWidgetId(widgetId);
3923 widgetId = -1;
3924 }
3925
3926 // Try to bind a new widget
3927 widgetId = mAppWidgetHost.allocateAppWidgetId();
3928
3929 if (!AppWidgetManagerCompat.getInstance(this)
3930 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3931 mAppWidgetHost.deleteAppWidgetId(widgetId);
3932 widgetId = -1;
3933 }
3934
3935 sp.edit()
3936 .putInt(QSB_WIDGET_ID, widgetId)
3937 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3938 .commit();
3939 }
3940
3941 if (widgetId != -1) {
3942 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3943 mQsb.updateAppWidgetOptions(opts);
3944 mQsb.setPadding(0, 0, 0, 0);
3945 mSearchDropTargetBar.addView(mQsb);
3946 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003947 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003948 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003949 }
3950
Michael Jurkad7c28052012-04-27 15:43:36 -07003951 @Override
3952 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003953 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003954 final List<CharSequence> text = event.getText();
3955 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003956 // Populate event with a fake title based on the current state.
3957 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07003958 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07003959 } else {
3960 text.add(getString(R.string.all_apps_home_button_label));
3961 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003962 return result;
3963 }
3964
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003965 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003966 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003967 */
3968 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3969 @Override
3970 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003971 closeSystemDialogs();
3972 }
3973 }
3974
3975 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003976 * Receives notifications whenever the appwidgets are reset.
3977 */
3978 private class AppWidgetResetObserver extends ContentObserver {
3979 public AppWidgetResetObserver() {
3980 super(new Handler());
3981 }
3982
3983 @Override
3984 public void onChange(boolean selfChange) {
3985 onAppWidgetReset();
3986 }
3987 }
3988
3989 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003990 * If the activity is currently paused, signal that we need to run the passed Runnable
3991 * in onResume.
3992 *
3993 * This needs to be called from incoming places where resources might have been loaded
3994 * while we are paused. That is becaues the Configuration might be wrong
3995 * when we're not running, and if it comes back to what it was when we
3996 * were paused, we are not restarted.
3997 *
3998 * Implementation of the method from LauncherModel.Callbacks.
3999 *
4000 * @return true if we are currently paused. The caller might be able to
4001 * skip some work in that case since we will come back again.
4002 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004003 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004004 if (mPaused) {
4005 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004006 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004007 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004008 }
4009 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004010 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004011 return true;
4012 } else {
4013 return false;
4014 }
4015 }
4016
Michael Jurkac402cd92013-05-20 15:49:32 +02004017 private boolean waitUntilResume(Runnable run) {
4018 return waitUntilResume(run, false);
4019 }
4020
Michael Jurka1e2f4652013-07-08 18:03:46 -07004021 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004022 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004023 }
4024
Michael Jurka7607c2f2013-04-03 14:33:19 -07004025 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004026 * If the activity is currently paused, signal that we need to re-run the loader
4027 * in onResume.
4028 *
4029 * This needs to be called from incoming places where resources might have been loaded
4030 * while we are paused. That is becaues the Configuration might be wrong
4031 * when we're not running, and if it comes back to what it was when we
4032 * were paused, we are not restarted.
4033 *
4034 * Implementation of the method from LauncherModel.Callbacks.
4035 *
4036 * @return true if we are currently paused. The caller might be able to
4037 * skip some work in that case since we will come back again.
4038 */
4039 public boolean setLoadOnResume() {
4040 if (mPaused) {
4041 Log.i(TAG, "setLoadOnResume");
4042 mOnResumeNeedsLoad = true;
4043 return true;
4044 } else {
4045 return false;
4046 }
4047 }
4048
4049 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004050 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004051 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004052 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004053 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004054 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004055 } else {
4056 return SCREEN_COUNT / 2;
4057 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004058 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004059
Joe Onorato9c1289c2009-08-17 11:03:03 -04004060 /**
4061 * Refreshes the shortcuts shown on the workspace.
4062 *
4063 * Implementation of the method from LauncherModel.Callbacks.
4064 */
4065 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004066 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004067
Michael Jurka7607c2f2013-04-03 14:33:19 -07004068 // If we're starting binding all over again, clear any bind calls we'd postponed in
4069 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4070 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004071 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004072
Winson Chungd64d1762013-08-20 14:37:16 -07004073 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004074 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004075 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004076
Michael Jurka05bf6442011-11-30 20:28:53 -08004077 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004078 if (mHotseat != null) {
4079 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004080 }
4081 }
4082
Adam Cohendcd297f2013-06-18 13:13:40 -07004083 @Override
4084 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004085 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004086
Adam Cohen5084cba2013-09-03 12:01:16 -07004087 // If there are no screens, we need to have an empty screen
4088 if (orderedScreenIds.size() == 0) {
4089 mWorkspace.addExtraEmptyScreen();
4090 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004091
4092 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004093 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004094 if (hasCustomContentToLeft()) {
4095 mWorkspace.createCustomContentContainer();
4096 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004097 }
Winson Chung64359a52013-07-08 17:17:08 -07004098 }
4099
4100 @Override
4101 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004102 // Log to disk
4103 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4104 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4105 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004106 int count = orderedScreenIds.size();
4107 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004108 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004109 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004110 }
4111
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08004112 @Override
4113 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
4114 final long screenId, final int[] cell, final int spanX, final int spanY) {
4115 showWorkspace(true, new Runnable() {
4116
4117 @Override
4118 public void run() {
4119 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
4120 addPendingItem(info, container, screenId, cell, spanX, spanY);
4121 }
4122 });
4123 }
4124
Adam Cohen39a06042013-07-19 14:30:12 -07004125 private boolean shouldShowWeightWatcher() {
4126 String spKey = LauncherAppState.getSharedPreferencesKey();
4127 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004128 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004129
4130 return show;
4131 }
4132
4133 private void toggleShowWeightWatcher() {
4134 String spKey = LauncherAppState.getSharedPreferencesKey();
4135 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4136 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4137
4138 show = !show;
4139
4140 SharedPreferences.Editor editor = sp.edit();
4141 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4142 editor.commit();
4143
4144 if (mWeightWatcher != null) {
4145 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4146 }
4147 }
4148
Winson Chungd64d1762013-08-20 14:37:16 -07004149 public void bindAppsAdded(final ArrayList<Long> newScreens,
4150 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004151 final ArrayList<ItemInfo> addAnimated,
4152 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004153 Runnable r = new Runnable() {
4154 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004155 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004156 }
4157 };
4158 if (waitUntilResume(r)) {
4159 return;
4160 }
4161
Winson Chungd64d1762013-08-20 14:37:16 -07004162 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004163 if (newScreens != null) {
4164 bindAddScreens(newScreens);
4165 }
Winson Chungd64d1762013-08-20 14:37:16 -07004166
4167 // We add the items without animation on non-visible pages, and with
4168 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004169 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004170 bindItems(addNotAnimated, 0,
4171 addNotAnimated.size(), false);
4172 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004173 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004174 bindItems(addAnimated, 0,
4175 addAnimated.size(), true);
4176 }
Winson Chungc58497e2013-09-03 17:48:37 -07004177
Winson Chung87412982013-10-03 18:34:14 -07004178 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004179 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004180
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004181 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004182 addedApps != null && mAppsCustomizeContent != null) {
4183 mAppsCustomizeContent.addApps(addedApps);
4184 }
Winson Chungd64d1762013-08-20 14:37:16 -07004185 }
4186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004187 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004188 * Bind the items start-end from the list.
4189 *
4190 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004191 */
Winson Chung64359a52013-07-08 17:17:08 -07004192 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4193 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004194 Runnable r = new Runnable() {
4195 public void run() {
4196 bindItems(shortcuts, start, end, forceAnimateIcons);
4197 }
4198 };
4199 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004200 return;
4201 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004202
Winson Chungf0c6ae02012-03-21 16:10:31 -07004203 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004204 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4205 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004206 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004207 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004208 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004209 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004210 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004211
4212 // Short circuit if we are loading dock items for a configuration which has no dock
4213 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4214 mHotseat == null) {
4215 continue;
4216 }
4217
Joe Onorato9c1289c2009-08-17 11:03:03 -04004218 switch (item.itemType) {
4219 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4220 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004221 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004222 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004223
Winson Chung64359a52013-07-08 17:17:08 -07004224 /*
4225 * TODO: FIX collision case
4226 */
Winson Chungce376632013-07-11 16:12:41 -07004227 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4228 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4229 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004230 View v = cl.getChildAt(item.cellX, item.cellY);
4231 Object tag = v.getTag();
4232 String desc = "Collision while binding workspace item: " + item
4233 + ". Collides with " + tag;
4234 if (LauncherAppState.isDogfoodBuild()) {
4235 throw (new RuntimeException(desc));
4236 } else {
4237 Log.d(TAG, desc);
4238 }
Winson Chungce376632013-07-11 16:12:41 -07004239 }
Winson Chung64359a52013-07-08 17:17:08 -07004240 }
4241
Adam Cohendcd297f2013-06-18 13:13:40 -07004242 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4243 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004244 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004245 // Animate all the applications up now
4246 shortcut.setAlpha(0f);
4247 shortcut.setScaleX(0f);
4248 shortcut.setScaleY(0f);
4249 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004250 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004251 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004252 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004253 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004254 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004255 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004256 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004257 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4258 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004259 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004260 default:
4261 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004263 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004264
Winson Chung997a9232013-07-24 15:33:46 -07004265 if (animateIcons) {
4266 // Animate to the correct page
4267 if (newShortcutsScreenId > -1) {
4268 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004269 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004270 final Runnable startBounceAnimRunnable = new Runnable() {
4271 public void run() {
4272 anim.playTogether(bounceAnims);
4273 anim.start();
4274 }
4275 };
Winson Chung997a9232013-07-24 15:33:46 -07004276 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004277 // We post the animation slightly delayed to prevent slowdowns
4278 // when we are loading right after we return to launcher.
4279 mWorkspace.postDelayed(new Runnable() {
4280 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004281 if (mWorkspace != null) {
4282 mWorkspace.snapToPage(newScreenIndex);
4283 mWorkspace.postDelayed(startBounceAnimRunnable,
4284 NEW_APPS_ANIMATION_DELAY);
4285 }
Winson Chung94d67682013-09-25 16:29:40 -07004286 }
4287 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004288 } else {
4289 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004290 }
4291 }
Winson Chung64359a52013-07-08 17:17:08 -07004292 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004293 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004294 }
4295
Joe Onorato9c1289c2009-08-17 11:03:03 -04004296 /**
4297 * Implementation of the method from LauncherModel.Callbacks.
4298 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004299 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004300 Runnable r = new Runnable() {
4301 public void run() {
4302 bindFolders(folders);
4303 }
4304 };
4305 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004306 return;
4307 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004308 sFolders.clear();
4309 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004310 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004311
4312 /**
4313 * Add the views for a widget to the workspace.
4314 *
4315 * Implementation of the method from LauncherModel.Callbacks.
4316 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004317 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004318 Runnable r = new Runnable() {
4319 public void run() {
4320 bindAppWidget(item);
4321 }
4322 };
4323 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004324 return;
4325 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004326
Daniel Sandler843e8602010-06-07 14:59:01 -04004327 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4328 if (DEBUG_WIDGETS) {
4329 Log.d(TAG, "bindAppWidget: " + item);
4330 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004331 final Workspace workspace = mWorkspace;
4332
Adam Cohen59400422014-03-05 18:07:04 -08004333 LauncherAppWidgetProviderInfo appWidgetInfo =
4334 LauncherModel.getProviderInfo(this, item.providerName);
4335
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004336 if (!mIsSafeModeEnabled
4337 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4338 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004339 if (appWidgetInfo == null) {
4340 if (DEBUG_WIDGETS) {
4341 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4342 + " belongs to component " + item.providerName
4343 + ", as the povider is null");
4344 }
4345 LauncherModel.deleteItemFromDatabase(this, item);
4346 return;
4347 }
4348 // Note: This assumes that the id remap broadcast is received before this step.
4349 // If that is not the case, the id remap will be ignored and user may see the
4350 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004351 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004352 pendingInfo.spanX = item.spanX;
4353 pendingInfo.spanY = item.spanY;
4354 pendingInfo.minSpanX = item.minSpanX;
4355 pendingInfo.minSpanY = item.minSpanY;
4356 Bundle options =
4357 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4358
Sunny Goyalff572272014-07-23 13:58:07 -07004359 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004360 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4361 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004362
4363 // TODO consider showing a permission dialog when the widget is clicked.
4364 if (!success) {
4365 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4366 if (DEBUG_WIDGETS) {
4367 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4368 + " belongs to component " + item.providerName
4369 + ", as the launcher is unable to bing a new widget id");
4370 }
4371 LauncherModel.deleteItemFromDatabase(this, item);
4372 return;
4373 }
4374
4375 item.appWidgetId = newWidgetId;
4376
4377 // If the widget has a configure activity, it is still needs to set it up, otherwise
4378 // the widget is ready to go.
4379 item.restoreStatus = (appWidgetInfo.configure == null)
4380 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4381 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4382
4383 LauncherModel.updateItemInDatabase(this, item);
4384 }
4385
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004386 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004387 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004388 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004389 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4390 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004391 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004392
Sunny Goyal651077b2014-06-30 14:15:31 -07004393 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4394 } else {
4395 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004396 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4397 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004398 view.updateIcon(mIconCache);
4399 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004400 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004401 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004402 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004403
Joe Onorato9c1289c2009-08-17 11:03:03 -04004404 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004405 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004406
Adam Cohendcd297f2013-06-18 13:13:40 -07004407 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004408 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004409 if (!item.isCustomWidget()) {
4410 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4411 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004412
Joe Onorato9c1289c2009-08-17 11:03:03 -04004413 workspace.requestLayout();
4414
Daniel Sandler843e8602010-06-07 14:59:01 -04004415 if (DEBUG_WIDGETS) {
4416 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4417 + (SystemClock.uptimeMillis()-start) + "ms");
4418 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004419 }
4420
Sunny Goyalff572272014-07-23 13:58:07 -07004421 /**
4422 * Restores a pending widget.
4423 *
4424 * @param appWidgetId The app widget id
4425 * @param cellInfo The position on screen where to create the widget.
4426 */
4427 private void completeRestoreAppWidget(final int appWidgetId) {
4428 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4429 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4430 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4431 return;
4432 }
4433
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004434 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004435 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4436
4437 mWorkspace.reinflateWidgetsIfNecessary();
4438 LauncherModel.updateItemInDatabase(this, info);
4439 }
4440
Adam Cohen1462de32012-07-24 22:34:36 -07004441 public void onPageBoundSynchronously(int page) {
4442 mSynchronouslyBoundPages.add(page);
4443 }
4444
Joe Onorato9c1289c2009-08-17 11:03:03 -04004445 /**
4446 * Callback saying that there aren't any more items to bind.
4447 *
4448 * Implementation of the method from LauncherModel.Callbacks.
4449 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004450 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004451 Runnable r = new Runnable() {
4452 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004453 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004454 }
4455 };
4456 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004457 return;
4458 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004459 if (mSavedState != null) {
4460 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004461 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004462 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004463 mSavedState = null;
4464 }
4465
Adam Cohen1462de32012-07-24 22:34:36 -07004466 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004467
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004468 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004469 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004470
4471 // If we received the result of any pending adds while the loader was running (e.g. the
4472 // widget configuration forced an orientation change), process them now.
4473 if (sPendingAddItem != null) {
4474 final long screenId = completeAdd(sPendingAddItem);
4475
4476 // TODO: this moves the user to the page where the pending item was added. Ideally,
4477 // the screen would be guaranteed to exist after bind, and the page would be set through
4478 // the workspace restore process.
4479 mWorkspace.post(new Runnable() {
4480 @Override
4481 public void run() {
4482 mWorkspace.snapToScreenId(screenId);
4483 }
4484 });
4485 sPendingAddItem = null;
4486 }
4487
Sunny Goyale755d462014-07-22 13:48:29 -07004488 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004489
4490 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004491 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004492 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004493 }
4494
Adam Cohen3ed316a2014-07-23 18:21:20 -07004495 private void sendLoadingCompleteBroadcastIfNecessary() {
4496 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4497 String permission =
4498 getResources().getString(R.string.receive_first_load_broadcast_permission);
4499 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4500 sendBroadcast(intent, permission);
4501 SharedPreferences.Editor editor = mSharedPrefs.edit();
4502 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4503 editor.apply();
4504 }
4505 }
4506
Winson Chunga0b7e862013-09-05 16:03:15 -07004507 public boolean isAllAppsButtonRank(int rank) {
4508 if (mHotseat != null) {
4509 return mHotseat.isAllAppsButtonRank(rank);
4510 }
4511 return false;
4512 }
4513
Winson Chunga2413752012-04-03 14:22:34 -07004514 private boolean canRunNewAppsAnimation() {
4515 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4516 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4517 }
4518
Winson Chungc9168342013-06-26 14:54:55 -07004519 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4520 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4521 PropertyValuesHolder.ofFloat("alpha", 1f),
4522 PropertyValuesHolder.ofFloat("scaleX", 1f),
4523 PropertyValuesHolder.ofFloat("scaleY", 1f));
4524 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4525 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4526 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4527 return bounceAnim;
4528 }
4529
Adam Cohen27772732013-11-11 14:00:56 +00004530 public boolean useVerticalBarLayout() {
4531 return LauncherAppState.getInstance().getDynamicGrid().
4532 getDeviceProfile().isVerticalBarLayout();
4533 }
4534
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004535 protected Rect getSearchBarBounds() {
4536 return LauncherAppState.getInstance().getDynamicGrid().
4537 getDeviceProfile().getSearchBarBounds();
4538 }
4539
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004540 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004541 if (mSearchDropTargetBar == null) {
4542 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004543 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004544 if (mQsb != null) {
4545 mSearchDropTargetBar.removeView(mQsb);
4546 mQsb = null;
4547 }
4548 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004549 }
4550
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004551 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004552 * Add the icons for all apps.
4553 *
4554 * Implementation of the method from LauncherModel.Callbacks.
4555 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004556 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004557 if (LauncherAppState.isDisableAllApps()) {
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004558 if (mAppsCustomizeContent != null) {
4559 mAppsCustomizeContent.onPackagesUpdated(
4560 LauncherModel.getSortedWidgetsAndShortcuts(this));
4561 }
Winson Chungc58497e2013-09-03 17:48:37 -07004562 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004563 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004564 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004565 mAppsCustomizeContent.onPackagesUpdated(
4566 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004567 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004568 }
Adam Cohen9211d422014-10-07 18:14:53 -07004569 if (mLauncherCallbacks != null) {
4570 mLauncherCallbacks.bindAllApplications(apps);
4571 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004572 }
4573
4574 /**
4575 * A package was updated.
4576 *
4577 * Implementation of the method from LauncherModel.Callbacks.
4578 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004579 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004580 Runnable r = new Runnable() {
4581 public void run() {
4582 bindAppsUpdated(apps);
4583 }
4584 };
4585 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004586 return;
4587 }
4588
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004589 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004590 mAppsCustomizeContent != null) {
4591 mAppsCustomizeContent.updateApps(apps);
4592 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004593 }
4594
Sunny Goyal4390ace2014-10-13 11:33:11 -07004595 @Override
4596 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4597 Runnable r = new Runnable() {
4598 public void run() {
4599 bindWidgetsRestored(widgets);
4600 }
4601 };
4602 if (waitUntilResume(r)) {
4603 return;
4604 }
4605 mWorkspace.widgetsRestored(widgets);
4606 }
4607
Joe Onorato9c1289c2009-08-17 11:03:03 -04004608 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004609 * Some shortcuts were updated in the background.
4610 *
4611 * Implementation of the method from LauncherModel.Callbacks.
4612 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004613 @Override
4614 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4615 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004616 Runnable r = new Runnable() {
4617 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004618 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004619 }
4620 };
4621 if (waitUntilResume(r)) {
4622 return;
4623 }
4624
Sunny Goyal4390ace2014-10-13 11:33:11 -07004625 if (!updated.isEmpty()) {
4626 mWorkspace.updateShortcuts(updated);
4627 }
4628
4629 if (!removed.isEmpty()) {
4630 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4631 for (ShortcutInfo si : removed) {
4632 removedComponents.add(si.getTargetComponent());
4633 }
4634 mWorkspace.removeItemsByComponentName(removedComponents, user);
4635 // Notify the drag controller
4636 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004637 }
4638 }
4639
4640 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004641 * Update the state of a package, typically related to install state.
4642 *
4643 * Implementation of the method from LauncherModel.Callbacks.
4644 */
Sunny Goyale755d462014-07-22 13:48:29 -07004645 @Override
4646 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004647 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004648 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004649 }
4650 }
4651
4652 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004653 * Update the label and icon of all the icons in a package
4654 *
4655 * Implementation of the method from LauncherModel.Callbacks.
4656 */
4657 @Override
4658 public void updatePackageBadge(String packageName) {
4659 if (mWorkspace != null) {
4660 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4661 }
4662 }
4663
4664 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004665 * A package was uninstalled. We take both the super set of packageNames
4666 * in addition to specific applications to remove, the reason being that
4667 * this can be called when a package is updated as well. In that scenario,
4668 * we only remove specific components from the workspace, where as
4669 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004670 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004671 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004672 * Implementation of the method from LauncherModel.Callbacks.
4673 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004674 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004675 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004676 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004677 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004678 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004679 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004680 }
Winson Chungd64d1762013-08-20 14:37:16 -07004681 };
4682 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004683 return;
4684 }
4685
Sunny Goyal1a745e82014-10-02 15:58:31 -07004686 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004687 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4688 for (AppInfo info : appInfos) {
4689 removedComponents.add(info.componentName);
4690 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004691 if (!packageNames.isEmpty()) {
4692 mWorkspace.removeItemsByPackageName(packageNames, user);
4693 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004694 if (!removedComponents.isEmpty()) {
4695 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004696 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004697 // Notify the drag controller
4698 mDragController.onAppsRemoved(packageNames, removedComponents);
4699
Sunny Goyal1a745e82014-10-02 15:58:31 -07004700 } else {
4701 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004702 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004703
Winson Chungdf95eb12013-10-16 14:57:07 -07004704 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004705 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004706 mAppsCustomizeContent != null) {
4707 mAppsCustomizeContent.removeApps(appInfos);
4708 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004709 }
Joe Onoratobe386092009-11-17 17:32:16 -08004710
4711 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004712 * A number of packages were updated.
4713 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004714 private ArrayList<Object> mWidgetsAndShortcuts;
4715 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004716 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004717 bindPackagesUpdated(mWidgetsAndShortcuts);
4718 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004719 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004720 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004721 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4722 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4723 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004724 return;
4725 }
4726
Winson Chung64359a52013-07-08 17:17:08 -07004727 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004728 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004729 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004730 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004731 }
4732
Winson Chung400438b2011-01-16 17:53:48 -08004733 private int mapConfigurationOriActivityInfoOri(int configOri) {
4734 final Display d = getWindowManager().getDefaultDisplay();
4735 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4736 switch (d.getRotation()) {
4737 case Surface.ROTATION_0:
4738 case Surface.ROTATION_180:
4739 // We are currently in the same basic orientation as the natural orientation
4740 naturalOri = configOri;
4741 break;
4742 case Surface.ROTATION_90:
4743 case Surface.ROTATION_270:
4744 // We are currently in the other basic orientation to the natural orientation
4745 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4746 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4747 break;
4748 }
4749
4750 int[] oriMap = {
4751 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4752 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4753 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4754 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4755 };
4756 // Since the map starts at portrait, we need to offset if this device's natural orientation
4757 // is landscape.
4758 int indexOffset = 0;
4759 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4760 indexOffset = 1;
4761 }
4762 return oriMap[(d.getRotation() + indexOffset) % 4];
4763 }
Adam Cohen446e9402011-09-15 18:21:21 -07004764
Winson Chung4b919f82012-05-01 10:44:08 -07004765 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004766 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004767 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4768 .getConfiguration().orientation));
4769 }
4770 }
4771 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004772 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004773 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004774 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004775 } else {
4776 mHandler.postDelayed(new Runnable() {
4777 public void run() {
4778 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4779 }
4780 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004781 }
Winson Chung4b919f82012-05-01 10:44:08 -07004782 }
Winson Chung400438b2011-01-16 17:53:48 -08004783 }
4784
Winson Chunge43a1e72014-01-15 10:33:02 -08004785 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004786 if (mLauncherCallbacks != null) {
4787 return mLauncherCallbacks.isLauncherPreinstalled();
4788 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004789 PackageManager pm = getPackageManager();
4790 try {
4791 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4792 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4793 return true;
4794 } else {
4795 return false;
4796 }
4797 } catch (NameNotFoundException e) {
4798 e.printStackTrace();
4799 return false;
4800 }
4801 }
4802
Adam Cohenea90f832014-05-21 15:03:34 -07004803 /**
4804 * This method indicates whether or not we should suggest default wallpaper dimensions
4805 * when our wallpaper cropper was not yet used to set a wallpaper.
4806 */
4807 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004808 if (mLauncherCallbacks != null) {
4809 return mLauncherCallbacks.overrideWallpaperDimensions();
4810 }
Adam Cohenea90f832014-05-21 15:03:34 -07004811 return true;
4812 }
4813
Adam Cohen432609a2014-03-13 17:03:22 -07004814 /**
4815 * To be overridden by subclasses to indicate that there is an activity to launch
4816 * before showing the standard launcher experience.
4817 */
4818 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004819 if (mLauncherCallbacks != null) {
4820 return mLauncherCallbacks.hasFirstRunActivity();
4821 }
Adam Cohen432609a2014-03-13 17:03:22 -07004822 return false;
4823 }
4824
4825 /**
4826 * To be overridden by subclasses to launch any first run activity
4827 */
4828 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004829 if (mLauncherCallbacks != null) {
4830 return mLauncherCallbacks.getFirstRunActivity();
4831 }
Adam Cohen432609a2014-03-13 17:03:22 -07004832 return null;
4833 }
4834
Winson Chunga6945242014-01-08 14:04:34 -08004835 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004836 return !ActivityManager.isRunningInTestHarness() &&
4837 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004838 }
4839
Adam Cohen4a9423d2014-03-28 14:22:31 -07004840 protected boolean hasRunFirstRunActivity() {
4841 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4842 }
4843
Adam Cohen432609a2014-03-13 17:03:22 -07004844 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004845 if (shouldRunFirstRunActivity() &&
4846 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004847 Intent firstRunIntent = getFirstRunActivity();
4848 if (firstRunIntent != null) {
4849 startActivity(firstRunIntent);
4850 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004851 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004852 }
4853 }
Adam Cohen432609a2014-03-13 17:03:22 -07004854 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004855 }
4856
4857 private void markFirstRunActivityShown() {
4858 SharedPreferences.Editor editor = mSharedPrefs.edit();
4859 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4860 editor.apply();
4861 }
4862
Adam Cohen432609a2014-03-13 17:03:22 -07004863 /**
4864 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4865 * screen that must be displayed and dismissed.
4866 */
4867 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004868 if (mLauncherCallbacks != null) {
4869 return mLauncherCallbacks.hasDismissableIntroScreen();
4870 }
Adam Cohen432609a2014-03-13 17:03:22 -07004871 return false;
4872 }
4873
4874 /**
4875 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4876 */
4877 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004878 if (mLauncherCallbacks != null) {
4879 return mLauncherCallbacks.getIntroScreen();
4880 }
Adam Cohen432609a2014-03-13 17:03:22 -07004881 return null;
4882 }
4883
4884 /**
4885 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4886 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4887 */
4888 private boolean shouldShowIntroScreen() {
4889 return hasDismissableIntroScreen() &&
4890 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4891 }
4892
4893 protected void showIntroScreen() {
4894 View introScreen = getIntroScreen();
4895 changeWallpaperVisiblity(false);
4896 if (introScreen != null) {
4897 mDragLayer.showOverlayView(introScreen);
4898 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004899 if (mLauncherOverlayContainer != null) {
4900 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4901 }
Adam Cohen432609a2014-03-13 17:03:22 -07004902 }
4903
4904 public void dismissIntroScreen() {
4905 markIntroScreenDismissed();
4906 if (showFirstRunActivity()) {
4907 // We delay hiding the intro view until the first run activity is showing. This
4908 // avoids a blip.
4909 mWorkspace.postDelayed(new Runnable() {
4910 @Override
4911 public void run() {
4912 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004913 if (mLauncherOverlayContainer != null) {
4914 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4915 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004916 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004917 }
4918 }, ACTIVITY_START_DELAY);
4919 } else {
4920 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004921 if (mLauncherOverlayContainer != null) {
4922 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4923 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004924 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004925 }
4926 changeWallpaperVisiblity(true);
4927 }
4928
4929 private void markIntroScreenDismissed() {
4930 SharedPreferences.Editor editor = mSharedPrefs.edit();
4931 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4932 editor.apply();
4933 }
4934
Sunny Goyal88d60f12014-09-08 03:47:29 -07004935 private void showFirstRunClings() {
4936 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4937 // on the device, then we always show the first run cling experience (or if there is no
4938 // launcher2). Otherwise, we prompt the user upon started for migration
4939 LauncherClings launcherClings = new LauncherClings(this);
4940 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4941 if (mModel.canMigrateFromOldLauncherDb(this)) {
4942 launcherClings.showMigrationCling();
4943 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004944 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004945 }
4946 }
4947 }
4948
Winson Chunga6945242014-01-08 14:04:34 -08004949 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004950 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4951 if (mHotseat != null) mHotseat.setAlpha(1f);
4952 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4953 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004954 }
4955
4956 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004957 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4958 if (mHotseat != null) mHotseat.setAlpha(0f);
4959 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4960 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004961 }
4962
Mathew Inwood72fbec12013-11-19 15:45:07 +00004963 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004964 // Called from search suggestion, not supported in other profiles.
4965 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4966 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4967 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4968 myUser);
4969 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004970 return null;
4971 }
Kenny Guyed131872014-04-30 03:02:21 +01004972 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004973 }
4974
4975 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4976 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004977 // Called from search suggestion, not supported in other profiles.
4978 return createShortcutDragInfo(shortcutIntent, caption, icon,
4979 UserHandleCompat.myUserHandle());
4980 }
4981
4982 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4983 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004984 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004985 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004986 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004987 }
4988
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004989 protected void moveWorkspaceToDefaultScreen() {
4990 mWorkspace.moveToDefaultScreen(false);
4991 }
4992
Mathew Inwood72fbec12013-11-19 15:45:07 +00004993 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4994 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004995 mWorkspace.onExternalDragStartedWithItem(dragView);
4996 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004997 }
4998
Anjali Koppalf5d29132014-02-28 13:33:27 -08004999 @Override
5000 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005001 if (mLauncherCallbacks != null) {
5002 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5003 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005004 }
5005
Winson Chung80baf5a2010-08-09 16:03:15 -07005006 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005007 * Prints out out state for debugging.
5008 */
5009 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005010 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005011 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005012 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5013 Log.d(TAG, "mRestoring=" + mRestoring);
5014 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5015 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005016 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005017 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005018
Winson Chung785d2eb2011-04-14 16:08:02 -07005019 if (mAppsCustomizeContent != null) {
5020 mAppsCustomizeContent.dumpState();
5021 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005022 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005023 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005024
5025 @Override
5026 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5027 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005028 synchronized (sDumpLogs) {
5029 writer.println(" ");
5030 writer.println("Debug logs: ");
5031 for (int i = 0; i < sDumpLogs.size(); i++) {
5032 writer.println(" " + sDumpLogs.get(i));
5033 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005034 }
Adam Cohen9211d422014-10-07 18:14:53 -07005035 if (mLauncherCallbacks != null) {
5036 mLauncherCallbacks.dump(prefix, fd, writer, args);
5037 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005038 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005039
5040 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005041 if (DEBUG_DUMP_LOG) {
5042 synchronized (sDumpLogs) {
5043 Log.d(TAG, "");
5044 Log.d(TAG, "*********************");
5045 Log.d(TAG, "Launcher debug logs: ");
5046 for (int i = 0; i < sDumpLogs.size(); i++) {
5047 Log.d(TAG, " " + sDumpLogs.get(i));
5048 }
5049 Log.d(TAG, "*********************");
5050 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005051 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005052 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005053 }
5054
5055 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005056 addDumpLog(tag, log, null, debugLog);
5057 }
5058
5059 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005060 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005061 if (e != null) {
5062 Log.d(tag, log, e);
5063 } else {
5064 Log.d(tag, log);
5065 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005066 }
Winson Chungede41292013-09-19 16:27:36 -07005067 if (DEBUG_DUMP_LOG) {
5068 sDateStamp.setTime(System.currentTimeMillis());
5069 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005070 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5071 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005072 }
Winson Chungede41292013-09-19 16:27:36 -07005073 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005074 }
5075
Adam Cohen59400422014-03-05 18:07:04 -08005076 public static CustomAppWidget getCustomAppWidget(String name) {
5077 return sCustomAppWidgets.get(name);
5078 }
5079
5080 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
5081 return sCustomAppWidgets;
5082 }
5083
Winson Chungede41292013-09-19 16:27:36 -07005084 public void dumpLogsToLocalData() {
5085 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005086 new AsyncTask<Void, Void, Void>() {
5087 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005088 boolean success = false;
5089 sDateStamp.setTime(sRunStart);
5090 String FILENAME = sDateStamp.getMonth() + "-"
5091 + sDateStamp.getDay() + "_"
5092 + sDateStamp.getHours() + "-"
5093 + sDateStamp.getMinutes() + "_"
5094 + sDateStamp.getSeconds() + ".txt";
5095
5096 FileOutputStream fos = null;
5097 File outFile = null;
5098 try {
5099 outFile = new File(getFilesDir(), FILENAME);
5100 outFile.createNewFile();
5101 fos = new FileOutputStream(outFile);
5102 } catch (Exception e) {
5103 e.printStackTrace();
5104 }
5105 if (fos != null) {
5106 PrintWriter writer = new PrintWriter(fos);
5107
5108 writer.println(" ");
5109 writer.println("Debug logs: ");
5110 synchronized (sDumpLogs) {
5111 for (int i = 0; i < sDumpLogs.size(); i++) {
5112 writer.println(" " + sDumpLogs.get(i));
5113 }
5114 }
5115 writer.close();
5116 }
5117 try {
5118 if (fos != null) {
5119 fos.close();
5120 success = true;
5121 }
5122 } catch (IOException e) {
5123 e.printStackTrace();
5124 }
Michael Jurka43467462013-11-14 12:03:58 +01005125 return null;
Winson Chungede41292013-09-19 16:27:36 -07005126 }
Michael Jurka43467462013-11-14 12:03:58 +01005127 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005128 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005130}
Michael Jurka2763be32011-02-24 11:19:57 -08005131
Michael Jurkaabded662011-03-04 12:06:57 -08005132interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005133 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005134 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005135 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005136 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005137 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005138}
Dan Sandlerd5024042014-01-09 15:01:33 -05005139
5140interface DebugIntents {
5141 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5142 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005143}