blob: 5d8e136cd6409b7fe7ab51d587d42a40afb0bcf7 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Adam Cohen2854d252014-08-27 16:04:07 -070025import android.animation.TimeInterpolator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000027import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070029import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070053import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.Bitmap;
55import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070056import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen2854d252014-08-27 16:04:07 -070085import android.view.ViewAnimationUtils;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070087import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070089import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080091import android.view.accessibility.AccessibilityEvent;
Adam Cohen9bfdb762014-07-21 17:44:06 -070092import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Advanceable;
96import android.widget.FrameLayout;
97import android.widget.ImageView;
Adam Cohen96d30a12013-07-16 18:13:21 -070098import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070099
Sunny Goyal651077b2014-06-30 14:15:31 -0700100import com.android.launcher3.DropTarget.DragObject;
101import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherActivityInfoCompat;
104import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700105import com.android.launcher3.compat.PackageInstallerCompat;
106import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100107import com.android.launcher3.compat.UserHandleCompat;
108import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700109
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.io.DataInputStream;
111import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700113import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000118import java.lang.reflect.Field;
119import java.lang.reflect.InvocationTargetException;
120import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700121import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700122import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700123import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700124import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700125import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700126import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700127import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000128import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130/**
131 * Default launcher application.
132 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100133public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700134 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700135 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800136 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700137 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400140 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700141 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400142 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700143 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700144
Dan Sandlerd5024042014-01-09 15:01:33 -0500145 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700151 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Michael Jurka8b805b12012-04-18 14:23:14 -0700153 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700154 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700155
Mathew Inwood876a8462013-06-14 14:12:41 +0100156 /**
157 * IntentStarter uses request codes starting with this. This must be greater than all activity
158 * request codes used internally.
159 */
160 protected static final int REQUEST_LAST = 100;
161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
163
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800164 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
Michael Jurka0a457bf2012-11-19 14:05:05 -0800166 // To turn on these properties, type
167 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800169 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Winson Chung2672ff92012-05-04 16:22:30 -0700171 // The Intent extra that defines whether to ignore the launch animation
172 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400173 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
176 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700177 // Type: int
178 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700180 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
181 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
183 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700184 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700186 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: boolean
188 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
189 // Type: long
190 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700191 // Type: int
192 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
193 // Type: int
194 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
195 // Type: parcelable
196 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000197 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800198 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000199 // Type: int[]
200 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
Adam Cohen432609a2014-03-13 17:03:22 -0700202 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800203 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
204
Adam Cohen3ed316a2014-07-23 18:21:20 -0700205 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
206 static final String ACTION_FIRST_LOAD_COMPLETE =
207 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
208
Adam Cohen39a06042013-07-19 14:30:12 -0700209 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700210 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700211
Sunny Goyal594d76d2014-11-06 10:12:54 -0800212 private static final String QSB_WIDGET_ID = "qsb_widget_id";
213 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
214
Winson Chunga6945242014-01-08 14:04:34 -0800215 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
216
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700217 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700218 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700219 private State mState = State.WORKSPACE;
220 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700221
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700222 private boolean mIsSafeModeEnabled;
223
Adam Cohenc2d6e892014-10-16 09:49:24 -0700224 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
225 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700226 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700227
Joe Onorato9c1289c2009-08-17 11:03:03 -0400228 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700229 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
230 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700231 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private static final Object sLock = new Object();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000235 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
236 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
237
Winson Chunga2413752012-04-03 14:22:34 -0700238 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700239 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
240 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700241 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700242 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700243
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800244 private final BroadcastReceiver mCloseSystemDialogsReceiver
245 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800246 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
247
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 private LayoutInflater mInflater;
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700251 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800252 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800253 private DragLayer mDragLayer;
254 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700255 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700256
Sunny Goyalffe83f12014-08-14 17:39:34 -0700257 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700258 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700259
Michael Jurkac9d95c52011-08-29 14:03:34 -0700260 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800261 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800262 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700263
Michael Jurka0280c3b2010-09-17 15:00:07 -0700264 private int[] mTmpAddItemCellCoordinates = new int[2];
265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 private FolderInfo mFolderInfo;
267
Winson Chung3d503fb2011-07-13 17:25:49 -0700268 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800269 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700270
Michael Jurka838a4ca2011-02-07 13:33:06 -0800271 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700272
Winson Chungc51db6a2011-10-05 11:44:49 -0700273 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700274 private AppsCustomizeTabHost mAppsCustomizeTabHost;
275 private AppsCustomizePagedView mAppsCustomizeContent;
276 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800277 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700280 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
281 // scroll issues (because the workspace may not have been measured yet) and extra work.
282 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
283 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284
285 private SpannableStringBuilder mDefaultKeySsb = null;
286
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287 private boolean mWorkspaceLoading = true;
288
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800289 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 private boolean mRestoring;
291 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700292 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293
Michael Jurka1e2f4652013-07-08 18:03:46 -0700294 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700295 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
296
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 private Bundle mSavedInstanceState;
298
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800300 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800301 private boolean mUserPresent = true;
302 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700303 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700306 private static LocaleConfiguration sLocaleConfiguration = null;
307
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700308 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700309
Adam Cohen61f560d2013-09-30 15:58:20 -0700310 private View.OnTouchListener mHapticFeedbackTouchListener;
311
Adam Cohended9f8d2010-11-03 13:25:16 -0700312 // Related to the auto-advancing of widgets
313 private final int ADVANCE_MSG = 1;
314 private final int mAdvanceInterval = 20000;
315 private final int mAdvanceStagger = 250;
316 private long mAutoAdvanceSentTime;
317 private long mAutoAdvanceTimeLeft = -1;
318 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
319 new HashMap<View, AppWidgetProviderInfo>();
320
Winson Chung400438b2011-01-16 17:53:48 -0800321 // Determines how long to wait after a rotation before restoring the screen orientation to
322 // match the sensor state.
323 private final int mRestoreScreenOrientationDelay = 500;
324
Craig Mautner360310b2012-10-26 15:13:08 -0700325 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700326
Adam Cohen1462de32012-07-24 22:34:36 -0700327 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700328 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700329
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700330 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700331 static Date sDateStamp = new Date();
332 static DateFormat sDateFormat =
333 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
334 static long sRunStart = System.currentTimeMillis();
335 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700336
Winson Chung46353de2012-02-16 14:05:10 -0800337 // We only want to get the SharedPreferences once since it does an FS stat each time we get
338 // it from the context.
339 private SharedPreferences mSharedPrefs;
340
Adam Cohene25af792013-06-06 23:08:25 -0700341 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
342
Winson Chungf0c6ae02012-03-21 16:10:31 -0700343 // Holds the page that we need to animate to, and the icon views that we need to animate up
344 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700345 private ImageView mFolderIconImageView;
346 private Bitmap mFolderIconBitmap;
347 private Canvas mFolderIconCanvas;
348 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700349
Michael Jurkaddd62e92011-02-16 17:49:14 -0800350 private BubbleTextView mWaitingForResume;
351
Adam Cohen59400422014-03-05 18:07:04 -0800352 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
353 new HashMap<String, CustomAppWidget>();
354
355 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
356 static {
357 if (ENABLE_CUSTOM_WIDGET_TEST) {
358 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
359 }
360 }
361
Michael Jurkac1f5d262011-09-30 19:32:27 -0700362 private Runnable mBuildLayersRunnable = new Runnable() {
363 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700364 if (mWorkspace != null) {
365 mWorkspace.buildPageHardwareLayers();
366 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700367 }
368 };
369
Adam Cohendb364c32014-05-20 14:23:40 -0700370 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800371
372 private static class PendingAddArguments {
373 int requestCode;
374 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700375 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700376 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800377 int cellX;
378 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700379 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800380 }
381
Daniel Sandlerff02d492013-08-05 02:12:05 -0400382 private Stats mStats;
383
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700384 FocusIndicatorView mFocusHandler;
385
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 @Override
387 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700388 if (DEBUG_STRICT_MODE) {
389 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
390 .detectDiskReads()
391 .detectDiskWrites()
392 .detectNetwork() // or .detectAll() for all detectable problems
393 .penaltyLog()
394 .build());
395 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
396 .detectLeakedSqlLiteObjects()
397 .detectLeakedClosableObjects()
398 .penaltyLog()
399 .penaltyDeath()
400 .build());
401 }
402
Adam Cohen9211d422014-10-07 18:14:53 -0700403 if (mLauncherCallbacks != null) {
404 mLauncherCallbacks.preOnCreate();
405 }
406
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400408
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400409 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400410 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700411 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700412
Winson Chung5f8afe62013-08-12 16:19:28 -0700413 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700414 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700415
416 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400417 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700418 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700419 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800420 mModel = app.setLauncher(this);
421 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700422 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400423 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700425
Daniel Sandlerff02d492013-08-05 02:12:05 -0400426 mStats = new Stats(this);
427
Sunny Goyalffe83f12014-08-14 17:39:34 -0700428 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700429
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700430 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
431 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700432
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700433 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
434 // this also ensures that any synchronous binding below doesn't re-trigger another
435 // LauncherModel load.
436 mPaused = false;
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200439 android.os.Debug.startMethodTracing(
440 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 }
442
443 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100447 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800449 registerContentObservers();
450
Joe Onorato7c312c12009-08-13 21:36:53 -0700451 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 mSavedState = savedInstanceState;
454 restoreState(mSavedState);
455
456 if (PROFILE_STARTUP) {
457 android.os.Debug.stopMethodTracing();
458 }
459
460 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700461 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 // If the user leaves launcher, then we should just load items asynchronously when
463 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500464 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 } else {
466 // We only load the page synchronously if the user rotates (or triggers a
467 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800468 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 }
471
472 // For handling default keys
473 mDefaultKeySsb = new SpannableStringBuilder();
474 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800475
476 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
477 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800478
Adam Cohenf9426d52012-06-04 17:26:21 -0700479 // On large interfaces, we want the screen to auto-rotate based on the current orientation
480 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700481
Adam Cohen9211d422014-10-07 18:14:53 -0700482 if (mLauncherCallbacks != null) {
483 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700484 if (mLauncherCallbacks.hasLauncherOverlay()) {
485 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700486 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
487 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
488 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700489 mWorkspace.setLauncherOverlay(mLauncherOverlay);
490 }
Adam Cohen9211d422014-10-07 18:14:53 -0700491 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700492
493 if (shouldShowIntroScreen()) {
494 showIntroScreen();
495 } else {
496 showFirstRunActivity();
497 showFirstRunClings();
498 }
Adam Cohen9211d422014-10-07 18:14:53 -0700499 }
500
501 private LauncherCallbacks mLauncherCallbacks;
502
503 public void onPostCreate(Bundle savedInstanceState) {
504 super.onPostCreate(savedInstanceState);
505 if (mLauncherCallbacks != null) {
506 mLauncherCallbacks.onPostCreate(savedInstanceState);
507 }
508 }
509
510 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
511 mLauncherCallbacks = callbacks;
512 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700513 }
514
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700515 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700516 public void onLauncherProviderChange() {
517 if (mLauncherCallbacks != null) {
518 mLauncherCallbacks.onLauncherProviderChange();
519 }
520 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700521
Adam Cohen9211d422014-10-07 18:14:53 -0700522 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700523 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700524 if (mLauncherCallbacks != null) {
525 return mLauncherCallbacks.hasCustomContentToLeft();
526 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700527 return false;
528 }
529
Dave Hawkeya8881582013-09-17 15:55:33 -0600530 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500531 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600532 * {@link #addToCustomContentPage}. This will only be invoked if
533 * {@link #hasCustomContentToLeft()} is {@code true}.
534 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500535 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700536 if (mLauncherCallbacks != null) {
537 mLauncherCallbacks.populateCustomContentContainer();
538 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600539 }
540
541 /**
542 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
543 * ensure the custom content page is added or removed if necessary.
544 */
545 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600546 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600547 // Not bound yet, wait for bindScreens to be called.
548 return;
549 }
550
551 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
552 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500553 mWorkspace.createCustomContentContainer();
554 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600555 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
556 mWorkspace.removeCustomContentPage();
557 }
558 }
559
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700561 if (sLocaleConfiguration == null) {
562 new AsyncTask<Void, Void, LocaleConfiguration>() {
563 @Override
564 protected LocaleConfiguration doInBackground(Void... unused) {
565 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
566 readConfiguration(Launcher.this, localeConfiguration);
567 return localeConfiguration;
568 }
569
570 @Override
571 protected void onPostExecute(LocaleConfiguration result) {
572 sLocaleConfiguration = result;
573 checkForLocaleChange(); // recursive, but now with a locale configuration
574 }
575 }.execute();
576 return;
577 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700578
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 final Configuration configuration = getResources().getConfiguration();
580
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700581 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 final String locale = configuration.locale.toString();
583
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700584 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 final int mcc = configuration.mcc;
586
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700587 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 final int mnc = configuration.mnc;
589
Romain Guy84f296c2009-11-04 15:00:44 -0800590 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591
Romain Guy84f296c2009-11-04 15:00:44 -0800592 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700593 sLocaleConfiguration.locale = locale;
594 sLocaleConfiguration.mcc = mcc;
595 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700596
Joe Onorato0589f0f2010-02-08 13:44:00 -0800597 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700598
599 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100600 new AsyncTask<Void, Void, Void>() {
601 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700602 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100603 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 }
Michael Jurka43467462013-11-14 12:03:58 +0100605 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700606 }
607 }
608
609 private static class LocaleConfiguration {
610 public String locale;
611 public int mcc = -1;
612 public int mnc = -1;
613 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700614
Romain Guy98d01652009-06-30 16:21:04 -0700615 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
616 DataInputStream in = null;
617 try {
Helena Josol28db2802014-10-09 17:04:09 +0100618 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700619 configuration.locale = in.readUTF();
620 configuration.mcc = in.readInt();
621 configuration.mnc = in.readInt();
622 } catch (FileNotFoundException e) {
623 // Ignore
624 } catch (IOException e) {
625 // Ignore
626 } finally {
627 if (in != null) {
628 try {
629 in.close();
630 } catch (IOException e) {
631 // Ignore
632 }
633 }
634 }
635 }
636
637 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
638 DataOutputStream out = null;
639 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100640 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100641 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700642 out.writeUTF(configuration.locale);
643 out.writeInt(configuration.mcc);
644 out.writeInt(configuration.mnc);
645 out.flush();
646 } catch (FileNotFoundException e) {
647 // Ignore
648 } catch (IOException e) {
649 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100650 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700651 } finally {
652 if (out != null) {
653 try {
654 out.close();
655 } catch (IOException e) {
656 // Ignore
657 }
658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
660 }
661
Anton Hanssona2f665f2013-09-26 12:18:32 +0100662 public Stats getStats() {
663 return mStats;
664 }
665
Bjorn Bringertc459e522013-06-07 19:36:01 +0100666 public LayoutInflater getInflater() {
667 return mInflater;
668 }
669
Winson Chung36a62fe2012-05-06 18:04:42 -0700670 boolean isDraggingEnabled() {
671 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
672 // that is subsequently removed from the workspace in startBinding().
673 return !mModel.isLoadingWorkspace();
674 }
675
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800676 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000677 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100678 if (Build.VERSION.SDK_INT >= 17) {
679 return View.generateViewId();
680 } else {
681 // View.generateViewId() is not available. The following fallback logic is a copy
682 // of its implementation.
683 for (;;) {
684 final int result = sNextGeneratedId.get();
685 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
686 int newValue = result + 1;
687 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
688 if (sNextGeneratedId.compareAndSet(result, newValue)) {
689 return result;
690 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000691 }
692 }
693 }
694
695 public int getViewIdForItem(ItemInfo info) {
696 // This cast is safe given the > 2B range for int.
697 int itemId = (int) info.id;
698 if (mItemIdToViewId.containsKey(itemId)) {
699 return mItemIdToViewId.get(itemId);
700 }
701 int viewId = generateViewId();
702 mItemIdToViewId.put(itemId, viewId);
703 return viewId;
704 }
705
706 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700707 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
708 * a configuration step, this allows the proper animations to run after other transitions.
709 */
Adam Cohendb364c32014-05-20 14:23:40 -0700710 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700711 long screenId = args.screenId;
712 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
713 // When the screen id represents an actual screen (as opposed to a rank) we make sure
714 // that the drop page actually exists.
715 screenId = ensurePendingDropLayoutExists(args.screenId);
716 }
Adam Cohendb364c32014-05-20 14:23:40 -0700717
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800718 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800719 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700720 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700721 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700722 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800723 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700724 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700725 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700726 case REQUEST_RECONFIGURE_APPWIDGET:
727 completeRestoreAppWidget(args.appWidgetId);
728 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800729 }
Michael Jurka27614d22012-04-02 06:40:22 -0700730 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
731 // if you turned the screen off and then back while in All Apps, Launcher would not
732 // return to the workspace. Clearing mAddInfo.container here fixes this issue
733 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700734 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800735 }
736
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800737 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700738 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700739 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700740 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700741 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800742 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700743
Adam Cohenad4e15c2013-10-17 16:21:35 -0700744 Runnable exitSpringLoaded = new Runnable() {
745 @Override
746 public void run() {
747 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
748 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
749 }
750 };
751
Michael Jurka8b805b12012-04-18 14:23:14 -0700752 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700753 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700754 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
755 if (resultCode == RESULT_CANCELED) {
756 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700757 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700759 } else if (resultCode == RESULT_OK) {
Adam Cohen59400422014-03-05 18:07:04 -0800760 addAppWidgetImpl(appWidgetId, (PendingAddWidgetInfo) mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700761 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700762 }
763 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200764 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
765 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
766 mWorkspace.exitOverviewMode(false);
767 }
768 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700769 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200770
Adam Cohened66b2b2012-01-23 17:28:51 -0800771 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
772 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700773
Adam Cohendb364c32014-05-20 14:23:40 -0700774 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800775 // We have special handling for widgets
776 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800777 final int appWidgetId;
778 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
779 : -1;
780 if (widgetId < 0) {
781 appWidgetId = pendingAddWidgetId;
782 } else {
783 appWidgetId = widgetId;
784 }
785
Adam Cohenad4e15c2013-10-17 16:21:35 -0700786 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800787 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700788 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
789 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700790 result = RESULT_CANCELED;
791 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700792 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700793 @Override
794 public void run() {
795 exitSpringLoadedDragModeDelayed(false, 0, null);
796 }
797 };
Adam Cohendb364c32014-05-20 14:23:40 -0700798 if (workspaceLocked) {
799 // No need to remove the empty screen if we're mid-binding, as the
800 // the bind will not add the empty screen.
801 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
802 } else {
803 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
804 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
805 }
Winson Chung5aaab772012-04-27 15:24:02 -0700806 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700807 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700808 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
809 // When the screen id represents an actual screen (as opposed to a rank)
810 // we make sure that the drop page actually exists.
811 mPendingAddInfo.screenId =
812 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
813 }
Adam Cohendb364c32014-05-20 14:23:40 -0700814 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
815
816 dropLayout.setDropPending(true);
817 final Runnable onComplete = new Runnable() {
818 @Override
819 public void run() {
820 completeTwoStageWidgetDrop(resultCode, appWidgetId);
821 dropLayout.setDropPending(false);
822 }
823 };
824 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
825 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
826 } else {
827 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
828 mPendingAddInfo);
829 sPendingAddItem = args;
830 }
Winson Chung5aaab772012-04-27 15:24:02 -0700831 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800832 return;
833 }
834
Sunny Goyalff572272014-07-23 13:58:07 -0700835 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
836 if (resultCode == RESULT_OK) {
837 // Update the widget view.
838 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
839 pendingAddWidgetId, mPendingAddInfo);
840 if (workspaceLocked) {
841 sPendingAddItem = args;
842 } else {
843 completeAdd(args);
844 }
845 }
846 // Leave the widget in the pending state if the user canceled the configure.
847 return;
848 }
849
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 // The pattern used here is that a user PICKs a specific application,
851 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
854 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700855 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700856 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
857 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800858 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700859 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800860 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700861 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700862 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
863 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
Adam Cohen00074722013-10-11 17:46:09 -0700865 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700866 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700867 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800870
871 }
872
873 @Override
874 protected void onActivityResult(
875 final int requestCode, final int resultCode, final Intent data) {
876 handleActivityResult(requestCode, resultCode, data);
877 if (mLauncherCallbacks != null) {
878 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
879 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 }
881
Adam Cohendb364c32014-05-20 14:23:40 -0700882 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
883 appWidgetId, ItemInfo info) {
884 PendingAddArguments args = new PendingAddArguments();
885 args.requestCode = requestCode;
886 args.intent = data;
887 args.container = info.container;
888 args.screenId = info.screenId;
889 args.cellX = info.cellX;
890 args.cellY = info.cellY;
891 args.appWidgetId = appWidgetId;
892 return args;
893 }
894
895 /**
896 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
897 *
898 * @param screenId the screen id to check
899 * @return the new screen, or screenId if it exists
900 */
901 private long ensurePendingDropLayoutExists(long screenId) {
902 CellLayout dropLayout =
903 (CellLayout) mWorkspace.getScreenWithId(screenId);
904 if (dropLayout == null) {
905 // it's possible that the add screen was removed because it was
906 // empty and a re-bind occurred
907 mWorkspace.addExtraEmptyScreen();
908 return mWorkspace.commitExtraEmptyScreen();
909 } else {
910 return screenId;
911 }
912 }
913
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700915 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700916 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 Runnable onCompleteRunnable = null;
918 int animationType = 0;
919
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700920 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800921 if (resultCode == RESULT_OK) {
922 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
923 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700924 mPendingAddWidgetInfo);
925 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 onCompleteRunnable = new Runnable() {
927 @Override
928 public void run() {
929 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700930 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700931 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
932 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 }
934 };
935 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800936 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800938 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700939 if (mDragLayer.getAnimatedView() != null) {
940 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
941 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
942 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700943 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700944 // The animated view may be null in the case of a rotation during widget configuration
945 onCompleteRunnable.run();
946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 }
948
949 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700950 protected void onStop() {
951 super.onStop();
952 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700953
954 if (mLauncherCallbacks != null) {
955 mLauncherCallbacks.onStop();
956 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700957 }
958
959 @Override
960 protected void onStart() {
961 super.onStart();
962 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700963
964 if (mLauncherCallbacks != null) {
965 mLauncherCallbacks.onStart();
966 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700967 }
968
969 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200971 long startTime = 0;
972 if (DEBUG_RESUME_TIME) {
973 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400974 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200975 }
Adam Cohen9211d422014-10-07 18:14:53 -0700976
977 if (mLauncherCallbacks != null) {
978 mLauncherCallbacks.preOnResume();
979 }
980
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700982
Winson Chung4a2afa32012-07-19 14:53:05 -0700983 // Restore the previous launcher state
984 if (mOnResumeState == State.WORKSPACE) {
985 showWorkspace(false);
986 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800987 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700988 }
989 mOnResumeState = State.NONE;
990
Craig Mautner360310b2012-10-26 15:13:08 -0700991 // Background was set to gradient in onPause(), restore to black if in all apps.
992 setWorkspaceBackground(mState == State.WORKSPACE);
993
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800994 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700995 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700996 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500997 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700999 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001001 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001002 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1003 // execute them here
1004 long startTimeCallbacks = 0;
1005 if (DEBUG_RESUME_TIME) {
1006 startTimeCallbacks = System.currentTimeMillis();
1007 }
1008
1009 if (mAppsCustomizeContent != null) {
1010 mAppsCustomizeContent.setBulkBind(true);
1011 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001012 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1013 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001014 }
1015 if (mAppsCustomizeContent != null) {
1016 mAppsCustomizeContent.setBulkBind(false);
1017 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001018 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001019 if (DEBUG_RESUME_TIME) {
1020 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1021 (System.currentTimeMillis() - startTimeCallbacks));
1022 }
Michael Jurka447bf842013-05-15 14:52:15 +02001023 }
Michael Jurka54554252013-08-01 12:52:23 +02001024 if (mOnResumeCallbacks.size() > 0) {
1025 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1026 mOnResumeCallbacks.get(i).run();
1027 }
1028 mOnResumeCallbacks.clear();
1029 }
Winson Chunge4e50662012-01-23 14:45:13 -08001030
1031 // Reset the pressed state of icons that were locked in the press state while activities
1032 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001033 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001034 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001035 mWaitingForResume.setStayPressed(false);
1036 }
Winson Chung82963d52013-10-09 11:20:57 -07001037
Adam Cohen06dff352012-06-01 17:17:08 -07001038 // It is possible that widgets can receive updates while launcher is not in the foreground.
1039 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1040 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1041 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001042 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001043
Winson Chung780fe592013-09-26 14:48:44 -07001044 // Process any items that were added while Launcher was away.
1045 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1046
Michael Jurka447bf842013-05-15 14:52:15 +02001047 if (DEBUG_RESUME_TIME) {
1048 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1049 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001050
1051 if (mWorkspace.getCustomContentCallbacks() != null) {
1052 // If we are resuming and the custom content is the current page, we call onShow().
1053 // It is also poassible that onShow will instead be called slightly after first layout
1054 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1055 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001056 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001057 }
1058 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001059 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001060 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001061
1062 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001063
1064 if (mLauncherCallbacks != null) {
1065 mLauncherCallbacks.onResume();
1066 }
Adam Cohen06dff352012-06-01 17:17:08 -07001067 }
1068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001070 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001071 // Ensure that items added to Launcher are queued until Launcher returns
1072 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001073 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001074
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001075 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001076 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001077 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001078 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001079
1080 // We call onHide() aggressively. The custom content callbacks should be able to
1081 // debounce excess onHide calls.
1082 if (mWorkspace.getCustomContentCallbacks() != null) {
1083 mWorkspace.getCustomContentCallbacks().onHide();
1084 }
Romain Guycbb89e42009-06-08 15:52:54 -07001085
Adam Cohen9211d422014-10-07 18:14:53 -07001086 if (mLauncherCallbacks != null) {
1087 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001088 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001089 }
1090
1091 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001092 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1093 // by a onResume or by scrolling otherwise.
1094 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001095
1096 // Custom content is completely hidden
1097 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001098
1099 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1100 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001101
1102 // Indicates whether the user is allowed to scroll away from the custom content.
1103 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001104 }
1105
Adam Cohenc2d6e892014-10-16 09:49:24 -07001106 public interface LauncherOverlay {
1107
1108 /**
1109 * Touch interaction leading to overscroll has begun
1110 */
1111 public void onScrollInteractionBegin();
1112
1113 /**
1114 * Touch interaction related to overscroll has ended
1115 */
1116 public void onScrollInteractionEnd();
1117
1118 /**
1119 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1120 * screen (or in the case of RTL, the rightmost screen).
1121 */
1122 public void onScrollChange(int progress, boolean rtl);
1123
1124 /**
1125 * Screen has stopped scrolling
1126 */
1127 public void onScrollSettled();
1128
1129 /**
1130 * This method can be called by the Launcher in order to force the LauncherOverlay
1131 * to exit fully immersive mode.
1132 */
1133 public void forceExitFullImmersion();
1134 }
1135
1136 public interface LauncherOverlayCallbacks {
1137 /**
1138 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1139 * however it doesn't modify any state within the launcher.
1140 */
1141 public boolean canEnterFullImmersion();
1142
1143 /**
1144 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1145 * eg. by occupying the full screen and handling all touch events.
1146 *
1147 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1148 * case, Launcher will modify any necessary state and assumes the overlay is
1149 * handling all interaction. If false, the LauncherOverlay should cancel any
1150 *
1151 */
1152 public boolean enterFullImmersion();
1153
1154 /**
1155 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1156 * full control over UI and state.
1157 */
1158 public void exitFullImmersion();
1159 }
1160
1161 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1162
1163 @Override
1164 public boolean canEnterFullImmersion() {
1165 return mState == State.WORKSPACE;
1166 }
1167
1168 @Override
1169 public boolean enterFullImmersion() {
1170 if (mState == State.WORKSPACE) {
1171 // When fully immersed, disregard any touches which fall through.
1172 mDragLayer.setBlockTouch(true);
1173 return true;
1174 }
1175 return false;
1176 }
1177
1178 @Override
1179 public void exitFullImmersion() {
1180 mDragLayer.setBlockTouch(false);
1181 }
1182 }
1183
Jorim Jaggid017f882014-01-14 17:08:48 -08001184 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001185 if (mLauncherCallbacks != null) {
1186 return mLauncherCallbacks.hasSettings();
1187 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001188 return false;
1189 }
1190
Adam Cohenbffe7452013-07-22 18:21:45 -07001191
Adam Cohen9211d422014-10-07 18:14:53 -07001192 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001193 CustomContentCallbacks callbacks, String description) {
1194 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001195 }
1196
Adam Cohenedb40762013-07-18 16:45:45 -07001197 // The custom content needs to offset its content to account for the QSB
1198 public int getTopOffsetForCustomContent() {
1199 return mWorkspace.getPaddingTop();
1200 }
1201
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001202 @Override
1203 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001204 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001205 if (mModel.isCurrentCallbacks(this)) {
1206 mModel.stopLoader();
1207 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001208 if (mAppsCustomizeContent != null) {
1209 mAppsCustomizeContent.surrender();
1210 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001211 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001212 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001213
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001214 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001215 @Override
1216 public void onWindowFocusChanged(boolean hasFocus) {
1217 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001218 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001219
1220 if (mLauncherCallbacks != null) {
1221 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1222 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001223 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 private boolean acceptFilter() {
1226 final InputMethodManager inputManager = (InputMethodManager)
1227 getSystemService(Context.INPUT_METHOD_SERVICE);
1228 return !inputManager.isFullscreenMode();
1229 }
1230
1231 @Override
1232 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001233 final int uniChar = event.getUnicodeChar();
1234 final boolean handled = super.onKeyDown(keyCode, event);
1235 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1236 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1238 keyCode, event);
1239 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001240 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001241 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001242 // showSearchDialog()
1243 // If there are multiple keystrokes before the search dialog takes focus,
1244 // onSearchRequested() will be called for every keystroke,
1245 // but it is idempotent, so it's fine.
1246 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
1248 }
1249
Joe Onorato8a9625e2010-01-28 15:55:35 -08001250 // Eat the long press event so the keyboard doesn't come up.
1251 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1252 return true;
1253 }
1254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 return handled;
1256 }
1257
Karl Rosaen138a0412009-04-23 19:00:21 -07001258 private String getTypedText() {
1259 return mDefaultKeySsb.toString();
1260 }
1261
1262 private void clearTypedText() {
1263 mDefaultKeySsb.clear();
1264 mDefaultKeySsb.clearSpans();
1265 Selection.setSelection(mDefaultKeySsb, 0);
1266 }
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001269 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1270 * State
1271 */
1272 private static State intToState(int stateOrdinal) {
1273 State state = State.WORKSPACE;
1274 final State[] stateValues = State.values();
1275 for (int i = 0; i < stateValues.length; i++) {
1276 if (stateValues[i].ordinal() == stateOrdinal) {
1277 state = stateValues[i];
1278 break;
1279 }
1280 }
1281 return state;
1282 }
1283
1284 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 * Restores the previous state, if it exists.
1286 *
1287 * @param savedState The previous state.
1288 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001289 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 private void restoreState(Bundle savedState) {
1291 if (savedState == null) {
1292 return;
1293 }
1294
Michael Jurka883f55b2010-10-21 15:47:14 -07001295 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001296 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001297 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001298 }
1299
Adam Cohen21cd0022013-10-09 18:57:02 -07001300 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1301 PagedView.INVALID_RESTORE_PAGE);
1302 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001303 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
1305
Winson Chung3d503fb2011-07-13 17:25:49 -07001306 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001307 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001308
Winson Chung3d503fb2011-07-13 17:25:49 -07001309 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1310 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001311 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001312 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1313 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001314 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1315 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1316 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001317 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001318 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 mRestoring = true;
1320 }
1321
1322 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1323 if (renameFolder) {
1324 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001325 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326 mRestoring = true;
1327 }
Winson Chunga12a2502010-12-20 14:41:35 -08001328
Winson Chung785d2eb2011-04-14 16:08:02 -07001329 // Restore the AppsCustomize tab
1330 if (mAppsCustomizeTabHost != null) {
1331 String curTab = savedState.getString("apps_customize_currentTab");
1332 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001333 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001334 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001335 mAppsCustomizeContent.loadAssociatedPages(
1336 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001337 }
1338
Winson Chung5afbf7b2011-07-25 11:53:08 -07001339 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1340 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001341 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001342 mItemIdToViewId = (HashMap<Integer, Integer>)
1343 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
1345
1346 /**
1347 * Finds all the views we need and configure them properly.
1348 */
1349 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001350 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001351
Craig Mautner360310b2012-10-26 15:13:08 -07001352 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001353 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001354 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1355 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001356 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001357 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001358
John Spurlock77e1f472013-09-11 10:09:51 -04001359 mLauncherView.setSystemUiVisibility(
1360 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001361 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362
Winson Chung4c98d922011-05-31 16:50:48 -07001363 // Setup the drag layer
1364 mDragLayer.setup(this, dragController);
1365
Winson Chung3d503fb2011-07-13 17:25:49 -07001366 // Setup the hotseat
1367 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1368 if (mHotseat != null) {
1369 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001370 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001371 }
1372
Jorim Jaggid017f882014-01-14 17:08:48 -08001373 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001374 View widgetButton = findViewById(R.id.widget_button);
1375 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001376 @Override
1377 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001378 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001379 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001380 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001381 }
1382 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001383 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1384
1385 View wallpaperButton = findViewById(R.id.wallpaper_button);
1386 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001387 @Override
1388 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001389 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001390 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001391 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001392 }
1393 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001394 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1395
1396 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001397 if (hasSettings()) {
1398 settingsButton.setOnClickListener(new OnClickListener() {
1399 @Override
1400 public void onClick(View arg0) {
1401 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001402 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001403 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001404 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001405 });
1406 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1407 } else {
1408 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001409 }
1410
Adam Cohendbdff6b2013-09-18 19:09:15 -07001411 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001412
Winson Chung4c98d922011-05-31 16:50:48 -07001413 // Setup the workspace
1414 mWorkspace.setHapticFeedbackEnabled(false);
1415 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001416 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001417 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001418
Winson Chungf0ea4d32011-06-06 14:27:16 -07001419 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001420 mSearchDropTargetBar = (SearchDropTargetBar)
1421 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001422
Winson Chungf0ea4d32011-06-06 14:27:16 -07001423 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001424 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001425 mAppsCustomizeContent = (AppsCustomizePagedView)
1426 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1427 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001428
Winson Chung3d503fb2011-07-13 17:25:49 -07001429 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001430 dragController.setDragScoller(mWorkspace);
1431 dragController.setScrollView(mDragLayer);
1432 dragController.setMoveTarget(mWorkspace);
1433 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001434 if (mSearchDropTargetBar != null) {
1435 mSearchDropTargetBar.setup(this, dragController);
Sunny Goyal594d76d2014-11-06 10:12:54 -08001436 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001437 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001438
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001439 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001440 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001441 mWeightWatcher = new WeightWatcher(this);
1442 mWeightWatcher.setAlpha(0.5f);
1443 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001444 new FrameLayout.LayoutParams(
1445 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001446 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001447 Gravity.BOTTOM)
1448 );
Adam Cohen39a06042013-07-19 14:30:12 -07001449
1450 boolean show = shouldShowWeightWatcher();
1451 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 }
1454
1455 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001456 * Sets the all apps button. This method is called from {@link Hotseat}.
1457 */
1458 public void setAllAppsButton(View allAppsButton) {
1459 mAllAppsButton = allAppsButton;
1460 }
1461
1462 public View getAllAppsButton() {
1463 return mAllAppsButton;
1464 }
1465
1466 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 * Creates a view representing a shortcut.
1468 *
1469 * @param info The data structure describing the shortcut.
1470 *
1471 * @return A View inflated from R.layout.application.
1472 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001473 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001475 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477
1478 /**
1479 * Creates a view representing a shortcut inflated from the specified resource.
1480 *
1481 * @param layoutResId The id of the XML layout used to create the shortcut.
1482 * @param parent The group the shortcut belongs to.
1483 * @param info The data structure describing the shortcut.
1484 *
1485 * @return A View inflated from layoutResId.
1486 */
Adam Cohen59400422014-03-05 18:07:04 -08001487 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001488 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001489 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001491 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 return favorite;
1493 }
1494
1495 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 * Add a shortcut to the workspace.
1497 *
1498 * @param data The intent describing the shortcut.
1499 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001501 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001502 int cellY) {
1503 int[] cellXY = mTmpAddItemCellCoordinates;
1504 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001505 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001506
Michael Jurkad3ef3062010-11-23 16:23:58 -08001507 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001508
Sunny Goyal2350bc92014-10-14 16:42:54 -07001509 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001510 if (info == null) {
1511 return;
1512 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001513 final View view = createShortcut(info);
1514
Adam Cohenfbba09b2011-07-18 21:43:05 -07001515 // First we check if we already know the exact location where we want to add this item.
1516 if (cellX >= 0 && cellY >= 0) {
1517 cellXY[0] = cellX;
1518 cellXY[1] = cellY;
1519 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001520
1521 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001522 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001523 true, null,null)) {
1524 return;
1525 }
1526 DragObject dragObject = new DragObject();
1527 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001528 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1529 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001530 return;
1531 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001532 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001533 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001534 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001535 foundCellSpan = (result != null);
1536 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001537 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001538 }
1539
1540 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001541 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001542 return;
1543 }
1544
Adam Cohendcd297f2013-06-18 13:13:40 -07001545 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546
1547 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001549 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
1551 }
1552
Adam Cohen2f093b62012-04-30 18:59:53 -07001553 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1554 int minHeight) {
1555 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001556 // We want to account for the extra amount of padding that we are adding to the widget
1557 // to ensure that it gets the full amount of space that it has requested
1558 int requiredWidth = minWidth + padding.left + padding.right;
1559 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001560 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001561 }
1562
Adam Cohen2f093b62012-04-30 18:59:53 -07001563 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1564 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001565 }
1566
Adam Cohen2f093b62012-04-30 18:59:53 -07001567 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1568 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001569 }
1570
Adam Cohen2f093b62012-04-30 18:59:53 -07001571 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1572 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001573 }
1574
Adam Cohen2f093b62012-04-30 18:59:53 -07001575 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1576 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001577 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001578 }
1579
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001581 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001583 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 */
Adam Cohen59400422014-03-05 18:07:04 -08001585 private void completeAddAppWidget(int appWidgetId, long container, long screenId,
1586 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1587
1588 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001589 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001590 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1591 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001592 }
Romain Guycbb89e42009-06-08 15:52:54 -07001593
Adam Cohen59400422014-03-05 18:07:04 -08001594 if (appWidgetInfo.isCustomWidget) {
1595 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001596 }
1597
Adam Cohen59400422014-03-05 18:07:04 -08001598 LauncherAppWidgetInfo launcherInfo;
1599 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1600 launcherInfo.spanX = info.spanX;
1601 launcherInfo.spanY = info.spanY;
1602 launcherInfo.minSpanX = info.minSpanX;
1603 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001604 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001605
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001607 container, screenId, info.cellX, info.cellY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608
1609 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001610 if (hostView == null) {
1611 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001612 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1613 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001614 } else {
1615 // The AppWidgetHostView has already been inflated and instantiated
1616 launcherInfo.hostView = hostView;
1617 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001619 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001620 launcherInfo.notifyWidgetSizeChanged(this);
1621
Adam Cohen59400422014-03-05 18:07:04 -08001622 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1623 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001624
1625 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001627 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 }
Romain Guycbb89e42009-06-08 15:52:54 -07001629
Adam Cohended9f8d2010-11-03 13:25:16 -07001630 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1631 @Override
1632 public void onReceive(Context context, Intent intent) {
1633 final String action = intent.getAction();
1634 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1635 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001636 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001637 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001638
Winson Chungc100e8e2011-08-09 16:02:43 -07001639 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001640 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001641 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001642 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001643 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001644 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1645 mUserPresent = true;
1646 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001647 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1648 mModel.resetLoadedState(false, true);
1649 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1650 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1651 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1652 mModel.resetLoadedState(false, true);
1653 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1654 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1655 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001656 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1657 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1658 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001659 }
1660 }
1661 };
1662
1663 @Override
1664 public void onAttachedToWindow() {
1665 super.onAttachedToWindow();
1666
1667 // Listen for broadcasts related to user-presence
1668 final IntentFilter filter = new IntentFilter();
1669 filter.addAction(Intent.ACTION_SCREEN_OFF);
1670 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001671 // For handling managed profiles
1672 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1673 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001674 if (ENABLE_DEBUG_INTENTS) {
1675 filter.addAction(DebugIntents.DELETE_DATABASE);
1676 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1677 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001678 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001679 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001680 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001681 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001682 mVisible = true;
1683 }
1684
Adam Cohen0b395352014-06-09 22:54:36 +00001685 /**
1686 * Sets up transparent navigation and status bars in LMP.
1687 * This method is a no-op for other platform versions.
1688 */
1689 @TargetApi(19)
1690 private void setupTransparentSystemBarsForLmp() {
1691 // TODO(sansid): use the APIs directly when compiling against L sdk.
1692 // Currently we use reflection to access the flags and the API to set the transparency
1693 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001694 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001695 try {
1696 getWindow().getAttributes().systemUiVisibility |=
1697 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1698 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1699 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1700 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1701 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1702 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1703 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1704 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1705
1706 Method setStatusBarColorMethod =
1707 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1708 Method setNavigationBarColorMethod =
1709 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1710 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1711 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1712 } catch (NoSuchFieldException e) {
1713 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1714 } catch (NoSuchMethodException ex) {
1715 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1716 } catch (IllegalAccessException e) {
1717 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1718 } catch (IllegalArgumentException e) {
1719 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1720 } catch (InvocationTargetException e) {
1721 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1722 } finally {}
1723 }
1724 }
1725
Adam Cohended9f8d2010-11-03 13:25:16 -07001726 @Override
1727 public void onDetachedFromWindow() {
1728 super.onDetachedFromWindow();
1729 mVisible = false;
1730
Adam Cohend113e0c2010-11-11 10:48:05 -08001731 if (mAttached) {
1732 unregisterReceiver(mReceiver);
1733 mAttached = false;
1734 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 updateRunning();
1736 }
1737
1738 public void onWindowVisibilityChanged(int visibility) {
1739 mVisible = visibility == View.VISIBLE;
1740 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001741 // The following code used to be in onResume, but it turns out onResume is called when
1742 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1743 // is a more appropriate event to handle
1744 if (mVisible) {
1745 mAppsCustomizeTabHost.onWindowVisible();
1746 if (!mWorkspaceLoading) {
1747 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001748 // We want to let Launcher draw itself at least once before we force it to build
1749 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001750 // apps is nice and speedy.
1751 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001752 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001753 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001754 if (mStarted) return;
1755 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001756 // We delay the layer building a bit in order to give
1757 // other message processing a time to run. In particular
1758 // this avoids a delay in hiding the IME if it was
1759 // currently shown, because doing that may involve
1760 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001761 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001762 final ViewTreeObserver.OnDrawListener listener = this;
1763 mWorkspace.post(new Runnable() {
1764 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001765 if (mWorkspace != null &&
1766 mWorkspace.getViewTreeObserver() != null) {
1767 mWorkspace.getViewTreeObserver().
1768 removeOnDrawListener(listener);
1769 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001770 }
1771 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001772 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001773 }
1774 });
1775 }
1776 clearTypedText();
1777 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001778 }
1779
1780 private void sendAdvanceMessage(long delay) {
1781 mHandler.removeMessages(ADVANCE_MSG);
1782 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1783 mHandler.sendMessageDelayed(msg, delay);
1784 mAutoAdvanceSentTime = System.currentTimeMillis();
1785 }
1786
1787 private void updateRunning() {
1788 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1789 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1790 mAutoAdvanceRunning = autoAdvanceRunning;
1791 if (autoAdvanceRunning) {
1792 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1793 sendAdvanceMessage(delay);
1794 } else {
1795 if (!mWidgetsToAdvance.isEmpty()) {
1796 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1797 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1798 }
1799 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001800 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001801 }
1802 }
1803 }
1804
1805 private final Handler mHandler = new Handler() {
1806 @Override
1807 public void handleMessage(Message msg) {
1808 if (msg.what == ADVANCE_MSG) {
1809 int i = 0;
1810 for (View key: mWidgetsToAdvance.keySet()) {
1811 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1812 final int delay = mAdvanceStagger * i;
1813 if (v instanceof Advanceable) {
1814 postDelayed(new Runnable() {
1815 public void run() {
1816 ((Advanceable) v).advance();
1817 }
1818 }, delay);
1819 }
1820 i++;
1821 }
1822 sendAdvanceMessage(mAdvanceInterval);
1823 }
1824 }
1825 };
1826
1827 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001828 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001829 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1830 if (v instanceof Advanceable) {
1831 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001832 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001833 updateRunning();
1834 }
1835 }
1836
1837 void removeWidgetToAutoAdvance(View hostView) {
1838 if (mWidgetsToAdvance.containsKey(hostView)) {
1839 mWidgetsToAdvance.remove(hostView);
1840 updateRunning();
1841 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001842 }
1843
Joe Onorato9c1289c2009-08-17 11:03:03 -04001844 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001845 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 launcherInfo.hostView = null;
1847 }
1848
Winson Chung93eef082012-03-23 15:59:27 -07001849 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1850 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1851 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001852 }
1853
Adam Cohen59400422014-03-05 18:07:04 -08001854 public ArrayList<AppInfo> getAllAppsList() {
1855 return mAppsCustomizeContent.getApps();
1856 }
1857
Winson Chunga6945242014-01-08 14:04:34 -08001858 public DragLayer getDragLayer() {
1859 return mDragLayer;
1860 }
1861
1862 public Workspace getWorkspace() {
1863 return mWorkspace;
1864 }
1865
1866 public Hotseat getHotseat() {
1867 return mHotseat;
1868 }
1869
Jorim Jaggid017f882014-01-14 17:08:48 -08001870 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001871 return mOverviewPanel;
1872 }
1873
1874 public SearchDropTargetBar getSearchBar() {
1875 return mSearchDropTargetBar;
1876 }
1877
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001878 public LauncherAppWidgetHost getAppWidgetHost() {
1879 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 }
Romain Guycbb89e42009-06-08 15:52:54 -07001881
Winson Chunga9abd0e2010-10-27 17:18:37 -07001882 public LauncherModel getModel() {
1883 return mModel;
1884 }
1885
Winson Chunga6945242014-01-08 14:04:34 -08001886 protected SharedPreferences getSharedPrefs() {
1887 return mSharedPrefs;
1888 }
1889
Bjorn Bringertc459e522013-06-07 19:36:01 +01001890 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001891 getWindow().closeAllPanels();
1892
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001893 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001894 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001895 }
1896
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 @Override
1898 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001899 long startTime = 0;
1900 if (DEBUG_RESUME_TIME) {
1901 startTime = System.currentTimeMillis();
1902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 super.onNewIntent(intent);
1904
1905 // Close the menu
1906 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001907 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001908 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001909
Adam Cohened307df2013-10-02 09:37:31 -07001910 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1911 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1912 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001913
Adam Cohen6fecd412013-10-02 17:41:50 -07001914 if (mWorkspace == null) {
1915 // Can be cases where mWorkspace is null, this prevents a NPE
1916 return;
1917 }
1918 Folder openFolder = mWorkspace.getOpenFolder();
1919 // In all these cases, only animate if we're already on home
1920 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001921
1922 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1923 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001924 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001925 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001926 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001927 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001928
Adam Cohen6fecd412013-10-02 17:41:50 -07001929 closeFolder();
1930 exitSpringLoadedDragMode();
1931
1932 // If we are already on home, then just animate back to the workspace,
1933 // otherwise, just wait until onResume to set the state back to Workspace
1934 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001935 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001936 } else {
1937 mOnResumeState = State.WORKSPACE;
1938 }
1939
1940 final View v = getWindow().peekDecorView();
1941 if (v != null && v.getWindowToken() != null) {
1942 InputMethodManager imm = (InputMethodManager)getSystemService(
1943 INPUT_METHOD_SERVICE);
1944 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1945 }
1946
1947 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001948 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001949 mAppsCustomizeTabHost.reset();
1950 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001951
Adam Cohen9211d422014-10-07 18:14:53 -07001952 if (mLauncherCallbacks != null) {
1953 mLauncherCallbacks.onHomeIntent();
1954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 }
Adam Cohened307df2013-10-02 09:37:31 -07001956
Michael Jurka447bf842013-05-15 14:52:15 +02001957 if (DEBUG_RESUME_TIME) {
1958 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960
Adam Cohen9211d422014-10-07 18:14:53 -07001961 if (mLauncherCallbacks != null) {
1962 mLauncherCallbacks.onNewIntent(intent);
1963 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001964 }
1965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001967 public void onRestoreInstanceState(Bundle state) {
1968 super.onRestoreInstanceState(state);
1969 for (int page: mSynchronouslyBoundPages) {
1970 mWorkspace.restoreInstanceStateForChild(page);
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
1974 @Override
1975 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001976 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001977 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1978 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001979 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001980 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981
Michael Jurka883f55b2010-10-21 15:47:14 -07001982 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001983 // We close any open folder since it will not be re-opened, and we need to make sure
1984 // this state is reflected.
1985 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001988 mWaitingForResult) {
1989 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001990 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1992 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001993 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1994 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1995 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001996 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
1998
1999 if (mFolderInfo != null && mWaitingForResult) {
2000 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2001 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2002 }
Michael Jurka946ad472010-07-09 18:05:18 -07002003
Winson Chung785d2eb2011-04-14 16:08:02 -07002004 // Save the current AppsCustomize tab
2005 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08002006 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2007 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002008 if (currentTabTag != null) {
2009 outState.putString("apps_customize_currentTab", currentTabTag);
2010 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002011 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2012 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002013 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002014 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002015
2016 if (mLauncherCallbacks != null) {
2017 mLauncherCallbacks.onSaveInstanceState(outState);
2018 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 }
2020
2021 @Override
2022 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002024
Winson Chunge7a03942011-08-05 15:05:12 -07002025 // Remove all pending runnables
2026 mHandler.removeMessages(ADVANCE_MSG);
2027 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002028 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002029
Winson Chungcd2b0142011-06-08 16:02:26 -07002030 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002031 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002032
2033 // It's possible to receive onDestroy after a new Launcher activity has
2034 // been created. In this case, don't interfere with the new Launcher.
2035 if (mModel.isCurrentCallbacks(this)) {
2036 mModel.stopLoader();
2037 app.setLauncher(null);
2038 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002041 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002043 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002045 mAppWidgetHost = null;
2046
2047 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048
2049 TextKeyListener.getInstance().release();
2050
Winson Chung81b52252012-08-27 15:34:29 -07002051 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2052 // to prevent leaking Launcher activities on orientation change.
2053 if (mModel != null) {
2054 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2055 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002056
2057 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002058 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002059
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002060 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002061 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002062 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002063 mWorkspace = null;
2064 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002065
Michael Jurka2ecf9952012-06-18 12:52:28 -07002066 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002067
2068 if (mLauncherCallbacks != null) {
2069 mLauncherCallbacks.onDestroy();
2070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 }
2072
Adam Cohena9cf38f2011-05-02 15:36:58 -07002073 public DragController getDragController() {
2074 return mDragController;
2075 }
2076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 @Override
2078 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002079 if (requestCode >= 0) {
2080 setWaitingForResult(true);
2081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 super.startActivityForResult(intent, requestCode);
2083 }
2084
Winson Chung70d72102011-08-12 11:24:35 -07002085 /**
2086 * Indicates that we want global search for this activity by setting the globalSearch
2087 * argument for {@link #startSearch} to true.
2088 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002090 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002092
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002093 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002094
Karl Rosaen138a0412009-04-23 19:00:21 -07002095 if (initialQuery == null) {
2096 // Use any text typed in the launcher as the initial query
2097 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002098 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 if (appSearchData == null) {
2100 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002101 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 }
Winson Chungadf0c182012-08-23 14:59:07 -07002103 Rect sourceBounds = new Rect();
2104 if (mSearchDropTargetBar != null) {
2105 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2106 }
Romain Guycbb89e42009-06-08 15:52:54 -07002107
Ian Parkinson047036e2014-09-01 15:40:53 +01002108 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002109 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002110 if (clearTextImmediately) {
2111 clearTypedText();
2112 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002113 }
2114
Ian Parkinson047036e2014-09-01 15:40:53 +01002115 /**
2116 * Start a text search.
2117 *
2118 * @return {@code true} if the search will start immediately, so any further keypresses
2119 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2120 * to buffer keypresses.
2121 */
2122 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002123 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002124 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2125 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2126 sourceBounds);
2127 }
2128
Michael Jurkaa33411c2012-06-14 16:18:21 -07002129 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002130 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002131 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002132 }
2133
2134 /**
2135 * Starts the global search activity. This code is a copied from SearchManager
2136 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002137 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002138 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002139 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002140 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2141 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2142 if (globalSearchActivity == null) {
2143 Log.w(TAG, "No global search activity found.");
2144 return;
2145 }
2146 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2147 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2148 intent.setComponent(globalSearchActivity);
2149 // Make sure that we have a Bundle to put source in
2150 if (appSearchData == null) {
2151 appSearchData = new Bundle();
2152 } else {
2153 appSearchData = new Bundle(appSearchData);
2154 }
Adam Cohen9211d422014-10-07 18:14:53 -07002155 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002156 if (!appSearchData.containsKey("source")) {
2157 appSearchData.putString("source", getPackageName());
2158 }
2159 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2160 if (!TextUtils.isEmpty(initialQuery)) {
2161 intent.putExtra(SearchManager.QUERY, initialQuery);
2162 }
2163 if (selectInitialQuery) {
2164 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2165 }
2166 intent.setSourceBounds(sourceBounds);
2167 try {
2168 startActivity(intent);
2169 } catch (ActivityNotFoundException ex) {
2170 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 }
2173
Yura4f93ec62014-02-04 14:15:21 +00002174 public boolean isOnCustomContent() {
2175 return mWorkspace.isOnOrMovingToCustomContent();
2176 }
2177
Winson Chung70d72102011-08-12 11:24:35 -07002178 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002179 public boolean onPrepareOptionsMenu(Menu menu) {
2180 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002181 if (!isOnCustomContent()) {
2182 // Close any open folders
2183 closeFolder();
2184 // Stop resizing any widgets
2185 mWorkspace.exitWidgetResizeMode();
2186 if (!mWorkspace.isInOverviewMode()) {
2187 // Show the overview mode
2188 showOverviewMode(true);
2189 } else {
2190 showWorkspace(true);
2191 }
Winson Chunge0298742014-01-17 12:03:00 -08002192 }
Adam Cohen9211d422014-10-07 18:14:53 -07002193 if (mLauncherCallbacks != null) {
2194 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2195 }
2196
Michael Jurkab94f3f82013-09-11 18:08:54 +02002197 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002198 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002200 @Override
2201 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002202 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002203 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002204 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002205 }
2206
Joe Onorato9c1289c2009-08-17 11:03:03 -04002207 public boolean isWorkspaceLocked() {
2208 return mWorkspaceLoading || mWaitingForResult;
2209 }
2210
Adam Cohen517a7f52014-03-01 12:12:59 -08002211 public boolean isWorkspaceLoading() {
2212 return mWorkspaceLoading;
2213 }
2214
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002215 private void setWorkspaceLoading(boolean value) {
2216 boolean isLocked = isWorkspaceLocked();
2217 mWorkspaceLoading = value;
2218 if (isLocked != isWorkspaceLocked()) {
2219 onWorkspaceLockedChanged();
2220 }
2221 }
2222
2223 private void setWaitingForResult(boolean value) {
2224 boolean isLocked = isWorkspaceLocked();
2225 mWaitingForResult = value;
2226 if (isLocked != isWorkspaceLocked()) {
2227 onWorkspaceLockedChanged();
2228 }
2229 }
2230
Adam Cohen9211d422014-10-07 18:14:53 -07002231 protected void onWorkspaceLockedChanged() {
2232 if (mLauncherCallbacks != null) {
2233 mLauncherCallbacks.onWorkspaceLockedChanged();
2234 }
2235 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002236
Michael Jurka0280c3b2010-09-17 15:00:07 -07002237 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002238 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002239 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002240 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2241 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002242 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002243 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002244 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002245
Adam Cohen59400422014-03-05 18:07:04 -08002246 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info, final
2247 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002248 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2249 }
2250
Adam Cohen59400422014-03-05 18:07:04 -08002251 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info,
2252 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2253 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002254 if (appWidgetInfo.configure != null) {
2255 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002256 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002257
Bjorn Bringert7984c942009-12-09 15:38:25 +00002258 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002259 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2260 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002263 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002264 Runnable onComplete = new Runnable() {
2265 @Override
2266 public void run() {
2267 // Exit spring loaded mode if necessary after adding the widget
2268 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2269 null);
2270 }
2271 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002272 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002273 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002274 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275 }
2276 }
Romain Guycbb89e42009-06-08 15:52:54 -07002277
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002278 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002279 // Close any folders that may be open.
2280 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002281 mWorkspace.moveToCustomContentScreen(animate);
2282 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002283
2284 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2285 int[] cell, int spanX, int spanY) {
2286 switch (info.itemType) {
2287 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2288 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2289 int span[] = new int[2];
2290 span[0] = spanX;
2291 span[1] = spanY;
2292 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2293 container, screenId, cell, span);
2294 break;
2295 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2296 processShortcutFromDrop(info.componentName, container, screenId, cell);
2297 break;
2298 default:
2299 throw new IllegalStateException("Unknown item type: " + info.itemType);
2300 }
2301 }
2302
Adam Cohenfbba09b2011-07-18 21:43:05 -07002303 /**
2304 * Process a shortcut drop.
2305 *
2306 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002307 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002308 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002309 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002310 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2311 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002312 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002313 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002314 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002315 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002316
2317 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002318 mPendingAddInfo.cellX = cell[0];
2319 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002320 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002321
2322 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2323 createShortcutIntent.setComponent(componentName);
2324 processShortcut(createShortcutIntent);
2325 }
2326
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 /**
2328 * Process a widget drop.
2329 *
2330 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002331 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002332 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002333 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002334 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2335 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002336 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002338 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002339 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002340 mPendingAddInfo.minSpanX = info.minSpanX;
2341 mPendingAddInfo.minSpanY = info.minSpanY;
2342
Adam Cohenfbba09b2011-07-18 21:43:05 -07002343 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002344 mPendingAddInfo.cellX = cell[0];
2345 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002346 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002347 if (span != null) {
2348 mPendingAddInfo.spanX = span[0];
2349 mPendingAddInfo.spanY = span[1];
2350 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002351
Adam Cohened66b2b2012-01-23 17:28:51 -08002352 AppWidgetHostView hostView = info.boundWidget;
2353 int appWidgetId;
2354 if (hostView != null) {
2355 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002356 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002357 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002358 // In this case, we either need to start an activity to get permission to bind
2359 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002360 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002361 Bundle options = info.bindOptions;
2362
Sunny Goyalffe83f12014-08-14 17:39:34 -07002363 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2364 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002365 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002366 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002367 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002368 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002369 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2370 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2371 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002372 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2373 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002374 // TODO: we need to make sure that this accounts for the options bundle.
2375 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002376 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2377 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002378 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002379 }
2380
Joe Onoratodeb98af2010-02-19 14:59:39 -08002381 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002382 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 }
2384
Winson Chung24ab2f12010-09-16 14:10:47 -07002385 void processWallpaper(Intent intent) {
2386 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2387 }
2388
Adam Cohendcd297f2013-06-18 13:13:40 -07002389 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002390 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002391 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002392 folderInfo.title = getText(R.string.folder_name);
2393
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002395 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002396 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002397 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398
2399 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002400 FolderIcon newFolder =
2401 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002402 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002403 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002404 // Force measure the new folder icon
2405 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2406 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002407 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 }
Romain Guycbb89e42009-06-08 15:52:54 -07002409
Joe Onorato9c1289c2009-08-17 11:03:03 -04002410 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002411 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002412 }
2413
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002414 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002415 if (mLauncherCallbacks != null) {
2416 return mLauncherCallbacks.getWallpaperPickerComponent();
2417 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002418 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002419 }
2420
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002421 /**
2422 * Registers various content observers. The current implementation registers
2423 * only a favorites observer to keep track of the favorites applications.
2424 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002425 private void registerContentObservers() {
2426 ContentResolver resolver = getContentResolver();
2427 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2428 true, mWidgetObserver);
2429 }
2430
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 @Override
2432 public boolean dispatchKeyEvent(KeyEvent event) {
2433 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2434 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002436 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002437 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002438 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002439 dumpState();
2440 return true;
2441 }
2442 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002443 }
2444 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2445 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002446 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447 return true;
2448 }
2449 }
2450
2451 return super.dispatchKeyEvent(event);
2452 }
2453
Joe Onorato88ec0992009-11-19 13:16:06 -08002454 @Override
2455 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002456 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2457 return;
2458 }
2459
Winson Chungc93e5ae2012-07-23 20:48:26 -07002460 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002461 if (mAppsCustomizeContent.getContentType() ==
2462 AppsCustomizePagedView.ContentType.Applications) {
2463 showWorkspace(true);
2464 } else {
2465 showOverviewMode(true);
2466 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002467 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002468 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002469 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002470 Folder openFolder = mWorkspace.getOpenFolder();
2471 if (openFolder.isEditingName()) {
2472 openFolder.dismissEditingName();
2473 } else {
2474 closeFolder();
2475 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002476 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002477 mWorkspace.exitWidgetResizeMode();
2478
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002479 // Back button is a no-op here, but give at least some feedback for the button press
2480 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002481 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002482 }
2483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002485 * Re-listen when widgets are reset.
2486 */
2487 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002488 if (mAppWidgetHost != null) {
2489 mAppWidgetHost.startListening();
2490 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002491 }
2492
2493 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 * Launches the intent referred by the clicked shortcut.
2495 *
2496 * @param v The view representing the clicked shortcut.
2497 */
2498 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002499 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2500 // view has detached (it's possible for this to happen if the view is removed mid touch).
2501 if (v.getWindowToken() == null) {
2502 return;
2503 }
2504
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002505 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002506 return;
2507 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002508
Adam Cohen1697b792013-09-17 19:08:21 -07002509 if (v instanceof Workspace) {
2510 if (mWorkspace.isInOverviewMode()) {
2511 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002512 }
2513 return;
2514 }
2515
2516 if (v instanceof CellLayout) {
2517 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002518 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002519 }
2520 }
2521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002523 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002524 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002526 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002527 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002528 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002529 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002530 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002531 } else if (tag instanceof AppInfo) {
2532 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002533 } else if (tag instanceof LauncherAppWidgetInfo) {
2534 if (v instanceof PendingAppWidgetHostView) {
2535 onClickPendingWidget((PendingAppWidgetHostView) v);
2536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537 }
2538 }
2539
Sunny Goyal508da152014-08-14 10:53:27 -07002540 public void onClickPagedViewIcon(View v) {
2541 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002542 if (mLauncherCallbacks != null) {
2543 mLauncherCallbacks.onClickPagedViewIcon(v);
2544 }
Sunny Goyal508da152014-08-14 10:53:27 -07002545 }
2546
Michael Jurka0e260592010-06-30 17:07:39 -07002547 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002548 return false;
2549 }
2550
Michael Jurkaaf442092010-06-10 17:01:57 -07002551 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002552 * Event handler for the app widget view which has not fully restored.
2553 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002554 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002555 if (mIsSafeModeEnabled) {
2556 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2557 return;
2558 }
2559
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002560 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002561 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002562 int widgetId = info.appWidgetId;
2563 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2564 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002565 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2566 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002567 mPendingAddInfo.copyFrom(info);
2568 mPendingAddWidgetId = widgetId;
2569
Sunny Goyalffe83f12014-08-14 17:39:34 -07002570 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2571 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002572 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002573 } else if (info.installProgress < 0) {
2574 // The install has not been queued
2575 final String packageName = info.providerName.getPackageName();
2576 showBrokenAppInstallDialog(packageName,
2577 new DialogInterface.OnClickListener() {
2578 public void onClick(DialogInterface dialog, int id) {
2579 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2580 }
2581 });
2582 } else {
2583 // Download has started.
2584 final String packageName = info.providerName.getPackageName();
2585 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002586 }
2587 }
2588
2589 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002590 * Event handler for the "grid" button that appears on the home screen, which
2591 * enters all apps mode.
2592 *
2593 * @param v The view that was clicked.
2594 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002595 protected void onClickAllAppsButton(View v) {
2596 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2597 if (isAllAppsVisible()) {
2598 showWorkspace(true);
2599 } else {
2600 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2601 }
Adam Cohen9211d422014-10-07 18:14:53 -07002602 if (mLauncherCallbacks != null) {
2603 mLauncherCallbacks.onClickAllAppsButton(v);
2604 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002605 }
2606
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002607 private void showBrokenAppInstallDialog(final String packageName,
2608 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002609 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002610 .setTitle(R.string.abandoned_promises_title)
2611 .setMessage(R.string.abandoned_promise_explanation)
2612 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2613 .setNeutralButton(R.string.abandoned_clean_this,
2614 new DialogInterface.OnClickListener() {
2615 public void onClick(DialogInterface dialog, int id) {
2616 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2617 mWorkspace.removeAbandonedPromise(packageName, user);
2618 }
2619 })
2620 .create().show();
2621 return;
2622 }
2623
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002624 /**
2625 * Event handler for an app shortcut click.
2626 *
2627 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2628 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002629 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002630 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2631 Object tag = v.getTag();
2632 if (!(tag instanceof ShortcutInfo)) {
2633 throw new IllegalArgumentException("Input must be a Shortcut");
2634 }
2635
2636 // Open shortcut
2637 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002638
2639 if (shortcut.isDisabled != 0) {
2640 int error = R.string.activity_not_available;
2641 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2642 error = R.string.safemode_shortcut_error;
2643 }
2644 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2645 return;
2646 }
2647
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002648 final Intent intent = shortcut.intent;
2649
2650 // Check for special shortcuts
2651 if (intent.getComponent() != null) {
2652 final String shortcutClass = intent.getComponent().getClassName();
2653
2654 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2655 MemoryDumpActivity.startDump(this);
2656 return;
2657 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2658 toggleShowWeightWatcher();
2659 return;
2660 }
2661 }
2662
Chris Wren40c5ed32014-06-24 18:24:23 -04002663 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002664 if ((v instanceof BubbleTextView)
2665 && shortcut.isPromise()
2666 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002667 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002668 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002669 new DialogInterface.OnClickListener() {
2670 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002671 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002672 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002673 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 return;
2675 }
2676
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002677 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002678 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002679
2680 if (mLauncherCallbacks != null) {
2681 mLauncherCallbacks.onClickAppShortcut(v);
2682 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002683 }
2684
Sunny Goyal508da152014-08-14 10:53:27 -07002685 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002687 final ShortcutInfo shortcut;
2688 final Intent intent;
2689 if (tag instanceof ShortcutInfo) {
2690 shortcut = (ShortcutInfo) tag;
2691 intent = shortcut.intent;
2692 int[] pos = new int[2];
2693 v.getLocationOnScreen(pos);
2694 intent.setSourceBounds(new Rect(pos[0], pos[1],
2695 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002696
Sunny Goyal508da152014-08-14 10:53:27 -07002697 } else if (tag instanceof AppInfo) {
2698 shortcut = null;
2699 intent = ((AppInfo) tag).intent;
2700 } else {
2701 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2702 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002703
2704 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002705 mStats.recordLaunch(intent, shortcut);
2706
2707 if (success && v instanceof BubbleTextView) {
2708 mWaitingForResume = (BubbleTextView) v;
2709 mWaitingForResume.setStayPressed(true);
2710 }
2711 }
2712
2713 /**
2714 * Event handler for a folder icon click.
2715 *
2716 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2717 */
2718 protected void onClickFolderIcon(View v) {
2719 if (LOGD) Log.d(TAG, "onClickFolder");
2720 if (!(v instanceof FolderIcon)){
2721 throw new IllegalArgumentException("Input must be a FolderIcon");
2722 }
2723
2724 FolderIcon folderIcon = (FolderIcon) v;
2725 final FolderInfo info = folderIcon.getFolderInfo();
2726 Folder openFolder = mWorkspace.getFolderForTag(info);
2727
2728 // If the folder info reports that the associated folder is open, then verify that
2729 // it is actually opened. There have been a few instances where this gets out of sync.
2730 if (info.opened && openFolder == null) {
2731 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2732 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2733 info.opened = false;
2734 }
2735
2736 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2737 // Close any open folder
2738 closeFolder();
2739 // Open the requested folder
2740 openFolder(folderIcon);
2741 } else {
2742 // Find the open folder...
2743 int folderScreen;
2744 if (openFolder != null) {
2745 folderScreen = mWorkspace.getPageForView(openFolder);
2746 // .. and close it
2747 closeFolder(openFolder);
2748 if (folderScreen != mWorkspace.getCurrentPage()) {
2749 // Close any folder open on the current screen
2750 closeFolder();
2751 // Pull the folder onto this screen
2752 openFolder(folderIcon);
2753 }
2754 }
2755 }
Adam Cohen9211d422014-10-07 18:14:53 -07002756
2757 if (mLauncherCallbacks != null) {
2758 mLauncherCallbacks.onClickFolderIcon(v);
2759 }
Michael Jurka5130e402011-10-13 04:55:35 -07002760 }
2761
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002762 /**
2763 * Event handler for the (Add) Widgets button that appears after a long press
2764 * on the home screen.
2765 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002766 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002767 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002768 if (mIsSafeModeEnabled) {
2769 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2770 } else {
2771 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2772 if (mLauncherCallbacks != null) {
2773 mLauncherCallbacks.onClickAddWidgetButton(view);
2774 }
Adam Cohen9211d422014-10-07 18:14:53 -07002775 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002776 }
2777
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002778 /**
2779 * Event handler for the wallpaper picker button that appears after a long press
2780 * on the home screen.
2781 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002782 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002783 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2784 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2785 pickWallpaper.setComponent(getWallpaperPickerComponent());
2786 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002787
2788 if (mLauncherCallbacks != null) {
2789 mLauncherCallbacks.onClickWallpaperPicker(v);
2790 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002791 }
2792
2793 /**
2794 * Event handler for a click on the settings button that appears after a long press
2795 * on the home screen.
2796 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002797 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002798 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002799 if (mLauncherCallbacks != null) {
2800 mLauncherCallbacks.onClickSettingsButton(v);
2801 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002802 }
2803
Michael Jurka5130e402011-10-13 04:55:35 -07002804 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002805 // Provide the same haptic feedback that the system offers for virtual keys.
2806 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002807 }
2808
Adam Cohen61f560d2013-09-30 15:58:20 -07002809 public void performHapticFeedbackOnTouchDown(View v) {
2810 // Provide the same haptic feedback that the system offers for virtual keys.
2811 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2812 }
2813
2814 public View.OnTouchListener getHapticFeedbackTouchListener() {
2815 if (mHapticFeedbackTouchListener == null) {
2816 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2817 @Override
2818 public boolean onTouch(View v, MotionEvent event) {
2819 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2820 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2821 }
2822 return false;
2823 }
2824 };
2825 }
2826 return mHapticFeedbackTouchListener;
2827 }
2828
Adam Cohen9211d422014-10-07 18:14:53 -07002829 public void onDragStarted(View view) {
2830 if (isOnCustomContent()) {
2831 // Custom content screen doesn't participate in drag and drop. If on custom
2832 // content screen, move to default.
2833 moveWorkspaceToDefaultScreen();
2834 }
2835
2836 if (mLauncherCallbacks != null) {
2837 mLauncherCallbacks.onDragStarted(view);
2838 }
2839 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002840
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002841 /**
2842 * Called when the user stops interacting with the launcher.
2843 * This implies that the user is now on the homescreen and is not doing housekeeping.
2844 */
Adam Cohen9211d422014-10-07 18:14:53 -07002845 protected void onInteractionEnd() {
2846 if (mLauncherCallbacks != null) {
2847 mLauncherCallbacks.onInteractionEnd();
2848 }
2849 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002850
2851 /**
2852 * Called when the user starts interacting with the launcher.
2853 * The possible interactions are:
2854 * - open all apps
2855 * - reorder an app shortcut, or a widget
2856 * - open the overview mode.
2857 * This is a good time to stop doing things that only make sense
2858 * when the user is on the homescreen and not doing housekeeping.
2859 */
Adam Cohen9211d422014-10-07 18:14:53 -07002860 protected void onInteractionBegin() {
2861 if (mLauncherCallbacks != null) {
2862 mLauncherCallbacks.onInteractionBegin();
2863 }
2864 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002865
Kenny Guyf07af7b2014-07-31 11:39:16 +01002866 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002867 try {
2868 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002869 launcherApps.showAppDetailsForProfile(componentName, user);
2870 } catch (SecurityException e) {
2871 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2872 Log.e(TAG, "Launcher does not have permission to launch settings");
2873 } catch (ActivityNotFoundException e) {
2874 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2875 Log.e(TAG, "Unable to launch settings");
2876 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002877 }
2878
Michael Jurka1e2f4652013-07-08 18:03:46 -07002879 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002880 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2881 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002882 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002883 // System applications cannot be installed. For now, show a toast explaining that.
2884 // We may give them the option of disabling apps this way.
2885 int messageId = R.string.uninstall_system_app_text;
2886 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002887 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002888 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002889 String packageName = componentName.getPackageName();
2890 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002891 Intent intent = new Intent(
2892 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002893 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2894 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002895 if (user != null) {
2896 user.addToIntent(intent, Intent.EXTRA_USER);
2897 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002898 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002899 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002900 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002901 }
2902
Michael Jurka86a720e2012-05-09 11:23:23 -07002903 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002904 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002905 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002906 // Only launch using the new animation if the shortcut has not opted out (this is a
2907 // private contract between launcher and may be ignored in the future).
2908 boolean useLaunchAnimation = (v != null) &&
2909 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002910 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2911 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002912
Kenny Guy1317e2d2014-05-08 18:52:50 +01002913 UserHandleCompat user = null;
2914 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2915 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2916 user = userManager.getUserForSerialNumber(serialNumber);
2917 }
2918
2919 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002920 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002921 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002922 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2923 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002924 optsBundle = opts.toBundle();
2925 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002926
2927 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2928 // Could be launching some bookkeeping activity
2929 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002930 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002931 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002932 launcherApps.startActivityForProfile(intent.getComponent(), user,
2933 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002934 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002935 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002936 } catch (SecurityException e) {
2937 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002938 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002940 "or use the exported attribute for this activity. "
2941 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002942 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002943 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002944 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002945
Michael Jurka86a720e2012-05-09 11:23:23 -07002946 boolean startActivitySafely(View v, Intent intent, Object tag) {
2947 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002948 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2949 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2950 return false;
2951 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002952 try {
2953 success = startActivity(v, intent, tag);
2954 } catch (ActivityNotFoundException e) {
2955 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2956 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2957 }
2958 return success;
2959 }
2960
Adam Cohen268c4752012-06-06 17:47:33 -07002961 /**
2962 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2963 * in the DragLayer in the exact absolute location of the original FolderIcon.
2964 */
2965 private void copyFolderIconToImage(FolderIcon fi) {
2966 final int width = fi.getMeasuredWidth();
2967 final int height = fi.getMeasuredHeight();
2968
2969 // Lazy load ImageView, Bitmap and Canvas
2970 if (mFolderIconImageView == null) {
2971 mFolderIconImageView = new ImageView(this);
2972 }
2973 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2974 mFolderIconBitmap.getHeight() != height) {
2975 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2976 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2977 }
2978
2979 DragLayer.LayoutParams lp;
2980 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2981 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2982 } else {
2983 lp = new DragLayer.LayoutParams(width, height);
2984 }
2985
Adam Cohen307fe232012-08-16 17:55:58 -07002986 // The layout from which the folder is being opened may be scaled, adjust the starting
2987 // view size by this scale factor.
2988 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002989 lp.customPosition = true;
2990 lp.x = mRectForFolderAnimation.left;
2991 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002992 lp.width = (int) (scale * width);
2993 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002994
2995 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2996 fi.draw(mFolderIconCanvas);
2997 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002998 if (fi.getFolder() != null) {
2999 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3000 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003001 }
Adam Cohen268c4752012-06-06 17:47:33 -07003002 // Just in case this image view is still in the drag layer from a previous animation,
3003 // we remove it and re-add it.
3004 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3005 mDragLayer.removeView(mFolderIconImageView);
3006 }
3007 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003008 if (fi.getFolder() != null) {
3009 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003010 }
Adam Cohen268c4752012-06-06 17:47:33 -07003011 }
3012
Adam Cohen2801caf2011-05-13 20:57:39 -07003013 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003014 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003015 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3016 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3017 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3018
Adam Cohenc51934b2011-07-26 21:07:43 -07003019 FolderInfo info = (FolderInfo) fi.getTag();
3020 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3021 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003022 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3023 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003024 }
3025
Adam Cohen268c4752012-06-06 17:47:33 -07003026 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3027 copyFolderIconToImage(fi);
3028 fi.setVisibility(View.INVISIBLE);
3029
Michael Jurka2ecf9952012-06-18 12:52:28 -07003030 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003031 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003032 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003033 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3034 }
3035 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003036 oa.start();
3037 }
3038
Adam Cohen268c4752012-06-06 17:47:33 -07003039 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003040 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003041 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3042 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3043 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3044
Adam Cohen268c4752012-06-06 17:47:33 -07003045 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003046
Adam Cohen268c4752012-06-06 17:47:33 -07003047 // We remove and re-draw the FolderIcon in-case it has changed
3048 mDragLayer.removeView(mFolderIconImageView);
3049 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003050 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003051 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003052 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003053 oa.addListener(new AnimatorListenerAdapter() {
3054 @Override
3055 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003056 if (cl != null) {
3057 cl.clearFolderLeaveBehind();
3058 // Remove the ImageView copy of the FolderIcon and make the original visible.
3059 mDragLayer.removeView(mFolderIconImageView);
3060 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003061 }
3062 }
3063 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003064 oa.start();
3065 }
3066
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003067 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003068 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003069 * is animated relative to the specified View. If the View is null, no animation
3070 * is played.
3071 *
3072 * @param folderInfo The FolderInfo describing the folder to open.
3073 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003074 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003075 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003076 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003077
Adam Cohena9cf38f2011-05-02 15:36:58 -07003078 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003079
Adam Cohen4554ee12011-08-03 16:13:21 -07003080 // Just verify that the folder hasn't already been added to the DragLayer.
3081 // There was a one-off crash where the folder had a parent already.
3082 if (folder.getParent() == null) {
3083 mDragLayer.addView(folder);
3084 mDragController.addDropTarget((DropTarget) folder);
3085 } else {
3086 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3087 folder.getParent() + ").");
3088 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003089 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003090 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003091
3092 // Notify the accessibility manager that this folder "window" has appeared and occluded
3093 // the workspace items
3094 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3095 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003096 }
3097
3098 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003099 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003100 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003101 if (folder.isEditingName()) {
3102 folder.dismissEditingName();
3103 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003104 closeFolder(folder);
3105 }
3106 }
3107
3108 void closeFolder(Folder folder) {
3109 folder.getInfo().opened = false;
3110
3111 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3112 if (parent != null) {
3113 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3114 shrinkAndFadeInFolderIcon(fi);
3115 }
3116 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003117
3118 // Notify the accessibility manager that this folder "window" has disappeard and no
3119 // longer occludeds the workspace items
3120 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003121 }
3122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003123 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003124 if (!isDraggingEnabled()) return false;
3125 if (isWorkspaceLocked()) return false;
3126 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003127
Adam Cohen1697b792013-09-17 19:08:21 -07003128 if (v instanceof Workspace) {
3129 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003130 if (mWorkspace.enterOverviewMode()) {
3131 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3132 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3133 return true;
3134 } else {
3135 return false;
3136 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003137 } else {
3138 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003139 }
Adam Cohen1697b792013-09-17 19:08:21 -07003140 }
3141
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003142 CellLayout.CellInfo longClickCellInfo = null;
3143 View itemUnderLongClick = null;
3144 if (v.getTag() instanceof ItemInfo) {
3145 ItemInfo info = (ItemInfo) v.getTag();
3146 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3147 itemUnderLongClick = longClickCellInfo.cell;
3148 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003149 }
3150
Winson Chung3d503fb2011-07-13 17:25:49 -07003151 // The hotseat touch handling does not go through Workspace, and we always allow long press
3152 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003153 final boolean inHotseat = isHotseatLayout(v);
3154 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003155 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003156 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003157 // User long pressed on empty space
3158 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3159 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003160 if (mWorkspace.isInOverviewMode()) {
3161 mWorkspace.startReordering(v);
3162 } else {
3163 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003164 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003165 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003166 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3167 mHotseat.getOrderInHotseat(
3168 longClickCellInfo.cellX,
3169 longClickCellInfo.cellY));
3170 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003172 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173 }
3174 }
3175 }
3176 return true;
3177 }
3178
Winson Chung3d503fb2011-07-13 17:25:49 -07003179 boolean isHotseatLayout(View layout) {
3180 return mHotseat != null && layout != null &&
3181 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3182 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003183
Winson Chung3d503fb2011-07-13 17:25:49 -07003184 /**
3185 * Returns the CellLayout of the specified container at the specified screen.
3186 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003187 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003188 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3189 if (mHotseat != null) {
3190 return mHotseat.getLayout();
3191 } else {
3192 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003193 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003194 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003195 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003196 }
3197 }
3198
Daniel Sandler843e8602010-06-07 14:59:01 -04003199 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003200 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003201 }
3202
Craig Mautner360310b2012-10-26 15:13:08 -07003203 private void setWorkspaceBackground(boolean workspace) {
3204 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003205 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003206 }
3207
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003208 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003209 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3210 int curflags = getWindow().getAttributes().flags
3211 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3212 if (wpflags != curflags) {
3213 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3214 }
Craig Mautner360310b2012-10-26 15:13:08 -07003215 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003216 }
3217
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003218 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3219 if (v instanceof LauncherTransitionable) {
3220 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3221 }
3222 }
3223
Michael Jurkabed61d22012-02-14 22:51:29 -08003224 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3225 if (v instanceof LauncherTransitionable) {
3226 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3227 }
Winson Chung70442722012-02-10 15:43:22 -08003228
3229 // Update the workspace transition step as well
3230 dispatchOnLauncherTransitionStep(v, 0f);
3231 }
3232
3233 private void dispatchOnLauncherTransitionStep(View v, float t) {
3234 if (v instanceof LauncherTransitionable) {
3235 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3236 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003237 }
3238
3239 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3240 if (v instanceof LauncherTransitionable) {
3241 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3242 }
Winson Chung70442722012-02-10 15:43:22 -08003243
3244 // Update the workspace transition step as well
3245 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003246 }
3247
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003248 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003249 * Things to test when changing the following seven functions.
3250 * - Home from workspace
3251 * - from center screen
3252 * - from other screens
3253 * - Home from all apps
3254 * - from center screen
3255 * - from other screens
3256 * - Back from all apps
3257 * - from center screen
3258 * - from other screens
3259 * - Launch app from workspace and quit
3260 * - with back
3261 * - with home
3262 * - Launch app from all apps and quit
3263 * - with back
3264 * - with home
3265 * - Go to a screen that's not the default, then all
3266 * apps, and launch and app, and go back
3267 * - with back
3268 * -with home
3269 * - On workspace, long press power and go back
3270 * - with back
3271 * - with home
3272 * - On all apps, long press power and go back
3273 * - with back
3274 * - with home
3275 * - On workspace, power off
3276 * - On all apps, power off
3277 * - Launch an app and turn off the screen while in that app
3278 * - Go back with home key
3279 * - Go back with back key TODO: make this not go to workspace
3280 * - From all apps
3281 * - From workspace
3282 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3283 * - From all apps
3284 * - From the center workspace
3285 * - From another workspace
3286 */
3287
3288 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003289 * Zoom the camera out from the workspace to reveal 'toView'.
3290 * Assumes that the view to show is anchored at either the very top or very bottom
3291 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003292 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003293 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003294 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3295 showAppsCustomizeHelper(animated, springLoaded, contentType);
3296 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003297
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08003298 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Winson Chungc58497e2013-09-03 17:48:37 -07003299 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3300 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003301 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003302 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003303 mStateAnimation.cancel();
3304 mStateAnimation = null;
3305 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003306
Kenny Guyd794a3f2014-09-16 15:17:58 +01003307 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003308
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003309 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003310
Adam Cohen6c5891a2014-07-09 23:53:15 -07003311 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003312 final int itemsAlphaStagger =
3313 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003314
Michael Jurkabed61d22012-02-14 22:51:29 -08003315 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003316 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003317
Adam Cohen2854d252014-08-27 16:04:07 -07003318 final ArrayList<View> layerViews = new ArrayList<View>();
3319
Adam Cohen6c5891a2014-07-09 23:53:15 -07003320 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3321 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003322 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003323 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003324 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003325 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3326 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003327 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3328 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003329
Adam Cohena38dc902014-09-07 17:48:55 +02003330 // If for some reason our views aren't initialized, don't animate
3331 boolean initialized = getAllAppsButton() != null;
3332
3333 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003334 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003335 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3336 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003337
Adam Cohen9bfdb762014-07-21 17:44:06 -07003338 final View page = content.getPageAt(content.getCurrentPage());
3339 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003340
Adam Cohen63f1ec02014-08-12 09:23:13 -07003341 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3342 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003343 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3344 } else {
3345 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3346 }
3347
Adam Cohen9bfdb762014-07-21 17:44:06 -07003348 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003349 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003350 revealView.setVisibility(View.VISIBLE);
3351 // We need to hide this view as the animation start will be posted.
3352 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003353
Adam Cohen9bfdb762014-07-21 17:44:06 -07003354 int width = revealView.getMeasuredWidth();
3355 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003356 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003357
Adam Cohen63f1ec02014-08-12 09:23:13 -07003358 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003359 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003360
Adam Cohen63f1ec02014-08-12 09:23:13 -07003361 // Get the y delta between the center of the page and the center of the all apps button
3362 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3363 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003364
Adam Cohen2854d252014-08-27 16:04:07 -07003365 float alpha = 0;
3366 float xDrift = 0;
3367 float yDrift = 0;
3368 if (material) {
3369 alpha = isWidgetTray ? 0.3f : 1f;
3370 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3371 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3372 } else {
3373 yDrift = 2 * height / 3;
3374 xDrift = 0;
3375 }
3376 final float initAlpha = alpha;
3377
Adam Cohen9bfdb762014-07-21 17:44:06 -07003378 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003379 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003380 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003381 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003382 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003383 PropertyValuesHolder panelDriftX =
3384 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003385
Adam Cohen2854d252014-08-27 16:04:07 -07003386 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3387 panelAlpha, panelDriftY, panelDriftX);
3388
Adam Cohen9bfdb762014-07-21 17:44:06 -07003389 panelAlphaAndDrift.setDuration(revealDuration);
3390 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003391
Adam Cohen9bfdb762014-07-21 17:44:06 -07003392 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003393
Adam Cohen4e243a22014-08-10 18:30:55 -07003394 if (page != null) {
3395 page.setVisibility(View.VISIBLE);
3396 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003397 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003398
Adam Cohen2854d252014-08-27 16:04:07 -07003399 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3400 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003401 pageDrift.setDuration(revealDuration);
3402 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003403 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003404 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003405
Adam Cohen63f1ec02014-08-12 09:23:13 -07003406 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003407 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003408 itemsAlpha.setDuration(revealDuration);
3409 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3410 itemsAlpha.setStartDelay(itemsAlphaStagger);
3411 mStateAnimation.play(itemsAlpha);
3412 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003413
Adam Cohen4e243a22014-08-10 18:30:55 -07003414 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3415 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003416 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003417 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003418 indicatorsAlpha.setDuration(revealDuration);
3419 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003420
Adam Cohen9bfdb762014-07-21 17:44:06 -07003421 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003422 final View allApps = getAllAppsButton();
3423 int allAppsButtonSize = LauncherAppState.getInstance().
3424 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3425 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003426 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003427 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003428 reveal.setDuration(revealDuration);
3429 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003430
3431 reveal.addListener(new AnimatorListenerAdapter() {
3432 public void onAnimationStart(Animator animation) {
3433 if (!isWidgetTray) {
3434 allApps.setVisibility(View.INVISIBLE);
3435 }
3436 }
3437 public void onAnimationEnd(Animator animation) {
3438 if (!isWidgetTray) {
3439 allApps.setVisibility(View.VISIBLE);
3440 }
3441 }
3442 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003443 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003444 }
Michael Jurka1899a362011-11-03 13:50:45 -07003445
Adam Cohen9bfdb762014-07-21 17:44:06 -07003446 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3447 @Override
3448 public void onAnimationEnd(Animator animation) {
3449 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3450 dispatchOnLauncherTransitionEnd(toView, animated, false);
3451
3452 revealView.setVisibility(View.INVISIBLE);
3453 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003454 if (page != null) {
3455 page.setLayerType(View.LAYER_TYPE_NONE, null);
3456 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003457 content.setPageBackgroundsVisible(true);
3458
3459 // Hide the search bar
3460 if (mSearchDropTargetBar != null) {
3461 mSearchDropTargetBar.hideSearchBar(false);
3462 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003463
3464 // This can hold unnecessary references to views.
3465 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003466 }
3467
Adam Cohen9bfdb762014-07-21 17:44:06 -07003468 });
3469
Adam Cohen6c5891a2014-07-09 23:53:15 -07003470 if (workspaceAnim != null) {
3471 mStateAnimation.play(workspaceAnim);
3472 }
Adam Cohen2854d252014-08-27 16:04:07 -07003473
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003474 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3475 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003476 final AnimatorSet stateAnimation = mStateAnimation;
3477 final Runnable startAnimRunnable = new Runnable() {
3478 public void run() {
3479 // Check that mStateAnimation hasn't changed while
3480 // we waited for a layout/draw pass
3481 if (mStateAnimation != stateAnimation)
3482 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003483 dispatchOnLauncherTransitionStart(fromView, animated, false);
3484 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003485
3486 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003487 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003488 for (int i = 0; i < layerViews.size(); i++) {
3489 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003490 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003491 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003492 }
Adam Cohen2854d252014-08-27 16:04:07 -07003493 }
3494 }
3495 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003496 }
3497 };
Adam Cohen2854d252014-08-27 16:04:07 -07003498 toView.bringToFront();
3499 toView.setVisibility(View.VISIBLE);
3500 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003501 } else {
3502 toView.setTranslationX(0.0f);
3503 toView.setTranslationY(0.0f);
3504 toView.setScaleX(1.0f);
3505 toView.setScaleY(1.0f);
3506 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003507 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003508
Daniel Sandlere4f98912013-06-25 15:13:26 -04003509 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003510 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003511 if (mSearchDropTargetBar != null) {
3512 mSearchDropTargetBar.hideSearchBar(false);
3513 }
Michael Jurkaabded662011-03-04 12:06:57 -08003514 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003515 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003516 dispatchOnLauncherTransitionStart(fromView, animated, false);
3517 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003518 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003519 dispatchOnLauncherTransitionStart(toView, animated, false);
3520 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003521 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003522 }
3523
3524 /**
3525 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003526 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003527 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003528 */
Adam Cohened307df2013-10-02 09:37:31 -07003529 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003530 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003531
Michael Jurkab3e22d92011-10-31 15:58:33 -07003532 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003533 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003534 mStateAnimation.cancel();
3535 mStateAnimation = null;
3536 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003537
Kenny Guyd794a3f2014-09-16 15:17:58 +01003538 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003539 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003540
Adam Cohen63f1ec02014-08-12 09:23:13 -07003541 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003542 final int itemsAlphaStagger =
3543 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003544
Winson Chungf0ea4d32011-06-06 14:27:16 -07003545 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003546 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003547 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003548 final ArrayList<View> layerViews = new ArrayList<View>();
3549
Adam Cohened307df2013-10-02 09:37:31 -07003550 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003551 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003552 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003553 } else if (toState == Workspace.State.SPRING_LOADED ||
3554 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003555 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003556 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003557 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003558
Adam Cohena38dc902014-09-07 17:48:55 +02003559 // If for some reason our views aren't initialized, don't animate
3560 boolean initialized = getAllAppsButton() != null;
3561
3562 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003563 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003564 if (workspaceAnim != null) {
3565 mStateAnimation.play(workspaceAnim);
3566 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003567
Adam Cohen9bfdb762014-07-21 17:44:06 -07003568 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3569 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003570
Adam Cohen9bfdb762014-07-21 17:44:06 -07003571 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003572
3573 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3574 int count = content.getChildCount();
3575 for (int i = 0; i < count; i++) {
3576 View child = content.getChildAt(i);
3577 if (child != page) {
3578 child.setVisibility(View.INVISIBLE);
3579 }
3580 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003581 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003582
Adam Cohen2854d252014-08-27 16:04:07 -07003583 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3584 // don't perform all these no-op animations. In particularly, this was causing
3585 // the all-apps button to pop in and out.
3586 if (fromView.getVisibility() == View.VISIBLE) {
3587 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3588 final boolean isWidgetTray =
3589 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003590
Adam Cohen2854d252014-08-27 16:04:07 -07003591 if (isWidgetTray) {
3592 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3593 } else {
3594 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003595 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003596
Adam Cohen2854d252014-08-27 16:04:07 -07003597 int width = revealView.getMeasuredWidth();
3598 int height = revealView.getMeasuredHeight();
3599 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3600
3601 // Hide the real page background, and swap in the fake one
3602 revealView.setVisibility(View.VISIBLE);
3603 content.setPageBackgroundsVisible(false);
3604
3605 final View allAppsButton = getAllAppsButton();
3606 revealView.setTranslationY(0);
3607 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3608 allAppsButton, null);
3609
3610 float xDrift = 0;
3611 float yDrift = 0;
3612 if (material) {
3613 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3614 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3615 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003616 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003617 xDrift = 0;
3618 }
3619
3620 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3621 TimeInterpolator decelerateInterpolator = material ?
3622 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003623 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003624
3625 // The vertical motion of the apps panel should be delayed by one frame
3626 // from the conceal animation in order to give the right feel. We correpsondingly
3627 // shorten the duration so that the slide and conceal end at the same time.
3628 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3629 0, yDrift);
3630 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3631 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3632 panelDriftY.setInterpolator(decelerateInterpolator);
3633 mStateAnimation.play(panelDriftY);
3634
3635 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3636 0, xDrift);
3637 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3638 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3639 panelDriftX.setInterpolator(decelerateInterpolator);
3640 mStateAnimation.play(panelDriftX);
3641
3642 if (isWidgetTray || !material) {
3643 float finalAlpha = material ? 0.4f : 0f;
3644 revealView.setAlpha(1f);
3645 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3646 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003647 panelAlpha.setDuration(material ? revealDuration : 150);
3648 panelAlpha.setInterpolator(decelerateInterpolator);
3649 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003650 mStateAnimation.play(panelAlpha);
3651 }
3652
3653 if (page != null) {
3654 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3655
3656 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3657 0, yDrift);
3658 page.setTranslationY(0);
3659 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3660 pageDrift.setInterpolator(decelerateInterpolator);
3661 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3662 mStateAnimation.play(pageDrift);
3663
3664 page.setAlpha(1f);
3665 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3666 itemsAlpha.setDuration(100);
3667 itemsAlpha.setInterpolator(decelerateInterpolator);
3668 mStateAnimation.play(itemsAlpha);
3669 }
3670
3671 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3672 pageIndicators.setAlpha(1f);
3673 ObjectAnimator indicatorsAlpha =
3674 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3675 indicatorsAlpha.setDuration(revealDuration);
3676 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3677 mStateAnimation.play(indicatorsAlpha);
3678
3679 width = revealView.getMeasuredWidth();
3680
3681 if (material) {
3682 if (!isWidgetTray) {
3683 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003684 }
Adam Cohen2854d252014-08-27 16:04:07 -07003685 int allAppsButtonSize = LauncherAppState.getInstance().
3686 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3687 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3688 Animator reveal =
3689 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3690 height / 2, revealRadius, finalRadius);
3691 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3692 reveal.setDuration(revealDuration);
3693 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003694
Adam Cohen2854d252014-08-27 16:04:07 -07003695 reveal.addListener(new AnimatorListenerAdapter() {
3696 public void onAnimationEnd(Animator animation) {
3697 revealView.setVisibility(View.INVISIBLE);
3698 if (!isWidgetTray) {
3699 allAppsButton.setVisibility(View.VISIBLE);
3700 }
3701 }
3702 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003703
Adam Cohen2854d252014-08-27 16:04:07 -07003704 mStateAnimation.play(reveal);
3705 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003706
Adam Cohen2854d252014-08-27 16:04:07 -07003707 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3708 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3709 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003710 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003711
3712 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003713 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003714 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003715 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003716 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3717 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003718 if (onCompleteRunnable != null) {
3719 onCompleteRunnable.run();
3720 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003721
3722 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003723 if (page != null) {
3724 page.setLayerType(View.LAYER_TYPE_NONE, null);
3725 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003726 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003727 // Unhide side pages
3728 int count = content.getChildCount();
3729 for (int i = 0; i < count; i++) {
3730 View child = content.getChildAt(i);
3731 child.setVisibility(View.VISIBLE);
3732 }
3733
3734 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003735 if (page != null) {
3736 page.setTranslationX(0);
3737 page.setTranslationY(0);
3738 page.setAlpha(1);
3739 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003740 content.setCurrentPage(content.getNextPage());
3741
Chet Haasebc2f0822012-10-26 17:59:53 -07003742 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003743
3744 // This can hold unnecessary references to views.
3745 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003746 }
3747 });
3748
Adam Cohen2854d252014-08-27 16:04:07 -07003749 final AnimatorSet stateAnimation = mStateAnimation;
3750 final Runnable startAnimRunnable = new Runnable() {
3751 public void run() {
3752 // Check that mStateAnimation hasn't changed while
3753 // we waited for a layout/draw pass
3754 if (mStateAnimation != stateAnimation)
3755 return;
3756 dispatchOnLauncherTransitionStart(fromView, animated, false);
3757 dispatchOnLauncherTransitionStart(toView, animated, false);
3758
Kenny Guyd794a3f2014-09-16 15:17:58 +01003759 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003760 for (int i = 0; i < layerViews.size(); i++) {
3761 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003762 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003763 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003764 }
Adam Cohen2854d252014-08-27 16:04:07 -07003765 }
3766 }
3767 mStateAnimation.start();
3768 }
3769 };
3770 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003771 } else {
3772 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003773 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003774 dispatchOnLauncherTransitionStart(fromView, animated, true);
3775 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003776 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003777 dispatchOnLauncherTransitionStart(toView, animated, true);
3778 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003779 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003780 }
3781
Michael Jurkae326f182011-11-21 14:05:46 -08003782 @Override
3783 public void onTrimMemory(int level) {
3784 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003785 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3786 // The widget preview db can result in holding onto over
3787 // 3MB of memory for caching which isn't necessary.
3788 SQLiteDatabase.releaseMemory();
3789
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003790 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003791 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003792 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003793 }
Michael Jurkae326f182011-11-21 14:05:46 -08003794 }
3795 }
3796
Adam Cohened307df2013-10-02 09:37:31 -07003797 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003798 showWorkspace(animated, null);
3799 }
3800
Adam Cohened307df2013-10-02 09:37:31 -07003801 protected void showWorkspace() {
3802 showWorkspace(true);
3803 }
3804
Adam Cohened66b2b2012-01-23 17:28:51 -08003805 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003806 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003807 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003808 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003809 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003810
Winson Chungc7d2b602012-05-16 17:02:20 -07003811 // Show the search bar (only animate if we were showing the drop target bar in spring
3812 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003813 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003814 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003815 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003816
Michael Jurkab3e22d92011-10-31 15:58:33 -07003817 // Set focus to the AppsCustomize button
3818 if (mAllAppsButton != null) {
3819 mAllAppsButton.requestFocus();
3820 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003821 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003822
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003823 // Change the state *after* we've called all the transition code
3824 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003825
Winson Chung5fb63472011-02-02 17:03:37 -08003826 // Resume the auto-advance of widgets
3827 mUserPresent = true;
3828 updateRunning();
3829
alanv1d4fde62012-10-17 13:15:47 -07003830 // Send an accessibility event to announce the context change
3831 getWindow().getDecorView()
3832 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003833
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003834 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003835 }
3836
Adam Cohened307df2013-10-02 09:37:31 -07003837 void showOverviewMode(boolean animated) {
3838 mWorkspace.setVisibility(View.VISIBLE);
3839 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3840 mState = State.WORKSPACE;
3841 onWorkspaceShown(animated);
3842 }
3843
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003844 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003845 }
3846
Winson Chung82963d52013-10-09 11:20:57 -07003847 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3848 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003849 if (mState != State.WORKSPACE) return;
3850
Winson Chung82963d52013-10-09 11:20:57 -07003851 if (resetPageToZero) {
3852 mAppsCustomizeTabHost.reset();
3853 }
Winson Chungc58497e2013-09-03 17:48:37 -07003854 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003855 mAppsCustomizeTabHost.post(new Runnable() {
3856 @Override
3857 public void run() {
3858 // We post this in-case the all apps view isn't yet constructed.
3859 mAppsCustomizeTabHost.requestFocus();
3860 }
3861 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003862
Michael Jurkab3e22d92011-10-31 15:58:33 -07003863 // Change the state *after* we've called all the transition code
3864 mState = State.APPS_CUSTOMIZE;
3865
3866 // Pause the auto-advance of widgets until we are out of AllApps
3867 mUserPresent = false;
3868 updateRunning();
3869 closeFolder();
3870
3871 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003872 getWindow().getDecorView()
3873 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003874 }
3875
Adam Cohen7777d962011-08-18 18:58:38 -07003876 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003877 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003878 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003879 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003880 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003881 }
Adam Cohen7777d962011-08-18 18:58:38 -07003882
Adam Cohenad4e15c2013-10-17 16:21:35 -07003883 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003884 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003885 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3886
Winson Chunge7a03942011-08-05 15:05:12 -07003887 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003888 @Override
3889 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003890 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003891 // Before we show workspace, hide all apps again because
3892 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3893 // clean up our state transition functions
3894 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003895 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003896 } else {
3897 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003898 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003899 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003900 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003901 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003902
Michael Jurkad3ef3062010-11-23 16:23:58 -08003903 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003904 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003905 final boolean animated = true;
3906 final boolean springLoaded = true;
3907 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003908 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003909 }
3910 // Otherwise, we are not in spring loaded mode, so don't do anything.
3911 }
3912
Michael Jurkab3e22d92011-10-31 15:58:33 -07003913 void lockAllApps() {
3914 // TODO
3915 }
3916
3917 void unlockAllApps() {
3918 // TODO
3919 }
3920
Sunny Goyal594d76d2014-11-06 10:12:54 -08003921 protected void disableVoiceButtonProxy(boolean disable) {
3922 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003923 }
3924
Cristina Stancu476493b2013-08-07 17:20:14 +01003925 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003926 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3927 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003928 }
3929
Adam Cohen24ce0b32014-01-14 16:18:14 -08003930 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003931 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3932 if (searchProvider == null) {
3933 return null;
3934 }
3935
3936 Bundle opts = new Bundle();
3937 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
3938 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
3939
3940 SharedPreferences sp = getSharedPreferences(
3941 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3942 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003943 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003944 if (!searchProvider.provider.flattenToString().equals(
3945 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003946 || (widgetInfo == null)
3947 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003948 // A valid widget is not already bound.
3949 if (widgetId > -1) {
3950 mAppWidgetHost.deleteAppWidgetId(widgetId);
3951 widgetId = -1;
3952 }
3953
3954 // Try to bind a new widget
3955 widgetId = mAppWidgetHost.allocateAppWidgetId();
3956
3957 if (!AppWidgetManagerCompat.getInstance(this)
3958 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3959 mAppWidgetHost.deleteAppWidgetId(widgetId);
3960 widgetId = -1;
3961 }
3962
3963 sp.edit()
3964 .putInt(QSB_WIDGET_ID, widgetId)
3965 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3966 .commit();
3967 }
3968
3969 if (widgetId != -1) {
3970 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3971 mQsb.updateAppWidgetOptions(opts);
3972 mQsb.setPadding(0, 0, 0, 0);
3973 mSearchDropTargetBar.addView(mQsb);
3974 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003975 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003976 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003977 }
3978
Michael Jurkad7c28052012-04-27 15:43:36 -07003979 @Override
3980 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003981 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003982 final List<CharSequence> text = event.getText();
3983 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003984 // Populate event with a fake title based on the current state.
3985 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07003986 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07003987 } else {
3988 text.add(getString(R.string.all_apps_home_button_label));
3989 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003990 return result;
3991 }
3992
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003993 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003994 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003995 */
3996 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3997 @Override
3998 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003999 closeSystemDialogs();
4000 }
4001 }
4002
4003 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004004 * Receives notifications whenever the appwidgets are reset.
4005 */
4006 private class AppWidgetResetObserver extends ContentObserver {
4007 public AppWidgetResetObserver() {
4008 super(new Handler());
4009 }
4010
4011 @Override
4012 public void onChange(boolean selfChange) {
4013 onAppWidgetReset();
4014 }
4015 }
4016
4017 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004018 * If the activity is currently paused, signal that we need to run the passed Runnable
4019 * in onResume.
4020 *
4021 * This needs to be called from incoming places where resources might have been loaded
4022 * while we are paused. That is becaues the Configuration might be wrong
4023 * when we're not running, and if it comes back to what it was when we
4024 * were paused, we are not restarted.
4025 *
4026 * Implementation of the method from LauncherModel.Callbacks.
4027 *
4028 * @return true if we are currently paused. The caller might be able to
4029 * skip some work in that case since we will come back again.
4030 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004031 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004032 if (mPaused) {
4033 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004034 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004035 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004036 }
4037 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004038 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004039 return true;
4040 } else {
4041 return false;
4042 }
4043 }
4044
Michael Jurkac402cd92013-05-20 15:49:32 +02004045 private boolean waitUntilResume(Runnable run) {
4046 return waitUntilResume(run, false);
4047 }
4048
Michael Jurka1e2f4652013-07-08 18:03:46 -07004049 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004050 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004051 }
4052
Michael Jurka7607c2f2013-04-03 14:33:19 -07004053 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004054 * If the activity is currently paused, signal that we need to re-run the loader
4055 * in onResume.
4056 *
4057 * This needs to be called from incoming places where resources might have been loaded
4058 * while we are paused. That is becaues the Configuration might be wrong
4059 * when we're not running, and if it comes back to what it was when we
4060 * were paused, we are not restarted.
4061 *
4062 * Implementation of the method from LauncherModel.Callbacks.
4063 *
4064 * @return true if we are currently paused. The caller might be able to
4065 * skip some work in that case since we will come back again.
4066 */
4067 public boolean setLoadOnResume() {
4068 if (mPaused) {
4069 Log.i(TAG, "setLoadOnResume");
4070 mOnResumeNeedsLoad = true;
4071 return true;
4072 } else {
4073 return false;
4074 }
4075 }
4076
4077 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004078 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004079 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004080 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004081 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004082 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004083 } else {
4084 return SCREEN_COUNT / 2;
4085 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004086 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004087
Joe Onorato9c1289c2009-08-17 11:03:03 -04004088 /**
4089 * Refreshes the shortcuts shown on the workspace.
4090 *
4091 * Implementation of the method from LauncherModel.Callbacks.
4092 */
4093 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004094 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004095
Michael Jurka7607c2f2013-04-03 14:33:19 -07004096 // If we're starting binding all over again, clear any bind calls we'd postponed in
4097 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4098 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004099 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004100
Winson Chungd64d1762013-08-20 14:37:16 -07004101 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004102 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004103 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004104
Michael Jurka05bf6442011-11-30 20:28:53 -08004105 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004106 if (mHotseat != null) {
4107 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004108 }
4109 }
4110
Adam Cohendcd297f2013-06-18 13:13:40 -07004111 @Override
4112 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004113 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004114
Adam Cohen5084cba2013-09-03 12:01:16 -07004115 // If there are no screens, we need to have an empty screen
4116 if (orderedScreenIds.size() == 0) {
4117 mWorkspace.addExtraEmptyScreen();
4118 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004119
4120 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004121 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004122 if (hasCustomContentToLeft()) {
4123 mWorkspace.createCustomContentContainer();
4124 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004125 }
Winson Chung64359a52013-07-08 17:17:08 -07004126 }
4127
4128 @Override
4129 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004130 // Log to disk
4131 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4132 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4133 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004134 int count = orderedScreenIds.size();
4135 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004136 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004137 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004138 }
4139
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08004140 @Override
4141 public void bindAddPendingItem(final PendingAddItemInfo info, final long container,
4142 final long screenId, final int[] cell, final int spanX, final int spanY) {
4143 showWorkspace(true, new Runnable() {
4144
4145 @Override
4146 public void run() {
4147 mWorkspace.snapToPage(mWorkspace.getPageIndexForScreenId(screenId));
4148 addPendingItem(info, container, screenId, cell, spanX, spanY);
4149 }
4150 });
4151 }
4152
Adam Cohen39a06042013-07-19 14:30:12 -07004153 private boolean shouldShowWeightWatcher() {
4154 String spKey = LauncherAppState.getSharedPreferencesKey();
4155 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004156 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004157
4158 return show;
4159 }
4160
4161 private void toggleShowWeightWatcher() {
4162 String spKey = LauncherAppState.getSharedPreferencesKey();
4163 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4164 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4165
4166 show = !show;
4167
4168 SharedPreferences.Editor editor = sp.edit();
4169 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4170 editor.commit();
4171
4172 if (mWeightWatcher != null) {
4173 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4174 }
4175 }
4176
Winson Chungd64d1762013-08-20 14:37:16 -07004177 public void bindAppsAdded(final ArrayList<Long> newScreens,
4178 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004179 final ArrayList<ItemInfo> addAnimated,
4180 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004181 Runnable r = new Runnable() {
4182 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004183 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004184 }
4185 };
4186 if (waitUntilResume(r)) {
4187 return;
4188 }
4189
Winson Chungd64d1762013-08-20 14:37:16 -07004190 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004191 if (newScreens != null) {
4192 bindAddScreens(newScreens);
4193 }
Winson Chungd64d1762013-08-20 14:37:16 -07004194
4195 // We add the items without animation on non-visible pages, and with
4196 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004197 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004198 bindItems(addNotAnimated, 0,
4199 addNotAnimated.size(), false);
4200 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004201 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004202 bindItems(addAnimated, 0,
4203 addAnimated.size(), true);
4204 }
Winson Chungc58497e2013-09-03 17:48:37 -07004205
Winson Chung87412982013-10-03 18:34:14 -07004206 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004207 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004208
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004209 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004210 addedApps != null && mAppsCustomizeContent != null) {
4211 mAppsCustomizeContent.addApps(addedApps);
4212 }
Winson Chungd64d1762013-08-20 14:37:16 -07004213 }
4214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004215 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004216 * Bind the items start-end from the list.
4217 *
4218 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004219 */
Winson Chung64359a52013-07-08 17:17:08 -07004220 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4221 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004222 Runnable r = new Runnable() {
4223 public void run() {
4224 bindItems(shortcuts, start, end, forceAnimateIcons);
4225 }
4226 };
4227 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004228 return;
4229 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004230
Winson Chungf0c6ae02012-03-21 16:10:31 -07004231 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004232 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4233 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004234 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004235 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004236 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004237 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004238 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004239
4240 // Short circuit if we are loading dock items for a configuration which has no dock
4241 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4242 mHotseat == null) {
4243 continue;
4244 }
4245
Joe Onorato9c1289c2009-08-17 11:03:03 -04004246 switch (item.itemType) {
4247 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4248 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004249 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004250 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004251
Winson Chung64359a52013-07-08 17:17:08 -07004252 /*
4253 * TODO: FIX collision case
4254 */
Winson Chungce376632013-07-11 16:12:41 -07004255 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4256 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4257 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004258 View v = cl.getChildAt(item.cellX, item.cellY);
4259 Object tag = v.getTag();
4260 String desc = "Collision while binding workspace item: " + item
4261 + ". Collides with " + tag;
4262 if (LauncherAppState.isDogfoodBuild()) {
4263 throw (new RuntimeException(desc));
4264 } else {
4265 Log.d(TAG, desc);
4266 }
Winson Chungce376632013-07-11 16:12:41 -07004267 }
Winson Chung64359a52013-07-08 17:17:08 -07004268 }
4269
Adam Cohendcd297f2013-06-18 13:13:40 -07004270 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4271 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004272 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004273 // Animate all the applications up now
4274 shortcut.setAlpha(0f);
4275 shortcut.setScaleX(0f);
4276 shortcut.setScaleY(0f);
4277 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004278 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004279 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004280 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004281 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004282 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004283 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004284 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004285 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4286 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004287 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004288 default:
4289 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004290 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004291 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004292
Winson Chung997a9232013-07-24 15:33:46 -07004293 if (animateIcons) {
4294 // Animate to the correct page
4295 if (newShortcutsScreenId > -1) {
4296 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004297 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004298 final Runnable startBounceAnimRunnable = new Runnable() {
4299 public void run() {
4300 anim.playTogether(bounceAnims);
4301 anim.start();
4302 }
4303 };
Winson Chung997a9232013-07-24 15:33:46 -07004304 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004305 // We post the animation slightly delayed to prevent slowdowns
4306 // when we are loading right after we return to launcher.
4307 mWorkspace.postDelayed(new Runnable() {
4308 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004309 if (mWorkspace != null) {
4310 mWorkspace.snapToPage(newScreenIndex);
4311 mWorkspace.postDelayed(startBounceAnimRunnable,
4312 NEW_APPS_ANIMATION_DELAY);
4313 }
Winson Chung94d67682013-09-25 16:29:40 -07004314 }
4315 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004316 } else {
4317 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004318 }
4319 }
Winson Chung64359a52013-07-08 17:17:08 -07004320 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004321 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004322 }
4323
Joe Onorato9c1289c2009-08-17 11:03:03 -04004324 /**
4325 * Implementation of the method from LauncherModel.Callbacks.
4326 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004327 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004328 Runnable r = new Runnable() {
4329 public void run() {
4330 bindFolders(folders);
4331 }
4332 };
4333 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004334 return;
4335 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004336 sFolders.clear();
4337 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004339
4340 /**
4341 * Add the views for a widget to the workspace.
4342 *
4343 * Implementation of the method from LauncherModel.Callbacks.
4344 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004345 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004346 Runnable r = new Runnable() {
4347 public void run() {
4348 bindAppWidget(item);
4349 }
4350 };
4351 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004352 return;
4353 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004354
Daniel Sandler843e8602010-06-07 14:59:01 -04004355 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4356 if (DEBUG_WIDGETS) {
4357 Log.d(TAG, "bindAppWidget: " + item);
4358 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004359 final Workspace workspace = mWorkspace;
4360
Adam Cohen59400422014-03-05 18:07:04 -08004361 LauncherAppWidgetProviderInfo appWidgetInfo =
4362 LauncherModel.getProviderInfo(this, item.providerName);
4363
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004364 if (!mIsSafeModeEnabled
4365 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4366 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004367 if (appWidgetInfo == null) {
4368 if (DEBUG_WIDGETS) {
4369 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4370 + " belongs to component " + item.providerName
4371 + ", as the povider is null");
4372 }
4373 LauncherModel.deleteItemFromDatabase(this, item);
4374 return;
4375 }
4376 // Note: This assumes that the id remap broadcast is received before this step.
4377 // If that is not the case, the id remap will be ignored and user may see the
4378 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004379 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004380 pendingInfo.spanX = item.spanX;
4381 pendingInfo.spanY = item.spanY;
4382 pendingInfo.minSpanX = item.minSpanX;
4383 pendingInfo.minSpanY = item.minSpanY;
4384 Bundle options =
4385 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4386
Sunny Goyalff572272014-07-23 13:58:07 -07004387 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004388 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4389 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004390
4391 // TODO consider showing a permission dialog when the widget is clicked.
4392 if (!success) {
4393 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4394 if (DEBUG_WIDGETS) {
4395 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4396 + " belongs to component " + item.providerName
4397 + ", as the launcher is unable to bing a new widget id");
4398 }
4399 LauncherModel.deleteItemFromDatabase(this, item);
4400 return;
4401 }
4402
4403 item.appWidgetId = newWidgetId;
4404
4405 // If the widget has a configure activity, it is still needs to set it up, otherwise
4406 // the widget is ready to go.
4407 item.restoreStatus = (appWidgetInfo.configure == null)
4408 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4409 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4410
4411 LauncherModel.updateItemInDatabase(this, item);
4412 }
4413
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004414 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004415 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004416 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004417 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4418 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004419 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004420
Sunny Goyal651077b2014-06-30 14:15:31 -07004421 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4422 } else {
4423 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004424 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4425 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004426 view.updateIcon(mIconCache);
4427 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004428 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004429 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004430 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004431
Joe Onorato9c1289c2009-08-17 11:03:03 -04004432 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004433 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004434
Adam Cohendcd297f2013-06-18 13:13:40 -07004435 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004436 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004437 if (!item.isCustomWidget()) {
4438 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4439 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004440
Joe Onorato9c1289c2009-08-17 11:03:03 -04004441 workspace.requestLayout();
4442
Daniel Sandler843e8602010-06-07 14:59:01 -04004443 if (DEBUG_WIDGETS) {
4444 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4445 + (SystemClock.uptimeMillis()-start) + "ms");
4446 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004447 }
4448
Sunny Goyalff572272014-07-23 13:58:07 -07004449 /**
4450 * Restores a pending widget.
4451 *
4452 * @param appWidgetId The app widget id
4453 * @param cellInfo The position on screen where to create the widget.
4454 */
4455 private void completeRestoreAppWidget(final int appWidgetId) {
4456 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4457 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4458 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4459 return;
4460 }
4461
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004462 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004463 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4464
4465 mWorkspace.reinflateWidgetsIfNecessary();
4466 LauncherModel.updateItemInDatabase(this, info);
4467 }
4468
Adam Cohen1462de32012-07-24 22:34:36 -07004469 public void onPageBoundSynchronously(int page) {
4470 mSynchronouslyBoundPages.add(page);
4471 }
4472
Joe Onorato9c1289c2009-08-17 11:03:03 -04004473 /**
4474 * Callback saying that there aren't any more items to bind.
4475 *
4476 * Implementation of the method from LauncherModel.Callbacks.
4477 */
Adam Cohene25af792013-06-06 23:08:25 -07004478 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004479 Runnable r = new Runnable() {
4480 public void run() {
4481 finishBindingItems(upgradePath);
4482 }
4483 };
4484 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004485 return;
4486 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004487 if (mSavedState != null) {
4488 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004489 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004490 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004491 mSavedState = null;
4492 }
4493
Adam Cohen1462de32012-07-24 22:34:36 -07004494 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004495
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004496 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004497 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004498
4499 // If we received the result of any pending adds while the loader was running (e.g. the
4500 // widget configuration forced an orientation change), process them now.
4501 if (sPendingAddItem != null) {
4502 final long screenId = completeAdd(sPendingAddItem);
4503
4504 // TODO: this moves the user to the page where the pending item was added. Ideally,
4505 // the screen would be guaranteed to exist after bind, and the page would be set through
4506 // the workspace restore process.
4507 mWorkspace.post(new Runnable() {
4508 @Override
4509 public void run() {
4510 mWorkspace.snapToScreenId(screenId);
4511 }
4512 });
4513 sPendingAddItem = null;
4514 }
4515
Adam Cohene25af792013-06-06 23:08:25 -07004516 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004517 mWorkspace.getUniqueComponents(true, null);
4518 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004519 }
Sunny Goyale755d462014-07-22 13:48:29 -07004520 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004521
4522 if (mLauncherCallbacks != null) {
4523 mLauncherCallbacks.finishBindingItems(upgradePath);
4524 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004525 }
4526
Adam Cohen3ed316a2014-07-23 18:21:20 -07004527 private void sendLoadingCompleteBroadcastIfNecessary() {
4528 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4529 String permission =
4530 getResources().getString(R.string.receive_first_load_broadcast_permission);
4531 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4532 sendBroadcast(intent, permission);
4533 SharedPreferences.Editor editor = mSharedPrefs.edit();
4534 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4535 editor.apply();
4536 }
4537 }
4538
Winson Chunga0b7e862013-09-05 16:03:15 -07004539 public boolean isAllAppsButtonRank(int rank) {
4540 if (mHotseat != null) {
4541 return mHotseat.isAllAppsButtonRank(rank);
4542 }
4543 return false;
4544 }
4545
Winson Chunga2413752012-04-03 14:22:34 -07004546 private boolean canRunNewAppsAnimation() {
4547 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4548 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4549 }
4550
Winson Chungc9168342013-06-26 14:54:55 -07004551 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4552 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4553 PropertyValuesHolder.ofFloat("alpha", 1f),
4554 PropertyValuesHolder.ofFloat("scaleX", 1f),
4555 PropertyValuesHolder.ofFloat("scaleY", 1f));
4556 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4557 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4558 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4559 return bounceAnim;
4560 }
4561
Adam Cohen27772732013-11-11 14:00:56 +00004562 public boolean useVerticalBarLayout() {
4563 return LauncherAppState.getInstance().getDynamicGrid().
4564 getDeviceProfile().isVerticalBarLayout();
4565 }
4566
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004567 protected Rect getSearchBarBounds() {
4568 return LauncherAppState.getInstance().getDynamicGrid().
4569 getDeviceProfile().getSearchBarBounds();
4570 }
4571
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004572 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004573 if (mSearchDropTargetBar == null) {
4574 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004575 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004576 if (mQsb != null) {
4577 mSearchDropTargetBar.removeView(mQsb);
4578 mQsb = null;
4579 }
4580 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004581 }
4582
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004583 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004584 * Add the icons for all apps.
4585 *
4586 * Implementation of the method from LauncherModel.Callbacks.
4587 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004588 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004589 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004590 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4591 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4592 getHotseat().addAllAppsFolder(mIconCache, apps,
4593 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4594 }
4595 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004596 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004597 if (mAppsCustomizeContent != null) {
4598 mAppsCustomizeContent.onPackagesUpdated(
4599 LauncherModel.getSortedWidgetsAndShortcuts(this));
4600 }
Winson Chungc58497e2013-09-03 17:48:37 -07004601 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004602 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004603 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004604 mAppsCustomizeContent.onPackagesUpdated(
4605 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004606 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004607 }
Adam Cohen9211d422014-10-07 18:14:53 -07004608 if (mLauncherCallbacks != null) {
4609 mLauncherCallbacks.bindAllApplications(apps);
4610 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004611 }
4612
4613 /**
4614 * A package was updated.
4615 *
4616 * Implementation of the method from LauncherModel.Callbacks.
4617 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004618 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004619 Runnable r = new Runnable() {
4620 public void run() {
4621 bindAppsUpdated(apps);
4622 }
4623 };
4624 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004625 return;
4626 }
4627
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004628 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004629 mAppsCustomizeContent != null) {
4630 mAppsCustomizeContent.updateApps(apps);
4631 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004632 }
4633
Sunny Goyal4390ace2014-10-13 11:33:11 -07004634 @Override
4635 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4636 Runnable r = new Runnable() {
4637 public void run() {
4638 bindWidgetsRestored(widgets);
4639 }
4640 };
4641 if (waitUntilResume(r)) {
4642 return;
4643 }
4644 mWorkspace.widgetsRestored(widgets);
4645 }
4646
Joe Onorato9c1289c2009-08-17 11:03:03 -04004647 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004648 * Some shortcuts were updated in the background.
4649 *
4650 * Implementation of the method from LauncherModel.Callbacks.
4651 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004652 @Override
4653 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4654 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004655 Runnable r = new Runnable() {
4656 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004657 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004658 }
4659 };
4660 if (waitUntilResume(r)) {
4661 return;
4662 }
4663
Sunny Goyal4390ace2014-10-13 11:33:11 -07004664 if (!updated.isEmpty()) {
4665 mWorkspace.updateShortcuts(updated);
4666 }
4667
4668 if (!removed.isEmpty()) {
4669 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4670 for (ShortcutInfo si : removed) {
4671 removedComponents.add(si.getTargetComponent());
4672 }
4673 mWorkspace.removeItemsByComponentName(removedComponents, user);
4674 // Notify the drag controller
4675 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004676 }
4677 }
4678
4679 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004680 * Update the state of a package, typically related to install state.
4681 *
4682 * Implementation of the method from LauncherModel.Callbacks.
4683 */
Sunny Goyale755d462014-07-22 13:48:29 -07004684 @Override
4685 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004686 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004687 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004688 }
4689 }
4690
4691 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004692 * Update the label and icon of all the icons in a package
4693 *
4694 * Implementation of the method from LauncherModel.Callbacks.
4695 */
4696 @Override
4697 public void updatePackageBadge(String packageName) {
4698 if (mWorkspace != null) {
4699 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4700 }
4701 }
4702
4703 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004704 * A package was uninstalled. We take both the super set of packageNames
4705 * in addition to specific applications to remove, the reason being that
4706 * this can be called when a package is updated as well. In that scenario,
4707 * we only remove specific components from the workspace, where as
4708 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004709 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004710 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004711 * Implementation of the method from LauncherModel.Callbacks.
4712 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004713 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004714 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004715 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004716 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004717 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004718 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004719 }
Winson Chungd64d1762013-08-20 14:37:16 -07004720 };
4721 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004722 return;
4723 }
4724
Sunny Goyal1a745e82014-10-02 15:58:31 -07004725 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004726 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4727 for (AppInfo info : appInfos) {
4728 removedComponents.add(info.componentName);
4729 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004730 if (!packageNames.isEmpty()) {
4731 mWorkspace.removeItemsByPackageName(packageNames, user);
4732 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004733 if (!removedComponents.isEmpty()) {
4734 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004735 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004736 // Notify the drag controller
4737 mDragController.onAppsRemoved(packageNames, removedComponents);
4738
Sunny Goyal1a745e82014-10-02 15:58:31 -07004739 } else {
4740 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004741 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004742
Winson Chungdf95eb12013-10-16 14:57:07 -07004743 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004744 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004745 mAppsCustomizeContent != null) {
4746 mAppsCustomizeContent.removeApps(appInfos);
4747 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004748 }
Joe Onoratobe386092009-11-17 17:32:16 -08004749
4750 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004751 * A number of packages were updated.
4752 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004753 private ArrayList<Object> mWidgetsAndShortcuts;
4754 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004755 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004756 bindPackagesUpdated(mWidgetsAndShortcuts);
4757 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004758 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004759 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004760 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4761 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4762 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004763 return;
4764 }
4765
Winson Chung64359a52013-07-08 17:17:08 -07004766 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004767 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004768 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004769 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004770 }
4771
Winson Chung400438b2011-01-16 17:53:48 -08004772 private int mapConfigurationOriActivityInfoOri(int configOri) {
4773 final Display d = getWindowManager().getDefaultDisplay();
4774 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4775 switch (d.getRotation()) {
4776 case Surface.ROTATION_0:
4777 case Surface.ROTATION_180:
4778 // We are currently in the same basic orientation as the natural orientation
4779 naturalOri = configOri;
4780 break;
4781 case Surface.ROTATION_90:
4782 case Surface.ROTATION_270:
4783 // We are currently in the other basic orientation to the natural orientation
4784 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4785 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4786 break;
4787 }
4788
4789 int[] oriMap = {
4790 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4791 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4792 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4793 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4794 };
4795 // Since the map starts at portrait, we need to offset if this device's natural orientation
4796 // is landscape.
4797 int indexOffset = 0;
4798 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4799 indexOffset = 1;
4800 }
4801 return oriMap[(d.getRotation() + indexOffset) % 4];
4802 }
Adam Cohen446e9402011-09-15 18:21:21 -07004803
Winson Chung4b919f82012-05-01 10:44:08 -07004804 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004805 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004806 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4807 .getConfiguration().orientation));
4808 }
4809 }
4810 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004811 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004812 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004813 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004814 } else {
4815 mHandler.postDelayed(new Runnable() {
4816 public void run() {
4817 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4818 }
4819 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004820 }
Winson Chung4b919f82012-05-01 10:44:08 -07004821 }
Winson Chung400438b2011-01-16 17:53:48 -08004822 }
4823
Winson Chunge43a1e72014-01-15 10:33:02 -08004824 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004825 if (mLauncherCallbacks != null) {
4826 return mLauncherCallbacks.isLauncherPreinstalled();
4827 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004828 PackageManager pm = getPackageManager();
4829 try {
4830 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4831 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4832 return true;
4833 } else {
4834 return false;
4835 }
4836 } catch (NameNotFoundException e) {
4837 e.printStackTrace();
4838 return false;
4839 }
4840 }
4841
Adam Cohenea90f832014-05-21 15:03:34 -07004842 /**
4843 * This method indicates whether or not we should suggest default wallpaper dimensions
4844 * when our wallpaper cropper was not yet used to set a wallpaper.
4845 */
4846 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004847 if (mLauncherCallbacks != null) {
4848 return mLauncherCallbacks.overrideWallpaperDimensions();
4849 }
Adam Cohenea90f832014-05-21 15:03:34 -07004850 return true;
4851 }
4852
Adam Cohen432609a2014-03-13 17:03:22 -07004853 /**
4854 * To be overridden by subclasses to indicate that there is an activity to launch
4855 * before showing the standard launcher experience.
4856 */
4857 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004858 if (mLauncherCallbacks != null) {
4859 return mLauncherCallbacks.hasFirstRunActivity();
4860 }
Adam Cohen432609a2014-03-13 17:03:22 -07004861 return false;
4862 }
4863
4864 /**
4865 * To be overridden by subclasses to launch any first run activity
4866 */
4867 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004868 if (mLauncherCallbacks != null) {
4869 return mLauncherCallbacks.getFirstRunActivity();
4870 }
Adam Cohen432609a2014-03-13 17:03:22 -07004871 return null;
4872 }
4873
Winson Chunga6945242014-01-08 14:04:34 -08004874 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004875 return !ActivityManager.isRunningInTestHarness() &&
4876 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004877 }
4878
Adam Cohen4a9423d2014-03-28 14:22:31 -07004879 protected boolean hasRunFirstRunActivity() {
4880 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4881 }
4882
Adam Cohen432609a2014-03-13 17:03:22 -07004883 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004884 if (shouldRunFirstRunActivity() &&
4885 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004886 Intent firstRunIntent = getFirstRunActivity();
4887 if (firstRunIntent != null) {
4888 startActivity(firstRunIntent);
4889 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004890 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004891 }
4892 }
Adam Cohen432609a2014-03-13 17:03:22 -07004893 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004894 }
4895
4896 private void markFirstRunActivityShown() {
4897 SharedPreferences.Editor editor = mSharedPrefs.edit();
4898 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4899 editor.apply();
4900 }
4901
Adam Cohen432609a2014-03-13 17:03:22 -07004902 /**
4903 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4904 * screen that must be displayed and dismissed.
4905 */
4906 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004907 if (mLauncherCallbacks != null) {
4908 return mLauncherCallbacks.hasDismissableIntroScreen();
4909 }
Adam Cohen432609a2014-03-13 17:03:22 -07004910 return false;
4911 }
4912
4913 /**
4914 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4915 */
4916 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004917 if (mLauncherCallbacks != null) {
4918 return mLauncherCallbacks.getIntroScreen();
4919 }
Adam Cohen432609a2014-03-13 17:03:22 -07004920 return null;
4921 }
4922
4923 /**
4924 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4925 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4926 */
4927 private boolean shouldShowIntroScreen() {
4928 return hasDismissableIntroScreen() &&
4929 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4930 }
4931
4932 protected void showIntroScreen() {
4933 View introScreen = getIntroScreen();
4934 changeWallpaperVisiblity(false);
4935 if (introScreen != null) {
4936 mDragLayer.showOverlayView(introScreen);
4937 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004938 if (mLauncherOverlayContainer != null) {
4939 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4940 }
Adam Cohen432609a2014-03-13 17:03:22 -07004941 }
4942
4943 public void dismissIntroScreen() {
4944 markIntroScreenDismissed();
4945 if (showFirstRunActivity()) {
4946 // We delay hiding the intro view until the first run activity is showing. This
4947 // avoids a blip.
4948 mWorkspace.postDelayed(new Runnable() {
4949 @Override
4950 public void run() {
4951 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004952 if (mLauncherOverlayContainer != null) {
4953 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4954 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004955 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004956 }
4957 }, ACTIVITY_START_DELAY);
4958 } else {
4959 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004960 if (mLauncherOverlayContainer != null) {
4961 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4962 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004963 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004964 }
4965 changeWallpaperVisiblity(true);
4966 }
4967
4968 private void markIntroScreenDismissed() {
4969 SharedPreferences.Editor editor = mSharedPrefs.edit();
4970 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4971 editor.apply();
4972 }
4973
Sunny Goyal88d60f12014-09-08 03:47:29 -07004974 private void showFirstRunClings() {
4975 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4976 // on the device, then we always show the first run cling experience (or if there is no
4977 // launcher2). Otherwise, we prompt the user upon started for migration
4978 LauncherClings launcherClings = new LauncherClings(this);
4979 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4980 if (mModel.canMigrateFromOldLauncherDb(this)) {
4981 launcherClings.showMigrationCling();
4982 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004983 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004984 }
4985 }
4986 }
4987
Winson Chunga6945242014-01-08 14:04:34 -08004988 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004989 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4990 if (mHotseat != null) mHotseat.setAlpha(1f);
4991 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4992 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004993 }
4994
4995 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004996 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4997 if (mHotseat != null) mHotseat.setAlpha(0f);
4998 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4999 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005000 }
5001
Mathew Inwood72fbec12013-11-19 15:45:07 +00005002 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005003 // Called from search suggestion, not supported in other profiles.
5004 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5005 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5006 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5007 myUser);
5008 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005009 return null;
5010 }
Kenny Guyed131872014-04-30 03:02:21 +01005011 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005012 }
5013
5014 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5015 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005016 // Called from search suggestion, not supported in other profiles.
5017 return createShortcutDragInfo(shortcutIntent, caption, icon,
5018 UserHandleCompat.myUserHandle());
5019 }
5020
5021 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5022 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005023 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005024 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005025 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005026 }
5027
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005028 protected void moveWorkspaceToDefaultScreen() {
5029 mWorkspace.moveToDefaultScreen(false);
5030 }
5031
Mathew Inwood72fbec12013-11-19 15:45:07 +00005032 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5033 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005034 mWorkspace.onExternalDragStartedWithItem(dragView);
5035 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005036 }
5037
Anjali Koppalf5d29132014-02-28 13:33:27 -08005038 @Override
5039 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005040 if (mLauncherCallbacks != null) {
5041 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5042 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005043 }
5044
Winson Chung80baf5a2010-08-09 16:03:15 -07005045 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005046 * Prints out out state for debugging.
5047 */
5048 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005049 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005050 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005051 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5052 Log.d(TAG, "mRestoring=" + mRestoring);
5053 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5054 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005055 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005056 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005057
Winson Chung785d2eb2011-04-14 16:08:02 -07005058 if (mAppsCustomizeContent != null) {
5059 mAppsCustomizeContent.dumpState();
5060 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005061 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005062 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005063
5064 @Override
5065 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5066 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005067 synchronized (sDumpLogs) {
5068 writer.println(" ");
5069 writer.println("Debug logs: ");
5070 for (int i = 0; i < sDumpLogs.size(); i++) {
5071 writer.println(" " + sDumpLogs.get(i));
5072 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005073 }
Adam Cohen9211d422014-10-07 18:14:53 -07005074 if (mLauncherCallbacks != null) {
5075 mLauncherCallbacks.dump(prefix, fd, writer, args);
5076 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005077 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005078
5079 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005080 if (DEBUG_DUMP_LOG) {
5081 synchronized (sDumpLogs) {
5082 Log.d(TAG, "");
5083 Log.d(TAG, "*********************");
5084 Log.d(TAG, "Launcher debug logs: ");
5085 for (int i = 0; i < sDumpLogs.size(); i++) {
5086 Log.d(TAG, " " + sDumpLogs.get(i));
5087 }
5088 Log.d(TAG, "*********************");
5089 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005090 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005091 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005092 }
5093
5094 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005095 addDumpLog(tag, log, null, debugLog);
5096 }
5097
5098 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005099 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005100 if (e != null) {
5101 Log.d(tag, log, e);
5102 } else {
5103 Log.d(tag, log);
5104 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005105 }
Winson Chungede41292013-09-19 16:27:36 -07005106 if (DEBUG_DUMP_LOG) {
5107 sDateStamp.setTime(System.currentTimeMillis());
5108 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005109 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5110 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005111 }
Winson Chungede41292013-09-19 16:27:36 -07005112 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005113 }
5114
Adam Cohen59400422014-03-05 18:07:04 -08005115 public static CustomAppWidget getCustomAppWidget(String name) {
5116 return sCustomAppWidgets.get(name);
5117 }
5118
5119 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
5120 return sCustomAppWidgets;
5121 }
5122
Winson Chungede41292013-09-19 16:27:36 -07005123 public void dumpLogsToLocalData() {
5124 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005125 new AsyncTask<Void, Void, Void>() {
5126 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005127 boolean success = false;
5128 sDateStamp.setTime(sRunStart);
5129 String FILENAME = sDateStamp.getMonth() + "-"
5130 + sDateStamp.getDay() + "_"
5131 + sDateStamp.getHours() + "-"
5132 + sDateStamp.getMinutes() + "_"
5133 + sDateStamp.getSeconds() + ".txt";
5134
5135 FileOutputStream fos = null;
5136 File outFile = null;
5137 try {
5138 outFile = new File(getFilesDir(), FILENAME);
5139 outFile.createNewFile();
5140 fos = new FileOutputStream(outFile);
5141 } catch (Exception e) {
5142 e.printStackTrace();
5143 }
5144 if (fos != null) {
5145 PrintWriter writer = new PrintWriter(fos);
5146
5147 writer.println(" ");
5148 writer.println("Debug logs: ");
5149 synchronized (sDumpLogs) {
5150 for (int i = 0; i < sDumpLogs.size(); i++) {
5151 writer.println(" " + sDumpLogs.get(i));
5152 }
5153 }
5154 writer.close();
5155 }
5156 try {
5157 if (fos != null) {
5158 fos.close();
5159 success = true;
5160 }
5161 } catch (IOException e) {
5162 e.printStackTrace();
5163 }
Michael Jurka43467462013-11-14 12:03:58 +01005164 return null;
Winson Chungede41292013-09-19 16:27:36 -07005165 }
Michael Jurka43467462013-11-14 12:03:58 +01005166 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005167 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005169}
Michael Jurka2763be32011-02-24 11:19:57 -08005170
Michael Jurkaabded662011-03-04 12:06:57 -08005171interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005172 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005173 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005174 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005175 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005176 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005177}
Dan Sandlerd5024042014-01-09 15:01:33 -05005178
5179interface DebugIntents {
5180 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5181 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005182}