blob: 22db3ac4dcf808d6ab736f38652ed9c9e8bc1d33 [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 Sandler6053b802013-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 Cohen9d5b7d82012-05-09 18:00:44 -0700263 private AppWidgetProviderInfo 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
Michael Jurkac1f5d262011-09-30 19:32:27 -0700354 private Runnable mBuildLayersRunnable = new Runnable() {
355 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700356 if (mWorkspace != null) {
357 mWorkspace.buildPageHardwareLayers();
358 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700359 }
360 };
361
Adam Cohendb364c32014-05-20 14:23:40 -0700362 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363
364 private static class PendingAddArguments {
365 int requestCode;
366 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700367 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700368 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800369 int cellX;
370 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700371 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800372 }
373
Daniel Sandlerff02d492013-08-05 02:12:05 -0400374 private Stats mStats;
375
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700376 FocusIndicatorView mFocusHandler;
377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 @Override
379 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700380 if (DEBUG_STRICT_MODE) {
381 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
382 .detectDiskReads()
383 .detectDiskWrites()
384 .detectNetwork() // or .detectAll() for all detectable problems
385 .penaltyLog()
386 .build());
387 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
388 .detectLeakedSqlLiteObjects()
389 .detectLeakedClosableObjects()
390 .penaltyLog()
391 .penaltyDeath()
392 .build());
393 }
394
Adam Cohen9211d422014-10-07 18:14:53 -0700395 if (mLauncherCallbacks != null) {
396 mLauncherCallbacks.preOnCreate();
397 }
398
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400400
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400401 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400402 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700403 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700404
Winson Chung5f8afe62013-08-12 16:19:28 -0700405 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700406 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700407
408 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400409 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700410 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700411 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800412 mModel = app.setLauncher(this);
413 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700414 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400415 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700417
Daniel Sandlerff02d492013-08-05 02:12:05 -0400418 mStats = new Stats(this);
419
Sunny Goyalffe83f12014-08-14 17:39:34 -0700420 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700421
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700422 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
423 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700424
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700425 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
426 // this also ensures that any synchronous binding below doesn't re-trigger another
427 // LauncherModel load.
428 mPaused = false;
429
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200431 android.os.Debug.startMethodTracing(
432 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 }
434
435 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100439 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800441 registerContentObservers();
442
Joe Onorato7c312c12009-08-13 21:36:53 -0700443 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 mSavedState = savedInstanceState;
446 restoreState(mSavedState);
447
448 if (PROFILE_STARTUP) {
449 android.os.Debug.stopMethodTracing();
450 }
451
452 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700453 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700454 // If the user leaves launcher, then we should just load items asynchronously when
455 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500456 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700457 } else {
458 // We only load the page synchronously if the user rotates (or triggers a
459 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800460 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 }
463
464 // For handling default keys
465 mDefaultKeySsb = new SpannableStringBuilder();
466 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800467
468 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
469 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800470
Adam Cohenf9426d52012-06-04 17:26:21 -0700471 // On large interfaces, we want the screen to auto-rotate based on the current orientation
472 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700473
Adam Cohen9211d422014-10-07 18:14:53 -0700474 if (mLauncherCallbacks != null) {
475 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700476 if (mLauncherCallbacks.hasLauncherOverlay()) {
477 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700478 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
479 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
480 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700481 mWorkspace.setLauncherOverlay(mLauncherOverlay);
482 }
Adam Cohen9211d422014-10-07 18:14:53 -0700483 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700484
485 if (shouldShowIntroScreen()) {
486 showIntroScreen();
487 } else {
488 showFirstRunActivity();
489 showFirstRunClings();
490 }
Adam Cohen9211d422014-10-07 18:14:53 -0700491 }
492
493 private LauncherCallbacks mLauncherCallbacks;
494
495 public void onPostCreate(Bundle savedInstanceState) {
496 super.onPostCreate(savedInstanceState);
497 if (mLauncherCallbacks != null) {
498 mLauncherCallbacks.onPostCreate(savedInstanceState);
499 }
500 }
501
502 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
503 mLauncherCallbacks = callbacks;
504 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700505 }
506
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700507 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700508 public void onLauncherProviderChange() {
509 if (mLauncherCallbacks != null) {
510 mLauncherCallbacks.onLauncherProviderChange();
511 }
512 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700513
Adam Cohen9211d422014-10-07 18:14:53 -0700514 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700515 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700516 if (mLauncherCallbacks != null) {
517 return mLauncherCallbacks.hasCustomContentToLeft();
518 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700519 return false;
520 }
521
Dave Hawkeya8881582013-09-17 15:55:33 -0600522 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500523 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600524 * {@link #addToCustomContentPage}. This will only be invoked if
525 * {@link #hasCustomContentToLeft()} is {@code true}.
526 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500527 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700528 if (mLauncherCallbacks != null) {
529 mLauncherCallbacks.populateCustomContentContainer();
530 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600531 }
532
533 /**
534 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
535 * ensure the custom content page is added or removed if necessary.
536 */
537 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600538 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600539 // Not bound yet, wait for bindScreens to be called.
540 return;
541 }
542
543 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
544 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500545 mWorkspace.createCustomContentContainer();
546 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600547 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
548 mWorkspace.removeCustomContentPage();
549 }
550 }
551
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700553 if (sLocaleConfiguration == null) {
554 new AsyncTask<Void, Void, LocaleConfiguration>() {
555 @Override
556 protected LocaleConfiguration doInBackground(Void... unused) {
557 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
558 readConfiguration(Launcher.this, localeConfiguration);
559 return localeConfiguration;
560 }
561
562 @Override
563 protected void onPostExecute(LocaleConfiguration result) {
564 sLocaleConfiguration = result;
565 checkForLocaleChange(); // recursive, but now with a locale configuration
566 }
567 }.execute();
568 return;
569 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700570
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 final Configuration configuration = getResources().getConfiguration();
572
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700573 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 final String locale = configuration.locale.toString();
575
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700576 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 final int mcc = configuration.mcc;
578
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700579 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 final int mnc = configuration.mnc;
581
Romain Guy84f296c2009-11-04 15:00:44 -0800582 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583
Romain Guy84f296c2009-11-04 15:00:44 -0800584 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700585 sLocaleConfiguration.locale = locale;
586 sLocaleConfiguration.mcc = mcc;
587 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700588
Joe Onorato0589f0f2010-02-08 13:44:00 -0800589 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700590
591 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100592 new AsyncTask<Void, Void, Void>() {
593 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700594 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100595 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700596 }
Michael Jurka43467462013-11-14 12:03:58 +0100597 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700598 }
599 }
600
601 private static class LocaleConfiguration {
602 public String locale;
603 public int mcc = -1;
604 public int mnc = -1;
605 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700606
Romain Guy98d01652009-06-30 16:21:04 -0700607 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
608 DataInputStream in = null;
609 try {
Helena Josol28db2802014-10-09 17:04:09 +0100610 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700611 configuration.locale = in.readUTF();
612 configuration.mcc = in.readInt();
613 configuration.mnc = in.readInt();
614 } catch (FileNotFoundException e) {
615 // Ignore
616 } catch (IOException e) {
617 // Ignore
618 } finally {
619 if (in != null) {
620 try {
621 in.close();
622 } catch (IOException e) {
623 // Ignore
624 }
625 }
626 }
627 }
628
629 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
630 DataOutputStream out = null;
631 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100632 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100633 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700634 out.writeUTF(configuration.locale);
635 out.writeInt(configuration.mcc);
636 out.writeInt(configuration.mnc);
637 out.flush();
638 } catch (FileNotFoundException e) {
639 // Ignore
640 } catch (IOException e) {
641 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100642 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700643 } finally {
644 if (out != null) {
645 try {
646 out.close();
647 } catch (IOException e) {
648 // Ignore
649 }
650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 }
652 }
653
Anton Hanssona2f665f2013-09-26 12:18:32 +0100654 public Stats getStats() {
655 return mStats;
656 }
657
Bjorn Bringertc459e522013-06-07 19:36:01 +0100658 public LayoutInflater getInflater() {
659 return mInflater;
660 }
661
Winson Chung36a62fe2012-05-06 18:04:42 -0700662 boolean isDraggingEnabled() {
663 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
664 // that is subsequently removed from the workspace in startBinding().
665 return !mModel.isLoadingWorkspace();
666 }
667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 static int getScreen() {
669 synchronized (sLock) {
670 return sScreen;
671 }
672 }
673
674 static void setScreen(int screen) {
675 synchronized (sLock) {
676 sScreen = screen;
677 }
678 }
679
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000680 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100681 if (Build.VERSION.SDK_INT >= 17) {
682 return View.generateViewId();
683 } else {
684 // View.generateViewId() is not available. The following fallback logic is a copy
685 // of its implementation.
686 for (;;) {
687 final int result = sNextGeneratedId.get();
688 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
689 int newValue = result + 1;
690 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
691 if (sNextGeneratedId.compareAndSet(result, newValue)) {
692 return result;
693 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000694 }
695 }
696 }
697
698 public int getViewIdForItem(ItemInfo info) {
699 // This cast is safe given the > 2B range for int.
700 int itemId = (int) info.id;
701 if (mItemIdToViewId.containsKey(itemId)) {
702 return mItemIdToViewId.get(itemId);
703 }
704 int viewId = generateViewId();
705 mItemIdToViewId.put(itemId, viewId);
706 return viewId;
707 }
708
709 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700710 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
711 * a configuration step, this allows the proper animations to run after other transitions.
712 */
Adam Cohendb364c32014-05-20 14:23:40 -0700713 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700714 long screenId = args.screenId;
715 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
716 // When the screen id represents an actual screen (as opposed to a rank) we make sure
717 // that the drop page actually exists.
718 screenId = ensurePendingDropLayoutExists(args.screenId);
719 }
Adam Cohendb364c32014-05-20 14:23:40 -0700720
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800721 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800722 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700723 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700724 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700725 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800726 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700727 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700728 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700729 case REQUEST_RECONFIGURE_APPWIDGET:
730 completeRestoreAppWidget(args.appWidgetId);
731 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800732 }
Michael Jurka27614d22012-04-02 06:40:22 -0700733 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
734 // if you turned the screen off and then back while in All Apps, Launcher would not
735 // return to the workspace. Clearing mAddInfo.container here fixes this issue
736 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700737 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800738 }
739
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 protected void onActivityResult(
742 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700743 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700744 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700745 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800746 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700747
Adam Cohenad4e15c2013-10-17 16:21:35 -0700748 Runnable exitSpringLoaded = new Runnable() {
749 @Override
750 public void run() {
751 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
752 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
753 }
754 };
755
Michael Jurka8b805b12012-04-18 14:23:14 -0700756 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700757 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700758 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
759 if (resultCode == RESULT_CANCELED) {
760 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700761 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700762 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700763 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
765 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700766 }
767 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200768 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
769 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
770 mWorkspace.exitOverviewMode(false);
771 }
772 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200774
Adam Cohened66b2b2012-01-23 17:28:51 -0800775 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
776 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700777
Adam Cohendb364c32014-05-20 14:23:40 -0700778 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800779 // We have special handling for widgets
780 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800781 final int appWidgetId;
782 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
783 : -1;
784 if (widgetId < 0) {
785 appWidgetId = pendingAddWidgetId;
786 } else {
787 appWidgetId = widgetId;
788 }
789
Adam Cohenad4e15c2013-10-17 16:21:35 -0700790 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800791 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700792 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
793 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700794 result = RESULT_CANCELED;
795 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700796 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700797 @Override
798 public void run() {
799 exitSpringLoadedDragModeDelayed(false, 0, null);
800 }
801 };
Adam Cohendb364c32014-05-20 14:23:40 -0700802 if (workspaceLocked) {
803 // No need to remove the empty screen if we're mid-binding, as the
804 // the bind will not add the empty screen.
805 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
806 } else {
807 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
808 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
809 }
Winson Chung5aaab772012-04-27 15:24:02 -0700810 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700811 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700812 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
813 // When the screen id represents an actual screen (as opposed to a rank)
814 // we make sure that the drop page actually exists.
815 mPendingAddInfo.screenId =
816 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
817 }
Adam Cohendb364c32014-05-20 14:23:40 -0700818 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
819
820 dropLayout.setDropPending(true);
821 final Runnable onComplete = new Runnable() {
822 @Override
823 public void run() {
824 completeTwoStageWidgetDrop(resultCode, appWidgetId);
825 dropLayout.setDropPending(false);
826 }
827 };
828 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
829 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
830 } else {
831 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
832 mPendingAddInfo);
833 sPendingAddItem = args;
834 }
Winson Chung5aaab772012-04-27 15:24:02 -0700835 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800836 return;
837 }
838
Sunny Goyalff572272014-07-23 13:58:07 -0700839 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
840 if (resultCode == RESULT_OK) {
841 // Update the widget view.
842 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
843 pendingAddWidgetId, mPendingAddInfo);
844 if (workspaceLocked) {
845 sPendingAddItem = args;
846 } else {
847 completeAdd(args);
848 }
849 }
850 // Leave the widget in the pending state if the user canceled the configure.
851 return;
852 }
853
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 // The pattern used here is that a user PICKs a specific application,
855 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700856
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
858 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700859 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700860 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
861 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800862 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700863 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800864 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700865 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700866 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
867 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 }
Adam Cohen00074722013-10-11 17:46:09 -0700869 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700870 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700871 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800873 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800874 }
875
Adam Cohendb364c32014-05-20 14:23:40 -0700876 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
877 appWidgetId, ItemInfo info) {
878 PendingAddArguments args = new PendingAddArguments();
879 args.requestCode = requestCode;
880 args.intent = data;
881 args.container = info.container;
882 args.screenId = info.screenId;
883 args.cellX = info.cellX;
884 args.cellY = info.cellY;
885 args.appWidgetId = appWidgetId;
886 return args;
887 }
888
889 /**
890 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
891 *
892 * @param screenId the screen id to check
893 * @return the new screen, or screenId if it exists
894 */
895 private long ensurePendingDropLayoutExists(long screenId) {
896 CellLayout dropLayout =
897 (CellLayout) mWorkspace.getScreenWithId(screenId);
898 if (dropLayout == null) {
899 // it's possible that the add screen was removed because it was
900 // empty and a re-bind occurred
901 mWorkspace.addExtraEmptyScreen();
902 return mWorkspace.commitExtraEmptyScreen();
903 } else {
904 return screenId;
905 }
906 }
907
Adam Cohened66b2b2012-01-23 17:28:51 -0800908 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700909 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700910 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800911 Runnable onCompleteRunnable = null;
912 int animationType = 0;
913
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700914 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800915 if (resultCode == RESULT_OK) {
916 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
917 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700918 mPendingAddWidgetInfo);
919 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800920 onCompleteRunnable = new Runnable() {
921 @Override
922 public void run() {
923 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700924 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700925 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
926 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 }
928 };
929 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800930 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800932 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700933 if (mDragLayer.getAnimatedView() != null) {
934 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
935 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
936 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700937 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700938 // The animated view may be null in the case of a rotation during widget configuration
939 onCompleteRunnable.run();
940 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 }
942
943 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700944 protected void onStop() {
945 super.onStop();
946 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700947
948 if (mLauncherCallbacks != null) {
949 mLauncherCallbacks.onStop();
950 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700951 }
952
953 @Override
954 protected void onStart() {
955 super.onStart();
956 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700957
958 if (mLauncherCallbacks != null) {
959 mLauncherCallbacks.onStart();
960 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700961 }
962
963 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200965 long startTime = 0;
966 if (DEBUG_RESUME_TIME) {
967 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400968 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200969 }
Adam Cohen9211d422014-10-07 18:14:53 -0700970
971 if (mLauncherCallbacks != null) {
972 mLauncherCallbacks.preOnResume();
973 }
974
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700976
Winson Chung4a2afa32012-07-19 14:53:05 -0700977 // Restore the previous launcher state
978 if (mOnResumeState == State.WORKSPACE) {
979 showWorkspace(false);
980 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800981 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700982 }
983 mOnResumeState = State.NONE;
984
Craig Mautner360310b2012-10-26 15:13:08 -0700985 // Background was set to gradient in onPause(), restore to black if in all apps.
986 setWorkspaceBackground(mState == State.WORKSPACE);
987
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800988 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700989 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700990 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500991 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700993 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700995 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200996 // We might have postponed some bind calls until onResume (see waitUntilResume) --
997 // execute them here
998 long startTimeCallbacks = 0;
999 if (DEBUG_RESUME_TIME) {
1000 startTimeCallbacks = System.currentTimeMillis();
1001 }
1002
1003 if (mAppsCustomizeContent != null) {
1004 mAppsCustomizeContent.setBulkBind(true);
1005 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001006 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1007 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001008 }
1009 if (mAppsCustomizeContent != null) {
1010 mAppsCustomizeContent.setBulkBind(false);
1011 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001012 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001013 if (DEBUG_RESUME_TIME) {
1014 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1015 (System.currentTimeMillis() - startTimeCallbacks));
1016 }
Michael Jurka447bf842013-05-15 14:52:15 +02001017 }
Michael Jurka54554252013-08-01 12:52:23 +02001018 if (mOnResumeCallbacks.size() > 0) {
1019 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1020 mOnResumeCallbacks.get(i).run();
1021 }
1022 mOnResumeCallbacks.clear();
1023 }
Winson Chunge4e50662012-01-23 14:45:13 -08001024
1025 // Reset the pressed state of icons that were locked in the press state while activities
1026 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001027 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001028 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001029 mWaitingForResume.setStayPressed(false);
1030 }
Winson Chung82963d52013-10-09 11:20:57 -07001031
Adam Cohen06dff352012-06-01 17:17:08 -07001032 // It is possible that widgets can receive updates while launcher is not in the foreground.
1033 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1034 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1035 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001036 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001037
Winson Chung780fe592013-09-26 14:48:44 -07001038 // Process any items that were added while Launcher was away.
1039 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1040
Michael Jurka447bf842013-05-15 14:52:15 +02001041 if (DEBUG_RESUME_TIME) {
1042 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1043 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001044
1045 if (mWorkspace.getCustomContentCallbacks() != null) {
1046 // If we are resuming and the custom content is the current page, we call onShow().
1047 // It is also poassible that onShow will instead be called slightly after first layout
1048 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1049 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001050 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001051 }
1052 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001053 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001054 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001055
1056 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001057
1058 if (mLauncherCallbacks != null) {
1059 mLauncherCallbacks.onResume();
1060 }
Adam Cohen06dff352012-06-01 17:17:08 -07001061 }
1062
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001064 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001065 // Ensure that items added to Launcher are queued until Launcher returns
1066 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001067 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001068
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001069 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001070 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001071 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001072 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001073
1074 // We call onHide() aggressively. The custom content callbacks should be able to
1075 // debounce excess onHide calls.
1076 if (mWorkspace.getCustomContentCallbacks() != null) {
1077 mWorkspace.getCustomContentCallbacks().onHide();
1078 }
Romain Guycbb89e42009-06-08 15:52:54 -07001079
Adam Cohen9211d422014-10-07 18:14:53 -07001080 if (mLauncherCallbacks != null) {
1081 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001082 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001083 }
1084
1085 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001086 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1087 // by a onResume or by scrolling otherwise.
1088 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001089
1090 // Custom content is completely hidden
1091 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001092
1093 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1094 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001095
1096 // Indicates whether the user is allowed to scroll away from the custom content.
1097 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001098 }
1099
Adam Cohenc2d6e892014-10-16 09:49:24 -07001100 public interface LauncherOverlay {
1101
1102 /**
1103 * Touch interaction leading to overscroll has begun
1104 */
1105 public void onScrollInteractionBegin();
1106
1107 /**
1108 * Touch interaction related to overscroll has ended
1109 */
1110 public void onScrollInteractionEnd();
1111
1112 /**
1113 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1114 * screen (or in the case of RTL, the rightmost screen).
1115 */
1116 public void onScrollChange(int progress, boolean rtl);
1117
1118 /**
1119 * Screen has stopped scrolling
1120 */
1121 public void onScrollSettled();
1122
1123 /**
1124 * This method can be called by the Launcher in order to force the LauncherOverlay
1125 * to exit fully immersive mode.
1126 */
1127 public void forceExitFullImmersion();
1128 }
1129
1130 public interface LauncherOverlayCallbacks {
1131 /**
1132 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1133 * however it doesn't modify any state within the launcher.
1134 */
1135 public boolean canEnterFullImmersion();
1136
1137 /**
1138 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1139 * eg. by occupying the full screen and handling all touch events.
1140 *
1141 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1142 * case, Launcher will modify any necessary state and assumes the overlay is
1143 * handling all interaction. If false, the LauncherOverlay should cancel any
1144 *
1145 */
1146 public boolean enterFullImmersion();
1147
1148 /**
1149 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1150 * full control over UI and state.
1151 */
1152 public void exitFullImmersion();
1153 }
1154
1155 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1156
1157 @Override
1158 public boolean canEnterFullImmersion() {
1159 return mState == State.WORKSPACE;
1160 }
1161
1162 @Override
1163 public boolean enterFullImmersion() {
1164 if (mState == State.WORKSPACE) {
1165 // When fully immersed, disregard any touches which fall through.
1166 mDragLayer.setBlockTouch(true);
1167 return true;
1168 }
1169 return false;
1170 }
1171
1172 @Override
1173 public void exitFullImmersion() {
1174 mDragLayer.setBlockTouch(false);
1175 }
1176 }
1177
Jorim Jaggid017f882014-01-14 17:08:48 -08001178 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001179 if (mLauncherCallbacks != null) {
1180 return mLauncherCallbacks.hasSettings();
1181 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001182 return false;
1183 }
1184
Adam Cohenbffe7452013-07-22 18:21:45 -07001185
Adam Cohen9211d422014-10-07 18:14:53 -07001186 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001187 CustomContentCallbacks callbacks, String description) {
1188 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001189 }
1190
Adam Cohenedb40762013-07-18 16:45:45 -07001191 // The custom content needs to offset its content to account for the QSB
1192 public int getTopOffsetForCustomContent() {
1193 return mWorkspace.getPaddingTop();
1194 }
1195
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001196 @Override
1197 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001198 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001199 if (mModel.isCurrentCallbacks(this)) {
1200 mModel.stopLoader();
1201 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001202 if (mAppsCustomizeContent != null) {
1203 mAppsCustomizeContent.surrender();
1204 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001205 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001206 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001207
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001208 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001209 @Override
1210 public void onWindowFocusChanged(boolean hasFocus) {
1211 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001212 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001213
1214 if (mLauncherCallbacks != null) {
1215 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1216 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001217 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001218
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 private boolean acceptFilter() {
1220 final InputMethodManager inputManager = (InputMethodManager)
1221 getSystemService(Context.INPUT_METHOD_SERVICE);
1222 return !inputManager.isFullscreenMode();
1223 }
1224
1225 @Override
1226 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001227 final int uniChar = event.getUnicodeChar();
1228 final boolean handled = super.onKeyDown(keyCode, event);
1229 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1230 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1232 keyCode, event);
1233 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001234 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001235 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001236 // showSearchDialog()
1237 // If there are multiple keystrokes before the search dialog takes focus,
1238 // onSearchRequested() will be called for every keystroke,
1239 // but it is idempotent, so it's fine.
1240 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 }
1242 }
1243
Joe Onorato8a9625e2010-01-28 15:55:35 -08001244 // Eat the long press event so the keyboard doesn't come up.
1245 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1246 return true;
1247 }
1248
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 return handled;
1250 }
1251
Karl Rosaen138a0412009-04-23 19:00:21 -07001252 private String getTypedText() {
1253 return mDefaultKeySsb.toString();
1254 }
1255
1256 private void clearTypedText() {
1257 mDefaultKeySsb.clear();
1258 mDefaultKeySsb.clearSpans();
1259 Selection.setSelection(mDefaultKeySsb, 0);
1260 }
1261
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001263 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1264 * State
1265 */
1266 private static State intToState(int stateOrdinal) {
1267 State state = State.WORKSPACE;
1268 final State[] stateValues = State.values();
1269 for (int i = 0; i < stateValues.length; i++) {
1270 if (stateValues[i].ordinal() == stateOrdinal) {
1271 state = stateValues[i];
1272 break;
1273 }
1274 }
1275 return state;
1276 }
1277
1278 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 * Restores the previous state, if it exists.
1280 *
1281 * @param savedState The previous state.
1282 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001283 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 private void restoreState(Bundle savedState) {
1285 if (savedState == null) {
1286 return;
1287 }
1288
Michael Jurka883f55b2010-10-21 15:47:14 -07001289 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001290 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001291 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001292 }
1293
Adam Cohen21cd0022013-10-09 18:57:02 -07001294 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1295 PagedView.INVALID_RESTORE_PAGE);
1296 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001297 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
1299
Winson Chung3d503fb2011-07-13 17:25:49 -07001300 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001301 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001302
Winson Chung3d503fb2011-07-13 17:25:49 -07001303 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1304 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001305 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001306 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1307 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001308 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1309 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1310 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001311 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001312 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 mRestoring = true;
1314 }
1315
1316 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1317 if (renameFolder) {
1318 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001319 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 mRestoring = true;
1321 }
Winson Chunga12a2502010-12-20 14:41:35 -08001322
Winson Chung785d2eb2011-04-14 16:08:02 -07001323 // Restore the AppsCustomize tab
1324 if (mAppsCustomizeTabHost != null) {
1325 String curTab = savedState.getString("apps_customize_currentTab");
1326 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001327 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001328 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001329 mAppsCustomizeContent.loadAssociatedPages(
1330 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001331 }
1332
Winson Chung5afbf7b2011-07-25 11:53:08 -07001333 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1334 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001335 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001336 mItemIdToViewId = (HashMap<Integer, Integer>)
1337 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
1339
1340 /**
1341 * Finds all the views we need and configure them properly.
1342 */
1343 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001344 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001345
Craig Mautner360310b2012-10-26 15:13:08 -07001346 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001347 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001348 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1349 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001350 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001351 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001352
John Spurlock77e1f472013-09-11 10:09:51 -04001353 mLauncherView.setSystemUiVisibility(
1354 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001355 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356
Winson Chung4c98d922011-05-31 16:50:48 -07001357 // Setup the drag layer
1358 mDragLayer.setup(this, dragController);
1359
Winson Chung3d503fb2011-07-13 17:25:49 -07001360 // Setup the hotseat
1361 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1362 if (mHotseat != null) {
1363 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001364 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001365 }
1366
Jorim Jaggid017f882014-01-14 17:08:48 -08001367 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001368 View widgetButton = findViewById(R.id.widget_button);
1369 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001370 @Override
1371 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001372 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001373 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001374 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001375 }
1376 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001377 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1378
1379 View wallpaperButton = findViewById(R.id.wallpaper_button);
1380 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001381 @Override
1382 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001383 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001384 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001385 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001386 }
1387 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001388 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1389
1390 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001391 if (hasSettings()) {
1392 settingsButton.setOnClickListener(new OnClickListener() {
1393 @Override
1394 public void onClick(View arg0) {
1395 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001396 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001397 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001398 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001399 });
1400 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1401 } else {
1402 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001403 }
1404
Adam Cohendbdff6b2013-09-18 19:09:15 -07001405 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001406
Winson Chung4c98d922011-05-31 16:50:48 -07001407 // Setup the workspace
1408 mWorkspace.setHapticFeedbackEnabled(false);
1409 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001410 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001411 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001412
Winson Chungf0ea4d32011-06-06 14:27:16 -07001413 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001414 mSearchDropTargetBar = (SearchDropTargetBar)
1415 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001416
Winson Chungf0ea4d32011-06-06 14:27:16 -07001417 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001418 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001419 mAppsCustomizeContent = (AppsCustomizePagedView)
1420 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1421 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001422
Winson Chung3d503fb2011-07-13 17:25:49 -07001423 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001424 dragController.setDragScoller(mWorkspace);
1425 dragController.setScrollView(mDragLayer);
1426 dragController.setMoveTarget(mWorkspace);
1427 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001428 if (mSearchDropTargetBar != null) {
1429 mSearchDropTargetBar.setup(this, dragController);
Sunny Goyal594d76d2014-11-06 10:12:54 -08001430 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001431 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001432
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001433 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001434 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001435 mWeightWatcher = new WeightWatcher(this);
1436 mWeightWatcher.setAlpha(0.5f);
1437 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001438 new FrameLayout.LayoutParams(
1439 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001440 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001441 Gravity.BOTTOM)
1442 );
Adam Cohen39a06042013-07-19 14:30:12 -07001443
1444 boolean show = shouldShowWeightWatcher();
1445 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 }
1448
1449 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001450 * Sets the all apps button. This method is called from {@link Hotseat}.
1451 */
1452 public void setAllAppsButton(View allAppsButton) {
1453 mAllAppsButton = allAppsButton;
1454 }
1455
1456 public View getAllAppsButton() {
1457 return mAllAppsButton;
1458 }
1459
1460 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 * Creates a view representing a shortcut.
1462 *
1463 * @param info The data structure describing the shortcut.
1464 *
1465 * @return A View inflated from R.layout.application.
1466 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001467 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001469 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 }
1471
1472 /**
1473 * Creates a view representing a shortcut inflated from the specified resource.
1474 *
1475 * @param layoutResId The id of the XML layout used to create the shortcut.
1476 * @param parent The group the shortcut belongs to.
1477 * @param info The data structure describing the shortcut.
1478 *
1479 * @return A View inflated from layoutResId.
1480 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001481 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001482 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001483 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001485 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 return favorite;
1487 }
1488
1489 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 * Add a shortcut to the workspace.
1491 *
1492 * @param data The intent describing the shortcut.
1493 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001495 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001496 int cellY) {
1497 int[] cellXY = mTmpAddItemCellCoordinates;
1498 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001499 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001500
Michael Jurkad3ef3062010-11-23 16:23:58 -08001501 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001502
Sunny Goyal2350bc92014-10-14 16:42:54 -07001503 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001504 if (info == null) {
1505 return;
1506 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001507 final View view = createShortcut(info);
1508
Adam Cohenfbba09b2011-07-18 21:43:05 -07001509 // First we check if we already know the exact location where we want to add this item.
1510 if (cellX >= 0 && cellY >= 0) {
1511 cellXY[0] = cellX;
1512 cellXY[1] = cellY;
1513 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001514
1515 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001516 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001517 true, null,null)) {
1518 return;
1519 }
1520 DragObject dragObject = new DragObject();
1521 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001522 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1523 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001524 return;
1525 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001526 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001527 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001528 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001529 foundCellSpan = (result != null);
1530 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001531 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001532 }
1533
1534 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001535 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001536 return;
1537 }
1538
Adam Cohendcd297f2013-06-18 13:13:40 -07001539 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540
1541 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001542 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001543 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 }
1545 }
1546
Adam Cohen2f093b62012-04-30 18:59:53 -07001547 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1548 int minHeight) {
1549 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001550 // We want to account for the extra amount of padding that we are adding to the widget
1551 // to ensure that it gets the full amount of space that it has requested
1552 int requiredWidth = minWidth + padding.left + padding.right;
1553 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001554 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001555 }
1556
Adam Cohen2f093b62012-04-30 18:59:53 -07001557 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1558 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001559 }
1560
Adam Cohen2f093b62012-04-30 18:59:53 -07001561 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1562 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001563 }
1564
Adam Cohen2f093b62012-04-30 18:59:53 -07001565 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1566 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001567 }
1568
Adam Cohen2f093b62012-04-30 18:59:53 -07001569 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1570 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001571 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001572 }
1573
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001575 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001577 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001578 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001580 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001581 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1582 if (appWidgetInfo == null) {
1583 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1584 }
Romain Guycbb89e42009-06-08 15:52:54 -07001585
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001586 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001587 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001588
Adam Cohen2f093b62012-04-30 18:59:53 -07001589 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1590 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001593 // We have saved the position to which the widget was dragged-- this really only matters
1594 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001595 int[] cellXY = mTmpAddItemCellCoordinates;
1596 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001597 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001598 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001599 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1600 cellXY[0] = mPendingAddInfo.cellX;
1601 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001602 spanXY[0] = mPendingAddInfo.spanX;
1603 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001604 foundCellSpan = true;
1605 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001606 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001607 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001608 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1609 spanXY[1], cellXY, finalSpan);
1610 spanXY[0] = finalSpan[0];
1611 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001612 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001613 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001614 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001615 }
1616
Michael Jurka0280c3b2010-09-17 15:00:07 -07001617 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001618 if (appWidgetId != -1) {
1619 // Deleting an app widget ID is a void call but writes to disk before returning
1620 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001621 new AsyncTask<Void, Void, Void>() {
1622 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001623 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001624 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001625 }
Michael Jurka43467462013-11-14 12:03:58 +01001626 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001627 }
Winson Chung93eef082012-03-23 15:59:27 -07001628 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001629 return;
1630 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001632 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001633 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1634 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001635 launcherInfo.spanX = spanXY[0];
1636 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001637 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1638 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001639 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001640
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001642 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643
1644 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001645 if (hostView == null) {
1646 // Perform actual inflation because we're live
1647 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1648 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1649 } else {
1650 // The AppWidgetHostView has already been inflated and instantiated
1651 launcherInfo.hostView = hostView;
1652 }
Romain Guycbb89e42009-06-08 15:52:54 -07001653
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001655 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001656 launcherInfo.notifyWidgetSizeChanged(this);
1657
Adam Cohendcd297f2013-06-18 13:13:40 -07001658 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001660
1661 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001663 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 }
Romain Guycbb89e42009-06-08 15:52:54 -07001665
Adam Cohended9f8d2010-11-03 13:25:16 -07001666 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1667 @Override
1668 public void onReceive(Context context, Intent intent) {
1669 final String action = intent.getAction();
1670 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1671 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001672 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001673 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001674
Winson Chungc100e8e2011-08-09 16:02:43 -07001675 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001676 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001677 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001678 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001679 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001680 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1681 mUserPresent = true;
1682 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001683 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1684 mModel.resetLoadedState(false, true);
1685 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1686 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1687 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1688 mModel.resetLoadedState(false, true);
1689 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1690 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1691 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001692 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1693 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1694 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001695 }
1696 }
1697 };
1698
1699 @Override
1700 public void onAttachedToWindow() {
1701 super.onAttachedToWindow();
1702
1703 // Listen for broadcasts related to user-presence
1704 final IntentFilter filter = new IntentFilter();
1705 filter.addAction(Intent.ACTION_SCREEN_OFF);
1706 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001707 // For handling managed profiles
1708 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1709 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001710 if (ENABLE_DEBUG_INTENTS) {
1711 filter.addAction(DebugIntents.DELETE_DATABASE);
1712 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1713 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001715 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001716 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001717 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 mVisible = true;
1719 }
1720
Adam Cohen0b395352014-06-09 22:54:36 +00001721 /**
1722 * Sets up transparent navigation and status bars in LMP.
1723 * This method is a no-op for other platform versions.
1724 */
1725 @TargetApi(19)
1726 private void setupTransparentSystemBarsForLmp() {
1727 // TODO(sansid): use the APIs directly when compiling against L sdk.
1728 // Currently we use reflection to access the flags and the API to set the transparency
1729 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001730 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001731 try {
1732 getWindow().getAttributes().systemUiVisibility |=
1733 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1734 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1735 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1736 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1737 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1738 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1739 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1740 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1741
1742 Method setStatusBarColorMethod =
1743 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1744 Method setNavigationBarColorMethod =
1745 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1746 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1747 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1748 } catch (NoSuchFieldException e) {
1749 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1750 } catch (NoSuchMethodException ex) {
1751 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1752 } catch (IllegalAccessException e) {
1753 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1754 } catch (IllegalArgumentException e) {
1755 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1756 } catch (InvocationTargetException e) {
1757 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1758 } finally {}
1759 }
1760 }
1761
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 @Override
1763 public void onDetachedFromWindow() {
1764 super.onDetachedFromWindow();
1765 mVisible = false;
1766
Adam Cohend113e0c2010-11-11 10:48:05 -08001767 if (mAttached) {
1768 unregisterReceiver(mReceiver);
1769 mAttached = false;
1770 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 updateRunning();
1772 }
1773
1774 public void onWindowVisibilityChanged(int visibility) {
1775 mVisible = visibility == View.VISIBLE;
1776 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001777 // The following code used to be in onResume, but it turns out onResume is called when
1778 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1779 // is a more appropriate event to handle
1780 if (mVisible) {
1781 mAppsCustomizeTabHost.onWindowVisible();
1782 if (!mWorkspaceLoading) {
1783 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001784 // We want to let Launcher draw itself at least once before we force it to build
1785 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001786 // apps is nice and speedy.
1787 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001788 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001789 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001790 if (mStarted) return;
1791 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001792 // We delay the layer building a bit in order to give
1793 // other message processing a time to run. In particular
1794 // this avoids a delay in hiding the IME if it was
1795 // currently shown, because doing that may involve
1796 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001797 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001798 final ViewTreeObserver.OnDrawListener listener = this;
1799 mWorkspace.post(new Runnable() {
1800 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001801 if (mWorkspace != null &&
1802 mWorkspace.getViewTreeObserver() != null) {
1803 mWorkspace.getViewTreeObserver().
1804 removeOnDrawListener(listener);
1805 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001806 }
1807 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001808 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001809 }
1810 });
1811 }
1812 clearTypedText();
1813 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001814 }
1815
1816 private void sendAdvanceMessage(long delay) {
1817 mHandler.removeMessages(ADVANCE_MSG);
1818 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1819 mHandler.sendMessageDelayed(msg, delay);
1820 mAutoAdvanceSentTime = System.currentTimeMillis();
1821 }
1822
1823 private void updateRunning() {
1824 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1825 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1826 mAutoAdvanceRunning = autoAdvanceRunning;
1827 if (autoAdvanceRunning) {
1828 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1829 sendAdvanceMessage(delay);
1830 } else {
1831 if (!mWidgetsToAdvance.isEmpty()) {
1832 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1833 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1834 }
1835 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001836 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001837 }
1838 }
1839 }
1840
1841 private final Handler mHandler = new Handler() {
1842 @Override
1843 public void handleMessage(Message msg) {
1844 if (msg.what == ADVANCE_MSG) {
1845 int i = 0;
1846 for (View key: mWidgetsToAdvance.keySet()) {
1847 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1848 final int delay = mAdvanceStagger * i;
1849 if (v instanceof Advanceable) {
1850 postDelayed(new Runnable() {
1851 public void run() {
1852 ((Advanceable) v).advance();
1853 }
1854 }, delay);
1855 }
1856 i++;
1857 }
1858 sendAdvanceMessage(mAdvanceInterval);
1859 }
1860 }
1861 };
1862
1863 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001864 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001865 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1866 if (v instanceof Advanceable) {
1867 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001868 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001869 updateRunning();
1870 }
1871 }
1872
1873 void removeWidgetToAutoAdvance(View hostView) {
1874 if (mWidgetsToAdvance.containsKey(hostView)) {
1875 mWidgetsToAdvance.remove(hostView);
1876 updateRunning();
1877 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001878 }
1879
Joe Onorato9c1289c2009-08-17 11:03:03 -04001880 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001881 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001882 launcherInfo.hostView = null;
1883 }
1884
Winson Chung93eef082012-03-23 15:59:27 -07001885 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1886 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1887 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001888 }
1889
Winson Chunga6945242014-01-08 14:04:34 -08001890 public DragLayer getDragLayer() {
1891 return mDragLayer;
1892 }
1893
1894 public Workspace getWorkspace() {
1895 return mWorkspace;
1896 }
1897
1898 public Hotseat getHotseat() {
1899 return mHotseat;
1900 }
1901
Jorim Jaggid017f882014-01-14 17:08:48 -08001902 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001903 return mOverviewPanel;
1904 }
1905
1906 public SearchDropTargetBar getSearchBar() {
1907 return mSearchDropTargetBar;
1908 }
1909
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001910 public LauncherAppWidgetHost getAppWidgetHost() {
1911 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
Romain Guycbb89e42009-06-08 15:52:54 -07001913
Winson Chunga9abd0e2010-10-27 17:18:37 -07001914 public LauncherModel getModel() {
1915 return mModel;
1916 }
1917
Winson Chunga6945242014-01-08 14:04:34 -08001918 protected SharedPreferences getSharedPrefs() {
1919 return mSharedPrefs;
1920 }
1921
Bjorn Bringertc459e522013-06-07 19:36:01 +01001922 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001923 getWindow().closeAllPanels();
1924
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001925 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001926 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001927 }
1928
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 @Override
1930 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001931 long startTime = 0;
1932 if (DEBUG_RESUME_TIME) {
1933 startTime = System.currentTimeMillis();
1934 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 super.onNewIntent(intent);
1936
1937 // Close the menu
1938 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001939 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001940 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001941
Adam Cohened307df2013-10-02 09:37:31 -07001942 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1943 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1944 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001945
Adam Cohen6fecd412013-10-02 17:41:50 -07001946 if (mWorkspace == null) {
1947 // Can be cases where mWorkspace is null, this prevents a NPE
1948 return;
1949 }
1950 Folder openFolder = mWorkspace.getOpenFolder();
1951 // In all these cases, only animate if we're already on home
1952 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001953
1954 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1955 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001956 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001957 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001958 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001959 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001960
Adam Cohen6fecd412013-10-02 17:41:50 -07001961 closeFolder();
1962 exitSpringLoadedDragMode();
1963
1964 // If we are already on home, then just animate back to the workspace,
1965 // otherwise, just wait until onResume to set the state back to Workspace
1966 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001967 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001968 } else {
1969 mOnResumeState = State.WORKSPACE;
1970 }
1971
1972 final View v = getWindow().peekDecorView();
1973 if (v != null && v.getWindowToken() != null) {
1974 InputMethodManager imm = (InputMethodManager)getSystemService(
1975 INPUT_METHOD_SERVICE);
1976 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1977 }
1978
1979 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001980 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001981 mAppsCustomizeTabHost.reset();
1982 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001983
Adam Cohen9211d422014-10-07 18:14:53 -07001984 if (mLauncherCallbacks != null) {
1985 mLauncherCallbacks.onHomeIntent();
1986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 }
Adam Cohened307df2013-10-02 09:37:31 -07001988
Michael Jurka447bf842013-05-15 14:52:15 +02001989 if (DEBUG_RESUME_TIME) {
1990 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1991 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992
Adam Cohen9211d422014-10-07 18:14:53 -07001993 if (mLauncherCallbacks != null) {
1994 mLauncherCallbacks.onNewIntent(intent);
1995 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001996 }
1997
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001999 public void onRestoreInstanceState(Bundle state) {
2000 super.onRestoreInstanceState(state);
2001 for (int page: mSynchronouslyBoundPages) {
2002 mWorkspace.restoreInstanceStateForChild(page);
2003 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 }
2005
2006 @Override
2007 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002008 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002009 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2010 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002011 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002012 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013
Michael Jurka883f55b2010-10-21 15:47:14 -07002014 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002015 // We close any open folder since it will not be re-opened, and we need to make sure
2016 // this state is reflected.
2017 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018
Adam Cohendcd297f2013-06-18 13:13:40 -07002019 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002020 mWaitingForResult) {
2021 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002022 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002023 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2024 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002025 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2026 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2027 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002028 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 }
2030
2031 if (mFolderInfo != null && mWaitingForResult) {
2032 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2033 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2034 }
Michael Jurka946ad472010-07-09 18:05:18 -07002035
Winson Chung785d2eb2011-04-14 16:08:02 -07002036 // Save the current AppsCustomize tab
2037 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08002038 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2039 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002040 if (currentTabTag != null) {
2041 outState.putString("apps_customize_currentTab", currentTabTag);
2042 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002043 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2044 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002045 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002046 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002047
2048 if (mLauncherCallbacks != null) {
2049 mLauncherCallbacks.onSaveInstanceState(outState);
2050 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 }
2052
2053 @Override
2054 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002056
Winson Chunge7a03942011-08-05 15:05:12 -07002057 // Remove all pending runnables
2058 mHandler.removeMessages(ADVANCE_MSG);
2059 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002060 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002061
Winson Chungcd2b0142011-06-08 16:02:26 -07002062 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002063 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002064
2065 // It's possible to receive onDestroy after a new Launcher activity has
2066 // been created. In this case, don't interfere with the new Launcher.
2067 if (mModel.isCurrentCallbacks(this)) {
2068 mModel.stopLoader();
2069 app.setLauncher(null);
2070 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002073 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002075 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002077 mAppWidgetHost = null;
2078
2079 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080
2081 TextKeyListener.getInstance().release();
2082
Winson Chung81b52252012-08-27 15:34:29 -07002083 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2084 // to prevent leaking Launcher activities on orientation change.
2085 if (mModel != null) {
2086 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2087 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002088
2089 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002090 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002091
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002092 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002093 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002094 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002095 mWorkspace = null;
2096 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002097
Michael Jurka2ecf9952012-06-18 12:52:28 -07002098 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002099
2100 if (mLauncherCallbacks != null) {
2101 mLauncherCallbacks.onDestroy();
2102 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 }
2104
Adam Cohena9cf38f2011-05-02 15:36:58 -07002105 public DragController getDragController() {
2106 return mDragController;
2107 }
2108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 @Override
2110 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002111 if (requestCode >= 0) {
2112 setWaitingForResult(true);
2113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 super.startActivityForResult(intent, requestCode);
2115 }
2116
Winson Chung70d72102011-08-12 11:24:35 -07002117 /**
2118 * Indicates that we want global search for this activity by setting the globalSearch
2119 * argument for {@link #startSearch} to true.
2120 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002122 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002124
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002125 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002126
Karl Rosaen138a0412009-04-23 19:00:21 -07002127 if (initialQuery == null) {
2128 // Use any text typed in the launcher as the initial query
2129 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 if (appSearchData == null) {
2132 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002133 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 }
Winson Chungadf0c182012-08-23 14:59:07 -07002135 Rect sourceBounds = new Rect();
2136 if (mSearchDropTargetBar != null) {
2137 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2138 }
Romain Guycbb89e42009-06-08 15:52:54 -07002139
Ian Parkinson047036e2014-09-01 15:40:53 +01002140 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002141 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002142 if (clearTextImmediately) {
2143 clearTypedText();
2144 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002145 }
2146
Ian Parkinson047036e2014-09-01 15:40:53 +01002147 /**
2148 * Start a text search.
2149 *
2150 * @return {@code true} if the search will start immediately, so any further keypresses
2151 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2152 * to buffer keypresses.
2153 */
2154 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002155 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002156 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2157 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2158 sourceBounds);
2159 }
2160
Michael Jurkaa33411c2012-06-14 16:18:21 -07002161 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002162 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002163 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002164 }
2165
2166 /**
2167 * Starts the global search activity. This code is a copied from SearchManager
2168 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002169 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002170 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002171 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002172 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2173 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2174 if (globalSearchActivity == null) {
2175 Log.w(TAG, "No global search activity found.");
2176 return;
2177 }
2178 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2179 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2180 intent.setComponent(globalSearchActivity);
2181 // Make sure that we have a Bundle to put source in
2182 if (appSearchData == null) {
2183 appSearchData = new Bundle();
2184 } else {
2185 appSearchData = new Bundle(appSearchData);
2186 }
Adam Cohen9211d422014-10-07 18:14:53 -07002187 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002188 if (!appSearchData.containsKey("source")) {
2189 appSearchData.putString("source", getPackageName());
2190 }
2191 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2192 if (!TextUtils.isEmpty(initialQuery)) {
2193 intent.putExtra(SearchManager.QUERY, initialQuery);
2194 }
2195 if (selectInitialQuery) {
2196 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2197 }
2198 intent.setSourceBounds(sourceBounds);
2199 try {
2200 startActivity(intent);
2201 } catch (ActivityNotFoundException ex) {
2202 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2203 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 }
2205
Yura4f93ec62014-02-04 14:15:21 +00002206 public boolean isOnCustomContent() {
2207 return mWorkspace.isOnOrMovingToCustomContent();
2208 }
2209
Winson Chung70d72102011-08-12 11:24:35 -07002210 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002211 public boolean onPrepareOptionsMenu(Menu menu) {
2212 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002213 if (!isOnCustomContent()) {
2214 // Close any open folders
2215 closeFolder();
2216 // Stop resizing any widgets
2217 mWorkspace.exitWidgetResizeMode();
2218 if (!mWorkspace.isInOverviewMode()) {
2219 // Show the overview mode
2220 showOverviewMode(true);
2221 } else {
2222 showWorkspace(true);
2223 }
Winson Chunge0298742014-01-17 12:03:00 -08002224 }
Adam Cohen9211d422014-10-07 18:14:53 -07002225 if (mLauncherCallbacks != null) {
2226 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2227 }
2228
Michael Jurkab94f3f82013-09-11 18:08:54 +02002229 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002230 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002232 @Override
2233 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002234 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002235 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002236 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002237 }
2238
Joe Onorato9c1289c2009-08-17 11:03:03 -04002239 public boolean isWorkspaceLocked() {
2240 return mWorkspaceLoading || mWaitingForResult;
2241 }
2242
Adam Cohen517a7f52014-03-01 12:12:59 -08002243 public boolean isWorkspaceLoading() {
2244 return mWorkspaceLoading;
2245 }
2246
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002247 private void setWorkspaceLoading(boolean value) {
2248 boolean isLocked = isWorkspaceLocked();
2249 mWorkspaceLoading = value;
2250 if (isLocked != isWorkspaceLocked()) {
2251 onWorkspaceLockedChanged();
2252 }
2253 }
2254
2255 private void setWaitingForResult(boolean value) {
2256 boolean isLocked = isWorkspaceLocked();
2257 mWaitingForResult = value;
2258 if (isLocked != isWorkspaceLocked()) {
2259 onWorkspaceLockedChanged();
2260 }
2261 }
2262
Adam Cohen9211d422014-10-07 18:14:53 -07002263 protected void onWorkspaceLockedChanged() {
2264 if (mLauncherCallbacks != null) {
2265 mLauncherCallbacks.onWorkspaceLockedChanged();
2266 }
2267 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002268
Michael Jurka0280c3b2010-09-17 15:00:07 -07002269 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002270 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002271 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002272 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2273 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002274 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002275 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002276 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002277
Adam Cohenad4e15c2013-10-17 16:21:35 -07002278 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2279 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2280 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2281 }
2282
2283 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2284 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2285 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002286 if (appWidgetInfo.configure != null) {
2287 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002288 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002289
Bjorn Bringert7984c942009-12-09 15:38:25 +00002290 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002291 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2292 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2293
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002294 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002295 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002296 Runnable onComplete = new Runnable() {
2297 @Override
2298 public void run() {
2299 // Exit spring loaded mode if necessary after adding the widget
2300 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2301 null);
2302 }
2303 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002304 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002305 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002306 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002307 }
2308 }
Romain Guycbb89e42009-06-08 15:52:54 -07002309
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002310 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002311 // Close any folders that may be open.
2312 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002313 mWorkspace.moveToCustomContentScreen(animate);
2314 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002315 /**
2316 * Process a shortcut drop.
2317 *
2318 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002319 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002320 * @param cell The cell it should be added to, optional
2321 * @param position The location on the screen where it was dropped, optional
2322 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002324 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002325 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002326 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002327 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002328 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329
2330 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002331 mPendingAddInfo.cellX = cell[0];
2332 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002333 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002334
2335 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2336 createShortcutIntent.setComponent(componentName);
2337 processShortcut(createShortcutIntent);
2338 }
2339
Adam Cohenfbba09b2011-07-18 21:43:05 -07002340 /**
2341 * Process a widget drop.
2342 *
2343 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002344 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002345 * @param cell The cell it should be added to, optional
2346 * @param position The location on the screen where it was dropped, optional
2347 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002348 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002349 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002350 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002351 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002352 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002353 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002354 mPendingAddInfo.minSpanX = info.minSpanX;
2355 mPendingAddInfo.minSpanY = info.minSpanY;
2356
Adam Cohenfbba09b2011-07-18 21:43:05 -07002357 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002358 mPendingAddInfo.cellX = cell[0];
2359 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002360 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002361 if (span != null) {
2362 mPendingAddInfo.spanX = span[0];
2363 mPendingAddInfo.spanY = span[1];
2364 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002365
Adam Cohened66b2b2012-01-23 17:28:51 -08002366 AppWidgetHostView hostView = info.boundWidget;
2367 int appWidgetId;
2368 if (hostView != null) {
2369 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002370 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002371 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002372 // In this case, we either need to start an activity to get permission to bind
2373 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002374 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002375 Bundle options = info.bindOptions;
2376
Sunny Goyalffe83f12014-08-14 17:39:34 -07002377 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2378 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002379 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002380 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002381 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002382 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002383 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2384 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2385 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002386 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2387 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002388 // TODO: we need to make sure that this accounts for the options bundle.
2389 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002390 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2391 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002392 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002393 }
2394
Joe Onoratodeb98af2010-02-19 14:59:39 -08002395 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002396 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 }
2398
Winson Chung24ab2f12010-09-16 14:10:47 -07002399 void processWallpaper(Intent intent) {
2400 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2401 }
2402
Adam Cohendcd297f2013-06-18 13:13:40 -07002403 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002404 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002405 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 folderInfo.title = getText(R.string.folder_name);
2407
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002409 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002410 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002411 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412
2413 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002414 FolderIcon newFolder =
2415 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002416 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002417 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002418 // Force measure the new folder icon
2419 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2420 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002421 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 }
Romain Guycbb89e42009-06-08 15:52:54 -07002423
Joe Onorato9c1289c2009-08-17 11:03:03 -04002424 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002425 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002426 }
2427
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002428 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002429 if (mLauncherCallbacks != null) {
2430 return mLauncherCallbacks.getWallpaperPickerComponent();
2431 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002432 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002433 }
2434
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002435 /**
2436 * Registers various content observers. The current implementation registers
2437 * only a favorites observer to keep track of the favorites applications.
2438 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002439 private void registerContentObservers() {
2440 ContentResolver resolver = getContentResolver();
2441 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2442 true, mWidgetObserver);
2443 }
2444
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002445 @Override
2446 public boolean dispatchKeyEvent(KeyEvent event) {
2447 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2448 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002450 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002451 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002452 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002453 dumpState();
2454 return true;
2455 }
2456 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002457 }
2458 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2459 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002460 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002461 return true;
2462 }
2463 }
2464
2465 return super.dispatchKeyEvent(event);
2466 }
2467
Joe Onorato88ec0992009-11-19 13:16:06 -08002468 @Override
2469 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002470 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2471 return;
2472 }
2473
Winson Chungc93e5ae2012-07-23 20:48:26 -07002474 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002475 if (mAppsCustomizeContent.getContentType() ==
2476 AppsCustomizePagedView.ContentType.Applications) {
2477 showWorkspace(true);
2478 } else {
2479 showOverviewMode(true);
2480 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002481 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002482 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002483 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002484 Folder openFolder = mWorkspace.getOpenFolder();
2485 if (openFolder.isEditingName()) {
2486 openFolder.dismissEditingName();
2487 } else {
2488 closeFolder();
2489 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002490 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002491 mWorkspace.exitWidgetResizeMode();
2492
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002493 // Back button is a no-op here, but give at least some feedback for the button press
2494 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002495 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002496 }
2497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002499 * Re-listen when widgets are reset.
2500 */
2501 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002502 if (mAppWidgetHost != null) {
2503 mAppWidgetHost.startListening();
2504 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002505 }
2506
2507 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 * Launches the intent referred by the clicked shortcut.
2509 *
2510 * @param v The view representing the clicked shortcut.
2511 */
2512 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002513 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2514 // view has detached (it's possible for this to happen if the view is removed mid touch).
2515 if (v.getWindowToken() == null) {
2516 return;
2517 }
2518
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002519 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002520 return;
2521 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002522
Adam Cohen1697b792013-09-17 19:08:21 -07002523 if (v instanceof Workspace) {
2524 if (mWorkspace.isInOverviewMode()) {
2525 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002526 }
2527 return;
2528 }
2529
2530 if (v instanceof CellLayout) {
2531 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002532 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002533 }
2534 }
2535
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002537 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002538 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002540 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002541 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002542 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002543 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002544 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002545 } else if (tag instanceof AppInfo) {
2546 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002547 } else if (tag instanceof LauncherAppWidgetInfo) {
2548 if (v instanceof PendingAppWidgetHostView) {
2549 onClickPendingWidget((PendingAppWidgetHostView) v);
2550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 }
2552 }
2553
Sunny Goyal508da152014-08-14 10:53:27 -07002554 public void onClickPagedViewIcon(View v) {
2555 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002556 if (mLauncherCallbacks != null) {
2557 mLauncherCallbacks.onClickPagedViewIcon(v);
2558 }
Sunny Goyal508da152014-08-14 10:53:27 -07002559 }
2560
Michael Jurka0e260592010-06-30 17:07:39 -07002561 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002562 return false;
2563 }
2564
Michael Jurkaaf442092010-06-10 17:01:57 -07002565 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002566 * Event handler for the app widget view which has not fully restored.
2567 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002568 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002569 if (mIsSafeModeEnabled) {
2570 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2571 return;
2572 }
2573
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002574 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002575 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002576 int widgetId = info.appWidgetId;
2577 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2578 if (appWidgetInfo != null) {
2579 mPendingAddWidgetInfo = appWidgetInfo;
2580 mPendingAddInfo.copyFrom(info);
2581 mPendingAddWidgetId = widgetId;
2582
Sunny Goyalffe83f12014-08-14 17:39:34 -07002583 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2584 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002585 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002586 } else if (info.installProgress < 0) {
2587 // The install has not been queued
2588 final String packageName = info.providerName.getPackageName();
2589 showBrokenAppInstallDialog(packageName,
2590 new DialogInterface.OnClickListener() {
2591 public void onClick(DialogInterface dialog, int id) {
2592 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2593 }
2594 });
2595 } else {
2596 // Download has started.
2597 final String packageName = info.providerName.getPackageName();
2598 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002599 }
2600 }
2601
2602 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002603 * Event handler for the "grid" button that appears on the home screen, which
2604 * enters all apps mode.
2605 *
2606 * @param v The view that was clicked.
2607 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002608 protected void onClickAllAppsButton(View v) {
2609 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2610 if (isAllAppsVisible()) {
2611 showWorkspace(true);
2612 } else {
2613 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2614 }
Adam Cohen9211d422014-10-07 18:14:53 -07002615 if (mLauncherCallbacks != null) {
2616 mLauncherCallbacks.onClickAllAppsButton(v);
2617 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002618 }
2619
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002620 private void showBrokenAppInstallDialog(final String packageName,
2621 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002622 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002623 .setTitle(R.string.abandoned_promises_title)
2624 .setMessage(R.string.abandoned_promise_explanation)
2625 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2626 .setNeutralButton(R.string.abandoned_clean_this,
2627 new DialogInterface.OnClickListener() {
2628 public void onClick(DialogInterface dialog, int id) {
2629 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2630 mWorkspace.removeAbandonedPromise(packageName, user);
2631 }
2632 })
2633 .create().show();
2634 return;
2635 }
2636
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002637 /**
2638 * Event handler for an app shortcut click.
2639 *
2640 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2641 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002642 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002643 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2644 Object tag = v.getTag();
2645 if (!(tag instanceof ShortcutInfo)) {
2646 throw new IllegalArgumentException("Input must be a Shortcut");
2647 }
2648
2649 // Open shortcut
2650 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002651
2652 if (shortcut.isDisabled != 0) {
2653 int error = R.string.activity_not_available;
2654 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2655 error = R.string.safemode_shortcut_error;
2656 }
2657 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2658 return;
2659 }
2660
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002661 final Intent intent = shortcut.intent;
2662
2663 // Check for special shortcuts
2664 if (intent.getComponent() != null) {
2665 final String shortcutClass = intent.getComponent().getClassName();
2666
2667 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2668 MemoryDumpActivity.startDump(this);
2669 return;
2670 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2671 toggleShowWeightWatcher();
2672 return;
2673 }
2674 }
2675
Chris Wren40c5ed32014-06-24 18:24:23 -04002676 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002677 if ((v instanceof BubbleTextView)
2678 && shortcut.isPromise()
2679 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002680 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002681 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002682 new DialogInterface.OnClickListener() {
2683 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002684 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002685 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002686 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002687 return;
2688 }
2689
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002690 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002691 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002692
2693 if (mLauncherCallbacks != null) {
2694 mLauncherCallbacks.onClickAppShortcut(v);
2695 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002696 }
2697
Sunny Goyal508da152014-08-14 10:53:27 -07002698 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002699 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002700 final ShortcutInfo shortcut;
2701 final Intent intent;
2702 if (tag instanceof ShortcutInfo) {
2703 shortcut = (ShortcutInfo) tag;
2704 intent = shortcut.intent;
2705 int[] pos = new int[2];
2706 v.getLocationOnScreen(pos);
2707 intent.setSourceBounds(new Rect(pos[0], pos[1],
2708 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002709
Sunny Goyal508da152014-08-14 10:53:27 -07002710 } else if (tag instanceof AppInfo) {
2711 shortcut = null;
2712 intent = ((AppInfo) tag).intent;
2713 } else {
2714 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2715 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716
2717 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002718 mStats.recordLaunch(intent, shortcut);
2719
2720 if (success && v instanceof BubbleTextView) {
2721 mWaitingForResume = (BubbleTextView) v;
2722 mWaitingForResume.setStayPressed(true);
2723 }
2724 }
2725
2726 /**
2727 * Event handler for a folder icon click.
2728 *
2729 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2730 */
2731 protected void onClickFolderIcon(View v) {
2732 if (LOGD) Log.d(TAG, "onClickFolder");
2733 if (!(v instanceof FolderIcon)){
2734 throw new IllegalArgumentException("Input must be a FolderIcon");
2735 }
2736
2737 FolderIcon folderIcon = (FolderIcon) v;
2738 final FolderInfo info = folderIcon.getFolderInfo();
2739 Folder openFolder = mWorkspace.getFolderForTag(info);
2740
2741 // If the folder info reports that the associated folder is open, then verify that
2742 // it is actually opened. There have been a few instances where this gets out of sync.
2743 if (info.opened && openFolder == null) {
2744 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2745 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2746 info.opened = false;
2747 }
2748
2749 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2750 // Close any open folder
2751 closeFolder();
2752 // Open the requested folder
2753 openFolder(folderIcon);
2754 } else {
2755 // Find the open folder...
2756 int folderScreen;
2757 if (openFolder != null) {
2758 folderScreen = mWorkspace.getPageForView(openFolder);
2759 // .. and close it
2760 closeFolder(openFolder);
2761 if (folderScreen != mWorkspace.getCurrentPage()) {
2762 // Close any folder open on the current screen
2763 closeFolder();
2764 // Pull the folder onto this screen
2765 openFolder(folderIcon);
2766 }
2767 }
2768 }
Adam Cohen9211d422014-10-07 18:14:53 -07002769
2770 if (mLauncherCallbacks != null) {
2771 mLauncherCallbacks.onClickFolderIcon(v);
2772 }
Michael Jurka5130e402011-10-13 04:55:35 -07002773 }
2774
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002775 /**
2776 * Event handler for the (Add) Widgets button that appears after a long press
2777 * on the home screen.
2778 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002779 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002780 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002781 if (mIsSafeModeEnabled) {
2782 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2783 } else {
2784 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2785 if (mLauncherCallbacks != null) {
2786 mLauncherCallbacks.onClickAddWidgetButton(view);
2787 }
Adam Cohen9211d422014-10-07 18:14:53 -07002788 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002789 }
2790
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002791 /**
2792 * Event handler for the wallpaper picker button that appears after a long press
2793 * on the home screen.
2794 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002795 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002796 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2797 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2798 pickWallpaper.setComponent(getWallpaperPickerComponent());
2799 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002800
2801 if (mLauncherCallbacks != null) {
2802 mLauncherCallbacks.onClickWallpaperPicker(v);
2803 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002804 }
2805
2806 /**
2807 * Event handler for a click on the settings button that appears after a long press
2808 * on the home screen.
2809 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002810 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002811 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002812 if (mLauncherCallbacks != null) {
2813 mLauncherCallbacks.onClickSettingsButton(v);
2814 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002815 }
2816
Michael Jurka5130e402011-10-13 04:55:35 -07002817 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002818 // Provide the same haptic feedback that the system offers for virtual keys.
2819 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002820 }
2821
Adam Cohen61f560d2013-09-30 15:58:20 -07002822 public void performHapticFeedbackOnTouchDown(View v) {
2823 // Provide the same haptic feedback that the system offers for virtual keys.
2824 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2825 }
2826
2827 public View.OnTouchListener getHapticFeedbackTouchListener() {
2828 if (mHapticFeedbackTouchListener == null) {
2829 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2830 @Override
2831 public boolean onTouch(View v, MotionEvent event) {
2832 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2833 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2834 }
2835 return false;
2836 }
2837 };
2838 }
2839 return mHapticFeedbackTouchListener;
2840 }
2841
Adam Cohen9211d422014-10-07 18:14:53 -07002842 public void onDragStarted(View view) {
2843 if (isOnCustomContent()) {
2844 // Custom content screen doesn't participate in drag and drop. If on custom
2845 // content screen, move to default.
2846 moveWorkspaceToDefaultScreen();
2847 }
2848
2849 if (mLauncherCallbacks != null) {
2850 mLauncherCallbacks.onDragStarted(view);
2851 }
2852 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002853
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002854 /**
2855 * Called when the user stops interacting with the launcher.
2856 * This implies that the user is now on the homescreen and is not doing housekeeping.
2857 */
Adam Cohen9211d422014-10-07 18:14:53 -07002858 protected void onInteractionEnd() {
2859 if (mLauncherCallbacks != null) {
2860 mLauncherCallbacks.onInteractionEnd();
2861 }
2862 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002863
2864 /**
2865 * Called when the user starts interacting with the launcher.
2866 * The possible interactions are:
2867 * - open all apps
2868 * - reorder an app shortcut, or a widget
2869 * - open the overview mode.
2870 * This is a good time to stop doing things that only make sense
2871 * when the user is on the homescreen and not doing housekeeping.
2872 */
Adam Cohen9211d422014-10-07 18:14:53 -07002873 protected void onInteractionBegin() {
2874 if (mLauncherCallbacks != null) {
2875 mLauncherCallbacks.onInteractionBegin();
2876 }
2877 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002878
Kenny Guyf07af7b2014-07-31 11:39:16 +01002879 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002880 try {
2881 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002882 launcherApps.showAppDetailsForProfile(componentName, user);
2883 } catch (SecurityException e) {
2884 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2885 Log.e(TAG, "Launcher does not have permission to launch settings");
2886 } catch (ActivityNotFoundException e) {
2887 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2888 Log.e(TAG, "Unable to launch settings");
2889 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002890 }
2891
Michael Jurka1e2f4652013-07-08 18:03:46 -07002892 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002893 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2894 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002895 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002896 // System applications cannot be installed. For now, show a toast explaining that.
2897 // We may give them the option of disabling apps this way.
2898 int messageId = R.string.uninstall_system_app_text;
2899 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002900 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002901 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002902 String packageName = componentName.getPackageName();
2903 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002904 Intent intent = new Intent(
2905 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002906 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2907 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002908 if (user != null) {
2909 user.addToIntent(intent, Intent.EXTRA_USER);
2910 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002911 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002912 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002913 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002914 }
2915
Michael Jurka86a720e2012-05-09 11:23:23 -07002916 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002917 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002918 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002919 // Only launch using the new animation if the shortcut has not opted out (this is a
2920 // private contract between launcher and may be ignored in the future).
2921 boolean useLaunchAnimation = (v != null) &&
2922 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002923 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2924 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002925
Kenny Guy1317e2d2014-05-08 18:52:50 +01002926 UserHandleCompat user = null;
2927 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2928 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2929 user = userManager.getUserForSerialNumber(serialNumber);
2930 }
2931
2932 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002933 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002934 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002935 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2936 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002937 optsBundle = opts.toBundle();
2938 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002939
2940 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2941 // Could be launching some bookkeeping activity
2942 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002943 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002944 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002945 launcherApps.startActivityForProfile(intent.getComponent(), user,
2946 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002947 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002948 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 } catch (SecurityException e) {
2950 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002951 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002952 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002953 "or use the exported attribute for this activity. "
2954 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002955 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002956 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002957 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002958
Michael Jurka86a720e2012-05-09 11:23:23 -07002959 boolean startActivitySafely(View v, Intent intent, Object tag) {
2960 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002961 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2962 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2963 return false;
2964 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002965 try {
2966 success = startActivity(v, intent, tag);
2967 } catch (ActivityNotFoundException e) {
2968 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2969 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2970 }
2971 return success;
2972 }
2973
Adam Cohen268c4752012-06-06 17:47:33 -07002974 /**
2975 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2976 * in the DragLayer in the exact absolute location of the original FolderIcon.
2977 */
2978 private void copyFolderIconToImage(FolderIcon fi) {
2979 final int width = fi.getMeasuredWidth();
2980 final int height = fi.getMeasuredHeight();
2981
2982 // Lazy load ImageView, Bitmap and Canvas
2983 if (mFolderIconImageView == null) {
2984 mFolderIconImageView = new ImageView(this);
2985 }
2986 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2987 mFolderIconBitmap.getHeight() != height) {
2988 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2989 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2990 }
2991
2992 DragLayer.LayoutParams lp;
2993 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2994 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2995 } else {
2996 lp = new DragLayer.LayoutParams(width, height);
2997 }
2998
Adam Cohen307fe232012-08-16 17:55:58 -07002999 // The layout from which the folder is being opened may be scaled, adjust the starting
3000 // view size by this scale factor.
3001 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003002 lp.customPosition = true;
3003 lp.x = mRectForFolderAnimation.left;
3004 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003005 lp.width = (int) (scale * width);
3006 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003007
3008 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3009 fi.draw(mFolderIconCanvas);
3010 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003011 if (fi.getFolder() != null) {
3012 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3013 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003014 }
Adam Cohen268c4752012-06-06 17:47:33 -07003015 // Just in case this image view is still in the drag layer from a previous animation,
3016 // we remove it and re-add it.
3017 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3018 mDragLayer.removeView(mFolderIconImageView);
3019 }
3020 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003021 if (fi.getFolder() != null) {
3022 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003023 }
Adam Cohen268c4752012-06-06 17:47:33 -07003024 }
3025
Adam Cohen2801caf2011-05-13 20:57:39 -07003026 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003027 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003028 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3029 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3030 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3031
Adam Cohenc51934b2011-07-26 21:07:43 -07003032 FolderInfo info = (FolderInfo) fi.getTag();
3033 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3034 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003035 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3036 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003037 }
3038
Adam Cohen268c4752012-06-06 17:47:33 -07003039 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3040 copyFolderIconToImage(fi);
3041 fi.setVisibility(View.INVISIBLE);
3042
Michael Jurka2ecf9952012-06-18 12:52:28 -07003043 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003044 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003045 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003046 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3047 }
3048 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003049 oa.start();
3050 }
3051
Adam Cohen268c4752012-06-06 17:47:33 -07003052 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003053 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003054 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3055 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3056 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3057
Adam Cohen268c4752012-06-06 17:47:33 -07003058 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003059
Adam Cohen268c4752012-06-06 17:47:33 -07003060 // We remove and re-draw the FolderIcon in-case it has changed
3061 mDragLayer.removeView(mFolderIconImageView);
3062 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003063 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003064 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003065 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003066 oa.addListener(new AnimatorListenerAdapter() {
3067 @Override
3068 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003069 if (cl != null) {
3070 cl.clearFolderLeaveBehind();
3071 // Remove the ImageView copy of the FolderIcon and make the original visible.
3072 mDragLayer.removeView(mFolderIconImageView);
3073 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003074 }
3075 }
3076 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003077 oa.start();
3078 }
3079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003080 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003081 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 * is animated relative to the specified View. If the View is null, no animation
3083 * is played.
3084 *
3085 * @param folderInfo The FolderInfo describing the folder to open.
3086 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003087 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003088 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003089 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003090
Adam Cohena9cf38f2011-05-02 15:36:58 -07003091 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003092
Adam Cohen4554ee12011-08-03 16:13:21 -07003093 // Just verify that the folder hasn't already been added to the DragLayer.
3094 // There was a one-off crash where the folder had a parent already.
3095 if (folder.getParent() == null) {
3096 mDragLayer.addView(folder);
3097 mDragController.addDropTarget((DropTarget) folder);
3098 } else {
3099 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3100 folder.getParent() + ").");
3101 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003102 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003103 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003104
3105 // Notify the accessibility manager that this folder "window" has appeared and occluded
3106 // the workspace items
3107 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3108 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003109 }
3110
3111 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003112 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003113 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003114 if (folder.isEditingName()) {
3115 folder.dismissEditingName();
3116 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003117 closeFolder(folder);
3118 }
3119 }
3120
3121 void closeFolder(Folder folder) {
3122 folder.getInfo().opened = false;
3123
3124 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3125 if (parent != null) {
3126 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3127 shrinkAndFadeInFolderIcon(fi);
3128 }
3129 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003130
3131 // Notify the accessibility manager that this folder "window" has disappeard and no
3132 // longer occludeds the workspace items
3133 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134 }
3135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003137 if (!isDraggingEnabled()) return false;
3138 if (isWorkspaceLocked()) return false;
3139 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003140
Adam Cohen1697b792013-09-17 19:08:21 -07003141 if (v instanceof Workspace) {
3142 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003143 if (mWorkspace.enterOverviewMode()) {
3144 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3145 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3146 return true;
3147 } else {
3148 return false;
3149 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003150 } else {
3151 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003152 }
Adam Cohen1697b792013-09-17 19:08:21 -07003153 }
3154
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003155 CellLayout.CellInfo longClickCellInfo = null;
3156 View itemUnderLongClick = null;
3157 if (v.getTag() instanceof ItemInfo) {
3158 ItemInfo info = (ItemInfo) v.getTag();
3159 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3160 itemUnderLongClick = longClickCellInfo.cell;
3161 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003162 }
3163
Winson Chung3d503fb2011-07-13 17:25:49 -07003164 // The hotseat touch handling does not go through Workspace, and we always allow long press
3165 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003166 final boolean inHotseat = isHotseatLayout(v);
3167 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003168 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003169 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003170 // User long pressed on empty space
3171 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3172 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003173 if (mWorkspace.isInOverviewMode()) {
3174 mWorkspace.startReordering(v);
3175 } else {
3176 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003177 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003179 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3180 mHotseat.getOrderInHotseat(
3181 longClickCellInfo.cellX,
3182 longClickCellInfo.cellY));
3183 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003184 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003185 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003186 }
3187 }
3188 }
3189 return true;
3190 }
3191
Winson Chung3d503fb2011-07-13 17:25:49 -07003192 boolean isHotseatLayout(View layout) {
3193 return mHotseat != null && layout != null &&
3194 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3195 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003196
Winson Chung3d503fb2011-07-13 17:25:49 -07003197 /**
3198 * Returns the CellLayout of the specified container at the specified screen.
3199 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003200 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003201 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3202 if (mHotseat != null) {
3203 return mHotseat.getLayout();
3204 } else {
3205 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003206 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003207 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003208 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003209 }
3210 }
3211
Daniel Sandler843e8602010-06-07 14:59:01 -04003212 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003213 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003214 }
3215
Craig Mautner360310b2012-10-26 15:13:08 -07003216 private void setWorkspaceBackground(boolean workspace) {
3217 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003218 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003219 }
3220
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003221 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003222 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3223 int curflags = getWindow().getAttributes().flags
3224 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3225 if (wpflags != curflags) {
3226 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3227 }
Craig Mautner360310b2012-10-26 15:13:08 -07003228 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003229 }
3230
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003231 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3232 if (v instanceof LauncherTransitionable) {
3233 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3234 }
3235 }
3236
Michael Jurkabed61d22012-02-14 22:51:29 -08003237 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3238 if (v instanceof LauncherTransitionable) {
3239 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3240 }
Winson Chung70442722012-02-10 15:43:22 -08003241
3242 // Update the workspace transition step as well
3243 dispatchOnLauncherTransitionStep(v, 0f);
3244 }
3245
3246 private void dispatchOnLauncherTransitionStep(View v, float t) {
3247 if (v instanceof LauncherTransitionable) {
3248 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3249 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003250 }
3251
3252 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3253 if (v instanceof LauncherTransitionable) {
3254 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3255 }
Winson Chung70442722012-02-10 15:43:22 -08003256
3257 // Update the workspace transition step as well
3258 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003259 }
3260
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003261 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003262 * Things to test when changing the following seven functions.
3263 * - Home from workspace
3264 * - from center screen
3265 * - from other screens
3266 * - Home from all apps
3267 * - from center screen
3268 * - from other screens
3269 * - Back from all apps
3270 * - from center screen
3271 * - from other screens
3272 * - Launch app from workspace and quit
3273 * - with back
3274 * - with home
3275 * - Launch app from all apps and quit
3276 * - with back
3277 * - with home
3278 * - Go to a screen that's not the default, then all
3279 * apps, and launch and app, and go back
3280 * - with back
3281 * -with home
3282 * - On workspace, long press power and go back
3283 * - with back
3284 * - with home
3285 * - On all apps, long press power and go back
3286 * - with back
3287 * - with home
3288 * - On workspace, power off
3289 * - On all apps, power off
3290 * - Launch an app and turn off the screen while in that app
3291 * - Go back with home key
3292 * - Go back with back key TODO: make this not go to workspace
3293 * - From all apps
3294 * - From workspace
3295 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3296 * - From all apps
3297 * - From the center workspace
3298 * - From another workspace
3299 */
3300
3301 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003302 * Zoom the camera out from the workspace to reveal 'toView'.
3303 * Assumes that the view to show is anchored at either the very top or very bottom
3304 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003305 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003306 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003307 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3308 showAppsCustomizeHelper(animated, springLoaded, contentType);
3309 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003310
Winson Chungc58497e2013-09-03 17:48:37 -07003311 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3312 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003313 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003314 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003315 mStateAnimation.cancel();
3316 mStateAnimation = null;
3317 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003318
Kenny Guyd794a3f2014-09-16 15:17:58 +01003319 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003320
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003321 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003322
Winson Chungf0ea4d32011-06-06 14:27:16 -07003323 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3324 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003325 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003326 final int itemsAlphaStagger =
3327 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003328
Winson Chungf0ea4d32011-06-06 14:27:16 -07003329 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003330 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003331 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003332
Adam Cohen2854d252014-08-27 16:04:07 -07003333 final ArrayList<View> layerViews = new ArrayList<View>();
3334
Adam Cohen6c5891a2014-07-09 23:53:15 -07003335 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3336 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003337 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003338 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003339 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003340 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3341 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003342 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3343 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003344
Adam Cohena38dc902014-09-07 17:48:55 +02003345 // If for some reason our views aren't initialized, don't animate
3346 boolean initialized = getAllAppsButton() != null;
3347
3348 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003349 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003350 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3351 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003352
Adam Cohen9bfdb762014-07-21 17:44:06 -07003353 final View page = content.getPageAt(content.getCurrentPage());
3354 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003355
Adam Cohen63f1ec02014-08-12 09:23:13 -07003356 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3357 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003358 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3359 } else {
3360 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3361 }
3362
Adam Cohen9bfdb762014-07-21 17:44:06 -07003363 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003364 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003365 revealView.setVisibility(View.VISIBLE);
3366 // We need to hide this view as the animation start will be posted.
3367 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003368
Adam Cohen9bfdb762014-07-21 17:44:06 -07003369 int width = revealView.getMeasuredWidth();
3370 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003371 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003372
Adam Cohen63f1ec02014-08-12 09:23:13 -07003373 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003374 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003375
Adam Cohen63f1ec02014-08-12 09:23:13 -07003376 // Get the y delta between the center of the page and the center of the all apps button
3377 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3378 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003379
Adam Cohen2854d252014-08-27 16:04:07 -07003380 float alpha = 0;
3381 float xDrift = 0;
3382 float yDrift = 0;
3383 if (material) {
3384 alpha = isWidgetTray ? 0.3f : 1f;
3385 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3386 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3387 } else {
3388 yDrift = 2 * height / 3;
3389 xDrift = 0;
3390 }
3391 final float initAlpha = alpha;
3392
Adam Cohen9bfdb762014-07-21 17:44:06 -07003393 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003394 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003395 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003396 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003397 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003398 PropertyValuesHolder panelDriftX =
3399 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003400
Adam Cohen2854d252014-08-27 16:04:07 -07003401 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3402 panelAlpha, panelDriftY, panelDriftX);
3403
Adam Cohen9bfdb762014-07-21 17:44:06 -07003404 panelAlphaAndDrift.setDuration(revealDuration);
3405 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003406
Adam Cohen9bfdb762014-07-21 17:44:06 -07003407 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003408
Adam Cohen4e243a22014-08-10 18:30:55 -07003409 if (page != null) {
3410 page.setVisibility(View.VISIBLE);
3411 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003412 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003413
Adam Cohen2854d252014-08-27 16:04:07 -07003414 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3415 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003416 pageDrift.setDuration(revealDuration);
3417 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003418 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003419 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003420
Adam Cohen63f1ec02014-08-12 09:23:13 -07003421 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003422 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003423 itemsAlpha.setDuration(revealDuration);
3424 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3425 itemsAlpha.setStartDelay(itemsAlphaStagger);
3426 mStateAnimation.play(itemsAlpha);
3427 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003428
Adam Cohen4e243a22014-08-10 18:30:55 -07003429 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3430 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003431 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003432 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003433 indicatorsAlpha.setDuration(revealDuration);
3434 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003435
Adam Cohen9bfdb762014-07-21 17:44:06 -07003436 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003437 final View allApps = getAllAppsButton();
3438 int allAppsButtonSize = LauncherAppState.getInstance().
3439 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3440 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003441 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003442 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003443 reveal.setDuration(revealDuration);
3444 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003445
3446 reveal.addListener(new AnimatorListenerAdapter() {
3447 public void onAnimationStart(Animator animation) {
3448 if (!isWidgetTray) {
3449 allApps.setVisibility(View.INVISIBLE);
3450 }
3451 }
3452 public void onAnimationEnd(Animator animation) {
3453 if (!isWidgetTray) {
3454 allApps.setVisibility(View.VISIBLE);
3455 }
3456 }
3457 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003458 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003459 }
Michael Jurka1899a362011-11-03 13:50:45 -07003460
Adam Cohen9bfdb762014-07-21 17:44:06 -07003461 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3462 @Override
3463 public void onAnimationEnd(Animator animation) {
3464 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3465 dispatchOnLauncherTransitionEnd(toView, animated, false);
3466
3467 revealView.setVisibility(View.INVISIBLE);
3468 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003469 if (page != null) {
3470 page.setLayerType(View.LAYER_TYPE_NONE, null);
3471 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003472 content.setPageBackgroundsVisible(true);
3473
3474 // Hide the search bar
3475 if (mSearchDropTargetBar != null) {
3476 mSearchDropTargetBar.hideSearchBar(false);
3477 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003478
3479 // This can hold unnecessary references to views.
3480 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003481 }
3482
Adam Cohen9bfdb762014-07-21 17:44:06 -07003483 });
3484
Adam Cohen6c5891a2014-07-09 23:53:15 -07003485 if (workspaceAnim != null) {
3486 mStateAnimation.play(workspaceAnim);
3487 }
Adam Cohen2854d252014-08-27 16:04:07 -07003488
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003489 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3490 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003491 final AnimatorSet stateAnimation = mStateAnimation;
3492 final Runnable startAnimRunnable = new Runnable() {
3493 public void run() {
3494 // Check that mStateAnimation hasn't changed while
3495 // we waited for a layout/draw pass
3496 if (mStateAnimation != stateAnimation)
3497 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003498 dispatchOnLauncherTransitionStart(fromView, animated, false);
3499 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003500
3501 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003502 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003503 for (int i = 0; i < layerViews.size(); i++) {
3504 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003505 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003506 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003507 }
Adam Cohen2854d252014-08-27 16:04:07 -07003508 }
3509 }
3510 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003511 }
3512 };
Adam Cohen2854d252014-08-27 16:04:07 -07003513 toView.bringToFront();
3514 toView.setVisibility(View.VISIBLE);
3515 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003516 } else {
3517 toView.setTranslationX(0.0f);
3518 toView.setTranslationY(0.0f);
3519 toView.setScaleX(1.0f);
3520 toView.setScaleY(1.0f);
3521 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003522 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003523
Daniel Sandlere4f98912013-06-25 15:13:26 -04003524 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003525 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003526 if (mSearchDropTargetBar != null) {
3527 mSearchDropTargetBar.hideSearchBar(false);
3528 }
Michael Jurkaabded662011-03-04 12:06:57 -08003529 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003530 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003531 dispatchOnLauncherTransitionStart(fromView, animated, false);
3532 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003533 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003534 dispatchOnLauncherTransitionStart(toView, animated, false);
3535 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003536 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003537 }
3538
3539 /**
3540 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003541 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003542 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003543 */
Adam Cohened307df2013-10-02 09:37:31 -07003544 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003545 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003546
Michael Jurkab3e22d92011-10-31 15:58:33 -07003547 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003548 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003549 mStateAnimation.cancel();
3550 mStateAnimation = null;
3551 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003552
Kenny Guyd794a3f2014-09-16 15:17:58 +01003553 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003554 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003555
Winson Chungf0ea4d32011-06-06 14:27:16 -07003556 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003557 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003558 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003559 final int itemsAlphaStagger =
3560 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003561
Winson Chungf0ea4d32011-06-06 14:27:16 -07003562 final float scaleFactor = (float)
3563 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3564 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003565 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003566 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003567 final ArrayList<View> layerViews = new ArrayList<View>();
3568
Adam Cohened307df2013-10-02 09:37:31 -07003569 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003570 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003571 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003572 } else if (toState == Workspace.State.SPRING_LOADED ||
3573 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003574 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003575 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003576 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003577
Adam Cohena38dc902014-09-07 17:48:55 +02003578 // If for some reason our views aren't initialized, don't animate
3579 boolean initialized = getAllAppsButton() != null;
3580
3581 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003582 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003583 if (workspaceAnim != null) {
3584 mStateAnimation.play(workspaceAnim);
3585 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003586
Adam Cohen9bfdb762014-07-21 17:44:06 -07003587 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3588 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003589
Adam Cohen9bfdb762014-07-21 17:44:06 -07003590 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003591
3592 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3593 int count = content.getChildCount();
3594 for (int i = 0; i < count; i++) {
3595 View child = content.getChildAt(i);
3596 if (child != page) {
3597 child.setVisibility(View.INVISIBLE);
3598 }
3599 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003600 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003601
Adam Cohen2854d252014-08-27 16:04:07 -07003602 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3603 // don't perform all these no-op animations. In particularly, this was causing
3604 // the all-apps button to pop in and out.
3605 if (fromView.getVisibility() == View.VISIBLE) {
3606 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3607 final boolean isWidgetTray =
3608 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003609
Adam Cohen2854d252014-08-27 16:04:07 -07003610 if (isWidgetTray) {
3611 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3612 } else {
3613 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003614 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003615
Adam Cohen2854d252014-08-27 16:04:07 -07003616 int width = revealView.getMeasuredWidth();
3617 int height = revealView.getMeasuredHeight();
3618 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3619
3620 // Hide the real page background, and swap in the fake one
3621 revealView.setVisibility(View.VISIBLE);
3622 content.setPageBackgroundsVisible(false);
3623
3624 final View allAppsButton = getAllAppsButton();
3625 revealView.setTranslationY(0);
3626 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3627 allAppsButton, null);
3628
3629 float xDrift = 0;
3630 float yDrift = 0;
3631 if (material) {
3632 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3633 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3634 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003635 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003636 xDrift = 0;
3637 }
3638
3639 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3640 TimeInterpolator decelerateInterpolator = material ?
3641 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003642 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003643
3644 // The vertical motion of the apps panel should be delayed by one frame
3645 // from the conceal animation in order to give the right feel. We correpsondingly
3646 // shorten the duration so that the slide and conceal end at the same time.
3647 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3648 0, yDrift);
3649 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3650 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3651 panelDriftY.setInterpolator(decelerateInterpolator);
3652 mStateAnimation.play(panelDriftY);
3653
3654 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3655 0, xDrift);
3656 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3657 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3658 panelDriftX.setInterpolator(decelerateInterpolator);
3659 mStateAnimation.play(panelDriftX);
3660
3661 if (isWidgetTray || !material) {
3662 float finalAlpha = material ? 0.4f : 0f;
3663 revealView.setAlpha(1f);
3664 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3665 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003666 panelAlpha.setDuration(material ? revealDuration : 150);
3667 panelAlpha.setInterpolator(decelerateInterpolator);
3668 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003669 mStateAnimation.play(panelAlpha);
3670 }
3671
3672 if (page != null) {
3673 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3674
3675 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3676 0, yDrift);
3677 page.setTranslationY(0);
3678 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3679 pageDrift.setInterpolator(decelerateInterpolator);
3680 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3681 mStateAnimation.play(pageDrift);
3682
3683 page.setAlpha(1f);
3684 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3685 itemsAlpha.setDuration(100);
3686 itemsAlpha.setInterpolator(decelerateInterpolator);
3687 mStateAnimation.play(itemsAlpha);
3688 }
3689
3690 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3691 pageIndicators.setAlpha(1f);
3692 ObjectAnimator indicatorsAlpha =
3693 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3694 indicatorsAlpha.setDuration(revealDuration);
3695 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3696 mStateAnimation.play(indicatorsAlpha);
3697
3698 width = revealView.getMeasuredWidth();
3699
3700 if (material) {
3701 if (!isWidgetTray) {
3702 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003703 }
Adam Cohen2854d252014-08-27 16:04:07 -07003704 int allAppsButtonSize = LauncherAppState.getInstance().
3705 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3706 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3707 Animator reveal =
3708 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3709 height / 2, revealRadius, finalRadius);
3710 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3711 reveal.setDuration(revealDuration);
3712 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003713
Adam Cohen2854d252014-08-27 16:04:07 -07003714 reveal.addListener(new AnimatorListenerAdapter() {
3715 public void onAnimationEnd(Animator animation) {
3716 revealView.setVisibility(View.INVISIBLE);
3717 if (!isWidgetTray) {
3718 allAppsButton.setVisibility(View.VISIBLE);
3719 }
3720 }
3721 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003722
Adam Cohen2854d252014-08-27 16:04:07 -07003723 mStateAnimation.play(reveal);
3724 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003725
Adam Cohen2854d252014-08-27 16:04:07 -07003726 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3727 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3728 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003729 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003730
3731 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003732 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003733 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003734 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003735 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3736 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003737 if (onCompleteRunnable != null) {
3738 onCompleteRunnable.run();
3739 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003740
3741 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003742 if (page != null) {
3743 page.setLayerType(View.LAYER_TYPE_NONE, null);
3744 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003745 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003746 // Unhide side pages
3747 int count = content.getChildCount();
3748 for (int i = 0; i < count; i++) {
3749 View child = content.getChildAt(i);
3750 child.setVisibility(View.VISIBLE);
3751 }
3752
3753 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003754 if (page != null) {
3755 page.setTranslationX(0);
3756 page.setTranslationY(0);
3757 page.setAlpha(1);
3758 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003759 content.setCurrentPage(content.getNextPage());
3760
Chet Haasebc2f0822012-10-26 17:59:53 -07003761 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003762
3763 // This can hold unnecessary references to views.
3764 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003765 }
3766 });
3767
Adam Cohen2854d252014-08-27 16:04:07 -07003768 final AnimatorSet stateAnimation = mStateAnimation;
3769 final Runnable startAnimRunnable = new Runnable() {
3770 public void run() {
3771 // Check that mStateAnimation hasn't changed while
3772 // we waited for a layout/draw pass
3773 if (mStateAnimation != stateAnimation)
3774 return;
3775 dispatchOnLauncherTransitionStart(fromView, animated, false);
3776 dispatchOnLauncherTransitionStart(toView, animated, false);
3777
Kenny Guyd794a3f2014-09-16 15:17:58 +01003778 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003779 for (int i = 0; i < layerViews.size(); i++) {
3780 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003781 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003782 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003783 }
Adam Cohen2854d252014-08-27 16:04:07 -07003784 }
3785 }
3786 mStateAnimation.start();
3787 }
3788 };
3789 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003790 } else {
3791 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003792 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003793 dispatchOnLauncherTransitionStart(fromView, animated, true);
3794 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003795 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003796 dispatchOnLauncherTransitionStart(toView, animated, true);
3797 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003798 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003799 }
3800
Michael Jurkae326f182011-11-21 14:05:46 -08003801 @Override
3802 public void onTrimMemory(int level) {
3803 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003804 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3805 // The widget preview db can result in holding onto over
3806 // 3MB of memory for caching which isn't necessary.
3807 SQLiteDatabase.releaseMemory();
3808
3809 // We reset the apps customize tray in order to
3810 // to free all the memory associated with widget previews
3811 if (mAppsCustomizeTabHost != null) {
3812 mAppsCustomizeTabHost.reset();
3813 }
Michael Jurkae326f182011-11-21 14:05:46 -08003814 }
3815 }
3816
Adam Cohened307df2013-10-02 09:37:31 -07003817 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003818 showWorkspace(animated, null);
3819 }
3820
Adam Cohened307df2013-10-02 09:37:31 -07003821 protected void showWorkspace() {
3822 showWorkspace(true);
3823 }
3824
Adam Cohened66b2b2012-01-23 17:28:51 -08003825 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003826 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003827 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003828 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003829 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003830
Winson Chungc7d2b602012-05-16 17:02:20 -07003831 // Show the search bar (only animate if we were showing the drop target bar in spring
3832 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003833 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003834 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003835 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003836
Michael Jurkab3e22d92011-10-31 15:58:33 -07003837 // Set focus to the AppsCustomize button
3838 if (mAllAppsButton != null) {
3839 mAllAppsButton.requestFocus();
3840 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003841 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003842
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003843 // Change the state *after* we've called all the transition code
3844 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003845
Winson Chung5fb63472011-02-02 17:03:37 -08003846 // Resume the auto-advance of widgets
3847 mUserPresent = true;
3848 updateRunning();
3849
alanv1d4fde62012-10-17 13:15:47 -07003850 // Send an accessibility event to announce the context change
3851 getWindow().getDecorView()
3852 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003853
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003854 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003855 }
3856
Adam Cohened307df2013-10-02 09:37:31 -07003857 void showOverviewMode(boolean animated) {
3858 mWorkspace.setVisibility(View.VISIBLE);
3859 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3860 mState = State.WORKSPACE;
3861 onWorkspaceShown(animated);
3862 }
3863
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003864 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003865 }
3866
Winson Chung82963d52013-10-09 11:20:57 -07003867 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3868 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003869 if (mState != State.WORKSPACE) return;
3870
Winson Chung82963d52013-10-09 11:20:57 -07003871 if (resetPageToZero) {
3872 mAppsCustomizeTabHost.reset();
3873 }
Winson Chungc58497e2013-09-03 17:48:37 -07003874 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003875 mAppsCustomizeTabHost.post(new Runnable() {
3876 @Override
3877 public void run() {
3878 // We post this in-case the all apps view isn't yet constructed.
3879 mAppsCustomizeTabHost.requestFocus();
3880 }
3881 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003882
Michael Jurkab3e22d92011-10-31 15:58:33 -07003883 // Change the state *after* we've called all the transition code
3884 mState = State.APPS_CUSTOMIZE;
3885
3886 // Pause the auto-advance of widgets until we are out of AllApps
3887 mUserPresent = false;
3888 updateRunning();
3889 closeFolder();
3890
3891 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003892 getWindow().getDecorView()
3893 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003894 }
3895
Adam Cohen7777d962011-08-18 18:58:38 -07003896 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003897 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003898 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003899 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003900 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003901 }
Adam Cohen7777d962011-08-18 18:58:38 -07003902
Adam Cohenad4e15c2013-10-17 16:21:35 -07003903 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003904 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003905 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3906
Winson Chunge7a03942011-08-05 15:05:12 -07003907 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003908 @Override
3909 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003910 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003911 // Before we show workspace, hide all apps again because
3912 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3913 // clean up our state transition functions
3914 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003915 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003916 } else {
3917 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003918 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003919 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003920 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003921 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003922
Michael Jurkad3ef3062010-11-23 16:23:58 -08003923 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003924 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003925 final boolean animated = true;
3926 final boolean springLoaded = true;
3927 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003928 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003929 }
3930 // Otherwise, we are not in spring loaded mode, so don't do anything.
3931 }
3932
Michael Jurkab3e22d92011-10-31 15:58:33 -07003933 void lockAllApps() {
3934 // TODO
3935 }
3936
3937 void unlockAllApps() {
3938 // TODO
3939 }
3940
Sunny Goyal594d76d2014-11-06 10:12:54 -08003941 protected void disableVoiceButtonProxy(boolean disable) {
3942 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003943 }
3944
Cristina Stancu476493b2013-08-07 17:20:14 +01003945 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003946 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3947 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003948 }
3949
Adam Cohen24ce0b32014-01-14 16:18:14 -08003950 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003951 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3952 if (searchProvider == null) {
3953 return null;
3954 }
3955
3956 Bundle opts = new Bundle();
3957 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
3958 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
3959
3960 SharedPreferences sp = getSharedPreferences(
3961 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3962 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003963 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003964 if (!searchProvider.provider.flattenToString().equals(
3965 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003966 || (widgetInfo == null)
3967 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003968 // A valid widget is not already bound.
3969 if (widgetId > -1) {
3970 mAppWidgetHost.deleteAppWidgetId(widgetId);
3971 widgetId = -1;
3972 }
3973
3974 // Try to bind a new widget
3975 widgetId = mAppWidgetHost.allocateAppWidgetId();
3976
3977 if (!AppWidgetManagerCompat.getInstance(this)
3978 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3979 mAppWidgetHost.deleteAppWidgetId(widgetId);
3980 widgetId = -1;
3981 }
3982
3983 sp.edit()
3984 .putInt(QSB_WIDGET_ID, widgetId)
3985 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3986 .commit();
3987 }
3988
3989 if (widgetId != -1) {
3990 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3991 mQsb.updateAppWidgetOptions(opts);
3992 mQsb.setPadding(0, 0, 0, 0);
3993 mSearchDropTargetBar.addView(mQsb);
3994 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003995 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003996 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003997 }
3998
Michael Jurkad7c28052012-04-27 15:43:36 -07003999 @Override
4000 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004001 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004002 final List<CharSequence> text = event.getText();
4003 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004004 // Populate event with a fake title based on the current state.
4005 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004006 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004007 } else {
4008 text.add(getString(R.string.all_apps_home_button_label));
4009 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004010 return result;
4011 }
4012
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004013 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004014 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004015 */
4016 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4017 @Override
4018 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004019 closeSystemDialogs();
4020 }
4021 }
4022
4023 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004024 * Receives notifications whenever the appwidgets are reset.
4025 */
4026 private class AppWidgetResetObserver extends ContentObserver {
4027 public AppWidgetResetObserver() {
4028 super(new Handler());
4029 }
4030
4031 @Override
4032 public void onChange(boolean selfChange) {
4033 onAppWidgetReset();
4034 }
4035 }
4036
4037 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004038 * If the activity is currently paused, signal that we need to run the passed Runnable
4039 * in onResume.
4040 *
4041 * This needs to be called from incoming places where resources might have been loaded
4042 * while we are paused. That is becaues the Configuration might be wrong
4043 * when we're not running, and if it comes back to what it was when we
4044 * were paused, we are not restarted.
4045 *
4046 * Implementation of the method from LauncherModel.Callbacks.
4047 *
4048 * @return true if we are currently paused. The caller might be able to
4049 * skip some work in that case since we will come back again.
4050 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004051 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004052 if (mPaused) {
4053 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004054 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004055 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004056 }
4057 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004058 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004059 return true;
4060 } else {
4061 return false;
4062 }
4063 }
4064
Michael Jurkac402cd92013-05-20 15:49:32 +02004065 private boolean waitUntilResume(Runnable run) {
4066 return waitUntilResume(run, false);
4067 }
4068
Michael Jurka1e2f4652013-07-08 18:03:46 -07004069 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004070 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004071 }
4072
Michael Jurka7607c2f2013-04-03 14:33:19 -07004073 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004074 * If the activity is currently paused, signal that we need to re-run the loader
4075 * in onResume.
4076 *
4077 * This needs to be called from incoming places where resources might have been loaded
4078 * while we are paused. That is becaues the Configuration might be wrong
4079 * when we're not running, and if it comes back to what it was when we
4080 * were paused, we are not restarted.
4081 *
4082 * Implementation of the method from LauncherModel.Callbacks.
4083 *
4084 * @return true if we are currently paused. The caller might be able to
4085 * skip some work in that case since we will come back again.
4086 */
4087 public boolean setLoadOnResume() {
4088 if (mPaused) {
4089 Log.i(TAG, "setLoadOnResume");
4090 mOnResumeNeedsLoad = true;
4091 return true;
4092 } else {
4093 return false;
4094 }
4095 }
4096
4097 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004098 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004099 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004100 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004101 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004102 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004103 } else {
4104 return SCREEN_COUNT / 2;
4105 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004106 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004107
Joe Onorato9c1289c2009-08-17 11:03:03 -04004108 /**
4109 * Refreshes the shortcuts shown on the workspace.
4110 *
4111 * Implementation of the method from LauncherModel.Callbacks.
4112 */
4113 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004114 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004115
Michael Jurka7607c2f2013-04-03 14:33:19 -07004116 // If we're starting binding all over again, clear any bind calls we'd postponed in
4117 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4118 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004119 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004120
Winson Chungd64d1762013-08-20 14:37:16 -07004121 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004122 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004123 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004124
Michael Jurka05bf6442011-11-30 20:28:53 -08004125 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004126 if (mHotseat != null) {
4127 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004128 }
4129 }
4130
Adam Cohendcd297f2013-06-18 13:13:40 -07004131 @Override
4132 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004133 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004134
Adam Cohen5084cba2013-09-03 12:01:16 -07004135 // If there are no screens, we need to have an empty screen
4136 if (orderedScreenIds.size() == 0) {
4137 mWorkspace.addExtraEmptyScreen();
4138 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004139
4140 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004141 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004142 if (hasCustomContentToLeft()) {
4143 mWorkspace.createCustomContentContainer();
4144 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004145 }
Winson Chung64359a52013-07-08 17:17:08 -07004146 }
4147
4148 @Override
4149 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004150 // Log to disk
4151 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4152 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4153 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004154 int count = orderedScreenIds.size();
4155 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004156 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004157 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004158 }
4159
Adam Cohen39a06042013-07-19 14:30:12 -07004160 private boolean shouldShowWeightWatcher() {
4161 String spKey = LauncherAppState.getSharedPreferencesKey();
4162 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004163 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004164
4165 return show;
4166 }
4167
4168 private void toggleShowWeightWatcher() {
4169 String spKey = LauncherAppState.getSharedPreferencesKey();
4170 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4171 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4172
4173 show = !show;
4174
4175 SharedPreferences.Editor editor = sp.edit();
4176 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4177 editor.commit();
4178
4179 if (mWeightWatcher != null) {
4180 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4181 }
4182 }
4183
Winson Chungd64d1762013-08-20 14:37:16 -07004184 public void bindAppsAdded(final ArrayList<Long> newScreens,
4185 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004186 final ArrayList<ItemInfo> addAnimated,
4187 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004188 Runnable r = new Runnable() {
4189 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004190 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004191 }
4192 };
4193 if (waitUntilResume(r)) {
4194 return;
4195 }
4196
Winson Chungd64d1762013-08-20 14:37:16 -07004197 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004198 if (newScreens != null) {
4199 bindAddScreens(newScreens);
4200 }
Winson Chungd64d1762013-08-20 14:37:16 -07004201
4202 // We add the items without animation on non-visible pages, and with
4203 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004204 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004205 bindItems(addNotAnimated, 0,
4206 addNotAnimated.size(), false);
4207 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004208 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004209 bindItems(addAnimated, 0,
4210 addAnimated.size(), true);
4211 }
Winson Chungc58497e2013-09-03 17:48:37 -07004212
Winson Chung87412982013-10-03 18:34:14 -07004213 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004214 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004215
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004216 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004217 addedApps != null && mAppsCustomizeContent != null) {
4218 mAppsCustomizeContent.addApps(addedApps);
4219 }
Winson Chungd64d1762013-08-20 14:37:16 -07004220 }
4221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004222 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004223 * Bind the items start-end from the list.
4224 *
4225 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004226 */
Winson Chung64359a52013-07-08 17:17:08 -07004227 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4228 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004229 Runnable r = new Runnable() {
4230 public void run() {
4231 bindItems(shortcuts, start, end, forceAnimateIcons);
4232 }
4233 };
4234 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004235 return;
4236 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004237
Winson Chungf0c6ae02012-03-21 16:10:31 -07004238 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004239 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4240 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004241 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004242 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004243 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004244 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004245 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004246
4247 // Short circuit if we are loading dock items for a configuration which has no dock
4248 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4249 mHotseat == null) {
4250 continue;
4251 }
4252
Joe Onorato9c1289c2009-08-17 11:03:03 -04004253 switch (item.itemType) {
4254 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4255 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004256 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004257 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004258
Winson Chung64359a52013-07-08 17:17:08 -07004259 /*
4260 * TODO: FIX collision case
4261 */
Winson Chungce376632013-07-11 16:12:41 -07004262 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4263 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4264 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004265 View v = cl.getChildAt(item.cellX, item.cellY);
4266 Object tag = v.getTag();
4267 String desc = "Collision while binding workspace item: " + item
4268 + ". Collides with " + tag;
4269 if (LauncherAppState.isDogfoodBuild()) {
4270 throw (new RuntimeException(desc));
4271 } else {
4272 Log.d(TAG, desc);
4273 }
Winson Chungce376632013-07-11 16:12:41 -07004274 }
Winson Chung64359a52013-07-08 17:17:08 -07004275 }
4276
Adam Cohendcd297f2013-06-18 13:13:40 -07004277 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4278 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004279 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004280 // Animate all the applications up now
4281 shortcut.setAlpha(0f);
4282 shortcut.setScaleX(0f);
4283 shortcut.setScaleY(0f);
4284 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004285 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004286 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004287 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004288 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004289 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004290 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004291 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004292 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4293 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004294 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004295 default:
4296 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004298 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004299
Winson Chung997a9232013-07-24 15:33:46 -07004300 if (animateIcons) {
4301 // Animate to the correct page
4302 if (newShortcutsScreenId > -1) {
4303 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004304 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004305 final Runnable startBounceAnimRunnable = new Runnable() {
4306 public void run() {
4307 anim.playTogether(bounceAnims);
4308 anim.start();
4309 }
4310 };
Winson Chung997a9232013-07-24 15:33:46 -07004311 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004312 // We post the animation slightly delayed to prevent slowdowns
4313 // when we are loading right after we return to launcher.
4314 mWorkspace.postDelayed(new Runnable() {
4315 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004316 if (mWorkspace != null) {
4317 mWorkspace.snapToPage(newScreenIndex);
4318 mWorkspace.postDelayed(startBounceAnimRunnable,
4319 NEW_APPS_ANIMATION_DELAY);
4320 }
Winson Chung94d67682013-09-25 16:29:40 -07004321 }
4322 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004323 } else {
4324 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004325 }
4326 }
Winson Chung64359a52013-07-08 17:17:08 -07004327 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004328 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004329 }
4330
Joe Onorato9c1289c2009-08-17 11:03:03 -04004331 /**
4332 * Implementation of the method from LauncherModel.Callbacks.
4333 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004334 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004335 Runnable r = new Runnable() {
4336 public void run() {
4337 bindFolders(folders);
4338 }
4339 };
4340 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004341 return;
4342 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004343 sFolders.clear();
4344 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004345 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004346
4347 /**
4348 * Add the views for a widget to the workspace.
4349 *
4350 * Implementation of the method from LauncherModel.Callbacks.
4351 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004352 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004353 Runnable r = new Runnable() {
4354 public void run() {
4355 bindAppWidget(item);
4356 }
4357 };
4358 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004359 return;
4360 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004361
Daniel Sandler843e8602010-06-07 14:59:01 -04004362 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4363 if (DEBUG_WIDGETS) {
4364 Log.d(TAG, "bindAppWidget: " + item);
4365 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004366 final Workspace workspace = mWorkspace;
4367
Sunny Goyalff572272014-07-23 13:58:07 -07004368 AppWidgetProviderInfo appWidgetInfo;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004369 if (!mIsSafeModeEnabled
4370 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4371 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004372
4373 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4374 if (appWidgetInfo == null) {
4375 if (DEBUG_WIDGETS) {
4376 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4377 + " belongs to component " + item.providerName
4378 + ", as the povider is null");
4379 }
4380 LauncherModel.deleteItemFromDatabase(this, item);
4381 return;
4382 }
4383 // Note: This assumes that the id remap broadcast is received before this step.
4384 // If that is not the case, the id remap will be ignored and user may see the
4385 // click to setup view.
4386 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4387 pendingInfo.spanX = item.spanX;
4388 pendingInfo.spanY = item.spanY;
4389 pendingInfo.minSpanX = item.minSpanX;
4390 pendingInfo.minSpanY = item.minSpanY;
4391 Bundle options =
4392 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4393
Sunny Goyalff572272014-07-23 13:58:07 -07004394 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004395 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4396 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004397
4398 // TODO consider showing a permission dialog when the widget is clicked.
4399 if (!success) {
4400 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4401 if (DEBUG_WIDGETS) {
4402 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4403 + " belongs to component " + item.providerName
4404 + ", as the launcher is unable to bing a new widget id");
4405 }
4406 LauncherModel.deleteItemFromDatabase(this, item);
4407 return;
4408 }
4409
4410 item.appWidgetId = newWidgetId;
4411
4412 // If the widget has a configure activity, it is still needs to set it up, otherwise
4413 // the widget is ready to go.
4414 item.restoreStatus = (appWidgetInfo.configure == null)
4415 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4416 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4417
4418 LauncherModel.updateItemInDatabase(this, item);
4419 }
4420
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004421 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004422 final int appWidgetId = item.appWidgetId;
4423 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4424 if (DEBUG_WIDGETS) {
4425 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4426 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004427
Sunny Goyal651077b2014-06-30 14:15:31 -07004428 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4429 } else {
4430 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004431 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4432 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004433 view.updateIcon(mIconCache);
4434 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004435 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004436 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004437 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004438
Joe Onorato9c1289c2009-08-17 11:03:03 -04004439 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004440 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004441
Adam Cohendcd297f2013-06-18 13:13:40 -07004442 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004443 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004444 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4445
Joe Onorato9c1289c2009-08-17 11:03:03 -04004446 workspace.requestLayout();
4447
Daniel Sandler843e8602010-06-07 14:59:01 -04004448 if (DEBUG_WIDGETS) {
4449 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4450 + (SystemClock.uptimeMillis()-start) + "ms");
4451 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004452 }
4453
Sunny Goyalff572272014-07-23 13:58:07 -07004454 /**
4455 * Restores a pending widget.
4456 *
4457 * @param appWidgetId The app widget id
4458 * @param cellInfo The position on screen where to create the widget.
4459 */
4460 private void completeRestoreAppWidget(final int appWidgetId) {
4461 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4462 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4463 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4464 return;
4465 }
4466
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004467 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004468 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4469
4470 mWorkspace.reinflateWidgetsIfNecessary();
4471 LauncherModel.updateItemInDatabase(this, info);
4472 }
4473
Adam Cohen1462de32012-07-24 22:34:36 -07004474 public void onPageBoundSynchronously(int page) {
4475 mSynchronouslyBoundPages.add(page);
4476 }
4477
Joe Onorato9c1289c2009-08-17 11:03:03 -04004478 /**
4479 * Callback saying that there aren't any more items to bind.
4480 *
4481 * Implementation of the method from LauncherModel.Callbacks.
4482 */
Adam Cohene25af792013-06-06 23:08:25 -07004483 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004484 Runnable r = new Runnable() {
4485 public void run() {
4486 finishBindingItems(upgradePath);
4487 }
4488 };
4489 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004490 return;
4491 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004492 if (mSavedState != null) {
4493 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004494 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004495 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004496 mSavedState = null;
4497 }
4498
Adam Cohen1462de32012-07-24 22:34:36 -07004499 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004500
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004501 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004502 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004503
4504 // If we received the result of any pending adds while the loader was running (e.g. the
4505 // widget configuration forced an orientation change), process them now.
4506 if (sPendingAddItem != null) {
4507 final long screenId = completeAdd(sPendingAddItem);
4508
4509 // TODO: this moves the user to the page where the pending item was added. Ideally,
4510 // the screen would be guaranteed to exist after bind, and the page would be set through
4511 // the workspace restore process.
4512 mWorkspace.post(new Runnable() {
4513 @Override
4514 public void run() {
4515 mWorkspace.snapToScreenId(screenId);
4516 }
4517 });
4518 sPendingAddItem = null;
4519 }
4520
Adam Cohene25af792013-06-06 23:08:25 -07004521 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004522 mWorkspace.getUniqueComponents(true, null);
4523 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004524 }
Sunny Goyale755d462014-07-22 13:48:29 -07004525 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004526
4527 if (mLauncherCallbacks != null) {
4528 mLauncherCallbacks.finishBindingItems(upgradePath);
4529 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004530 }
4531
Adam Cohen3ed316a2014-07-23 18:21:20 -07004532 private void sendLoadingCompleteBroadcastIfNecessary() {
4533 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4534 String permission =
4535 getResources().getString(R.string.receive_first_load_broadcast_permission);
4536 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4537 sendBroadcast(intent, permission);
4538 SharedPreferences.Editor editor = mSharedPrefs.edit();
4539 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4540 editor.apply();
4541 }
4542 }
4543
Winson Chunga0b7e862013-09-05 16:03:15 -07004544 public boolean isAllAppsButtonRank(int rank) {
4545 if (mHotseat != null) {
4546 return mHotseat.isAllAppsButtonRank(rank);
4547 }
4548 return false;
4549 }
4550
Winson Chunga2413752012-04-03 14:22:34 -07004551 private boolean canRunNewAppsAnimation() {
4552 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4553 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4554 }
4555
Winson Chungc9168342013-06-26 14:54:55 -07004556 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4557 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4558 PropertyValuesHolder.ofFloat("alpha", 1f),
4559 PropertyValuesHolder.ofFloat("scaleX", 1f),
4560 PropertyValuesHolder.ofFloat("scaleY", 1f));
4561 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4562 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4563 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4564 return bounceAnim;
4565 }
4566
Adam Cohen27772732013-11-11 14:00:56 +00004567 public boolean useVerticalBarLayout() {
4568 return LauncherAppState.getInstance().getDynamicGrid().
4569 getDeviceProfile().isVerticalBarLayout();
4570 }
4571
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004572 protected Rect getSearchBarBounds() {
4573 return LauncherAppState.getInstance().getDynamicGrid().
4574 getDeviceProfile().getSearchBarBounds();
4575 }
4576
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004577 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004578 if (mSearchDropTargetBar == null) {
4579 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004580 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004581 if (mQsb != null) {
4582 mSearchDropTargetBar.removeView(mQsb);
4583 mQsb = null;
4584 }
4585 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004586 }
4587
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004588 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004589 * Add the icons for all apps.
4590 *
4591 * Implementation of the method from LauncherModel.Callbacks.
4592 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004593 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004594 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004595 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4596 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4597 getHotseat().addAllAppsFolder(mIconCache, apps,
4598 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4599 }
4600 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004601 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004602 if (mAppsCustomizeContent != null) {
4603 mAppsCustomizeContent.onPackagesUpdated(
4604 LauncherModel.getSortedWidgetsAndShortcuts(this));
4605 }
Winson Chungc58497e2013-09-03 17:48:37 -07004606 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004607 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004608 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004609 mAppsCustomizeContent.onPackagesUpdated(
4610 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004611 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004612 }
Adam Cohen9211d422014-10-07 18:14:53 -07004613 if (mLauncherCallbacks != null) {
4614 mLauncherCallbacks.bindAllApplications(apps);
4615 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004616 }
4617
4618 /**
4619 * A package was updated.
4620 *
4621 * Implementation of the method from LauncherModel.Callbacks.
4622 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004623 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004624 Runnable r = new Runnable() {
4625 public void run() {
4626 bindAppsUpdated(apps);
4627 }
4628 };
4629 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004630 return;
4631 }
4632
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004633 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004634 mAppsCustomizeContent != null) {
4635 mAppsCustomizeContent.updateApps(apps);
4636 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004637 }
4638
Sunny Goyal4390ace2014-10-13 11:33:11 -07004639 @Override
4640 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4641 Runnable r = new Runnable() {
4642 public void run() {
4643 bindWidgetsRestored(widgets);
4644 }
4645 };
4646 if (waitUntilResume(r)) {
4647 return;
4648 }
4649 mWorkspace.widgetsRestored(widgets);
4650 }
4651
Joe Onorato9c1289c2009-08-17 11:03:03 -04004652 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004653 * Some shortcuts were updated in the background.
4654 *
4655 * Implementation of the method from LauncherModel.Callbacks.
4656 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004657 @Override
4658 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4659 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004660 Runnable r = new Runnable() {
4661 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004662 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004663 }
4664 };
4665 if (waitUntilResume(r)) {
4666 return;
4667 }
4668
Sunny Goyal4390ace2014-10-13 11:33:11 -07004669 if (!updated.isEmpty()) {
4670 mWorkspace.updateShortcuts(updated);
4671 }
4672
4673 if (!removed.isEmpty()) {
4674 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4675 for (ShortcutInfo si : removed) {
4676 removedComponents.add(si.getTargetComponent());
4677 }
4678 mWorkspace.removeItemsByComponentName(removedComponents, user);
4679 // Notify the drag controller
4680 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004681 }
4682 }
4683
4684 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004685 * Update the state of a package, typically related to install state.
4686 *
4687 * Implementation of the method from LauncherModel.Callbacks.
4688 */
Sunny Goyale755d462014-07-22 13:48:29 -07004689 @Override
4690 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004691 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004692 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004693 }
4694 }
4695
4696 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004697 * Update the label and icon of all the icons in a package
4698 *
4699 * Implementation of the method from LauncherModel.Callbacks.
4700 */
4701 @Override
4702 public void updatePackageBadge(String packageName) {
4703 if (mWorkspace != null) {
4704 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4705 }
4706 }
4707
4708 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004709 * A package was uninstalled. We take both the super set of packageNames
4710 * in addition to specific applications to remove, the reason being that
4711 * this can be called when a package is updated as well. In that scenario,
4712 * we only remove specific components from the workspace, where as
4713 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004714 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004715 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004716 * Implementation of the method from LauncherModel.Callbacks.
4717 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004718 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004719 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004720 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004721 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004722 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004723 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004724 }
Winson Chungd64d1762013-08-20 14:37:16 -07004725 };
4726 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004727 return;
4728 }
4729
Sunny Goyal1a745e82014-10-02 15:58:31 -07004730 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004731 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4732 for (AppInfo info : appInfos) {
4733 removedComponents.add(info.componentName);
4734 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004735 if (!packageNames.isEmpty()) {
4736 mWorkspace.removeItemsByPackageName(packageNames, user);
4737 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004738 if (!removedComponents.isEmpty()) {
4739 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004740 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004741 // Notify the drag controller
4742 mDragController.onAppsRemoved(packageNames, removedComponents);
4743
Sunny Goyal1a745e82014-10-02 15:58:31 -07004744 } else {
4745 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004746 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004747
Winson Chungdf95eb12013-10-16 14:57:07 -07004748 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004749 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004750 mAppsCustomizeContent != null) {
4751 mAppsCustomizeContent.removeApps(appInfos);
4752 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004753 }
Joe Onoratobe386092009-11-17 17:32:16 -08004754
4755 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004756 * A number of packages were updated.
4757 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004758 private ArrayList<Object> mWidgetsAndShortcuts;
4759 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004760 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004761 bindPackagesUpdated(mWidgetsAndShortcuts);
4762 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004763 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004764 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004765 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4766 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4767 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004768 return;
4769 }
4770
Winson Chung64359a52013-07-08 17:17:08 -07004771 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004772 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004773 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004774 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004775 }
4776
Winson Chung400438b2011-01-16 17:53:48 -08004777 private int mapConfigurationOriActivityInfoOri(int configOri) {
4778 final Display d = getWindowManager().getDefaultDisplay();
4779 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4780 switch (d.getRotation()) {
4781 case Surface.ROTATION_0:
4782 case Surface.ROTATION_180:
4783 // We are currently in the same basic orientation as the natural orientation
4784 naturalOri = configOri;
4785 break;
4786 case Surface.ROTATION_90:
4787 case Surface.ROTATION_270:
4788 // We are currently in the other basic orientation to the natural orientation
4789 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4790 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4791 break;
4792 }
4793
4794 int[] oriMap = {
4795 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4796 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4797 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4798 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4799 };
4800 // Since the map starts at portrait, we need to offset if this device's natural orientation
4801 // is landscape.
4802 int indexOffset = 0;
4803 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4804 indexOffset = 1;
4805 }
4806 return oriMap[(d.getRotation() + indexOffset) % 4];
4807 }
Adam Cohen446e9402011-09-15 18:21:21 -07004808
Winson Chung4b919f82012-05-01 10:44:08 -07004809 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004810 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004811 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4812 .getConfiguration().orientation));
4813 }
4814 }
4815 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004816 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004817 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004818 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004819 } else {
4820 mHandler.postDelayed(new Runnable() {
4821 public void run() {
4822 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4823 }
4824 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004825 }
Winson Chung4b919f82012-05-01 10:44:08 -07004826 }
Winson Chung400438b2011-01-16 17:53:48 -08004827 }
4828
Winson Chunge43a1e72014-01-15 10:33:02 -08004829 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004830 if (mLauncherCallbacks != null) {
4831 return mLauncherCallbacks.isLauncherPreinstalled();
4832 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004833 PackageManager pm = getPackageManager();
4834 try {
4835 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4836 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4837 return true;
4838 } else {
4839 return false;
4840 }
4841 } catch (NameNotFoundException e) {
4842 e.printStackTrace();
4843 return false;
4844 }
4845 }
4846
Adam Cohenea90f832014-05-21 15:03:34 -07004847 /**
4848 * This method indicates whether or not we should suggest default wallpaper dimensions
4849 * when our wallpaper cropper was not yet used to set a wallpaper.
4850 */
4851 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004852 if (mLauncherCallbacks != null) {
4853 return mLauncherCallbacks.overrideWallpaperDimensions();
4854 }
Adam Cohenea90f832014-05-21 15:03:34 -07004855 return true;
4856 }
4857
Adam Cohen432609a2014-03-13 17:03:22 -07004858 /**
4859 * To be overridden by subclasses to indicate that there is an activity to launch
4860 * before showing the standard launcher experience.
4861 */
4862 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004863 if (mLauncherCallbacks != null) {
4864 return mLauncherCallbacks.hasFirstRunActivity();
4865 }
Adam Cohen432609a2014-03-13 17:03:22 -07004866 return false;
4867 }
4868
4869 /**
4870 * To be overridden by subclasses to launch any first run activity
4871 */
4872 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004873 if (mLauncherCallbacks != null) {
4874 return mLauncherCallbacks.getFirstRunActivity();
4875 }
Adam Cohen432609a2014-03-13 17:03:22 -07004876 return null;
4877 }
4878
Winson Chunga6945242014-01-08 14:04:34 -08004879 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004880 return !ActivityManager.isRunningInTestHarness() &&
4881 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004882 }
4883
Adam Cohen4a9423d2014-03-28 14:22:31 -07004884 protected boolean hasRunFirstRunActivity() {
4885 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4886 }
4887
Adam Cohen432609a2014-03-13 17:03:22 -07004888 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004889 if (shouldRunFirstRunActivity() &&
4890 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004891 Intent firstRunIntent = getFirstRunActivity();
4892 if (firstRunIntent != null) {
4893 startActivity(firstRunIntent);
4894 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004895 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004896 }
4897 }
Adam Cohen432609a2014-03-13 17:03:22 -07004898 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004899 }
4900
4901 private void markFirstRunActivityShown() {
4902 SharedPreferences.Editor editor = mSharedPrefs.edit();
4903 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4904 editor.apply();
4905 }
4906
Adam Cohen432609a2014-03-13 17:03:22 -07004907 /**
4908 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4909 * screen that must be displayed and dismissed.
4910 */
4911 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004912 if (mLauncherCallbacks != null) {
4913 return mLauncherCallbacks.hasDismissableIntroScreen();
4914 }
Adam Cohen432609a2014-03-13 17:03:22 -07004915 return false;
4916 }
4917
4918 /**
4919 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4920 */
4921 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004922 if (mLauncherCallbacks != null) {
4923 return mLauncherCallbacks.getIntroScreen();
4924 }
Adam Cohen432609a2014-03-13 17:03:22 -07004925 return null;
4926 }
4927
4928 /**
4929 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4930 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4931 */
4932 private boolean shouldShowIntroScreen() {
4933 return hasDismissableIntroScreen() &&
4934 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4935 }
4936
4937 protected void showIntroScreen() {
4938 View introScreen = getIntroScreen();
4939 changeWallpaperVisiblity(false);
4940 if (introScreen != null) {
4941 mDragLayer.showOverlayView(introScreen);
4942 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004943 if (mLauncherOverlayContainer != null) {
4944 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4945 }
Adam Cohen432609a2014-03-13 17:03:22 -07004946 }
4947
4948 public void dismissIntroScreen() {
4949 markIntroScreenDismissed();
4950 if (showFirstRunActivity()) {
4951 // We delay hiding the intro view until the first run activity is showing. This
4952 // avoids a blip.
4953 mWorkspace.postDelayed(new Runnable() {
4954 @Override
4955 public void run() {
4956 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004957 if (mLauncherOverlayContainer != null) {
4958 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4959 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004960 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004961 }
4962 }, ACTIVITY_START_DELAY);
4963 } else {
4964 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004965 if (mLauncherOverlayContainer != null) {
4966 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4967 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004968 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004969 }
4970 changeWallpaperVisiblity(true);
4971 }
4972
4973 private void markIntroScreenDismissed() {
4974 SharedPreferences.Editor editor = mSharedPrefs.edit();
4975 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4976 editor.apply();
4977 }
4978
Sunny Goyal88d60f12014-09-08 03:47:29 -07004979 private void showFirstRunClings() {
4980 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4981 // on the device, then we always show the first run cling experience (or if there is no
4982 // launcher2). Otherwise, we prompt the user upon started for migration
4983 LauncherClings launcherClings = new LauncherClings(this);
4984 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4985 if (mModel.canMigrateFromOldLauncherDb(this)) {
4986 launcherClings.showMigrationCling();
4987 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004988 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004989 }
4990 }
4991 }
4992
Winson Chunga6945242014-01-08 14:04:34 -08004993 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004994 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4995 if (mHotseat != null) mHotseat.setAlpha(1f);
4996 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4997 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004998 }
4999
5000 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005001 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5002 if (mHotseat != null) mHotseat.setAlpha(0f);
5003 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5004 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005005 }
5006
Mathew Inwood72fbec12013-11-19 15:45:07 +00005007 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005008 // Called from search suggestion, not supported in other profiles.
5009 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5010 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5011 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5012 myUser);
5013 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005014 return null;
5015 }
Kenny Guyed131872014-04-30 03:02:21 +01005016 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005017 }
5018
5019 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5020 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005021 // Called from search suggestion, not supported in other profiles.
5022 return createShortcutDragInfo(shortcutIntent, caption, icon,
5023 UserHandleCompat.myUserHandle());
5024 }
5025
5026 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5027 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005028 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005029 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005030 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005031 }
5032
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005033 protected void moveWorkspaceToDefaultScreen() {
5034 mWorkspace.moveToDefaultScreen(false);
5035 }
5036
Mathew Inwood72fbec12013-11-19 15:45:07 +00005037 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5038 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005039 mWorkspace.onExternalDragStartedWithItem(dragView);
5040 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005041 }
5042
Anjali Koppalf5d29132014-02-28 13:33:27 -08005043 @Override
5044 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005045 if (mLauncherCallbacks != null) {
5046 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5047 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005048 }
5049
Winson Chung80baf5a2010-08-09 16:03:15 -07005050 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005051 * Prints out out state for debugging.
5052 */
5053 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005054 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005055 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005056 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5057 Log.d(TAG, "mRestoring=" + mRestoring);
5058 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5059 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005060 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005061 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005062
Winson Chung785d2eb2011-04-14 16:08:02 -07005063 if (mAppsCustomizeContent != null) {
5064 mAppsCustomizeContent.dumpState();
5065 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005066 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005067 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005068
5069 @Override
5070 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5071 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005072 synchronized (sDumpLogs) {
5073 writer.println(" ");
5074 writer.println("Debug logs: ");
5075 for (int i = 0; i < sDumpLogs.size(); i++) {
5076 writer.println(" " + sDumpLogs.get(i));
5077 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005078 }
Adam Cohen9211d422014-10-07 18:14:53 -07005079 if (mLauncherCallbacks != null) {
5080 mLauncherCallbacks.dump(prefix, fd, writer, args);
5081 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005082 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005083
5084 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005085 if (DEBUG_DUMP_LOG) {
5086 synchronized (sDumpLogs) {
5087 Log.d(TAG, "");
5088 Log.d(TAG, "*********************");
5089 Log.d(TAG, "Launcher debug logs: ");
5090 for (int i = 0; i < sDumpLogs.size(); i++) {
5091 Log.d(TAG, " " + sDumpLogs.get(i));
5092 }
5093 Log.d(TAG, "*********************");
5094 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005095 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005096 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005097 }
5098
5099 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005100 addDumpLog(tag, log, null, debugLog);
5101 }
5102
5103 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005104 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005105 if (e != null) {
5106 Log.d(tag, log, e);
5107 } else {
5108 Log.d(tag, log);
5109 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005110 }
Winson Chungede41292013-09-19 16:27:36 -07005111 if (DEBUG_DUMP_LOG) {
5112 sDateStamp.setTime(System.currentTimeMillis());
5113 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005114 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5115 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005116 }
Winson Chungede41292013-09-19 16:27:36 -07005117 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005118 }
5119
Winson Chungede41292013-09-19 16:27:36 -07005120 public void dumpLogsToLocalData() {
5121 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005122 new AsyncTask<Void, Void, Void>() {
5123 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005124 boolean success = false;
5125 sDateStamp.setTime(sRunStart);
5126 String FILENAME = sDateStamp.getMonth() + "-"
5127 + sDateStamp.getDay() + "_"
5128 + sDateStamp.getHours() + "-"
5129 + sDateStamp.getMinutes() + "_"
5130 + sDateStamp.getSeconds() + ".txt";
5131
5132 FileOutputStream fos = null;
5133 File outFile = null;
5134 try {
5135 outFile = new File(getFilesDir(), FILENAME);
5136 outFile.createNewFile();
5137 fos = new FileOutputStream(outFile);
5138 } catch (Exception e) {
5139 e.printStackTrace();
5140 }
5141 if (fos != null) {
5142 PrintWriter writer = new PrintWriter(fos);
5143
5144 writer.println(" ");
5145 writer.println("Debug logs: ");
5146 synchronized (sDumpLogs) {
5147 for (int i = 0; i < sDumpLogs.size(); i++) {
5148 writer.println(" " + sDumpLogs.get(i));
5149 }
5150 }
5151 writer.close();
5152 }
5153 try {
5154 if (fos != null) {
5155 fos.close();
5156 success = true;
5157 }
5158 } catch (IOException e) {
5159 e.printStackTrace();
5160 }
Michael Jurka43467462013-11-14 12:03:58 +01005161 return null;
Winson Chungede41292013-09-19 16:27:36 -07005162 }
Michael Jurka43467462013-11-14 12:03:58 +01005163 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005164 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005165 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005166}
Michael Jurka2763be32011-02-24 11:19:57 -08005167
Michael Jurkaabded662011-03-04 12:06:57 -08005168interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005169 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005170 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005171 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005172 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005173 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005174}
Dan Sandlerd5024042014-01-09 15:01:33 -05005175
5176interface DebugIntents {
5177 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5178 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005179}