blob: f30867b9bb53bce68f06e7136bd36d2738557151 [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;
165 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Michael Jurka0a457bf2012-11-19 14:05:05 -0800167 // To turn on these properties, type
168 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800170 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Winson Chung2672ff92012-05-04 16:22:30 -0700172 // The Intent extra that defines whether to ignore the launch animation
173 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400174 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
177 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700178 // Type: int
179 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
182 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
184 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700185 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: boolean
189 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
190 // Type: long
191 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700192 // Type: int
193 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
194 // Type: int
195 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
196 // Type: parcelable
197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000198 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800199 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000200 // Type: int[]
201 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Adam Cohen432609a2014-03-13 17:03:22 -0700203 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800204 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
205
Adam Cohen3ed316a2014-07-23 18:21:20 -0700206 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
207 static final String ACTION_FIRST_LOAD_COMPLETE =
208 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
209
Adam Cohen39a06042013-07-19 14:30:12 -0700210 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700211 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700212
Sunny Goyal594d76d2014-11-06 10:12:54 -0800213 private static final String QSB_WIDGET_ID = "qsb_widget_id";
214 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
215
Winson Chunga6945242014-01-08 14:04:34 -0800216 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
217
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700218 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700219 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700220 private State mState = State.WORKSPACE;
221 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700222
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700223 private boolean mIsSafeModeEnabled;
224
Adam Cohenc2d6e892014-10-16 09:49:24 -0700225 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
226 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700227 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700230 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
231 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700232 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800235 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000237 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
238 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
239
Winson Chunga2413752012-04-03 14:22:34 -0700240 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700241 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
242 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700243 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700244 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700245
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800246 private final BroadcastReceiver mCloseSystemDialogsReceiver
247 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800248 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private LayoutInflater mInflater;
251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700253 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800254 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800255 private DragLayer mDragLayer;
256 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700257 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700258
Sunny Goyalffe83f12014-08-14 17:39:34 -0700259 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700260 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700261
Michael Jurkac9d95c52011-08-29 14:03:34 -0700262 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800263 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800264 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700265
Michael Jurka0280c3b2010-09-17 15:00:07 -0700266 private int[] mTmpAddItemCellCoordinates = new int[2];
267
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 private FolderInfo mFolderInfo;
269
Winson Chung3d503fb2011-07-13 17:25:49 -0700270 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800271 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700272
Michael Jurka838a4ca2011-02-07 13:33:06 -0800273 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700274
Winson Chungc51db6a2011-10-05 11:44:49 -0700275 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700276 private AppsCustomizeTabHost mAppsCustomizeTabHost;
277 private AppsCustomizePagedView mAppsCustomizeContent;
278 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800279 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700282 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
283 // scroll issues (because the workspace may not have been measured yet) and extra work.
284 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
285 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286
287 private SpannableStringBuilder mDefaultKeySsb = null;
288
Joe Onorato9c1289c2009-08-17 11:03:03 -0400289 private boolean mWorkspaceLoading = true;
290
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800291 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 private boolean mRestoring;
293 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700294 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295
Michael Jurka1e2f4652013-07-08 18:03:46 -0700296 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700297 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
298
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 private Bundle mSavedInstanceState;
300
Joe Onorato9c1289c2009-08-17 11:03:03 -0400301 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800302 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800303 private boolean mUserPresent = true;
304 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700305 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800306 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400307
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700308 private static LocaleConfiguration sLocaleConfiguration = null;
309
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700310 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700311
Adam Cohen61f560d2013-09-30 15:58:20 -0700312 private View.OnTouchListener mHapticFeedbackTouchListener;
313
Adam Cohended9f8d2010-11-03 13:25:16 -0700314 // Related to the auto-advancing of widgets
315 private final int ADVANCE_MSG = 1;
316 private final int mAdvanceInterval = 20000;
317 private final int mAdvanceStagger = 250;
318 private long mAutoAdvanceSentTime;
319 private long mAutoAdvanceTimeLeft = -1;
320 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
321 new HashMap<View, AppWidgetProviderInfo>();
322
Winson Chung400438b2011-01-16 17:53:48 -0800323 // Determines how long to wait after a rotation before restoring the screen orientation to
324 // match the sensor state.
325 private final int mRestoreScreenOrientationDelay = 500;
326
Craig Mautner360310b2012-10-26 15:13:08 -0700327 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700328
Adam Cohen1462de32012-07-24 22:34:36 -0700329 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700330 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700331
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700332 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700333 static Date sDateStamp = new Date();
334 static DateFormat sDateFormat =
335 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
336 static long sRunStart = System.currentTimeMillis();
337 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700338
Winson Chung46353de2012-02-16 14:05:10 -0800339 // We only want to get the SharedPreferences once since it does an FS stat each time we get
340 // it from the context.
341 private SharedPreferences mSharedPrefs;
342
Adam Cohene25af792013-06-06 23:08:25 -0700343 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
344
Winson Chungf0c6ae02012-03-21 16:10:31 -0700345 // Holds the page that we need to animate to, and the icon views that we need to animate up
346 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700347 private ImageView mFolderIconImageView;
348 private Bitmap mFolderIconBitmap;
349 private Canvas mFolderIconCanvas;
350 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700351
Michael Jurkaddd62e92011-02-16 17:49:14 -0800352 private BubbleTextView mWaitingForResume;
353
Adam Cohen59400422014-03-05 18:07:04 -0800354 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
355 new HashMap<String, CustomAppWidget>();
356
357 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
358 static {
359 if (ENABLE_CUSTOM_WIDGET_TEST) {
360 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
361 }
362 }
363
Michael Jurkac1f5d262011-09-30 19:32:27 -0700364 private Runnable mBuildLayersRunnable = new Runnable() {
365 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700366 if (mWorkspace != null) {
367 mWorkspace.buildPageHardwareLayers();
368 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700369 }
370 };
371
Adam Cohendb364c32014-05-20 14:23:40 -0700372 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800373
374 private static class PendingAddArguments {
375 int requestCode;
376 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700377 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700378 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800379 int cellX;
380 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700381 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800382 }
383
Daniel Sandlerff02d492013-08-05 02:12:05 -0400384 private Stats mStats;
385
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700386 FocusIndicatorView mFocusHandler;
387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 @Override
389 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700390 if (DEBUG_STRICT_MODE) {
391 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
392 .detectDiskReads()
393 .detectDiskWrites()
394 .detectNetwork() // or .detectAll() for all detectable problems
395 .penaltyLog()
396 .build());
397 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
398 .detectLeakedSqlLiteObjects()
399 .detectLeakedClosableObjects()
400 .penaltyLog()
401 .penaltyDeath()
402 .build());
403 }
404
Adam Cohen9211d422014-10-07 18:14:53 -0700405 if (mLauncherCallbacks != null) {
406 mLauncherCallbacks.preOnCreate();
407 }
408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400410
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400411 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400412 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700413 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700414
Winson Chung5f8afe62013-08-12 16:19:28 -0700415 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700416 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700417
418 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400419 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700420 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700421 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800422 mModel = app.setLauncher(this);
423 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700424 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400425 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700427
Daniel Sandlerff02d492013-08-05 02:12:05 -0400428 mStats = new Stats(this);
429
Sunny Goyalffe83f12014-08-14 17:39:34 -0700430 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700431
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700432 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
433 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700434
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700435 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
436 // this also ensures that any synchronous binding below doesn't re-trigger another
437 // LauncherModel load.
438 mPaused = false;
439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200441 android.os.Debug.startMethodTracing(
442 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 }
444
445 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100449 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800451 registerContentObservers();
452
Joe Onorato7c312c12009-08-13 21:36:53 -0700453 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 mSavedState = savedInstanceState;
456 restoreState(mSavedState);
457
458 if (PROFILE_STARTUP) {
459 android.os.Debug.stopMethodTracing();
460 }
461
462 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700463 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 // If the user leaves launcher, then we should just load items asynchronously when
465 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500466 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 } else {
468 // We only load the page synchronously if the user rotates (or triggers a
469 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800470 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700471 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 }
473
474 // For handling default keys
475 mDefaultKeySsb = new SpannableStringBuilder();
476 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800477
478 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
479 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800480
Adam Cohenf9426d52012-06-04 17:26:21 -0700481 // On large interfaces, we want the screen to auto-rotate based on the current orientation
482 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700483
Adam Cohen9211d422014-10-07 18:14:53 -0700484 if (mLauncherCallbacks != null) {
485 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700486 if (mLauncherCallbacks.hasLauncherOverlay()) {
487 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700488 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
489 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
490 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700491 mWorkspace.setLauncherOverlay(mLauncherOverlay);
492 }
Adam Cohen9211d422014-10-07 18:14:53 -0700493 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700494
495 if (shouldShowIntroScreen()) {
496 showIntroScreen();
497 } else {
498 showFirstRunActivity();
499 showFirstRunClings();
500 }
Adam Cohen9211d422014-10-07 18:14:53 -0700501 }
502
503 private LauncherCallbacks mLauncherCallbacks;
504
505 public void onPostCreate(Bundle savedInstanceState) {
506 super.onPostCreate(savedInstanceState);
507 if (mLauncherCallbacks != null) {
508 mLauncherCallbacks.onPostCreate(savedInstanceState);
509 }
510 }
511
512 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
513 mLauncherCallbacks = callbacks;
514 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700515 }
516
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700517 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700518 public void onLauncherProviderChange() {
519 if (mLauncherCallbacks != null) {
520 mLauncherCallbacks.onLauncherProviderChange();
521 }
522 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700523
Adam Cohen9211d422014-10-07 18:14:53 -0700524 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700525 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700526 if (mLauncherCallbacks != null) {
527 return mLauncherCallbacks.hasCustomContentToLeft();
528 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700529 return false;
530 }
531
Dave Hawkeya8881582013-09-17 15:55:33 -0600532 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500533 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600534 * {@link #addToCustomContentPage}. This will only be invoked if
535 * {@link #hasCustomContentToLeft()} is {@code true}.
536 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500537 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700538 if (mLauncherCallbacks != null) {
539 mLauncherCallbacks.populateCustomContentContainer();
540 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600541 }
542
543 /**
544 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
545 * ensure the custom content page is added or removed if necessary.
546 */
547 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600548 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600549 // Not bound yet, wait for bindScreens to be called.
550 return;
551 }
552
553 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
554 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500555 mWorkspace.createCustomContentContainer();
556 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600557 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
558 mWorkspace.removeCustomContentPage();
559 }
560 }
561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700563 if (sLocaleConfiguration == null) {
564 new AsyncTask<Void, Void, LocaleConfiguration>() {
565 @Override
566 protected LocaleConfiguration doInBackground(Void... unused) {
567 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
568 readConfiguration(Launcher.this, localeConfiguration);
569 return localeConfiguration;
570 }
571
572 @Override
573 protected void onPostExecute(LocaleConfiguration result) {
574 sLocaleConfiguration = result;
575 checkForLocaleChange(); // recursive, but now with a locale configuration
576 }
577 }.execute();
578 return;
579 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 final Configuration configuration = getResources().getConfiguration();
582
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700583 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 final String locale = configuration.locale.toString();
585
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700586 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 final int mcc = configuration.mcc;
588
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700589 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 final int mnc = configuration.mnc;
591
Romain Guy84f296c2009-11-04 15:00:44 -0800592 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593
Romain Guy84f296c2009-11-04 15:00:44 -0800594 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700595 sLocaleConfiguration.locale = locale;
596 sLocaleConfiguration.mcc = mcc;
597 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700598
Joe Onorato0589f0f2010-02-08 13:44:00 -0800599 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700600
601 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100602 new AsyncTask<Void, Void, Void>() {
603 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100605 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700606 }
Michael Jurka43467462013-11-14 12:03:58 +0100607 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700608 }
609 }
610
611 private static class LocaleConfiguration {
612 public String locale;
613 public int mcc = -1;
614 public int mnc = -1;
615 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700616
Romain Guy98d01652009-06-30 16:21:04 -0700617 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
618 DataInputStream in = null;
619 try {
Helena Josol28db2802014-10-09 17:04:09 +0100620 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700621 configuration.locale = in.readUTF();
622 configuration.mcc = in.readInt();
623 configuration.mnc = in.readInt();
624 } catch (FileNotFoundException e) {
625 // Ignore
626 } catch (IOException e) {
627 // Ignore
628 } finally {
629 if (in != null) {
630 try {
631 in.close();
632 } catch (IOException e) {
633 // Ignore
634 }
635 }
636 }
637 }
638
639 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
640 DataOutputStream out = null;
641 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100642 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100643 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700644 out.writeUTF(configuration.locale);
645 out.writeInt(configuration.mcc);
646 out.writeInt(configuration.mnc);
647 out.flush();
648 } catch (FileNotFoundException e) {
649 // Ignore
650 } catch (IOException e) {
651 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100652 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700653 } finally {
654 if (out != null) {
655 try {
656 out.close();
657 } catch (IOException e) {
658 // Ignore
659 }
660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
662 }
663
Anton Hanssona2f665f2013-09-26 12:18:32 +0100664 public Stats getStats() {
665 return mStats;
666 }
667
Bjorn Bringertc459e522013-06-07 19:36:01 +0100668 public LayoutInflater getInflater() {
669 return mInflater;
670 }
671
Winson Chung36a62fe2012-05-06 18:04:42 -0700672 boolean isDraggingEnabled() {
673 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
674 // that is subsequently removed from the workspace in startBinding().
675 return !mModel.isLoadingWorkspace();
676 }
677
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 static int getScreen() {
679 synchronized (sLock) {
680 return sScreen;
681 }
682 }
683
684 static void setScreen(int screen) {
685 synchronized (sLock) {
686 sScreen = screen;
687 }
688 }
689
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000690 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100691 if (Build.VERSION.SDK_INT >= 17) {
692 return View.generateViewId();
693 } else {
694 // View.generateViewId() is not available. The following fallback logic is a copy
695 // of its implementation.
696 for (;;) {
697 final int result = sNextGeneratedId.get();
698 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
699 int newValue = result + 1;
700 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
701 if (sNextGeneratedId.compareAndSet(result, newValue)) {
702 return result;
703 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000704 }
705 }
706 }
707
708 public int getViewIdForItem(ItemInfo info) {
709 // This cast is safe given the > 2B range for int.
710 int itemId = (int) info.id;
711 if (mItemIdToViewId.containsKey(itemId)) {
712 return mItemIdToViewId.get(itemId);
713 }
714 int viewId = generateViewId();
715 mItemIdToViewId.put(itemId, viewId);
716 return viewId;
717 }
718
719 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700720 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
721 * a configuration step, this allows the proper animations to run after other transitions.
722 */
Adam Cohendb364c32014-05-20 14:23:40 -0700723 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700724 long screenId = args.screenId;
725 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
726 // When the screen id represents an actual screen (as opposed to a rank) we make sure
727 // that the drop page actually exists.
728 screenId = ensurePendingDropLayoutExists(args.screenId);
729 }
Adam Cohendb364c32014-05-20 14:23:40 -0700730
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800731 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800732 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700733 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700734 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700735 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800736 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700737 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700738 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700739 case REQUEST_RECONFIGURE_APPWIDGET:
740 completeRestoreAppWidget(args.appWidgetId);
741 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800742 }
Michael Jurka27614d22012-04-02 06:40:22 -0700743 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
744 // if you turned the screen off and then back while in All Apps, Launcher would not
745 // return to the workspace. Clearing mAddInfo.container here fixes this issue
746 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700747 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800748 }
749
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800750 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700751 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700752 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700753 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700754 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800755 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700756
Adam Cohenad4e15c2013-10-17 16:21:35 -0700757 Runnable exitSpringLoaded = new Runnable() {
758 @Override
759 public void run() {
760 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
761 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
762 }
763 };
764
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700767 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
768 if (resultCode == RESULT_CANCELED) {
769 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700770 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700771 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800773 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700774 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700775 }
776 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200777 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
778 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
779 mWorkspace.exitOverviewMode(false);
780 }
781 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700782 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200783
Adam Cohened66b2b2012-01-23 17:28:51 -0800784 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
785 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700786
Adam Cohendb364c32014-05-20 14:23:40 -0700787 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800788 // We have special handling for widgets
789 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800790 final int appWidgetId;
791 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
792 : -1;
793 if (widgetId < 0) {
794 appWidgetId = pendingAddWidgetId;
795 } else {
796 appWidgetId = widgetId;
797 }
798
Adam Cohenad4e15c2013-10-17 16:21:35 -0700799 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800800 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700801 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
802 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700803 result = RESULT_CANCELED;
804 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700805 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700806 @Override
807 public void run() {
808 exitSpringLoadedDragModeDelayed(false, 0, null);
809 }
810 };
Adam Cohendb364c32014-05-20 14:23:40 -0700811 if (workspaceLocked) {
812 // No need to remove the empty screen if we're mid-binding, as the
813 // the bind will not add the empty screen.
814 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
815 } else {
816 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
817 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
818 }
Winson Chung5aaab772012-04-27 15:24:02 -0700819 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700820 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700821 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
822 // When the screen id represents an actual screen (as opposed to a rank)
823 // we make sure that the drop page actually exists.
824 mPendingAddInfo.screenId =
825 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
826 }
Adam Cohendb364c32014-05-20 14:23:40 -0700827 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
828
829 dropLayout.setDropPending(true);
830 final Runnable onComplete = new Runnable() {
831 @Override
832 public void run() {
833 completeTwoStageWidgetDrop(resultCode, appWidgetId);
834 dropLayout.setDropPending(false);
835 }
836 };
837 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
838 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
839 } else {
840 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
841 mPendingAddInfo);
842 sPendingAddItem = args;
843 }
Winson Chung5aaab772012-04-27 15:24:02 -0700844 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800845 return;
846 }
847
Sunny Goyalff572272014-07-23 13:58:07 -0700848 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
849 if (resultCode == RESULT_OK) {
850 // Update the widget view.
851 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
852 pendingAddWidgetId, mPendingAddInfo);
853 if (workspaceLocked) {
854 sPendingAddItem = args;
855 } else {
856 completeAdd(args);
857 }
858 }
859 // Leave the widget in the pending state if the user canceled the configure.
860 return;
861 }
862
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 // The pattern used here is that a user PICKs a specific application,
864 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700865
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
867 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700868 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700869 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
870 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800871 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700872 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800873 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700874 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700875 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
876 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 }
Adam Cohen00074722013-10-11 17:46:09 -0700878 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700879 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700880 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800882 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800883
884 }
885
886 @Override
887 protected void onActivityResult(
888 final int requestCode, final int resultCode, final Intent data) {
889 handleActivityResult(requestCode, resultCode, data);
890 if (mLauncherCallbacks != null) {
891 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
892 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800893 }
894
Adam Cohendb364c32014-05-20 14:23:40 -0700895 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
896 appWidgetId, ItemInfo info) {
897 PendingAddArguments args = new PendingAddArguments();
898 args.requestCode = requestCode;
899 args.intent = data;
900 args.container = info.container;
901 args.screenId = info.screenId;
902 args.cellX = info.cellX;
903 args.cellY = info.cellY;
904 args.appWidgetId = appWidgetId;
905 return args;
906 }
907
908 /**
909 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
910 *
911 * @param screenId the screen id to check
912 * @return the new screen, or screenId if it exists
913 */
914 private long ensurePendingDropLayoutExists(long screenId) {
915 CellLayout dropLayout =
916 (CellLayout) mWorkspace.getScreenWithId(screenId);
917 if (dropLayout == null) {
918 // it's possible that the add screen was removed because it was
919 // empty and a re-bind occurred
920 mWorkspace.addExtraEmptyScreen();
921 return mWorkspace.commitExtraEmptyScreen();
922 } else {
923 return screenId;
924 }
925 }
926
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700928 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700929 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 Runnable onCompleteRunnable = null;
931 int animationType = 0;
932
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700933 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 if (resultCode == RESULT_OK) {
935 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
936 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700937 mPendingAddWidgetInfo);
938 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800939 onCompleteRunnable = new Runnable() {
940 @Override
941 public void run() {
942 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700943 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700944 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
945 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800946 }
947 };
948 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800949 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800950 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800951 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700952 if (mDragLayer.getAnimatedView() != null) {
953 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
954 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
955 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700956 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700957 // The animated view may be null in the case of a rotation during widget configuration
958 onCompleteRunnable.run();
959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 }
961
962 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700963 protected void onStop() {
964 super.onStop();
965 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700966
967 if (mLauncherCallbacks != null) {
968 mLauncherCallbacks.onStop();
969 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700970 }
971
972 @Override
973 protected void onStart() {
974 super.onStart();
975 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700976
977 if (mLauncherCallbacks != null) {
978 mLauncherCallbacks.onStart();
979 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700980 }
981
982 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200984 long startTime = 0;
985 if (DEBUG_RESUME_TIME) {
986 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400987 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200988 }
Adam Cohen9211d422014-10-07 18:14:53 -0700989
990 if (mLauncherCallbacks != null) {
991 mLauncherCallbacks.preOnResume();
992 }
993
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700995
Winson Chung4a2afa32012-07-19 14:53:05 -0700996 // Restore the previous launcher state
997 if (mOnResumeState == State.WORKSPACE) {
998 showWorkspace(false);
999 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c62013-11-06 15:49:51 -08001000 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001001 }
1002 mOnResumeState = State.NONE;
1003
Craig Mautner360310b2012-10-26 15:13:08 -07001004 // Background was set to gradient in onPause(), restore to black if in all apps.
1005 setWorkspaceBackground(mState == State.WORKSPACE);
1006
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001007 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001008 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001009 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001010 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001011 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001012 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001014 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1016 // execute them here
1017 long startTimeCallbacks = 0;
1018 if (DEBUG_RESUME_TIME) {
1019 startTimeCallbacks = System.currentTimeMillis();
1020 }
1021
1022 if (mAppsCustomizeContent != null) {
1023 mAppsCustomizeContent.setBulkBind(true);
1024 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001025 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1026 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001027 }
1028 if (mAppsCustomizeContent != null) {
1029 mAppsCustomizeContent.setBulkBind(false);
1030 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001031 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001032 if (DEBUG_RESUME_TIME) {
1033 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1034 (System.currentTimeMillis() - startTimeCallbacks));
1035 }
Michael Jurka447bf842013-05-15 14:52:15 +02001036 }
Michael Jurka54554252013-08-01 12:52:23 +02001037 if (mOnResumeCallbacks.size() > 0) {
1038 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1039 mOnResumeCallbacks.get(i).run();
1040 }
1041 mOnResumeCallbacks.clear();
1042 }
Winson Chunge4e50662012-01-23 14:45:13 -08001043
1044 // Reset the pressed state of icons that were locked in the press state while activities
1045 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001046 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001047 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001048 mWaitingForResume.setStayPressed(false);
1049 }
Winson Chung82963d52013-10-09 11:20:57 -07001050
Adam Cohen06dff352012-06-01 17:17:08 -07001051 // It is possible that widgets can receive updates while launcher is not in the foreground.
1052 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1053 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1054 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001055 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001056
Winson Chung780fe592013-09-26 14:48:44 -07001057 // Process any items that were added while Launcher was away.
1058 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1059
Michael Jurka447bf842013-05-15 14:52:15 +02001060 if (DEBUG_RESUME_TIME) {
1061 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1062 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001063
1064 if (mWorkspace.getCustomContentCallbacks() != null) {
1065 // If we are resuming and the custom content is the current page, we call onShow().
1066 // It is also poassible that onShow will instead be called slightly after first layout
1067 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1068 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001069 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001070 }
1071 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001072 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001073 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001074
1075 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001076
1077 if (mLauncherCallbacks != null) {
1078 mLauncherCallbacks.onResume();
1079 }
Adam Cohen06dff352012-06-01 17:17:08 -07001080 }
1081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001083 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001084 // Ensure that items added to Launcher are queued until Launcher returns
1085 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001086 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001087
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001088 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001089 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001090 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001091 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001092
1093 // We call onHide() aggressively. The custom content callbacks should be able to
1094 // debounce excess onHide calls.
1095 if (mWorkspace.getCustomContentCallbacks() != null) {
1096 mWorkspace.getCustomContentCallbacks().onHide();
1097 }
Romain Guycbb89e42009-06-08 15:52:54 -07001098
Adam Cohen9211d422014-10-07 18:14:53 -07001099 if (mLauncherCallbacks != null) {
1100 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001101 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001102 }
1103
1104 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001105 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1106 // by a onResume or by scrolling otherwise.
1107 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001108
1109 // Custom content is completely hidden
1110 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001111
1112 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1113 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001114
1115 // Indicates whether the user is allowed to scroll away from the custom content.
1116 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001117 }
1118
Adam Cohenc2d6e892014-10-16 09:49:24 -07001119 public interface LauncherOverlay {
1120
1121 /**
1122 * Touch interaction leading to overscroll has begun
1123 */
1124 public void onScrollInteractionBegin();
1125
1126 /**
1127 * Touch interaction related to overscroll has ended
1128 */
1129 public void onScrollInteractionEnd();
1130
1131 /**
1132 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1133 * screen (or in the case of RTL, the rightmost screen).
1134 */
1135 public void onScrollChange(int progress, boolean rtl);
1136
1137 /**
1138 * Screen has stopped scrolling
1139 */
1140 public void onScrollSettled();
1141
1142 /**
1143 * This method can be called by the Launcher in order to force the LauncherOverlay
1144 * to exit fully immersive mode.
1145 */
1146 public void forceExitFullImmersion();
1147 }
1148
1149 public interface LauncherOverlayCallbacks {
1150 /**
1151 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1152 * however it doesn't modify any state within the launcher.
1153 */
1154 public boolean canEnterFullImmersion();
1155
1156 /**
1157 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1158 * eg. by occupying the full screen and handling all touch events.
1159 *
1160 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1161 * case, Launcher will modify any necessary state and assumes the overlay is
1162 * handling all interaction. If false, the LauncherOverlay should cancel any
1163 *
1164 */
1165 public boolean enterFullImmersion();
1166
1167 /**
1168 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1169 * full control over UI and state.
1170 */
1171 public void exitFullImmersion();
1172 }
1173
1174 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1175
1176 @Override
1177 public boolean canEnterFullImmersion() {
1178 return mState == State.WORKSPACE;
1179 }
1180
1181 @Override
1182 public boolean enterFullImmersion() {
1183 if (mState == State.WORKSPACE) {
1184 // When fully immersed, disregard any touches which fall through.
1185 mDragLayer.setBlockTouch(true);
1186 return true;
1187 }
1188 return false;
1189 }
1190
1191 @Override
1192 public void exitFullImmersion() {
1193 mDragLayer.setBlockTouch(false);
1194 }
1195 }
1196
Jorim Jaggid017f882014-01-14 17:08:48 -08001197 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001198 if (mLauncherCallbacks != null) {
1199 return mLauncherCallbacks.hasSettings();
1200 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001201 return false;
1202 }
1203
Adam Cohenbffe7452013-07-22 18:21:45 -07001204
Adam Cohen9211d422014-10-07 18:14:53 -07001205 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001206 CustomContentCallbacks callbacks, String description) {
1207 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001208 }
1209
Adam Cohenedb40762013-07-18 16:45:45 -07001210 // The custom content needs to offset its content to account for the QSB
1211 public int getTopOffsetForCustomContent() {
1212 return mWorkspace.getPaddingTop();
1213 }
1214
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001215 @Override
1216 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001217 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001218 if (mModel.isCurrentCallbacks(this)) {
1219 mModel.stopLoader();
1220 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001221 if (mAppsCustomizeContent != null) {
1222 mAppsCustomizeContent.surrender();
1223 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001224 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001225 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001226
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001227 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001228 @Override
1229 public void onWindowFocusChanged(boolean hasFocus) {
1230 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001231 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001232
1233 if (mLauncherCallbacks != null) {
1234 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1235 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001236 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 private boolean acceptFilter() {
1239 final InputMethodManager inputManager = (InputMethodManager)
1240 getSystemService(Context.INPUT_METHOD_SERVICE);
1241 return !inputManager.isFullscreenMode();
1242 }
1243
1244 @Override
1245 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001246 final int uniChar = event.getUnicodeChar();
1247 final boolean handled = super.onKeyDown(keyCode, event);
1248 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1249 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1251 keyCode, event);
1252 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001253 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001254 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001255 // showSearchDialog()
1256 // If there are multiple keystrokes before the search dialog takes focus,
1257 // onSearchRequested() will be called for every keystroke,
1258 // but it is idempotent, so it's fine.
1259 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
1261 }
1262
Joe Onorato8a9625e2010-01-28 15:55:35 -08001263 // Eat the long press event so the keyboard doesn't come up.
1264 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1265 return true;
1266 }
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 return handled;
1269 }
1270
Karl Rosaen138a0412009-04-23 19:00:21 -07001271 private String getTypedText() {
1272 return mDefaultKeySsb.toString();
1273 }
1274
1275 private void clearTypedText() {
1276 mDefaultKeySsb.clear();
1277 mDefaultKeySsb.clearSpans();
1278 Selection.setSelection(mDefaultKeySsb, 0);
1279 }
1280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001282 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1283 * State
1284 */
1285 private static State intToState(int stateOrdinal) {
1286 State state = State.WORKSPACE;
1287 final State[] stateValues = State.values();
1288 for (int i = 0; i < stateValues.length; i++) {
1289 if (stateValues[i].ordinal() == stateOrdinal) {
1290 state = stateValues[i];
1291 break;
1292 }
1293 }
1294 return state;
1295 }
1296
1297 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 * Restores the previous state, if it exists.
1299 *
1300 * @param savedState The previous state.
1301 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001302 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 private void restoreState(Bundle savedState) {
1304 if (savedState == null) {
1305 return;
1306 }
1307
Michael Jurka883f55b2010-10-21 15:47:14 -07001308 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001309 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001310 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001311 }
1312
Adam Cohen21cd0022013-10-09 18:57:02 -07001313 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1314 PagedView.INVALID_RESTORE_PAGE);
1315 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001316 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 }
1318
Winson Chung3d503fb2011-07-13 17:25:49 -07001319 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001320 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001321
Winson Chung3d503fb2011-07-13 17:25:49 -07001322 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1323 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001324 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001325 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1326 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001327 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1328 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1329 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001330 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001331 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 mRestoring = true;
1333 }
1334
1335 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1336 if (renameFolder) {
1337 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001338 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 mRestoring = true;
1340 }
Winson Chunga12a2502010-12-20 14:41:35 -08001341
Winson Chung785d2eb2011-04-14 16:08:02 -07001342 // Restore the AppsCustomize tab
1343 if (mAppsCustomizeTabHost != null) {
1344 String curTab = savedState.getString("apps_customize_currentTab");
1345 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001346 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001347 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001348 mAppsCustomizeContent.loadAssociatedPages(
1349 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001350 }
1351
Winson Chung5afbf7b2011-07-25 11:53:08 -07001352 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1353 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001354 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001355 mItemIdToViewId = (HashMap<Integer, Integer>)
1356 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
1358
1359 /**
1360 * Finds all the views we need and configure them properly.
1361 */
1362 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001363 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001364
Craig Mautner360310b2012-10-26 15:13:08 -07001365 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001366 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001367 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1368 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001369 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001370 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001371
John Spurlock77e1f472013-09-11 10:09:51 -04001372 mLauncherView.setSystemUiVisibility(
1373 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001374 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375
Winson Chung4c98d922011-05-31 16:50:48 -07001376 // Setup the drag layer
1377 mDragLayer.setup(this, dragController);
1378
Winson Chung3d503fb2011-07-13 17:25:49 -07001379 // Setup the hotseat
1380 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1381 if (mHotseat != null) {
1382 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001383 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001384 }
1385
Jorim Jaggid017f882014-01-14 17:08:48 -08001386 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001387 View widgetButton = findViewById(R.id.widget_button);
1388 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001389 @Override
1390 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001391 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001392 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001393 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001394 }
1395 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001396 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1397
1398 View wallpaperButton = findViewById(R.id.wallpaper_button);
1399 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001400 @Override
1401 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001402 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001403 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001404 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001405 }
1406 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001407 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1408
1409 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001410 if (hasSettings()) {
1411 settingsButton.setOnClickListener(new OnClickListener() {
1412 @Override
1413 public void onClick(View arg0) {
1414 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001415 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001416 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001417 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001418 });
1419 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1420 } else {
1421 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001422 }
1423
Adam Cohendbdff6b2013-09-18 19:09:15 -07001424 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001425
Winson Chung4c98d922011-05-31 16:50:48 -07001426 // Setup the workspace
1427 mWorkspace.setHapticFeedbackEnabled(false);
1428 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001429 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001430 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001431
Winson Chungf0ea4d32011-06-06 14:27:16 -07001432 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001433 mSearchDropTargetBar = (SearchDropTargetBar)
1434 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001435
Winson Chungf0ea4d32011-06-06 14:27:16 -07001436 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001437 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001438 mAppsCustomizeContent = (AppsCustomizePagedView)
1439 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1440 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001441
Winson Chung3d503fb2011-07-13 17:25:49 -07001442 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001443 dragController.setDragScoller(mWorkspace);
1444 dragController.setScrollView(mDragLayer);
1445 dragController.setMoveTarget(mWorkspace);
1446 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001447 if (mSearchDropTargetBar != null) {
1448 mSearchDropTargetBar.setup(this, dragController);
Sunny Goyal594d76d2014-11-06 10:12:54 -08001449 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001450 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001451
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001452 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001453 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001454 mWeightWatcher = new WeightWatcher(this);
1455 mWeightWatcher.setAlpha(0.5f);
1456 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001457 new FrameLayout.LayoutParams(
1458 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001459 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001460 Gravity.BOTTOM)
1461 );
Adam Cohen39a06042013-07-19 14:30:12 -07001462
1463 boolean show = shouldShowWeightWatcher();
1464 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 }
1467
1468 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001469 * Sets the all apps button. This method is called from {@link Hotseat}.
1470 */
1471 public void setAllAppsButton(View allAppsButton) {
1472 mAllAppsButton = allAppsButton;
1473 }
1474
1475 public View getAllAppsButton() {
1476 return mAllAppsButton;
1477 }
1478
1479 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 * Creates a view representing a shortcut.
1481 *
1482 * @param info The data structure describing the shortcut.
1483 *
1484 * @return A View inflated from R.layout.application.
1485 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001486 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001488 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
1490
1491 /**
1492 * Creates a view representing a shortcut inflated from the specified resource.
1493 *
1494 * @param layoutResId The id of the XML layout used to create the shortcut.
1495 * @param parent The group the shortcut belongs to.
1496 * @param info The data structure describing the shortcut.
1497 *
1498 * @return A View inflated from layoutResId.
1499 */
Adam Cohen59400422014-03-05 18:07:04 -08001500 public View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001501 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001502 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001504 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 return favorite;
1506 }
1507
1508 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 * Add a shortcut to the workspace.
1510 *
1511 * @param data The intent describing the shortcut.
1512 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001514 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001515 int cellY) {
1516 int[] cellXY = mTmpAddItemCellCoordinates;
1517 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001518 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001519
Michael Jurkad3ef3062010-11-23 16:23:58 -08001520 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001521
Sunny Goyal2350bc92014-10-14 16:42:54 -07001522 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001523 if (info == null) {
1524 return;
1525 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001526 final View view = createShortcut(info);
1527
Adam Cohenfbba09b2011-07-18 21:43:05 -07001528 // First we check if we already know the exact location where we want to add this item.
1529 if (cellX >= 0 && cellY >= 0) {
1530 cellXY[0] = cellX;
1531 cellXY[1] = cellY;
1532 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001533
1534 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001535 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001536 true, null,null)) {
1537 return;
1538 }
1539 DragObject dragObject = new DragObject();
1540 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001541 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1542 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001543 return;
1544 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001545 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001546 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001548 foundCellSpan = (result != null);
1549 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001550 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001551 }
1552
1553 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001554 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001555 return;
1556 }
1557
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559
1560 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001562 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
1564 }
1565
Adam Cohen2f093b62012-04-30 18:59:53 -07001566 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1567 int minHeight) {
1568 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001569 // We want to account for the extra amount of padding that we are adding to the widget
1570 // to ensure that it gets the full amount of space that it has requested
1571 int requiredWidth = minWidth + padding.left + padding.right;
1572 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001573 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001574 }
1575
Adam Cohen2f093b62012-04-30 18:59:53 -07001576 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1577 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001578 }
1579
Adam Cohen2f093b62012-04-30 18:59:53 -07001580 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1581 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001582 }
1583
Adam Cohen2f093b62012-04-30 18:59:53 -07001584 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1585 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001586 }
1587
Adam Cohen2f093b62012-04-30 18:59:53 -07001588 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1589 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001590 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001591 }
1592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001594 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001596 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001597 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 */
Adam Cohen59400422014-03-05 18:07:04 -08001599 private void completeAddAppWidget(int appWidgetId, long container, long screenId,
1600 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1601
1602 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001603 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001604 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1605 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001606 }
Romain Guycbb89e42009-06-08 15:52:54 -07001607
Adam Cohen59400422014-03-05 18:07:04 -08001608 if (appWidgetInfo.isCustomWidget) {
1609 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001610 }
1611
Adam Cohen59400422014-03-05 18:07:04 -08001612 LauncherAppWidgetInfo launcherInfo;
1613 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1614 launcherInfo.spanX = info.spanX;
1615 launcherInfo.spanY = info.spanY;
1616 launcherInfo.minSpanX = info.minSpanX;
1617 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001618 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohen59400422014-03-05 18:07:04 -08001621 container, screenId, info.cellX, info.cellY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622
1623 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001624 if (hostView == null) {
1625 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001626 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1627 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001628 } else {
1629 // The AppWidgetHostView has already been inflated and instantiated
1630 launcherInfo.hostView = hostView;
1631 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001633 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001634 launcherInfo.notifyWidgetSizeChanged(this);
1635
Adam Cohen59400422014-03-05 18:07:04 -08001636 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1637 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001638
1639 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001641 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 }
Romain Guycbb89e42009-06-08 15:52:54 -07001643
Adam Cohended9f8d2010-11-03 13:25:16 -07001644 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1645 @Override
1646 public void onReceive(Context context, Intent intent) {
1647 final String action = intent.getAction();
1648 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1649 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001650 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001652
Winson Chungc100e8e2011-08-09 16:02:43 -07001653 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001654 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001655 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001656 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001657 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001658 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1659 mUserPresent = true;
1660 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001661 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1662 mModel.resetLoadedState(false, true);
1663 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1664 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1665 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1666 mModel.resetLoadedState(false, true);
1667 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1668 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1669 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001670 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1671 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1672 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001673 }
1674 }
1675 };
1676
1677 @Override
1678 public void onAttachedToWindow() {
1679 super.onAttachedToWindow();
1680
1681 // Listen for broadcasts related to user-presence
1682 final IntentFilter filter = new IntentFilter();
1683 filter.addAction(Intent.ACTION_SCREEN_OFF);
1684 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001685 // For handling managed profiles
1686 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1687 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001688 if (ENABLE_DEBUG_INTENTS) {
1689 filter.addAction(DebugIntents.DELETE_DATABASE);
1690 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1691 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001692 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001693 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001694 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001695 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 mVisible = true;
1697 }
1698
Adam Cohen0b395352014-06-09 22:54:36 +00001699 /**
1700 * Sets up transparent navigation and status bars in LMP.
1701 * This method is a no-op for other platform versions.
1702 */
1703 @TargetApi(19)
1704 private void setupTransparentSystemBarsForLmp() {
1705 // TODO(sansid): use the APIs directly when compiling against L sdk.
1706 // Currently we use reflection to access the flags and the API to set the transparency
1707 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001708 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001709 try {
1710 getWindow().getAttributes().systemUiVisibility |=
1711 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1712 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1713 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1714 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1715 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1716 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1717 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1718 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1719
1720 Method setStatusBarColorMethod =
1721 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1722 Method setNavigationBarColorMethod =
1723 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1724 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1725 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1726 } catch (NoSuchFieldException e) {
1727 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1728 } catch (NoSuchMethodException ex) {
1729 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1730 } catch (IllegalAccessException e) {
1731 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1732 } catch (IllegalArgumentException e) {
1733 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1734 } catch (InvocationTargetException e) {
1735 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1736 } finally {}
1737 }
1738 }
1739
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 @Override
1741 public void onDetachedFromWindow() {
1742 super.onDetachedFromWindow();
1743 mVisible = false;
1744
Adam Cohend113e0c2010-11-11 10:48:05 -08001745 if (mAttached) {
1746 unregisterReceiver(mReceiver);
1747 mAttached = false;
1748 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 updateRunning();
1750 }
1751
1752 public void onWindowVisibilityChanged(int visibility) {
1753 mVisible = visibility == View.VISIBLE;
1754 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001755 // The following code used to be in onResume, but it turns out onResume is called when
1756 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1757 // is a more appropriate event to handle
1758 if (mVisible) {
1759 mAppsCustomizeTabHost.onWindowVisible();
1760 if (!mWorkspaceLoading) {
1761 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001762 // We want to let Launcher draw itself at least once before we force it to build
1763 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001764 // apps is nice and speedy.
1765 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001766 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001767 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001768 if (mStarted) return;
1769 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001770 // We delay the layer building a bit in order to give
1771 // other message processing a time to run. In particular
1772 // this avoids a delay in hiding the IME if it was
1773 // currently shown, because doing that may involve
1774 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001775 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001776 final ViewTreeObserver.OnDrawListener listener = this;
1777 mWorkspace.post(new Runnable() {
1778 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001779 if (mWorkspace != null &&
1780 mWorkspace.getViewTreeObserver() != null) {
1781 mWorkspace.getViewTreeObserver().
1782 removeOnDrawListener(listener);
1783 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001784 }
1785 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001786 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001787 }
1788 });
1789 }
1790 clearTypedText();
1791 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001792 }
1793
1794 private void sendAdvanceMessage(long delay) {
1795 mHandler.removeMessages(ADVANCE_MSG);
1796 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1797 mHandler.sendMessageDelayed(msg, delay);
1798 mAutoAdvanceSentTime = System.currentTimeMillis();
1799 }
1800
1801 private void updateRunning() {
1802 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1803 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1804 mAutoAdvanceRunning = autoAdvanceRunning;
1805 if (autoAdvanceRunning) {
1806 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1807 sendAdvanceMessage(delay);
1808 } else {
1809 if (!mWidgetsToAdvance.isEmpty()) {
1810 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1811 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1812 }
1813 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001814 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001815 }
1816 }
1817 }
1818
1819 private final Handler mHandler = new Handler() {
1820 @Override
1821 public void handleMessage(Message msg) {
1822 if (msg.what == ADVANCE_MSG) {
1823 int i = 0;
1824 for (View key: mWidgetsToAdvance.keySet()) {
1825 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1826 final int delay = mAdvanceStagger * i;
1827 if (v instanceof Advanceable) {
1828 postDelayed(new Runnable() {
1829 public void run() {
1830 ((Advanceable) v).advance();
1831 }
1832 }, delay);
1833 }
1834 i++;
1835 }
1836 sendAdvanceMessage(mAdvanceInterval);
1837 }
1838 }
1839 };
1840
1841 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001842 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001843 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1844 if (v instanceof Advanceable) {
1845 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001846 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001847 updateRunning();
1848 }
1849 }
1850
1851 void removeWidgetToAutoAdvance(View hostView) {
1852 if (mWidgetsToAdvance.containsKey(hostView)) {
1853 mWidgetsToAdvance.remove(hostView);
1854 updateRunning();
1855 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001856 }
1857
Joe Onorato9c1289c2009-08-17 11:03:03 -04001858 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001859 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001860 launcherInfo.hostView = null;
1861 }
1862
Winson Chung93eef082012-03-23 15:59:27 -07001863 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1864 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1865 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001866 }
1867
Adam Cohen59400422014-03-05 18:07:04 -08001868 public ArrayList<AppInfo> getAllAppsList() {
1869 return mAppsCustomizeContent.getApps();
1870 }
1871
Winson Chunga6945242014-01-08 14:04:34 -08001872 public DragLayer getDragLayer() {
1873 return mDragLayer;
1874 }
1875
1876 public Workspace getWorkspace() {
1877 return mWorkspace;
1878 }
1879
1880 public Hotseat getHotseat() {
1881 return mHotseat;
1882 }
1883
Jorim Jaggid017f882014-01-14 17:08:48 -08001884 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001885 return mOverviewPanel;
1886 }
1887
1888 public SearchDropTargetBar getSearchBar() {
1889 return mSearchDropTargetBar;
1890 }
1891
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001892 public LauncherAppWidgetHost getAppWidgetHost() {
1893 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 }
Romain Guycbb89e42009-06-08 15:52:54 -07001895
Winson Chunga9abd0e2010-10-27 17:18:37 -07001896 public LauncherModel getModel() {
1897 return mModel;
1898 }
1899
Winson Chunga6945242014-01-08 14:04:34 -08001900 protected SharedPreferences getSharedPrefs() {
1901 return mSharedPrefs;
1902 }
1903
Bjorn Bringertc459e522013-06-07 19:36:01 +01001904 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001905 getWindow().closeAllPanels();
1906
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001907 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001908 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001909 }
1910
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 @Override
1912 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001913 long startTime = 0;
1914 if (DEBUG_RESUME_TIME) {
1915 startTime = System.currentTimeMillis();
1916 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 super.onNewIntent(intent);
1918
1919 // Close the menu
1920 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001921 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001922 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001923
Adam Cohened307df2013-10-02 09:37:31 -07001924 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1925 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1926 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001927
Adam Cohen6fecd412013-10-02 17:41:50 -07001928 if (mWorkspace == null) {
1929 // Can be cases where mWorkspace is null, this prevents a NPE
1930 return;
1931 }
1932 Folder openFolder = mWorkspace.getOpenFolder();
1933 // In all these cases, only animate if we're already on home
1934 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001935
1936 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1937 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001938 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001939 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001940 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001941 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001942
Adam Cohen6fecd412013-10-02 17:41:50 -07001943 closeFolder();
1944 exitSpringLoadedDragMode();
1945
1946 // If we are already on home, then just animate back to the workspace,
1947 // otherwise, just wait until onResume to set the state back to Workspace
1948 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001949 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001950 } else {
1951 mOnResumeState = State.WORKSPACE;
1952 }
1953
1954 final View v = getWindow().peekDecorView();
1955 if (v != null && v.getWindowToken() != null) {
1956 InputMethodManager imm = (InputMethodManager)getSystemService(
1957 INPUT_METHOD_SERVICE);
1958 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1959 }
1960
1961 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001962 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001963 mAppsCustomizeTabHost.reset();
1964 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001965
Adam Cohen9211d422014-10-07 18:14:53 -07001966 if (mLauncherCallbacks != null) {
1967 mLauncherCallbacks.onHomeIntent();
1968 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
Adam Cohened307df2013-10-02 09:37:31 -07001970
Michael Jurka447bf842013-05-15 14:52:15 +02001971 if (DEBUG_RESUME_TIME) {
1972 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974
Adam Cohen9211d422014-10-07 18:14:53 -07001975 if (mLauncherCallbacks != null) {
1976 mLauncherCallbacks.onNewIntent(intent);
1977 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001978 }
1979
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001981 public void onRestoreInstanceState(Bundle state) {
1982 super.onRestoreInstanceState(state);
1983 for (int page: mSynchronouslyBoundPages) {
1984 mWorkspace.restoreInstanceStateForChild(page);
1985 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 }
1987
1988 @Override
1989 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001990 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001991 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1992 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001993 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001994 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995
Michael Jurka883f55b2010-10-21 15:47:14 -07001996 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001997 // We close any open folder since it will not be re-opened, and we need to make sure
1998 // this state is reflected.
1999 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000
Adam Cohendcd297f2013-06-18 13:13:40 -07002001 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002002 mWaitingForResult) {
2003 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002004 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002005 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2006 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002007 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2008 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2009 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002010 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 }
2012
2013 if (mFolderInfo != null && mWaitingForResult) {
2014 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2015 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2016 }
Michael Jurka946ad472010-07-09 18:05:18 -07002017
Winson Chung785d2eb2011-04-14 16:08:02 -07002018 // Save the current AppsCustomize tab
2019 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08002020 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2021 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002022 if (currentTabTag != null) {
2023 outState.putString("apps_customize_currentTab", currentTabTag);
2024 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002025 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2026 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002027 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002028 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002029
2030 if (mLauncherCallbacks != null) {
2031 mLauncherCallbacks.onSaveInstanceState(outState);
2032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 }
2034
2035 @Override
2036 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002038
Winson Chunge7a03942011-08-05 15:05:12 -07002039 // Remove all pending runnables
2040 mHandler.removeMessages(ADVANCE_MSG);
2041 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002042 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002043
Winson Chungcd2b0142011-06-08 16:02:26 -07002044 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002045 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002046
2047 // It's possible to receive onDestroy after a new Launcher activity has
2048 // been created. In this case, don't interfere with the new Launcher.
2049 if (mModel.isCurrentCallbacks(this)) {
2050 mModel.stopLoader();
2051 app.setLauncher(null);
2052 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002055 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002057 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002059 mAppWidgetHost = null;
2060
2061 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062
2063 TextKeyListener.getInstance().release();
2064
Winson Chung81b52252012-08-27 15:34:29 -07002065 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2066 // to prevent leaking Launcher activities on orientation change.
2067 if (mModel != null) {
2068 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2069 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002070
2071 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002072 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002073
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002074 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002075 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002076 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002077 mWorkspace = null;
2078 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002079
Michael Jurka2ecf9952012-06-18 12:52:28 -07002080 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002081
2082 if (mLauncherCallbacks != null) {
2083 mLauncherCallbacks.onDestroy();
2084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 }
2086
Adam Cohena9cf38f2011-05-02 15:36:58 -07002087 public DragController getDragController() {
2088 return mDragController;
2089 }
2090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 @Override
2092 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002093 if (requestCode >= 0) {
2094 setWaitingForResult(true);
2095 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 super.startActivityForResult(intent, requestCode);
2097 }
2098
Winson Chung70d72102011-08-12 11:24:35 -07002099 /**
2100 * Indicates that we want global search for this activity by setting the globalSearch
2101 * argument for {@link #startSearch} to true.
2102 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002104 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002106
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002107 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002108
Karl Rosaen138a0412009-04-23 19:00:21 -07002109 if (initialQuery == null) {
2110 // Use any text typed in the launcher as the initial query
2111 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002112 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 if (appSearchData == null) {
2114 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002115 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 }
Winson Chungadf0c182012-08-23 14:59:07 -07002117 Rect sourceBounds = new Rect();
2118 if (mSearchDropTargetBar != null) {
2119 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2120 }
Romain Guycbb89e42009-06-08 15:52:54 -07002121
Ian Parkinson047036e2014-09-01 15:40:53 +01002122 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002123 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002124 if (clearTextImmediately) {
2125 clearTypedText();
2126 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002127 }
2128
Ian Parkinson047036e2014-09-01 15:40:53 +01002129 /**
2130 * Start a text search.
2131 *
2132 * @return {@code true} if the search will start immediately, so any further keypresses
2133 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2134 * to buffer keypresses.
2135 */
2136 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002137 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002138 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2139 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2140 sourceBounds);
2141 }
2142
Michael Jurkaa33411c2012-06-14 16:18:21 -07002143 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002144 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002145 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002146 }
2147
2148 /**
2149 * Starts the global search activity. This code is a copied from SearchManager
2150 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002151 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002152 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002153 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002154 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2155 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2156 if (globalSearchActivity == null) {
2157 Log.w(TAG, "No global search activity found.");
2158 return;
2159 }
2160 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2161 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2162 intent.setComponent(globalSearchActivity);
2163 // Make sure that we have a Bundle to put source in
2164 if (appSearchData == null) {
2165 appSearchData = new Bundle();
2166 } else {
2167 appSearchData = new Bundle(appSearchData);
2168 }
Adam Cohen9211d422014-10-07 18:14:53 -07002169 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002170 if (!appSearchData.containsKey("source")) {
2171 appSearchData.putString("source", getPackageName());
2172 }
2173 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2174 if (!TextUtils.isEmpty(initialQuery)) {
2175 intent.putExtra(SearchManager.QUERY, initialQuery);
2176 }
2177 if (selectInitialQuery) {
2178 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2179 }
2180 intent.setSourceBounds(sourceBounds);
2181 try {
2182 startActivity(intent);
2183 } catch (ActivityNotFoundException ex) {
2184 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2185 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 }
2187
Yura4f93ec62014-02-04 14:15:21 +00002188 public boolean isOnCustomContent() {
2189 return mWorkspace.isOnOrMovingToCustomContent();
2190 }
2191
Winson Chung70d72102011-08-12 11:24:35 -07002192 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002193 public boolean onPrepareOptionsMenu(Menu menu) {
2194 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002195 if (!isOnCustomContent()) {
2196 // Close any open folders
2197 closeFolder();
2198 // Stop resizing any widgets
2199 mWorkspace.exitWidgetResizeMode();
2200 if (!mWorkspace.isInOverviewMode()) {
2201 // Show the overview mode
2202 showOverviewMode(true);
2203 } else {
2204 showWorkspace(true);
2205 }
Winson Chunge0298742014-01-17 12:03:00 -08002206 }
Adam Cohen9211d422014-10-07 18:14:53 -07002207 if (mLauncherCallbacks != null) {
2208 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2209 }
2210
Michael Jurkab94f3f82013-09-11 18:08:54 +02002211 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002214 @Override
2215 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002216 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002217 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002218 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002219 }
2220
Joe Onorato9c1289c2009-08-17 11:03:03 -04002221 public boolean isWorkspaceLocked() {
2222 return mWorkspaceLoading || mWaitingForResult;
2223 }
2224
Adam Cohen517a7f52014-03-01 12:12:59 -08002225 public boolean isWorkspaceLoading() {
2226 return mWorkspaceLoading;
2227 }
2228
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002229 private void setWorkspaceLoading(boolean value) {
2230 boolean isLocked = isWorkspaceLocked();
2231 mWorkspaceLoading = value;
2232 if (isLocked != isWorkspaceLocked()) {
2233 onWorkspaceLockedChanged();
2234 }
2235 }
2236
2237 private void setWaitingForResult(boolean value) {
2238 boolean isLocked = isWorkspaceLocked();
2239 mWaitingForResult = value;
2240 if (isLocked != isWorkspaceLocked()) {
2241 onWorkspaceLockedChanged();
2242 }
2243 }
2244
Adam Cohen9211d422014-10-07 18:14:53 -07002245 protected void onWorkspaceLockedChanged() {
2246 if (mLauncherCallbacks != null) {
2247 mLauncherCallbacks.onWorkspaceLockedChanged();
2248 }
2249 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002250
Michael Jurka0280c3b2010-09-17 15:00:07 -07002251 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002253 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002254 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2255 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002256 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002258 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002259
Sunny Goyale6b63a32015-01-16 16:14:29 -08002260 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002261 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002262 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2263 }
2264
Sunny Goyale6b63a32015-01-16 16:14:29 -08002265 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002266 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2267 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002268 if (appWidgetInfo.configure != null) {
2269 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002270 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002271
Bjorn Bringert7984c942009-12-09 15:38:25 +00002272 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002273 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2274 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2275
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002277 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002278 Runnable onComplete = new Runnable() {
2279 @Override
2280 public void run() {
2281 // Exit spring loaded mode if necessary after adding the widget
2282 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2283 null);
2284 }
2285 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002286 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002287 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002288 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002289 }
2290 }
Romain Guycbb89e42009-06-08 15:52:54 -07002291
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002292 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002293 // Close any folders that may be open.
2294 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002295 mWorkspace.moveToCustomContentScreen(animate);
2296 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002297 /**
2298 * Process a shortcut drop.
2299 *
2300 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002301 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002302 * @param cell The cell it should be added to, optional
2303 * @param position The location on the screen where it was dropped, optional
2304 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002305 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002306 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002307 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002308 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002309 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002310 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002311
2312 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002313 mPendingAddInfo.cellX = cell[0];
2314 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002315 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002316
2317 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2318 createShortcutIntent.setComponent(componentName);
2319 processShortcut(createShortcutIntent);
2320 }
2321
Adam Cohenfbba09b2011-07-18 21:43:05 -07002322 /**
2323 * Process a widget drop.
2324 *
2325 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002326 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 * @param cell The cell it should be added to, optional
2328 * @param position The location on the screen where it was dropped, optional
2329 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002330 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002331 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002332 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002334 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002335 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002336 mPendingAddInfo.minSpanX = info.minSpanX;
2337 mPendingAddInfo.minSpanY = info.minSpanY;
2338
Adam Cohenfbba09b2011-07-18 21:43:05 -07002339 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 mPendingAddInfo.cellX = cell[0];
2341 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002343 if (span != null) {
2344 mPendingAddInfo.spanX = span[0];
2345 mPendingAddInfo.spanY = span[1];
2346 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002347
Adam Cohened66b2b2012-01-23 17:28:51 -08002348 AppWidgetHostView hostView = info.boundWidget;
2349 int appWidgetId;
2350 if (hostView != null) {
2351 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002352 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002353 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002354 // In this case, we either need to start an activity to get permission to bind
2355 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002356 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002357 Bundle options = info.bindOptions;
2358
Sunny Goyalffe83f12014-08-14 17:39:34 -07002359 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2360 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002361 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002362 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002363 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002364 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002365 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2366 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2367 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002368 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2369 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002370 // TODO: we need to make sure that this accounts for the options bundle.
2371 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002372 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2373 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002374 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002375 }
2376
Joe Onoratodeb98af2010-02-19 14:59:39 -08002377 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002378 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002379 }
2380
Winson Chung24ab2f12010-09-16 14:10:47 -07002381 void processWallpaper(Intent intent) {
2382 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2383 }
2384
Adam Cohendcd297f2013-06-18 13:13:40 -07002385 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002386 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002387 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 folderInfo.title = getText(R.string.folder_name);
2389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002391 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002392 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002393 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394
2395 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002396 FolderIcon newFolder =
2397 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002398 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002399 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002400 // Force measure the new folder icon
2401 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2402 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002403 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 }
Romain Guycbb89e42009-06-08 15:52:54 -07002405
Joe Onorato9c1289c2009-08-17 11:03:03 -04002406 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002407 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002408 }
2409
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002410 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002411 if (mLauncherCallbacks != null) {
2412 return mLauncherCallbacks.getWallpaperPickerComponent();
2413 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002414 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002415 }
2416
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002417 /**
2418 * Registers various content observers. The current implementation registers
2419 * only a favorites observer to keep track of the favorites applications.
2420 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002421 private void registerContentObservers() {
2422 ContentResolver resolver = getContentResolver();
2423 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2424 true, mWidgetObserver);
2425 }
2426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 @Override
2428 public boolean dispatchKeyEvent(KeyEvent event) {
2429 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2430 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002432 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002433 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002434 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002435 dumpState();
2436 return true;
2437 }
2438 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002439 }
2440 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2441 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002442 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 return true;
2444 }
2445 }
2446
2447 return super.dispatchKeyEvent(event);
2448 }
2449
Joe Onorato88ec0992009-11-19 13:16:06 -08002450 @Override
2451 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002452 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2453 return;
2454 }
2455
Winson Chungc93e5ae2012-07-23 20:48:26 -07002456 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002457 if (mAppsCustomizeContent.getContentType() ==
2458 AppsCustomizePagedView.ContentType.Applications) {
2459 showWorkspace(true);
2460 } else {
2461 showOverviewMode(true);
2462 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002463 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002464 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002465 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002466 Folder openFolder = mWorkspace.getOpenFolder();
2467 if (openFolder.isEditingName()) {
2468 openFolder.dismissEditingName();
2469 } else {
2470 closeFolder();
2471 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002472 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002473 mWorkspace.exitWidgetResizeMode();
2474
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002475 // Back button is a no-op here, but give at least some feedback for the button press
2476 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002477 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002478 }
2479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002481 * Re-listen when widgets are reset.
2482 */
2483 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002484 if (mAppWidgetHost != null) {
2485 mAppWidgetHost.startListening();
2486 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002487 }
2488
2489 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002490 * Launches the intent referred by the clicked shortcut.
2491 *
2492 * @param v The view representing the clicked shortcut.
2493 */
2494 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002495 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2496 // view has detached (it's possible for this to happen if the view is removed mid touch).
2497 if (v.getWindowToken() == null) {
2498 return;
2499 }
2500
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002501 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002502 return;
2503 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002504
Adam Cohen1697b792013-09-17 19:08:21 -07002505 if (v instanceof Workspace) {
2506 if (mWorkspace.isInOverviewMode()) {
2507 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002508 }
2509 return;
2510 }
2511
2512 if (v instanceof CellLayout) {
2513 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002514 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002515 }
2516 }
2517
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002518 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002519 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002520 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002522 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002523 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002524 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002525 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002526 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002527 } else if (tag instanceof AppInfo) {
2528 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002529 } else if (tag instanceof LauncherAppWidgetInfo) {
2530 if (v instanceof PendingAppWidgetHostView) {
2531 onClickPendingWidget((PendingAppWidgetHostView) v);
2532 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 }
2534 }
2535
Sunny Goyal508da152014-08-14 10:53:27 -07002536 public void onClickPagedViewIcon(View v) {
2537 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002538 if (mLauncherCallbacks != null) {
2539 mLauncherCallbacks.onClickPagedViewIcon(v);
2540 }
Sunny Goyal508da152014-08-14 10:53:27 -07002541 }
2542
Michael Jurka0e260592010-06-30 17:07:39 -07002543 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002544 return false;
2545 }
2546
Michael Jurkaaf442092010-06-10 17:01:57 -07002547 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002548 * Event handler for the app widget view which has not fully restored.
2549 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002550 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002551 if (mIsSafeModeEnabled) {
2552 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2553 return;
2554 }
2555
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002556 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002557 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002558 int widgetId = info.appWidgetId;
2559 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2560 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002561 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2562 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002563 mPendingAddInfo.copyFrom(info);
2564 mPendingAddWidgetId = widgetId;
2565
Sunny Goyalffe83f12014-08-14 17:39:34 -07002566 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2567 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002568 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002569 } else if (info.installProgress < 0) {
2570 // The install has not been queued
2571 final String packageName = info.providerName.getPackageName();
2572 showBrokenAppInstallDialog(packageName,
2573 new DialogInterface.OnClickListener() {
2574 public void onClick(DialogInterface dialog, int id) {
2575 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2576 }
2577 });
2578 } else {
2579 // Download has started.
2580 final String packageName = info.providerName.getPackageName();
2581 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002582 }
2583 }
2584
2585 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002586 * Event handler for the "grid" button that appears on the home screen, which
2587 * enters all apps mode.
2588 *
2589 * @param v The view that was clicked.
2590 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002591 protected void onClickAllAppsButton(View v) {
2592 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2593 if (isAllAppsVisible()) {
2594 showWorkspace(true);
2595 } else {
2596 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2597 }
Adam Cohen9211d422014-10-07 18:14:53 -07002598 if (mLauncherCallbacks != null) {
2599 mLauncherCallbacks.onClickAllAppsButton(v);
2600 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002601 }
2602
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002603 private void showBrokenAppInstallDialog(final String packageName,
2604 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002605 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002606 .setTitle(R.string.abandoned_promises_title)
2607 .setMessage(R.string.abandoned_promise_explanation)
2608 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2609 .setNeutralButton(R.string.abandoned_clean_this,
2610 new DialogInterface.OnClickListener() {
2611 public void onClick(DialogInterface dialog, int id) {
2612 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2613 mWorkspace.removeAbandonedPromise(packageName, user);
2614 }
2615 })
2616 .create().show();
2617 return;
2618 }
2619
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002620 /**
2621 * Event handler for an app shortcut click.
2622 *
2623 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2624 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002625 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002626 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2627 Object tag = v.getTag();
2628 if (!(tag instanceof ShortcutInfo)) {
2629 throw new IllegalArgumentException("Input must be a Shortcut");
2630 }
2631
2632 // Open shortcut
2633 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002634
2635 if (shortcut.isDisabled != 0) {
2636 int error = R.string.activity_not_available;
2637 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2638 error = R.string.safemode_shortcut_error;
2639 }
2640 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2641 return;
2642 }
2643
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002644 final Intent intent = shortcut.intent;
2645
2646 // Check for special shortcuts
2647 if (intent.getComponent() != null) {
2648 final String shortcutClass = intent.getComponent().getClassName();
2649
2650 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2651 MemoryDumpActivity.startDump(this);
2652 return;
2653 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2654 toggleShowWeightWatcher();
2655 return;
2656 }
2657 }
2658
Chris Wren40c5ed32014-06-24 18:24:23 -04002659 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002660 if ((v instanceof BubbleTextView)
2661 && shortcut.isPromise()
2662 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002663 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002664 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002665 new DialogInterface.OnClickListener() {
2666 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002667 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002668 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002669 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002670 return;
2671 }
2672
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002673 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002674 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002675
2676 if (mLauncherCallbacks != null) {
2677 mLauncherCallbacks.onClickAppShortcut(v);
2678 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002679 }
2680
Sunny Goyal508da152014-08-14 10:53:27 -07002681 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002682 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002683 final ShortcutInfo shortcut;
2684 final Intent intent;
2685 if (tag instanceof ShortcutInfo) {
2686 shortcut = (ShortcutInfo) tag;
2687 intent = shortcut.intent;
2688 int[] pos = new int[2];
2689 v.getLocationOnScreen(pos);
2690 intent.setSourceBounds(new Rect(pos[0], pos[1],
2691 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002692
Sunny Goyal508da152014-08-14 10:53:27 -07002693 } else if (tag instanceof AppInfo) {
2694 shortcut = null;
2695 intent = ((AppInfo) tag).intent;
2696 } else {
2697 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2698 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002699
2700 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002701 mStats.recordLaunch(intent, shortcut);
2702
2703 if (success && v instanceof BubbleTextView) {
2704 mWaitingForResume = (BubbleTextView) v;
2705 mWaitingForResume.setStayPressed(true);
2706 }
2707 }
2708
2709 /**
2710 * Event handler for a folder icon click.
2711 *
2712 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2713 */
2714 protected void onClickFolderIcon(View v) {
2715 if (LOGD) Log.d(TAG, "onClickFolder");
2716 if (!(v instanceof FolderIcon)){
2717 throw new IllegalArgumentException("Input must be a FolderIcon");
2718 }
2719
2720 FolderIcon folderIcon = (FolderIcon) v;
2721 final FolderInfo info = folderIcon.getFolderInfo();
2722 Folder openFolder = mWorkspace.getFolderForTag(info);
2723
2724 // If the folder info reports that the associated folder is open, then verify that
2725 // it is actually opened. There have been a few instances where this gets out of sync.
2726 if (info.opened && openFolder == null) {
2727 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2728 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2729 info.opened = false;
2730 }
2731
2732 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2733 // Close any open folder
2734 closeFolder();
2735 // Open the requested folder
2736 openFolder(folderIcon);
2737 } else {
2738 // Find the open folder...
2739 int folderScreen;
2740 if (openFolder != null) {
2741 folderScreen = mWorkspace.getPageForView(openFolder);
2742 // .. and close it
2743 closeFolder(openFolder);
2744 if (folderScreen != mWorkspace.getCurrentPage()) {
2745 // Close any folder open on the current screen
2746 closeFolder();
2747 // Pull the folder onto this screen
2748 openFolder(folderIcon);
2749 }
2750 }
2751 }
Adam Cohen9211d422014-10-07 18:14:53 -07002752
2753 if (mLauncherCallbacks != null) {
2754 mLauncherCallbacks.onClickFolderIcon(v);
2755 }
Michael Jurka5130e402011-10-13 04:55:35 -07002756 }
2757
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002758 /**
2759 * Event handler for the (Add) Widgets button that appears after a long press
2760 * on the home screen.
2761 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002762 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002763 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002764 if (mIsSafeModeEnabled) {
2765 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2766 } else {
2767 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2768 if (mLauncherCallbacks != null) {
2769 mLauncherCallbacks.onClickAddWidgetButton(view);
2770 }
Adam Cohen9211d422014-10-07 18:14:53 -07002771 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002772 }
2773
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002774 /**
2775 * Event handler for the wallpaper picker button that appears after a long press
2776 * on the home screen.
2777 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002778 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002779 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2780 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2781 pickWallpaper.setComponent(getWallpaperPickerComponent());
2782 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002783
2784 if (mLauncherCallbacks != null) {
2785 mLauncherCallbacks.onClickWallpaperPicker(v);
2786 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002787 }
2788
2789 /**
2790 * Event handler for a click on the settings button that appears after a long press
2791 * on the home screen.
2792 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002793 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002794 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002795 if (mLauncherCallbacks != null) {
2796 mLauncherCallbacks.onClickSettingsButton(v);
2797 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002798 }
2799
Michael Jurka5130e402011-10-13 04:55:35 -07002800 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002801 // Provide the same haptic feedback that the system offers for virtual keys.
2802 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002803 }
2804
Adam Cohen61f560d2013-09-30 15:58:20 -07002805 public void performHapticFeedbackOnTouchDown(View v) {
2806 // Provide the same haptic feedback that the system offers for virtual keys.
2807 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2808 }
2809
2810 public View.OnTouchListener getHapticFeedbackTouchListener() {
2811 if (mHapticFeedbackTouchListener == null) {
2812 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2813 @Override
2814 public boolean onTouch(View v, MotionEvent event) {
2815 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2816 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2817 }
2818 return false;
2819 }
2820 };
2821 }
2822 return mHapticFeedbackTouchListener;
2823 }
2824
Adam Cohen9211d422014-10-07 18:14:53 -07002825 public void onDragStarted(View view) {
2826 if (isOnCustomContent()) {
2827 // Custom content screen doesn't participate in drag and drop. If on custom
2828 // content screen, move to default.
2829 moveWorkspaceToDefaultScreen();
2830 }
2831
2832 if (mLauncherCallbacks != null) {
2833 mLauncherCallbacks.onDragStarted(view);
2834 }
2835 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002836
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002837 /**
2838 * Called when the user stops interacting with the launcher.
2839 * This implies that the user is now on the homescreen and is not doing housekeeping.
2840 */
Adam Cohen9211d422014-10-07 18:14:53 -07002841 protected void onInteractionEnd() {
2842 if (mLauncherCallbacks != null) {
2843 mLauncherCallbacks.onInteractionEnd();
2844 }
2845 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002846
2847 /**
2848 * Called when the user starts interacting with the launcher.
2849 * The possible interactions are:
2850 * - open all apps
2851 * - reorder an app shortcut, or a widget
2852 * - open the overview mode.
2853 * This is a good time to stop doing things that only make sense
2854 * when the user is on the homescreen and not doing housekeeping.
2855 */
Adam Cohen9211d422014-10-07 18:14:53 -07002856 protected void onInteractionBegin() {
2857 if (mLauncherCallbacks != null) {
2858 mLauncherCallbacks.onInteractionBegin();
2859 }
2860 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002861
Kenny Guyf07af7b2014-07-31 11:39:16 +01002862 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002863 try {
2864 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002865 launcherApps.showAppDetailsForProfile(componentName, user);
2866 } catch (SecurityException e) {
2867 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2868 Log.e(TAG, "Launcher does not have permission to launch settings");
2869 } catch (ActivityNotFoundException e) {
2870 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2871 Log.e(TAG, "Unable to launch settings");
2872 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002873 }
2874
Michael Jurka1e2f4652013-07-08 18:03:46 -07002875 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002876 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2877 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002878 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002879 // System applications cannot be installed. For now, show a toast explaining that.
2880 // We may give them the option of disabling apps this way.
2881 int messageId = R.string.uninstall_system_app_text;
2882 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002883 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002884 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002885 String packageName = componentName.getPackageName();
2886 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002887 Intent intent = new Intent(
2888 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002889 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2890 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002891 if (user != null) {
2892 user.addToIntent(intent, Intent.EXTRA_USER);
2893 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002894 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002895 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002896 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002897 }
2898
Michael Jurka86a720e2012-05-09 11:23:23 -07002899 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002900 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002901 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002902 // Only launch using the new animation if the shortcut has not opted out (this is a
2903 // private contract between launcher and may be ignored in the future).
2904 boolean useLaunchAnimation = (v != null) &&
2905 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002906 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2907 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002908
Kenny Guy1317e2d2014-05-08 18:52:50 +01002909 UserHandleCompat user = null;
2910 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2911 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2912 user = userManager.getUserForSerialNumber(serialNumber);
2913 }
2914
2915 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002916 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002917 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002918 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2919 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002920 optsBundle = opts.toBundle();
2921 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002922
2923 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2924 // Could be launching some bookkeeping activity
2925 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002926 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002927 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002928 launcherApps.startActivityForProfile(intent.getComponent(), user,
2929 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002930 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002931 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 } catch (SecurityException e) {
2933 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002934 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002935 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002936 "or use the exported attribute for this activity. "
2937 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002939 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002940 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002941
Michael Jurka86a720e2012-05-09 11:23:23 -07002942 boolean startActivitySafely(View v, Intent intent, Object tag) {
2943 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002944 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2945 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2946 return false;
2947 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002948 try {
2949 success = startActivity(v, intent, tag);
2950 } catch (ActivityNotFoundException e) {
2951 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2952 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2953 }
2954 return success;
2955 }
2956
Adam Cohen268c4752012-06-06 17:47:33 -07002957 /**
2958 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2959 * in the DragLayer in the exact absolute location of the original FolderIcon.
2960 */
2961 private void copyFolderIconToImage(FolderIcon fi) {
2962 final int width = fi.getMeasuredWidth();
2963 final int height = fi.getMeasuredHeight();
2964
2965 // Lazy load ImageView, Bitmap and Canvas
2966 if (mFolderIconImageView == null) {
2967 mFolderIconImageView = new ImageView(this);
2968 }
2969 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2970 mFolderIconBitmap.getHeight() != height) {
2971 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2972 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2973 }
2974
2975 DragLayer.LayoutParams lp;
2976 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2977 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2978 } else {
2979 lp = new DragLayer.LayoutParams(width, height);
2980 }
2981
Adam Cohen307fe232012-08-16 17:55:58 -07002982 // The layout from which the folder is being opened may be scaled, adjust the starting
2983 // view size by this scale factor.
2984 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002985 lp.customPosition = true;
2986 lp.x = mRectForFolderAnimation.left;
2987 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002988 lp.width = (int) (scale * width);
2989 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002990
2991 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2992 fi.draw(mFolderIconCanvas);
2993 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002994 if (fi.getFolder() != null) {
2995 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2996 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002997 }
Adam Cohen268c4752012-06-06 17:47:33 -07002998 // Just in case this image view is still in the drag layer from a previous animation,
2999 // we remove it and re-add it.
3000 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3001 mDragLayer.removeView(mFolderIconImageView);
3002 }
3003 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003004 if (fi.getFolder() != null) {
3005 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003006 }
Adam Cohen268c4752012-06-06 17:47:33 -07003007 }
3008
Adam Cohen2801caf2011-05-13 20:57:39 -07003009 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003010 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003011 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3012 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3013 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3014
Adam Cohenc51934b2011-07-26 21:07:43 -07003015 FolderInfo info = (FolderInfo) fi.getTag();
3016 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3017 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003018 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3019 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003020 }
3021
Adam Cohen268c4752012-06-06 17:47:33 -07003022 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3023 copyFolderIconToImage(fi);
3024 fi.setVisibility(View.INVISIBLE);
3025
Michael Jurka2ecf9952012-06-18 12:52:28 -07003026 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003027 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003028 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003029 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3030 }
3031 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003032 oa.start();
3033 }
3034
Adam Cohen268c4752012-06-06 17:47:33 -07003035 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003036 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003037 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3038 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3039 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3040
Adam Cohen268c4752012-06-06 17:47:33 -07003041 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003042
Adam Cohen268c4752012-06-06 17:47:33 -07003043 // We remove and re-draw the FolderIcon in-case it has changed
3044 mDragLayer.removeView(mFolderIconImageView);
3045 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003046 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003047 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003048 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003049 oa.addListener(new AnimatorListenerAdapter() {
3050 @Override
3051 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003052 if (cl != null) {
3053 cl.clearFolderLeaveBehind();
3054 // Remove the ImageView copy of the FolderIcon and make the original visible.
3055 mDragLayer.removeView(mFolderIconImageView);
3056 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003057 }
3058 }
3059 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003060 oa.start();
3061 }
3062
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003063 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003064 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003065 * is animated relative to the specified View. If the View is null, no animation
3066 * is played.
3067 *
3068 * @param folderInfo The FolderInfo describing the folder to open.
3069 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003070 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003071 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003072 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003073
Adam Cohena9cf38f2011-05-02 15:36:58 -07003074 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003075
Adam Cohen4554ee12011-08-03 16:13:21 -07003076 // Just verify that the folder hasn't already been added to the DragLayer.
3077 // There was a one-off crash where the folder had a parent already.
3078 if (folder.getParent() == null) {
3079 mDragLayer.addView(folder);
3080 mDragController.addDropTarget((DropTarget) folder);
3081 } else {
3082 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3083 folder.getParent() + ").");
3084 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003085 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003086 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003087
3088 // Notify the accessibility manager that this folder "window" has appeared and occluded
3089 // the workspace items
3090 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3091 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003092 }
3093
3094 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003095 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003096 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003097 if (folder.isEditingName()) {
3098 folder.dismissEditingName();
3099 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003100 closeFolder(folder);
3101 }
3102 }
3103
3104 void closeFolder(Folder folder) {
3105 folder.getInfo().opened = false;
3106
3107 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3108 if (parent != null) {
3109 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3110 shrinkAndFadeInFolderIcon(fi);
3111 }
3112 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003113
3114 // Notify the accessibility manager that this folder "window" has disappeard and no
3115 // longer occludeds the workspace items
3116 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003117 }
3118
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003119 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003120 if (!isDraggingEnabled()) return false;
3121 if (isWorkspaceLocked()) return false;
3122 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003123
Adam Cohen1697b792013-09-17 19:08:21 -07003124 if (v instanceof Workspace) {
3125 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003126 if (mWorkspace.enterOverviewMode()) {
3127 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3128 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3129 return true;
3130 } else {
3131 return false;
3132 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003133 } else {
3134 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003135 }
Adam Cohen1697b792013-09-17 19:08:21 -07003136 }
3137
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003138 CellLayout.CellInfo longClickCellInfo = null;
3139 View itemUnderLongClick = null;
3140 if (v.getTag() instanceof ItemInfo) {
3141 ItemInfo info = (ItemInfo) v.getTag();
3142 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3143 itemUnderLongClick = longClickCellInfo.cell;
3144 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 }
3146
Winson Chung3d503fb2011-07-13 17:25:49 -07003147 // The hotseat touch handling does not go through Workspace, and we always allow long press
3148 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003149 final boolean inHotseat = isHotseatLayout(v);
3150 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003151 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003152 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003153 // User long pressed on empty space
3154 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3155 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003156 if (mWorkspace.isInOverviewMode()) {
3157 mWorkspace.startReordering(v);
3158 } else {
3159 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003161 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003162 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3163 mHotseat.getOrderInHotseat(
3164 longClickCellInfo.cellX,
3165 longClickCellInfo.cellY));
3166 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003168 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003169 }
3170 }
3171 }
3172 return true;
3173 }
3174
Winson Chung3d503fb2011-07-13 17:25:49 -07003175 boolean isHotseatLayout(View layout) {
3176 return mHotseat != null && layout != null &&
3177 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3178 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003179
Winson Chung3d503fb2011-07-13 17:25:49 -07003180 /**
3181 * Returns the CellLayout of the specified container at the specified screen.
3182 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003183 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003184 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3185 if (mHotseat != null) {
3186 return mHotseat.getLayout();
3187 } else {
3188 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003189 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003190 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003191 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003192 }
3193 }
3194
Daniel Sandler843e8602010-06-07 14:59:01 -04003195 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003196 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003197 }
3198
Craig Mautner360310b2012-10-26 15:13:08 -07003199 private void setWorkspaceBackground(boolean workspace) {
3200 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003201 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003202 }
3203
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003204 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003205 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3206 int curflags = getWindow().getAttributes().flags
3207 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3208 if (wpflags != curflags) {
3209 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3210 }
Craig Mautner360310b2012-10-26 15:13:08 -07003211 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003212 }
3213
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003214 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3215 if (v instanceof LauncherTransitionable) {
3216 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3217 }
3218 }
3219
Michael Jurkabed61d22012-02-14 22:51:29 -08003220 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3221 if (v instanceof LauncherTransitionable) {
3222 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3223 }
Winson Chung70442722012-02-10 15:43:22 -08003224
3225 // Update the workspace transition step as well
3226 dispatchOnLauncherTransitionStep(v, 0f);
3227 }
3228
3229 private void dispatchOnLauncherTransitionStep(View v, float t) {
3230 if (v instanceof LauncherTransitionable) {
3231 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3232 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003233 }
3234
3235 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3236 if (v instanceof LauncherTransitionable) {
3237 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3238 }
Winson Chung70442722012-02-10 15:43:22 -08003239
3240 // Update the workspace transition step as well
3241 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003242 }
3243
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003244 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003245 * Things to test when changing the following seven functions.
3246 * - Home from workspace
3247 * - from center screen
3248 * - from other screens
3249 * - Home from all apps
3250 * - from center screen
3251 * - from other screens
3252 * - Back from all apps
3253 * - from center screen
3254 * - from other screens
3255 * - Launch app from workspace and quit
3256 * - with back
3257 * - with home
3258 * - Launch app from all apps and quit
3259 * - with back
3260 * - with home
3261 * - Go to a screen that's not the default, then all
3262 * apps, and launch and app, and go back
3263 * - with back
3264 * -with home
3265 * - On workspace, long press power and go back
3266 * - with back
3267 * - with home
3268 * - On all apps, long press power and go back
3269 * - with back
3270 * - with home
3271 * - On workspace, power off
3272 * - On all apps, power off
3273 * - Launch an app and turn off the screen while in that app
3274 * - Go back with home key
3275 * - Go back with back key TODO: make this not go to workspace
3276 * - From all apps
3277 * - From workspace
3278 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3279 * - From all apps
3280 * - From the center workspace
3281 * - From another workspace
3282 */
3283
3284 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003285 * Zoom the camera out from the workspace to reveal 'toView'.
3286 * Assumes that the view to show is anchored at either the very top or very bottom
3287 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003288 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003289 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003290 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3291 showAppsCustomizeHelper(animated, springLoaded, contentType);
3292 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003293
Winson Chungc58497e2013-09-03 17:48:37 -07003294 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3295 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003296 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003297 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003298 mStateAnimation.cancel();
3299 mStateAnimation = null;
3300 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003301
Kenny Guyd794a3f2014-09-16 15:17:58 +01003302 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003303
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003304 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003305
Winson Chungf0ea4d32011-06-06 14:27:16 -07003306 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3307 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003308 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003309 final int itemsAlphaStagger =
3310 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003311
Winson Chungf0ea4d32011-06-06 14:27:16 -07003312 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003313 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003314 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003315
Adam Cohen2854d252014-08-27 16:04:07 -07003316 final ArrayList<View> layerViews = new ArrayList<View>();
3317
Adam Cohen6c5891a2014-07-09 23:53:15 -07003318 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3319 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003320 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003321 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003322 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003323 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3324 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003325 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3326 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003327
Adam Cohena38dc902014-09-07 17:48:55 +02003328 // If for some reason our views aren't initialized, don't animate
3329 boolean initialized = getAllAppsButton() != null;
3330
3331 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003332 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003333 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3334 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003335
Adam Cohen9bfdb762014-07-21 17:44:06 -07003336 final View page = content.getPageAt(content.getCurrentPage());
3337 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003338
Adam Cohen63f1ec02014-08-12 09:23:13 -07003339 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3340 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003341 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3342 } else {
3343 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3344 }
3345
Adam Cohen9bfdb762014-07-21 17:44:06 -07003346 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003347 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003348 revealView.setVisibility(View.VISIBLE);
3349 // We need to hide this view as the animation start will be posted.
3350 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003351
Adam Cohen9bfdb762014-07-21 17:44:06 -07003352 int width = revealView.getMeasuredWidth();
3353 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003354 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003355
Adam Cohen63f1ec02014-08-12 09:23:13 -07003356 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003357 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003358
Adam Cohen63f1ec02014-08-12 09:23:13 -07003359 // Get the y delta between the center of the page and the center of the all apps button
3360 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3361 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003362
Adam Cohen2854d252014-08-27 16:04:07 -07003363 float alpha = 0;
3364 float xDrift = 0;
3365 float yDrift = 0;
3366 if (material) {
3367 alpha = isWidgetTray ? 0.3f : 1f;
3368 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3369 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3370 } else {
3371 yDrift = 2 * height / 3;
3372 xDrift = 0;
3373 }
3374 final float initAlpha = alpha;
3375
Adam Cohen9bfdb762014-07-21 17:44:06 -07003376 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003377 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003378 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003379 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003380 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003381 PropertyValuesHolder panelDriftX =
3382 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003383
Adam Cohen2854d252014-08-27 16:04:07 -07003384 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3385 panelAlpha, panelDriftY, panelDriftX);
3386
Adam Cohen9bfdb762014-07-21 17:44:06 -07003387 panelAlphaAndDrift.setDuration(revealDuration);
3388 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003389
Adam Cohen9bfdb762014-07-21 17:44:06 -07003390 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003391
Adam Cohen4e243a22014-08-10 18:30:55 -07003392 if (page != null) {
3393 page.setVisibility(View.VISIBLE);
3394 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003395 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003396
Adam Cohen2854d252014-08-27 16:04:07 -07003397 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3398 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003399 pageDrift.setDuration(revealDuration);
3400 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003401 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003402 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003403
Adam Cohen63f1ec02014-08-12 09:23:13 -07003404 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003405 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003406 itemsAlpha.setDuration(revealDuration);
3407 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3408 itemsAlpha.setStartDelay(itemsAlphaStagger);
3409 mStateAnimation.play(itemsAlpha);
3410 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003411
Adam Cohen4e243a22014-08-10 18:30:55 -07003412 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3413 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003414 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003415 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003416 indicatorsAlpha.setDuration(revealDuration);
3417 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003418
Adam Cohen9bfdb762014-07-21 17:44:06 -07003419 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003420 final View allApps = getAllAppsButton();
3421 int allAppsButtonSize = LauncherAppState.getInstance().
3422 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3423 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003424 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003425 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003426 reveal.setDuration(revealDuration);
3427 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003428
3429 reveal.addListener(new AnimatorListenerAdapter() {
3430 public void onAnimationStart(Animator animation) {
3431 if (!isWidgetTray) {
3432 allApps.setVisibility(View.INVISIBLE);
3433 }
3434 }
3435 public void onAnimationEnd(Animator animation) {
3436 if (!isWidgetTray) {
3437 allApps.setVisibility(View.VISIBLE);
3438 }
3439 }
3440 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003441 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003442 }
Michael Jurka1899a362011-11-03 13:50:45 -07003443
Adam Cohen9bfdb762014-07-21 17:44:06 -07003444 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3445 @Override
3446 public void onAnimationEnd(Animator animation) {
3447 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3448 dispatchOnLauncherTransitionEnd(toView, animated, false);
3449
3450 revealView.setVisibility(View.INVISIBLE);
3451 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003452 if (page != null) {
3453 page.setLayerType(View.LAYER_TYPE_NONE, null);
3454 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003455 content.setPageBackgroundsVisible(true);
3456
3457 // Hide the search bar
3458 if (mSearchDropTargetBar != null) {
3459 mSearchDropTargetBar.hideSearchBar(false);
3460 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003461
3462 // This can hold unnecessary references to views.
3463 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003464 }
3465
Adam Cohen9bfdb762014-07-21 17:44:06 -07003466 });
3467
Adam Cohen6c5891a2014-07-09 23:53:15 -07003468 if (workspaceAnim != null) {
3469 mStateAnimation.play(workspaceAnim);
3470 }
Adam Cohen2854d252014-08-27 16:04:07 -07003471
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003472 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3473 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003474 final AnimatorSet stateAnimation = mStateAnimation;
3475 final Runnable startAnimRunnable = new Runnable() {
3476 public void run() {
3477 // Check that mStateAnimation hasn't changed while
3478 // we waited for a layout/draw pass
3479 if (mStateAnimation != stateAnimation)
3480 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003481 dispatchOnLauncherTransitionStart(fromView, animated, false);
3482 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003483
3484 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003485 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003486 for (int i = 0; i < layerViews.size(); i++) {
3487 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003488 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003489 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003490 }
Adam Cohen2854d252014-08-27 16:04:07 -07003491 }
3492 }
3493 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003494 }
3495 };
Adam Cohen2854d252014-08-27 16:04:07 -07003496 toView.bringToFront();
3497 toView.setVisibility(View.VISIBLE);
3498 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003499 } else {
3500 toView.setTranslationX(0.0f);
3501 toView.setTranslationY(0.0f);
3502 toView.setScaleX(1.0f);
3503 toView.setScaleY(1.0f);
3504 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003505 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003506
Daniel Sandlere4f98912013-06-25 15:13:26 -04003507 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003508 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003509 if (mSearchDropTargetBar != null) {
3510 mSearchDropTargetBar.hideSearchBar(false);
3511 }
Michael Jurkaabded662011-03-04 12:06:57 -08003512 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003513 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003514 dispatchOnLauncherTransitionStart(fromView, animated, false);
3515 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003516 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003517 dispatchOnLauncherTransitionStart(toView, animated, false);
3518 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003519 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003520 }
3521
3522 /**
3523 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003524 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003525 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003526 */
Adam Cohened307df2013-10-02 09:37:31 -07003527 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003528 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003529
Michael Jurkab3e22d92011-10-31 15:58:33 -07003530 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003531 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003532 mStateAnimation.cancel();
3533 mStateAnimation = null;
3534 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003535
Kenny Guyd794a3f2014-09-16 15:17:58 +01003536 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003537 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003538
Winson Chungf0ea4d32011-06-06 14:27:16 -07003539 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003540 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
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 float scaleFactor = (float)
3546 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3547 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003548 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003549 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003550 final ArrayList<View> layerViews = new ArrayList<View>();
3551
Adam Cohened307df2013-10-02 09:37:31 -07003552 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003553 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003554 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003555 } else if (toState == Workspace.State.SPRING_LOADED ||
3556 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003557 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003558 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003559 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003560
Adam Cohena38dc902014-09-07 17:48:55 +02003561 // If for some reason our views aren't initialized, don't animate
3562 boolean initialized = getAllAppsButton() != null;
3563
3564 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003565 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003566 if (workspaceAnim != null) {
3567 mStateAnimation.play(workspaceAnim);
3568 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003569
Adam Cohen9bfdb762014-07-21 17:44:06 -07003570 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3571 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003572
Adam Cohen9bfdb762014-07-21 17:44:06 -07003573 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003574
3575 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3576 int count = content.getChildCount();
3577 for (int i = 0; i < count; i++) {
3578 View child = content.getChildAt(i);
3579 if (child != page) {
3580 child.setVisibility(View.INVISIBLE);
3581 }
3582 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003583 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003584
Adam Cohen2854d252014-08-27 16:04:07 -07003585 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3586 // don't perform all these no-op animations. In particularly, this was causing
3587 // the all-apps button to pop in and out.
3588 if (fromView.getVisibility() == View.VISIBLE) {
3589 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3590 final boolean isWidgetTray =
3591 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003592
Adam Cohen2854d252014-08-27 16:04:07 -07003593 if (isWidgetTray) {
3594 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3595 } else {
3596 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003597 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003598
Adam Cohen2854d252014-08-27 16:04:07 -07003599 int width = revealView.getMeasuredWidth();
3600 int height = revealView.getMeasuredHeight();
3601 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3602
3603 // Hide the real page background, and swap in the fake one
3604 revealView.setVisibility(View.VISIBLE);
3605 content.setPageBackgroundsVisible(false);
3606
3607 final View allAppsButton = getAllAppsButton();
3608 revealView.setTranslationY(0);
3609 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3610 allAppsButton, null);
3611
3612 float xDrift = 0;
3613 float yDrift = 0;
3614 if (material) {
3615 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3616 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3617 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003618 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003619 xDrift = 0;
3620 }
3621
3622 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3623 TimeInterpolator decelerateInterpolator = material ?
3624 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003625 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003626
3627 // The vertical motion of the apps panel should be delayed by one frame
3628 // from the conceal animation in order to give the right feel. We correpsondingly
3629 // shorten the duration so that the slide and conceal end at the same time.
3630 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3631 0, yDrift);
3632 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3633 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3634 panelDriftY.setInterpolator(decelerateInterpolator);
3635 mStateAnimation.play(panelDriftY);
3636
3637 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3638 0, xDrift);
3639 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3640 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3641 panelDriftX.setInterpolator(decelerateInterpolator);
3642 mStateAnimation.play(panelDriftX);
3643
3644 if (isWidgetTray || !material) {
3645 float finalAlpha = material ? 0.4f : 0f;
3646 revealView.setAlpha(1f);
3647 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3648 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003649 panelAlpha.setDuration(material ? revealDuration : 150);
3650 panelAlpha.setInterpolator(decelerateInterpolator);
3651 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003652 mStateAnimation.play(panelAlpha);
3653 }
3654
3655 if (page != null) {
3656 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3657
3658 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3659 0, yDrift);
3660 page.setTranslationY(0);
3661 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3662 pageDrift.setInterpolator(decelerateInterpolator);
3663 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3664 mStateAnimation.play(pageDrift);
3665
3666 page.setAlpha(1f);
3667 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3668 itemsAlpha.setDuration(100);
3669 itemsAlpha.setInterpolator(decelerateInterpolator);
3670 mStateAnimation.play(itemsAlpha);
3671 }
3672
3673 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3674 pageIndicators.setAlpha(1f);
3675 ObjectAnimator indicatorsAlpha =
3676 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3677 indicatorsAlpha.setDuration(revealDuration);
3678 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3679 mStateAnimation.play(indicatorsAlpha);
3680
3681 width = revealView.getMeasuredWidth();
3682
3683 if (material) {
3684 if (!isWidgetTray) {
3685 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003686 }
Adam Cohen2854d252014-08-27 16:04:07 -07003687 int allAppsButtonSize = LauncherAppState.getInstance().
3688 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3689 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3690 Animator reveal =
3691 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3692 height / 2, revealRadius, finalRadius);
3693 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3694 reveal.setDuration(revealDuration);
3695 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003696
Adam Cohen2854d252014-08-27 16:04:07 -07003697 reveal.addListener(new AnimatorListenerAdapter() {
3698 public void onAnimationEnd(Animator animation) {
3699 revealView.setVisibility(View.INVISIBLE);
3700 if (!isWidgetTray) {
3701 allAppsButton.setVisibility(View.VISIBLE);
3702 }
3703 }
3704 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003705
Adam Cohen2854d252014-08-27 16:04:07 -07003706 mStateAnimation.play(reveal);
3707 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003708
Adam Cohen2854d252014-08-27 16:04:07 -07003709 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3710 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3711 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003712 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003713
3714 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003715 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003716 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003717 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003718 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3719 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003720 if (onCompleteRunnable != null) {
3721 onCompleteRunnable.run();
3722 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003723
3724 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003725 if (page != null) {
3726 page.setLayerType(View.LAYER_TYPE_NONE, null);
3727 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003728 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003729 // Unhide side pages
3730 int count = content.getChildCount();
3731 for (int i = 0; i < count; i++) {
3732 View child = content.getChildAt(i);
3733 child.setVisibility(View.VISIBLE);
3734 }
3735
3736 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003737 if (page != null) {
3738 page.setTranslationX(0);
3739 page.setTranslationY(0);
3740 page.setAlpha(1);
3741 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003742 content.setCurrentPage(content.getNextPage());
3743
Chet Haasebc2f0822012-10-26 17:59:53 -07003744 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003745
3746 // This can hold unnecessary references to views.
3747 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003748 }
3749 });
3750
Adam Cohen2854d252014-08-27 16:04:07 -07003751 final AnimatorSet stateAnimation = mStateAnimation;
3752 final Runnable startAnimRunnable = new Runnable() {
3753 public void run() {
3754 // Check that mStateAnimation hasn't changed while
3755 // we waited for a layout/draw pass
3756 if (mStateAnimation != stateAnimation)
3757 return;
3758 dispatchOnLauncherTransitionStart(fromView, animated, false);
3759 dispatchOnLauncherTransitionStart(toView, animated, false);
3760
Kenny Guyd794a3f2014-09-16 15:17:58 +01003761 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003762 for (int i = 0; i < layerViews.size(); i++) {
3763 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003764 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003765 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003766 }
Adam Cohen2854d252014-08-27 16:04:07 -07003767 }
3768 }
3769 mStateAnimation.start();
3770 }
3771 };
3772 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003773 } else {
3774 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003775 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003776 dispatchOnLauncherTransitionStart(fromView, animated, true);
3777 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003778 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003779 dispatchOnLauncherTransitionStart(toView, animated, true);
3780 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003781 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003782 }
3783
Michael Jurkae326f182011-11-21 14:05:46 -08003784 @Override
3785 public void onTrimMemory(int level) {
3786 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003787 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3788 // The widget preview db can result in holding onto over
3789 // 3MB of memory for caching which isn't necessary.
3790 SQLiteDatabase.releaseMemory();
3791
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003792 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003793 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003794 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003795 }
Michael Jurkae326f182011-11-21 14:05:46 -08003796 }
3797 }
3798
Adam Cohened307df2013-10-02 09:37:31 -07003799 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003800 showWorkspace(animated, null);
3801 }
3802
Adam Cohened307df2013-10-02 09:37:31 -07003803 protected void showWorkspace() {
3804 showWorkspace(true);
3805 }
3806
Adam Cohened66b2b2012-01-23 17:28:51 -08003807 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003808 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003809 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003810 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003811 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003812
Winson Chungc7d2b602012-05-16 17:02:20 -07003813 // Show the search bar (only animate if we were showing the drop target bar in spring
3814 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003815 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003816 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003817 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003818
Michael Jurkab3e22d92011-10-31 15:58:33 -07003819 // Set focus to the AppsCustomize button
3820 if (mAllAppsButton != null) {
3821 mAllAppsButton.requestFocus();
3822 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003823 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003824
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003825 // Change the state *after* we've called all the transition code
3826 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003827
Winson Chung5fb63472011-02-02 17:03:37 -08003828 // Resume the auto-advance of widgets
3829 mUserPresent = true;
3830 updateRunning();
3831
alanv1d4fde62012-10-17 13:15:47 -07003832 // Send an accessibility event to announce the context change
3833 getWindow().getDecorView()
3834 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003835
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003836 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003837 }
3838
Adam Cohened307df2013-10-02 09:37:31 -07003839 void showOverviewMode(boolean animated) {
3840 mWorkspace.setVisibility(View.VISIBLE);
3841 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3842 mState = State.WORKSPACE;
3843 onWorkspaceShown(animated);
3844 }
3845
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003846 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003847 }
3848
Winson Chung82963d52013-10-09 11:20:57 -07003849 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3850 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003851 if (mState != State.WORKSPACE) return;
3852
Winson Chung82963d52013-10-09 11:20:57 -07003853 if (resetPageToZero) {
3854 mAppsCustomizeTabHost.reset();
3855 }
Winson Chungc58497e2013-09-03 17:48:37 -07003856 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003857 mAppsCustomizeTabHost.post(new Runnable() {
3858 @Override
3859 public void run() {
3860 // We post this in-case the all apps view isn't yet constructed.
3861 mAppsCustomizeTabHost.requestFocus();
3862 }
3863 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003864
Michael Jurkab3e22d92011-10-31 15:58:33 -07003865 // Change the state *after* we've called all the transition code
3866 mState = State.APPS_CUSTOMIZE;
3867
3868 // Pause the auto-advance of widgets until we are out of AllApps
3869 mUserPresent = false;
3870 updateRunning();
3871 closeFolder();
3872
3873 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003874 getWindow().getDecorView()
3875 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003876 }
3877
Adam Cohen7777d962011-08-18 18:58:38 -07003878 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003879 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003880 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003881 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003882 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003883 }
Adam Cohen7777d962011-08-18 18:58:38 -07003884
Adam Cohenad4e15c2013-10-17 16:21:35 -07003885 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003886 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003887 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3888
Winson Chunge7a03942011-08-05 15:05:12 -07003889 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003890 @Override
3891 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003892 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003893 // Before we show workspace, hide all apps again because
3894 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3895 // clean up our state transition functions
3896 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003897 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003898 } else {
3899 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003900 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003901 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003902 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003903 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003904
Michael Jurkad3ef3062010-11-23 16:23:58 -08003905 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003906 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003907 final boolean animated = true;
3908 final boolean springLoaded = true;
3909 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003910 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003911 }
3912 // Otherwise, we are not in spring loaded mode, so don't do anything.
3913 }
3914
Michael Jurkab3e22d92011-10-31 15:58:33 -07003915 void lockAllApps() {
3916 // TODO
3917 }
3918
3919 void unlockAllApps() {
3920 // TODO
3921 }
3922
Sunny Goyal594d76d2014-11-06 10:12:54 -08003923 protected void disableVoiceButtonProxy(boolean disable) {
3924 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003925 }
3926
Cristina Stancu476493b2013-08-07 17:20:14 +01003927 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003928 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3929 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003930 }
3931
Adam Cohen24ce0b32014-01-14 16:18:14 -08003932 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003933 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3934 if (searchProvider == null) {
3935 return null;
3936 }
3937
3938 Bundle opts = new Bundle();
3939 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
3940 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
3941
3942 SharedPreferences sp = getSharedPreferences(
3943 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3944 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003945 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003946 if (!searchProvider.provider.flattenToString().equals(
3947 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003948 || (widgetInfo == null)
3949 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003950 // A valid widget is not already bound.
3951 if (widgetId > -1) {
3952 mAppWidgetHost.deleteAppWidgetId(widgetId);
3953 widgetId = -1;
3954 }
3955
3956 // Try to bind a new widget
3957 widgetId = mAppWidgetHost.allocateAppWidgetId();
3958
3959 if (!AppWidgetManagerCompat.getInstance(this)
3960 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3961 mAppWidgetHost.deleteAppWidgetId(widgetId);
3962 widgetId = -1;
3963 }
3964
3965 sp.edit()
3966 .putInt(QSB_WIDGET_ID, widgetId)
3967 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3968 .commit();
3969 }
3970
3971 if (widgetId != -1) {
3972 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3973 mQsb.updateAppWidgetOptions(opts);
3974 mQsb.setPadding(0, 0, 0, 0);
3975 mSearchDropTargetBar.addView(mQsb);
3976 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003977 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003978 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003979 }
3980
Michael Jurkad7c28052012-04-27 15:43:36 -07003981 @Override
3982 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003983 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003984 final List<CharSequence> text = event.getText();
3985 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003986 // Populate event with a fake title based on the current state.
3987 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07003988 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07003989 } else {
3990 text.add(getString(R.string.all_apps_home_button_label));
3991 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003992 return result;
3993 }
3994
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003995 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003996 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003997 */
3998 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3999 @Override
4000 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004001 closeSystemDialogs();
4002 }
4003 }
4004
4005 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004006 * Receives notifications whenever the appwidgets are reset.
4007 */
4008 private class AppWidgetResetObserver extends ContentObserver {
4009 public AppWidgetResetObserver() {
4010 super(new Handler());
4011 }
4012
4013 @Override
4014 public void onChange(boolean selfChange) {
4015 onAppWidgetReset();
4016 }
4017 }
4018
4019 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004020 * If the activity is currently paused, signal that we need to run the passed Runnable
4021 * in onResume.
4022 *
4023 * This needs to be called from incoming places where resources might have been loaded
4024 * while we are paused. That is becaues the Configuration might be wrong
4025 * when we're not running, and if it comes back to what it was when we
4026 * were paused, we are not restarted.
4027 *
4028 * Implementation of the method from LauncherModel.Callbacks.
4029 *
4030 * @return true if we are currently paused. The caller might be able to
4031 * skip some work in that case since we will come back again.
4032 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004033 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004034 if (mPaused) {
4035 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004036 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004037 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004038 }
4039 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004040 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004041 return true;
4042 } else {
4043 return false;
4044 }
4045 }
4046
Michael Jurkac402cd92013-05-20 15:49:32 +02004047 private boolean waitUntilResume(Runnable run) {
4048 return waitUntilResume(run, false);
4049 }
4050
Michael Jurka1e2f4652013-07-08 18:03:46 -07004051 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004052 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004053 }
4054
Michael Jurka7607c2f2013-04-03 14:33:19 -07004055 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004056 * If the activity is currently paused, signal that we need to re-run the loader
4057 * in onResume.
4058 *
4059 * This needs to be called from incoming places where resources might have been loaded
4060 * while we are paused. That is becaues the Configuration might be wrong
4061 * when we're not running, and if it comes back to what it was when we
4062 * were paused, we are not restarted.
4063 *
4064 * Implementation of the method from LauncherModel.Callbacks.
4065 *
4066 * @return true if we are currently paused. The caller might be able to
4067 * skip some work in that case since we will come back again.
4068 */
4069 public boolean setLoadOnResume() {
4070 if (mPaused) {
4071 Log.i(TAG, "setLoadOnResume");
4072 mOnResumeNeedsLoad = true;
4073 return true;
4074 } else {
4075 return false;
4076 }
4077 }
4078
4079 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004080 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004081 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004082 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004083 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004084 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004085 } else {
4086 return SCREEN_COUNT / 2;
4087 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004088 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004089
Joe Onorato9c1289c2009-08-17 11:03:03 -04004090 /**
4091 * Refreshes the shortcuts shown on the workspace.
4092 *
4093 * Implementation of the method from LauncherModel.Callbacks.
4094 */
4095 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004096 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004097
Michael Jurka7607c2f2013-04-03 14:33:19 -07004098 // If we're starting binding all over again, clear any bind calls we'd postponed in
4099 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4100 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004101 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004102
Winson Chungd64d1762013-08-20 14:37:16 -07004103 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004104 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004105 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004106
Michael Jurka05bf644e2011-11-30 20:28:53 -08004107 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004108 if (mHotseat != null) {
4109 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004110 }
4111 }
4112
Adam Cohendcd297f2013-06-18 13:13:40 -07004113 @Override
4114 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004115 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004116
Adam Cohen5084cba2013-09-03 12:01:16 -07004117 // If there are no screens, we need to have an empty screen
4118 if (orderedScreenIds.size() == 0) {
4119 mWorkspace.addExtraEmptyScreen();
4120 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004121
4122 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004123 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004124 if (hasCustomContentToLeft()) {
4125 mWorkspace.createCustomContentContainer();
4126 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004127 }
Winson Chung64359a52013-07-08 17:17:08 -07004128 }
4129
4130 @Override
4131 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004132 // Log to disk
4133 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4134 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4135 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004136 int count = orderedScreenIds.size();
4137 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004138 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004139 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004140 }
4141
Adam Cohen39a06042013-07-19 14:30:12 -07004142 private boolean shouldShowWeightWatcher() {
4143 String spKey = LauncherAppState.getSharedPreferencesKey();
4144 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07004145 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004146
4147 return show;
4148 }
4149
4150 private void toggleShowWeightWatcher() {
4151 String spKey = LauncherAppState.getSharedPreferencesKey();
4152 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4153 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4154
4155 show = !show;
4156
4157 SharedPreferences.Editor editor = sp.edit();
4158 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4159 editor.commit();
4160
4161 if (mWeightWatcher != null) {
4162 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4163 }
4164 }
4165
Winson Chungd64d1762013-08-20 14:37:16 -07004166 public void bindAppsAdded(final ArrayList<Long> newScreens,
4167 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004168 final ArrayList<ItemInfo> addAnimated,
4169 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004170 Runnable r = new Runnable() {
4171 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004172 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004173 }
4174 };
4175 if (waitUntilResume(r)) {
4176 return;
4177 }
4178
Winson Chungd64d1762013-08-20 14:37:16 -07004179 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004180 if (newScreens != null) {
4181 bindAddScreens(newScreens);
4182 }
Winson Chungd64d1762013-08-20 14:37:16 -07004183
4184 // We add the items without animation on non-visible pages, and with
4185 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004186 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004187 bindItems(addNotAnimated, 0,
4188 addNotAnimated.size(), false);
4189 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004190 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004191 bindItems(addAnimated, 0,
4192 addAnimated.size(), true);
4193 }
Winson Chungc58497e2013-09-03 17:48:37 -07004194
Winson Chung87412982013-10-03 18:34:14 -07004195 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004196 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004197
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004198 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004199 addedApps != null && mAppsCustomizeContent != null) {
4200 mAppsCustomizeContent.addApps(addedApps);
4201 }
Winson Chungd64d1762013-08-20 14:37:16 -07004202 }
4203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004204 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004205 * Bind the items start-end from the list.
4206 *
4207 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004208 */
Winson Chung64359a52013-07-08 17:17:08 -07004209 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4210 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004211 Runnable r = new Runnable() {
4212 public void run() {
4213 bindItems(shortcuts, start, end, forceAnimateIcons);
4214 }
4215 };
4216 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004217 return;
4218 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004219
Winson Chungf0c6ae02012-03-21 16:10:31 -07004220 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004221 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4222 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004223 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004224 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004225 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004226 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004227 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004228
4229 // Short circuit if we are loading dock items for a configuration which has no dock
4230 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4231 mHotseat == null) {
4232 continue;
4233 }
4234
Joe Onorato9c1289c2009-08-17 11:03:03 -04004235 switch (item.itemType) {
4236 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4237 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004238 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004239 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004240
Winson Chung64359a52013-07-08 17:17:08 -07004241 /*
4242 * TODO: FIX collision case
4243 */
Winson Chungce376632013-07-11 16:12:41 -07004244 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4245 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4246 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004247 View v = cl.getChildAt(item.cellX, item.cellY);
4248 Object tag = v.getTag();
4249 String desc = "Collision while binding workspace item: " + item
4250 + ". Collides with " + tag;
4251 if (LauncherAppState.isDogfoodBuild()) {
4252 throw (new RuntimeException(desc));
4253 } else {
4254 Log.d(TAG, desc);
4255 }
Winson Chungce376632013-07-11 16:12:41 -07004256 }
Winson Chung64359a52013-07-08 17:17:08 -07004257 }
4258
Adam Cohendcd297f2013-06-18 13:13:40 -07004259 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4260 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004261 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004262 // Animate all the applications up now
4263 shortcut.setAlpha(0f);
4264 shortcut.setScaleX(0f);
4265 shortcut.setScaleY(0f);
4266 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004267 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004268 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004269 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004270 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004271 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004272 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004273 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004274 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4275 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004276 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004277 default:
4278 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004279 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004280 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004281
Winson Chung997a9232013-07-24 15:33:46 -07004282 if (animateIcons) {
4283 // Animate to the correct page
4284 if (newShortcutsScreenId > -1) {
4285 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004286 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004287 final Runnable startBounceAnimRunnable = new Runnable() {
4288 public void run() {
4289 anim.playTogether(bounceAnims);
4290 anim.start();
4291 }
4292 };
Winson Chung997a9232013-07-24 15:33:46 -07004293 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004294 // We post the animation slightly delayed to prevent slowdowns
4295 // when we are loading right after we return to launcher.
4296 mWorkspace.postDelayed(new Runnable() {
4297 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004298 if (mWorkspace != null) {
4299 mWorkspace.snapToPage(newScreenIndex);
4300 mWorkspace.postDelayed(startBounceAnimRunnable,
4301 NEW_APPS_ANIMATION_DELAY);
4302 }
Winson Chung94d67682013-09-25 16:29:40 -07004303 }
4304 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004305 } else {
4306 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004307 }
4308 }
Winson Chung64359a52013-07-08 17:17:08 -07004309 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004310 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004311 }
4312
Joe Onorato9c1289c2009-08-17 11:03:03 -04004313 /**
4314 * Implementation of the method from LauncherModel.Callbacks.
4315 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004316 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004317 Runnable r = new Runnable() {
4318 public void run() {
4319 bindFolders(folders);
4320 }
4321 };
4322 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004323 return;
4324 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004325 sFolders.clear();
4326 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004327 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004328
4329 /**
4330 * Add the views for a widget to the workspace.
4331 *
4332 * Implementation of the method from LauncherModel.Callbacks.
4333 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004334 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004335 Runnable r = new Runnable() {
4336 public void run() {
4337 bindAppWidget(item);
4338 }
4339 };
4340 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004341 return;
4342 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004343
Daniel Sandler843e8602010-06-07 14:59:01 -04004344 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4345 if (DEBUG_WIDGETS) {
4346 Log.d(TAG, "bindAppWidget: " + item);
4347 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004348 final Workspace workspace = mWorkspace;
4349
Adam Cohen59400422014-03-05 18:07:04 -08004350 LauncherAppWidgetProviderInfo appWidgetInfo =
4351 LauncherModel.getProviderInfo(this, item.providerName);
4352
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004353 if (!mIsSafeModeEnabled
4354 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4355 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004356 if (appWidgetInfo == null) {
4357 if (DEBUG_WIDGETS) {
4358 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4359 + " belongs to component " + item.providerName
4360 + ", as the povider is null");
4361 }
4362 LauncherModel.deleteItemFromDatabase(this, item);
4363 return;
4364 }
4365 // Note: This assumes that the id remap broadcast is received before this step.
4366 // If that is not the case, the id remap will be ignored and user may see the
4367 // click to setup view.
Adam Cohen59400422014-03-05 18:07:04 -08004368 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004369 pendingInfo.spanX = item.spanX;
4370 pendingInfo.spanY = item.spanY;
4371 pendingInfo.minSpanX = item.minSpanX;
4372 pendingInfo.minSpanY = item.minSpanY;
4373 Bundle options =
4374 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4375
Sunny Goyalff572272014-07-23 13:58:07 -07004376 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004377 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4378 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004379
4380 // TODO consider showing a permission dialog when the widget is clicked.
4381 if (!success) {
4382 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4383 if (DEBUG_WIDGETS) {
4384 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4385 + " belongs to component " + item.providerName
4386 + ", as the launcher is unable to bing a new widget id");
4387 }
4388 LauncherModel.deleteItemFromDatabase(this, item);
4389 return;
4390 }
4391
4392 item.appWidgetId = newWidgetId;
4393
4394 // If the widget has a configure activity, it is still needs to set it up, otherwise
4395 // the widget is ready to go.
4396 item.restoreStatus = (appWidgetInfo.configure == null)
4397 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4398 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4399
4400 LauncherModel.updateItemInDatabase(this, item);
4401 }
4402
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004403 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004404 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004405 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004406 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4407 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004408 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004409
Sunny Goyal651077b2014-06-30 14:15:31 -07004410 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4411 } else {
4412 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004413 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4414 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004415 view.updateIcon(mIconCache);
4416 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004417 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004418 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004419 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004420
Joe Onorato9c1289c2009-08-17 11:03:03 -04004421 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004422 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004423
Adam Cohendcd297f2013-06-18 13:13:40 -07004424 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004425 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004426 if (!item.isCustomWidget()) {
4427 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4428 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004429
Joe Onorato9c1289c2009-08-17 11:03:03 -04004430 workspace.requestLayout();
4431
Daniel Sandler843e8602010-06-07 14:59:01 -04004432 if (DEBUG_WIDGETS) {
4433 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4434 + (SystemClock.uptimeMillis()-start) + "ms");
4435 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004436 }
4437
Sunny Goyalff572272014-07-23 13:58:07 -07004438 /**
4439 * Restores a pending widget.
4440 *
4441 * @param appWidgetId The app widget id
4442 * @param cellInfo The position on screen where to create the widget.
4443 */
4444 private void completeRestoreAppWidget(final int appWidgetId) {
4445 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4446 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4447 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4448 return;
4449 }
4450
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004451 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004452 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4453
4454 mWorkspace.reinflateWidgetsIfNecessary();
4455 LauncherModel.updateItemInDatabase(this, info);
4456 }
4457
Adam Cohen1462de32012-07-24 22:34:36 -07004458 public void onPageBoundSynchronously(int page) {
4459 mSynchronouslyBoundPages.add(page);
4460 }
4461
Joe Onorato9c1289c2009-08-17 11:03:03 -04004462 /**
4463 * Callback saying that there aren't any more items to bind.
4464 *
4465 * Implementation of the method from LauncherModel.Callbacks.
4466 */
Adam Cohene25af792013-06-06 23:08:25 -07004467 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004468 Runnable r = new Runnable() {
4469 public void run() {
4470 finishBindingItems(upgradePath);
4471 }
4472 };
4473 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004474 return;
4475 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004476 if (mSavedState != null) {
4477 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004478 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004479 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004480 mSavedState = null;
4481 }
4482
Adam Cohen1462de32012-07-24 22:34:36 -07004483 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004484
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004485 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004486 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004487
4488 // If we received the result of any pending adds while the loader was running (e.g. the
4489 // widget configuration forced an orientation change), process them now.
4490 if (sPendingAddItem != null) {
4491 final long screenId = completeAdd(sPendingAddItem);
4492
4493 // TODO: this moves the user to the page where the pending item was added. Ideally,
4494 // the screen would be guaranteed to exist after bind, and the page would be set through
4495 // the workspace restore process.
4496 mWorkspace.post(new Runnable() {
4497 @Override
4498 public void run() {
4499 mWorkspace.snapToScreenId(screenId);
4500 }
4501 });
4502 sPendingAddItem = null;
4503 }
4504
Adam Cohene25af792013-06-06 23:08:25 -07004505 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004506 mWorkspace.getUniqueComponents(true, null);
4507 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004508 }
Sunny Goyale755d462014-07-22 13:48:29 -07004509 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004510
4511 if (mLauncherCallbacks != null) {
4512 mLauncherCallbacks.finishBindingItems(upgradePath);
4513 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004514 }
4515
Adam Cohen3ed316a2014-07-23 18:21:20 -07004516 private void sendLoadingCompleteBroadcastIfNecessary() {
4517 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4518 String permission =
4519 getResources().getString(R.string.receive_first_load_broadcast_permission);
4520 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4521 sendBroadcast(intent, permission);
4522 SharedPreferences.Editor editor = mSharedPrefs.edit();
4523 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4524 editor.apply();
4525 }
4526 }
4527
Winson Chunga0b7e862013-09-05 16:03:15 -07004528 public boolean isAllAppsButtonRank(int rank) {
4529 if (mHotseat != null) {
4530 return mHotseat.isAllAppsButtonRank(rank);
4531 }
4532 return false;
4533 }
4534
Winson Chunga2413752012-04-03 14:22:34 -07004535 private boolean canRunNewAppsAnimation() {
4536 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4537 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4538 }
4539
Winson Chungc9168342013-06-26 14:54:55 -07004540 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4541 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4542 PropertyValuesHolder.ofFloat("alpha", 1f),
4543 PropertyValuesHolder.ofFloat("scaleX", 1f),
4544 PropertyValuesHolder.ofFloat("scaleY", 1f));
4545 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4546 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4547 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4548 return bounceAnim;
4549 }
4550
Adam Cohen27772732013-11-11 14:00:56 +00004551 public boolean useVerticalBarLayout() {
4552 return LauncherAppState.getInstance().getDynamicGrid().
4553 getDeviceProfile().isVerticalBarLayout();
4554 }
4555
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004556 protected Rect getSearchBarBounds() {
4557 return LauncherAppState.getInstance().getDynamicGrid().
4558 getDeviceProfile().getSearchBarBounds();
4559 }
4560
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004561 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004562 if (mSearchDropTargetBar == null) {
4563 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004564 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004565 if (mQsb != null) {
4566 mSearchDropTargetBar.removeView(mQsb);
4567 mQsb = null;
4568 }
4569 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004570 }
4571
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004572 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004573 * Add the icons for all apps.
4574 *
4575 * Implementation of the method from LauncherModel.Callbacks.
4576 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004577 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004578 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004579 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4580 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4581 getHotseat().addAllAppsFolder(mIconCache, apps,
4582 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4583 }
4584 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004585 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004586 if (mAppsCustomizeContent != null) {
4587 mAppsCustomizeContent.onPackagesUpdated(
4588 LauncherModel.getSortedWidgetsAndShortcuts(this));
4589 }
Winson Chungc58497e2013-09-03 17:48:37 -07004590 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004591 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004592 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004593 mAppsCustomizeContent.onPackagesUpdated(
4594 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004595 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004596 }
Adam Cohen9211d422014-10-07 18:14:53 -07004597 if (mLauncherCallbacks != null) {
4598 mLauncherCallbacks.bindAllApplications(apps);
4599 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004600 }
4601
4602 /**
4603 * A package was updated.
4604 *
4605 * Implementation of the method from LauncherModel.Callbacks.
4606 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004607 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004608 Runnable r = new Runnable() {
4609 public void run() {
4610 bindAppsUpdated(apps);
4611 }
4612 };
4613 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004614 return;
4615 }
4616
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004617 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004618 mAppsCustomizeContent != null) {
4619 mAppsCustomizeContent.updateApps(apps);
4620 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004621 }
4622
Sunny Goyal4390ace2014-10-13 11:33:11 -07004623 @Override
4624 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4625 Runnable r = new Runnable() {
4626 public void run() {
4627 bindWidgetsRestored(widgets);
4628 }
4629 };
4630 if (waitUntilResume(r)) {
4631 return;
4632 }
4633 mWorkspace.widgetsRestored(widgets);
4634 }
4635
Joe Onorato9c1289c2009-08-17 11:03:03 -04004636 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004637 * Some shortcuts were updated in the background.
4638 *
4639 * Implementation of the method from LauncherModel.Callbacks.
4640 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004641 @Override
4642 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4643 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004644 Runnable r = new Runnable() {
4645 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004646 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004647 }
4648 };
4649 if (waitUntilResume(r)) {
4650 return;
4651 }
4652
Sunny Goyal4390ace2014-10-13 11:33:11 -07004653 if (!updated.isEmpty()) {
4654 mWorkspace.updateShortcuts(updated);
4655 }
4656
4657 if (!removed.isEmpty()) {
4658 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4659 for (ShortcutInfo si : removed) {
4660 removedComponents.add(si.getTargetComponent());
4661 }
4662 mWorkspace.removeItemsByComponentName(removedComponents, user);
4663 // Notify the drag controller
4664 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004665 }
4666 }
4667
4668 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004669 * Update the state of a package, typically related to install state.
4670 *
4671 * Implementation of the method from LauncherModel.Callbacks.
4672 */
Sunny Goyale755d462014-07-22 13:48:29 -07004673 @Override
4674 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004675 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004676 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004677 }
4678 }
4679
4680 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004681 * Update the label and icon of all the icons in a package
4682 *
4683 * Implementation of the method from LauncherModel.Callbacks.
4684 */
4685 @Override
4686 public void updatePackageBadge(String packageName) {
4687 if (mWorkspace != null) {
4688 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4689 }
4690 }
4691
4692 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004693 * A package was uninstalled. We take both the super set of packageNames
4694 * in addition to specific applications to remove, the reason being that
4695 * this can be called when a package is updated as well. In that scenario,
4696 * we only remove specific components from the workspace, where as
4697 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004698 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004699 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004700 * Implementation of the method from LauncherModel.Callbacks.
4701 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004702 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004703 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004704 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004705 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004706 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004707 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004708 }
Winson Chungd64d1762013-08-20 14:37:16 -07004709 };
4710 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004711 return;
4712 }
4713
Sunny Goyal1a745e82014-10-02 15:58:31 -07004714 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004715 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4716 for (AppInfo info : appInfos) {
4717 removedComponents.add(info.componentName);
4718 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004719 if (!packageNames.isEmpty()) {
4720 mWorkspace.removeItemsByPackageName(packageNames, user);
4721 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004722 if (!removedComponents.isEmpty()) {
4723 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004724 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004725 // Notify the drag controller
4726 mDragController.onAppsRemoved(packageNames, removedComponents);
4727
Sunny Goyal1a745e82014-10-02 15:58:31 -07004728 } else {
4729 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004730 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004731
Winson Chungdf95eb12013-10-16 14:57:07 -07004732 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004733 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004734 mAppsCustomizeContent != null) {
4735 mAppsCustomizeContent.removeApps(appInfos);
4736 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004737 }
Joe Onoratobe386092009-11-17 17:32:16 -08004738
4739 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004740 * A number of packages were updated.
4741 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004742 private ArrayList<Object> mWidgetsAndShortcuts;
4743 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004744 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004745 bindPackagesUpdated(mWidgetsAndShortcuts);
4746 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004747 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004748 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004749 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4750 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4751 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004752 return;
4753 }
4754
Winson Chung64359a52013-07-08 17:17:08 -07004755 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004756 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004757 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004758 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004759 }
4760
Winson Chung400438b2011-01-16 17:53:48 -08004761 private int mapConfigurationOriActivityInfoOri(int configOri) {
4762 final Display d = getWindowManager().getDefaultDisplay();
4763 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4764 switch (d.getRotation()) {
4765 case Surface.ROTATION_0:
4766 case Surface.ROTATION_180:
4767 // We are currently in the same basic orientation as the natural orientation
4768 naturalOri = configOri;
4769 break;
4770 case Surface.ROTATION_90:
4771 case Surface.ROTATION_270:
4772 // We are currently in the other basic orientation to the natural orientation
4773 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4774 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4775 break;
4776 }
4777
4778 int[] oriMap = {
4779 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4780 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4781 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4782 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4783 };
4784 // Since the map starts at portrait, we need to offset if this device's natural orientation
4785 // is landscape.
4786 int indexOffset = 0;
4787 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4788 indexOffset = 1;
4789 }
4790 return oriMap[(d.getRotation() + indexOffset) % 4];
4791 }
Adam Cohen446e9402011-09-15 18:21:21 -07004792
Winson Chung4b919f82012-05-01 10:44:08 -07004793 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004794 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004795 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4796 .getConfiguration().orientation));
4797 }
4798 }
4799 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004800 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004801 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004802 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004803 } else {
4804 mHandler.postDelayed(new Runnable() {
4805 public void run() {
4806 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4807 }
4808 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004809 }
Winson Chung4b919f82012-05-01 10:44:08 -07004810 }
Winson Chung400438b2011-01-16 17:53:48 -08004811 }
4812
Winson Chunge43a1e72014-01-15 10:33:02 -08004813 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004814 if (mLauncherCallbacks != null) {
4815 return mLauncherCallbacks.isLauncherPreinstalled();
4816 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004817 PackageManager pm = getPackageManager();
4818 try {
4819 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4820 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4821 return true;
4822 } else {
4823 return false;
4824 }
4825 } catch (NameNotFoundException e) {
4826 e.printStackTrace();
4827 return false;
4828 }
4829 }
4830
Adam Cohenea90f832014-05-21 15:03:34 -07004831 /**
4832 * This method indicates whether or not we should suggest default wallpaper dimensions
4833 * when our wallpaper cropper was not yet used to set a wallpaper.
4834 */
4835 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004836 if (mLauncherCallbacks != null) {
4837 return mLauncherCallbacks.overrideWallpaperDimensions();
4838 }
Adam Cohenea90f832014-05-21 15:03:34 -07004839 return true;
4840 }
4841
Adam Cohen432609a2014-03-13 17:03:22 -07004842 /**
4843 * To be overridden by subclasses to indicate that there is an activity to launch
4844 * before showing the standard launcher experience.
4845 */
4846 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004847 if (mLauncherCallbacks != null) {
4848 return mLauncherCallbacks.hasFirstRunActivity();
4849 }
Adam Cohen432609a2014-03-13 17:03:22 -07004850 return false;
4851 }
4852
4853 /**
4854 * To be overridden by subclasses to launch any first run activity
4855 */
4856 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004857 if (mLauncherCallbacks != null) {
4858 return mLauncherCallbacks.getFirstRunActivity();
4859 }
Adam Cohen432609a2014-03-13 17:03:22 -07004860 return null;
4861 }
4862
Winson Chunga6945242014-01-08 14:04:34 -08004863 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004864 return !ActivityManager.isRunningInTestHarness() &&
4865 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004866 }
4867
Adam Cohen4a9423d2014-03-28 14:22:31 -07004868 protected boolean hasRunFirstRunActivity() {
4869 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4870 }
4871
Adam Cohen432609a2014-03-13 17:03:22 -07004872 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004873 if (shouldRunFirstRunActivity() &&
4874 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004875 Intent firstRunIntent = getFirstRunActivity();
4876 if (firstRunIntent != null) {
4877 startActivity(firstRunIntent);
4878 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004879 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004880 }
4881 }
Adam Cohen432609a2014-03-13 17:03:22 -07004882 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004883 }
4884
4885 private void markFirstRunActivityShown() {
4886 SharedPreferences.Editor editor = mSharedPrefs.edit();
4887 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4888 editor.apply();
4889 }
4890
Adam Cohen432609a2014-03-13 17:03:22 -07004891 /**
4892 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4893 * screen that must be displayed and dismissed.
4894 */
4895 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004896 if (mLauncherCallbacks != null) {
4897 return mLauncherCallbacks.hasDismissableIntroScreen();
4898 }
Adam Cohen432609a2014-03-13 17:03:22 -07004899 return false;
4900 }
4901
4902 /**
4903 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4904 */
4905 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004906 if (mLauncherCallbacks != null) {
4907 return mLauncherCallbacks.getIntroScreen();
4908 }
Adam Cohen432609a2014-03-13 17:03:22 -07004909 return null;
4910 }
4911
4912 /**
4913 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4914 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4915 */
4916 private boolean shouldShowIntroScreen() {
4917 return hasDismissableIntroScreen() &&
4918 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4919 }
4920
4921 protected void showIntroScreen() {
4922 View introScreen = getIntroScreen();
4923 changeWallpaperVisiblity(false);
4924 if (introScreen != null) {
4925 mDragLayer.showOverlayView(introScreen);
4926 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004927 if (mLauncherOverlayContainer != null) {
4928 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4929 }
Adam Cohen432609a2014-03-13 17:03:22 -07004930 }
4931
4932 public void dismissIntroScreen() {
4933 markIntroScreenDismissed();
4934 if (showFirstRunActivity()) {
4935 // We delay hiding the intro view until the first run activity is showing. This
4936 // avoids a blip.
4937 mWorkspace.postDelayed(new Runnable() {
4938 @Override
4939 public void run() {
4940 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004941 if (mLauncherOverlayContainer != null) {
4942 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4943 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004944 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004945 }
4946 }, ACTIVITY_START_DELAY);
4947 } else {
4948 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004949 if (mLauncherOverlayContainer != null) {
4950 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4951 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004952 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004953 }
4954 changeWallpaperVisiblity(true);
4955 }
4956
4957 private void markIntroScreenDismissed() {
4958 SharedPreferences.Editor editor = mSharedPrefs.edit();
4959 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4960 editor.apply();
4961 }
4962
Sunny Goyal88d60f12014-09-08 03:47:29 -07004963 private void showFirstRunClings() {
4964 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4965 // on the device, then we always show the first run cling experience (or if there is no
4966 // launcher2). Otherwise, we prompt the user upon started for migration
4967 LauncherClings launcherClings = new LauncherClings(this);
4968 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4969 if (mModel.canMigrateFromOldLauncherDb(this)) {
4970 launcherClings.showMigrationCling();
4971 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004972 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004973 }
4974 }
4975 }
4976
Winson Chunga6945242014-01-08 14:04:34 -08004977 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004978 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4979 if (mHotseat != null) mHotseat.setAlpha(1f);
4980 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4981 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004982 }
4983
4984 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004985 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4986 if (mHotseat != null) mHotseat.setAlpha(0f);
4987 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4988 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004989 }
4990
Mathew Inwood72fbec12013-11-19 15:45:07 +00004991 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004992 // Called from search suggestion, not supported in other profiles.
4993 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4994 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4995 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4996 myUser);
4997 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004998 return null;
4999 }
Kenny Guyed131872014-04-30 03:02:21 +01005000 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005001 }
5002
5003 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5004 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005005 // Called from search suggestion, not supported in other profiles.
5006 return createShortcutDragInfo(shortcutIntent, caption, icon,
5007 UserHandleCompat.myUserHandle());
5008 }
5009
5010 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5011 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005012 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005013 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005014 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005015 }
5016
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005017 protected void moveWorkspaceToDefaultScreen() {
5018 mWorkspace.moveToDefaultScreen(false);
5019 }
5020
Mathew Inwood72fbec12013-11-19 15:45:07 +00005021 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5022 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005023 mWorkspace.onExternalDragStartedWithItem(dragView);
5024 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005025 }
5026
Anjali Koppalf5d29132014-02-28 13:33:27 -08005027 @Override
5028 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005029 if (mLauncherCallbacks != null) {
5030 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5031 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005032 }
5033
Winson Chung80baf5a2010-08-09 16:03:15 -07005034 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005035 * Prints out out state for debugging.
5036 */
5037 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005038 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005039 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005040 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5041 Log.d(TAG, "mRestoring=" + mRestoring);
5042 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5043 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005044 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005045 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005046
Winson Chung785d2eb2011-04-14 16:08:02 -07005047 if (mAppsCustomizeContent != null) {
5048 mAppsCustomizeContent.dumpState();
5049 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005050 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005051 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005052
5053 @Override
5054 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5055 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005056 synchronized (sDumpLogs) {
5057 writer.println(" ");
5058 writer.println("Debug logs: ");
5059 for (int i = 0; i < sDumpLogs.size(); i++) {
5060 writer.println(" " + sDumpLogs.get(i));
5061 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005062 }
Adam Cohen9211d422014-10-07 18:14:53 -07005063 if (mLauncherCallbacks != null) {
5064 mLauncherCallbacks.dump(prefix, fd, writer, args);
5065 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005066 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005067
5068 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005069 if (DEBUG_DUMP_LOG) {
5070 synchronized (sDumpLogs) {
5071 Log.d(TAG, "");
5072 Log.d(TAG, "*********************");
5073 Log.d(TAG, "Launcher debug logs: ");
5074 for (int i = 0; i < sDumpLogs.size(); i++) {
5075 Log.d(TAG, " " + sDumpLogs.get(i));
5076 }
5077 Log.d(TAG, "*********************");
5078 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005079 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005080 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005081 }
5082
5083 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005084 addDumpLog(tag, log, null, debugLog);
5085 }
5086
5087 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005088 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005089 if (e != null) {
5090 Log.d(tag, log, e);
5091 } else {
5092 Log.d(tag, log);
5093 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005094 }
Winson Chungede41292013-09-19 16:27:36 -07005095 if (DEBUG_DUMP_LOG) {
5096 sDateStamp.setTime(System.currentTimeMillis());
5097 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005098 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5099 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005100 }
Winson Chungede41292013-09-19 16:27:36 -07005101 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005102 }
5103
Adam Cohen59400422014-03-05 18:07:04 -08005104 public static CustomAppWidget getCustomAppWidget(String name) {
5105 return sCustomAppWidgets.get(name);
5106 }
5107
5108 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
5109 return sCustomAppWidgets;
5110 }
5111
Winson Chungede41292013-09-19 16:27:36 -07005112 public void dumpLogsToLocalData() {
5113 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005114 new AsyncTask<Void, Void, Void>() {
5115 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005116 boolean success = false;
5117 sDateStamp.setTime(sRunStart);
5118 String FILENAME = sDateStamp.getMonth() + "-"
5119 + sDateStamp.getDay() + "_"
5120 + sDateStamp.getHours() + "-"
5121 + sDateStamp.getMinutes() + "_"
5122 + sDateStamp.getSeconds() + ".txt";
5123
5124 FileOutputStream fos = null;
5125 File outFile = null;
5126 try {
5127 outFile = new File(getFilesDir(), FILENAME);
5128 outFile.createNewFile();
5129 fos = new FileOutputStream(outFile);
5130 } catch (Exception e) {
5131 e.printStackTrace();
5132 }
5133 if (fos != null) {
5134 PrintWriter writer = new PrintWriter(fos);
5135
5136 writer.println(" ");
5137 writer.println("Debug logs: ");
5138 synchronized (sDumpLogs) {
5139 for (int i = 0; i < sDumpLogs.size(); i++) {
5140 writer.println(" " + sDumpLogs.get(i));
5141 }
5142 }
5143 writer.close();
5144 }
5145 try {
5146 if (fos != null) {
5147 fos.close();
5148 success = true;
5149 }
5150 } catch (IOException e) {
5151 e.printStackTrace();
5152 }
Michael Jurka43467462013-11-14 12:03:58 +01005153 return null;
Winson Chungede41292013-09-19 16:27:36 -07005154 }
Michael Jurka43467462013-11-14 12:03:58 +01005155 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005156 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005157 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005158}
Michael Jurka2763be32011-02-24 11:19:57 -08005159
Michael Jurkaabded662011-03-04 12:06:57 -08005160interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005161 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005162 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005163 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005164 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005165 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005166}
Dan Sandlerd5024042014-01-09 15:01:33 -05005167
5168interface DebugIntents {
5169 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5170 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005171}