blob: ffbb873520a1a339b94be92f683402e6f419f8f2 [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]
169 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
170 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800171 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Winson Chung2672ff92012-05-04 16:22:30 -0700173 // The Intent extra that defines whether to ignore the launch animation
174 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400175 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: int
178 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700179 // Type: int
180 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700182 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
183 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
185 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700186 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700188 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 // Type: boolean
190 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
191 // Type: long
192 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700193 // Type: int
194 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
195 // Type: int
196 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
197 // Type: parcelable
198 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000199 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800200 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000201 // Type: int[]
202 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
Adam Cohen432609a2014-03-13 17:03:22 -0700204 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800205 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
206
Adam Cohen3ed316a2014-07-23 18:21:20 -0700207 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
208 static final String ACTION_FIRST_LOAD_COMPLETE =
209 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
210
Adam Cohen39a06042013-07-19 14:30:12 -0700211 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700212 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700213
Sunny Goyal594d76d2014-11-06 10:12:54 -0800214 private static final String QSB_WIDGET_ID = "qsb_widget_id";
215 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
216
Winson Chunga6945242014-01-08 14:04:34 -0800217 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
218
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700219 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700220 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700221 private State mState = State.WORKSPACE;
222 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700223
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700224 private boolean mIsSafeModeEnabled;
225
Adam Cohenc2d6e892014-10-16 09:49:24 -0700226 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
227 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700228 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700231 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
232 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700233 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800236 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000238 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
239 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
240
Winson Chunga2413752012-04-03 14:22:34 -0700241 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700242 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
243 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700244 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700245 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700246
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800247 private final BroadcastReceiver mCloseSystemDialogsReceiver
248 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800249 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private LayoutInflater mInflater;
252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700254 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800255 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800256 private DragLayer mDragLayer;
257 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700258 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700259
Sunny Goyalffe83f12014-08-14 17:39:34 -0700260 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700261 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700262
Michael Jurkac9d95c52011-08-29 14:03:34 -0700263 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700264 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800265 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700266
Michael Jurka0280c3b2010-09-17 15:00:07 -0700267 private int[] mTmpAddItemCellCoordinates = new int[2];
268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 private FolderInfo mFolderInfo;
270
Winson Chung3d503fb2011-07-13 17:25:49 -0700271 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800272 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700273
Michael Jurka838a4ca2011-02-07 13:33:06 -0800274 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700275
Winson Chungc51db6a2011-10-05 11:44:49 -0700276 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700277 private AppsCustomizeTabHost mAppsCustomizeTabHost;
278 private AppsCustomizePagedView mAppsCustomizeContent;
279 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800280 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700283 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
284 // scroll issues (because the workspace may not have been measured yet) and extra work.
285 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
286 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287
288 private SpannableStringBuilder mDefaultKeySsb = null;
289
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290 private boolean mWorkspaceLoading = true;
291
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800292 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 private boolean mRestoring;
294 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700295 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296
Michael Jurka1e2f4652013-07-08 18:03:46 -0700297 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700298 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 private Bundle mSavedInstanceState;
301
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800303 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mUserPresent = true;
305 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700306 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800307 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400308
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700309 private static LocaleConfiguration sLocaleConfiguration = null;
310
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700311 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700312
Adam Cohen61f560d2013-09-30 15:58:20 -0700313 private View.OnTouchListener mHapticFeedbackTouchListener;
314
Adam Cohended9f8d2010-11-03 13:25:16 -0700315 // Related to the auto-advancing of widgets
316 private final int ADVANCE_MSG = 1;
317 private final int mAdvanceInterval = 20000;
318 private final int mAdvanceStagger = 250;
319 private long mAutoAdvanceSentTime;
320 private long mAutoAdvanceTimeLeft = -1;
321 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
322 new HashMap<View, AppWidgetProviderInfo>();
323
Winson Chung400438b2011-01-16 17:53:48 -0800324 // Determines how long to wait after a rotation before restoring the screen orientation to
325 // match the sensor state.
326 private final int mRestoreScreenOrientationDelay = 500;
327
Craig Mautner360310b2012-10-26 15:13:08 -0700328 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700329
Adam Cohen1462de32012-07-24 22:34:36 -0700330 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700331 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700332
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700333 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700334 static Date sDateStamp = new Date();
335 static DateFormat sDateFormat =
336 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
337 static long sRunStart = System.currentTimeMillis();
338 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700339
Winson Chung46353de2012-02-16 14:05:10 -0800340 // We only want to get the SharedPreferences once since it does an FS stat each time we get
341 // it from the context.
342 private SharedPreferences mSharedPrefs;
343
Adam Cohene25af792013-06-06 23:08:25 -0700344 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
345
Winson Chungf0c6ae02012-03-21 16:10:31 -0700346 // Holds the page that we need to animate to, and the icon views that we need to animate up
347 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700348 private ImageView mFolderIconImageView;
349 private Bitmap mFolderIconBitmap;
350 private Canvas mFolderIconCanvas;
351 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700352
Michael Jurkaddd62e92011-02-16 17:49:14 -0800353 private BubbleTextView mWaitingForResume;
354
Michael Jurkac1f5d262011-09-30 19:32:27 -0700355 private Runnable mBuildLayersRunnable = new Runnable() {
356 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700357 if (mWorkspace != null) {
358 mWorkspace.buildPageHardwareLayers();
359 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700360 }
361 };
362
Adam Cohendb364c32014-05-20 14:23:40 -0700363 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364
Michael Jurka7267fa52013-09-26 15:29:57 -0700365 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800366
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800367 private static class PendingAddArguments {
368 int requestCode;
369 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700370 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700371 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800372 int cellX;
373 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700374 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800375 }
376
Daniel Sandlerff02d492013-08-05 02:12:05 -0400377 private Stats mStats;
378
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700379 FocusIndicatorView mFocusHandler;
380
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800381 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800382 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700383 }
384
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 @Override
386 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700387 if (DEBUG_STRICT_MODE) {
388 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
389 .detectDiskReads()
390 .detectDiskWrites()
391 .detectNetwork() // or .detectAll() for all detectable problems
392 .penaltyLog()
393 .build());
394 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
395 .detectLeakedSqlLiteObjects()
396 .detectLeakedClosableObjects()
397 .penaltyLog()
398 .penaltyDeath()
399 .build());
400 }
401
Adam Cohen9211d422014-10-07 18:14:53 -0700402 if (mLauncherCallbacks != null) {
403 mLauncherCallbacks.preOnCreate();
404 }
405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400407
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400408 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400409 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700410 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700411
Winson Chung5f8afe62013-08-12 16:19:28 -0700412 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700413 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700414
415 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400416 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700417 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700418 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800419 mModel = app.setLauncher(this);
420 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700421 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400422 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700424
Daniel Sandlerff02d492013-08-05 02:12:05 -0400425 mStats = new Stats(this);
426
Sunny Goyalffe83f12014-08-14 17:39:34 -0700427 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700428
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700429 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
430 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700431
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700432 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
433 // this also ensures that any synchronous binding below doesn't re-trigger another
434 // LauncherModel load.
435 mPaused = false;
436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200438 android.os.Debug.startMethodTracing(
439 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 }
441
442 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100446 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800448 registerContentObservers();
449
Joe Onorato7c312c12009-08-13 21:36:53 -0700450 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 mSavedState = savedInstanceState;
453 restoreState(mSavedState);
454
455 if (PROFILE_STARTUP) {
456 android.os.Debug.stopMethodTracing();
457 }
458
459 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700460 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 // If the user leaves launcher, then we should just load items asynchronously when
462 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500463 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700464 } else {
465 // We only load the page synchronously if the user rotates (or triggers a
466 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800467 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
470
471 // For handling default keys
472 mDefaultKeySsb = new SpannableStringBuilder();
473 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800474
475 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
476 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800477
Adam Cohenf9426d52012-06-04 17:26:21 -0700478 // On large interfaces, we want the screen to auto-rotate based on the current orientation
479 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700480
Adam Cohen9211d422014-10-07 18:14:53 -0700481 if (mLauncherCallbacks != null) {
482 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700483 if (mLauncherCallbacks.hasLauncherOverlay()) {
484 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700485 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
486 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
487 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700488 mWorkspace.setLauncherOverlay(mLauncherOverlay);
489 }
Adam Cohen9211d422014-10-07 18:14:53 -0700490 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700491
492 if (shouldShowIntroScreen()) {
493 showIntroScreen();
494 } else {
495 showFirstRunActivity();
496 showFirstRunClings();
497 }
Adam Cohen9211d422014-10-07 18:14:53 -0700498 }
499
500 private LauncherCallbacks mLauncherCallbacks;
501
502 public void onPostCreate(Bundle savedInstanceState) {
503 super.onPostCreate(savedInstanceState);
504 if (mLauncherCallbacks != null) {
505 mLauncherCallbacks.onPostCreate(savedInstanceState);
506 }
507 }
508
509 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
510 mLauncherCallbacks = callbacks;
511 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700512 }
513
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700514 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700515 public void onLauncherProviderChange() {
516 if (mLauncherCallbacks != null) {
517 mLauncherCallbacks.onLauncherProviderChange();
518 }
519 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700520
Adam Cohen9211d422014-10-07 18:14:53 -0700521 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700522 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700523 if (mLauncherCallbacks != null) {
524 return mLauncherCallbacks.hasCustomContentToLeft();
525 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700526 return false;
527 }
528
Dave Hawkeya8881582013-09-17 15:55:33 -0600529 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500530 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600531 * {@link #addToCustomContentPage}. This will only be invoked if
532 * {@link #hasCustomContentToLeft()} is {@code true}.
533 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500534 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700535 if (mLauncherCallbacks != null) {
536 mLauncherCallbacks.populateCustomContentContainer();
537 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600538 }
539
540 /**
541 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
542 * ensure the custom content page is added or removed if necessary.
543 */
544 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600545 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600546 // Not bound yet, wait for bindScreens to be called.
547 return;
548 }
549
550 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
551 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500552 mWorkspace.createCustomContentContainer();
553 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600554 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
555 mWorkspace.removeCustomContentPage();
556 }
557 }
558
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700560 if (sLocaleConfiguration == null) {
561 new AsyncTask<Void, Void, LocaleConfiguration>() {
562 @Override
563 protected LocaleConfiguration doInBackground(Void... unused) {
564 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
565 readConfiguration(Launcher.this, localeConfiguration);
566 return localeConfiguration;
567 }
568
569 @Override
570 protected void onPostExecute(LocaleConfiguration result) {
571 sLocaleConfiguration = result;
572 checkForLocaleChange(); // recursive, but now with a locale configuration
573 }
574 }.execute();
575 return;
576 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700577
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 final Configuration configuration = getResources().getConfiguration();
579
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700580 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 final String locale = configuration.locale.toString();
582
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700583 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 final int mcc = configuration.mcc;
585
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700586 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 final int mnc = configuration.mnc;
588
Romain Guy84f296c2009-11-04 15:00:44 -0800589 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590
Romain Guy84f296c2009-11-04 15:00:44 -0800591 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700592 sLocaleConfiguration.locale = locale;
593 sLocaleConfiguration.mcc = mcc;
594 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700595
Joe Onorato0589f0f2010-02-08 13:44:00 -0800596 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700597
598 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100599 new AsyncTask<Void, Void, Void>() {
600 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700601 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100602 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700603 }
Michael Jurka43467462013-11-14 12:03:58 +0100604 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700605 }
606 }
607
608 private static class LocaleConfiguration {
609 public String locale;
610 public int mcc = -1;
611 public int mnc = -1;
612 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700613
Romain Guy98d01652009-06-30 16:21:04 -0700614 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
615 DataInputStream in = null;
616 try {
Helena Josol28db2802014-10-09 17:04:09 +0100617 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700618 configuration.locale = in.readUTF();
619 configuration.mcc = in.readInt();
620 configuration.mnc = in.readInt();
621 } catch (FileNotFoundException e) {
622 // Ignore
623 } catch (IOException e) {
624 // Ignore
625 } finally {
626 if (in != null) {
627 try {
628 in.close();
629 } catch (IOException e) {
630 // Ignore
631 }
632 }
633 }
634 }
635
636 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
637 DataOutputStream out = null;
638 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100639 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100640 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700641 out.writeUTF(configuration.locale);
642 out.writeInt(configuration.mcc);
643 out.writeInt(configuration.mnc);
644 out.flush();
645 } catch (FileNotFoundException e) {
646 // Ignore
647 } catch (IOException e) {
648 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100649 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700650 } finally {
651 if (out != null) {
652 try {
653 out.close();
654 } catch (IOException e) {
655 // Ignore
656 }
657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 }
659 }
660
Anton Hanssona2f665f2013-09-26 12:18:32 +0100661 public Stats getStats() {
662 return mStats;
663 }
664
Bjorn Bringertc459e522013-06-07 19:36:01 +0100665 public LayoutInflater getInflater() {
666 return mInflater;
667 }
668
Winson Chung36a62fe2012-05-06 18:04:42 -0700669 boolean isDraggingEnabled() {
670 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
671 // that is subsequently removed from the workspace in startBinding().
672 return !mModel.isLoadingWorkspace();
673 }
674
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 static int getScreen() {
676 synchronized (sLock) {
677 return sScreen;
678 }
679 }
680
681 static void setScreen(int screen) {
682 synchronized (sLock) {
683 sScreen = screen;
684 }
685 }
686
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000687 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100688 if (Build.VERSION.SDK_INT >= 17) {
689 return View.generateViewId();
690 } else {
691 // View.generateViewId() is not available. The following fallback logic is a copy
692 // of its implementation.
693 for (;;) {
694 final int result = sNextGeneratedId.get();
695 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
696 int newValue = result + 1;
697 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
698 if (sNextGeneratedId.compareAndSet(result, newValue)) {
699 return result;
700 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000701 }
702 }
703 }
704
705 public int getViewIdForItem(ItemInfo info) {
706 // This cast is safe given the > 2B range for int.
707 int itemId = (int) info.id;
708 if (mItemIdToViewId.containsKey(itemId)) {
709 return mItemIdToViewId.get(itemId);
710 }
711 int viewId = generateViewId();
712 mItemIdToViewId.put(itemId, viewId);
713 return viewId;
714 }
715
716 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700717 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
718 * a configuration step, this allows the proper animations to run after other transitions.
719 */
Adam Cohendb364c32014-05-20 14:23:40 -0700720 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700721 long screenId = args.screenId;
722 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
723 // When the screen id represents an actual screen (as opposed to a rank) we make sure
724 // that the drop page actually exists.
725 screenId = ensurePendingDropLayoutExists(args.screenId);
726 }
Adam Cohendb364c32014-05-20 14:23:40 -0700727
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800728 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800729 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700730 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700731 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700732 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800733 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700734 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700735 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700736 case REQUEST_RECONFIGURE_APPWIDGET:
737 completeRestoreAppWidget(args.appWidgetId);
738 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 }
Michael Jurka27614d22012-04-02 06:40:22 -0700740 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
741 // if you turned the screen off and then back while in All Apps, Launcher would not
742 // return to the workspace. Clearing mAddInfo.container here fixes this issue
743 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700744 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800745 }
746
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700748 protected void onActivityResult(
749 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700750 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700751 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700752 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800753 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700754
Adam Cohenad4e15c2013-10-17 16:21:35 -0700755 Runnable exitSpringLoaded = new Runnable() {
756 @Override
757 public void run() {
758 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
759 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
760 }
761 };
762
Michael Jurka8b805b12012-04-18 14:23:14 -0700763 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
766 if (resultCode == RESULT_CANCELED) {
767 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700768 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700769 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700770 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700771 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
772 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 }
774 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200775 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
776 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
777 mWorkspace.exitOverviewMode(false);
778 }
779 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700780 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200781
Adam Cohened66b2b2012-01-23 17:28:51 -0800782 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
783 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700784
Adam Cohendb364c32014-05-20 14:23:40 -0700785 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800786 // We have special handling for widgets
787 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800788 final int appWidgetId;
789 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
790 : -1;
791 if (widgetId < 0) {
792 appWidgetId = pendingAddWidgetId;
793 } else {
794 appWidgetId = widgetId;
795 }
796
Adam Cohenad4e15c2013-10-17 16:21:35 -0700797 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800798 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700799 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
800 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700801 result = RESULT_CANCELED;
802 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700803 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700804 @Override
805 public void run() {
806 exitSpringLoadedDragModeDelayed(false, 0, null);
807 }
808 };
Adam Cohendb364c32014-05-20 14:23:40 -0700809 if (workspaceLocked) {
810 // No need to remove the empty screen if we're mid-binding, as the
811 // the bind will not add the empty screen.
812 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
813 } else {
814 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
815 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
816 }
Winson Chung5aaab772012-04-27 15:24:02 -0700817 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700818 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700819 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
820 // When the screen id represents an actual screen (as opposed to a rank)
821 // we make sure that the drop page actually exists.
822 mPendingAddInfo.screenId =
823 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
824 }
Adam Cohendb364c32014-05-20 14:23:40 -0700825 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
826
827 dropLayout.setDropPending(true);
828 final Runnable onComplete = new Runnable() {
829 @Override
830 public void run() {
831 completeTwoStageWidgetDrop(resultCode, appWidgetId);
832 dropLayout.setDropPending(false);
833 }
834 };
835 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
836 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
837 } else {
838 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
839 mPendingAddInfo);
840 sPendingAddItem = args;
841 }
Winson Chung5aaab772012-04-27 15:24:02 -0700842 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800843 return;
844 }
845
Sunny Goyalff572272014-07-23 13:58:07 -0700846 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
847 if (resultCode == RESULT_OK) {
848 // Update the widget view.
849 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
850 pendingAddWidgetId, mPendingAddInfo);
851 if (workspaceLocked) {
852 sPendingAddItem = args;
853 } else {
854 completeAdd(args);
855 }
856 }
857 // Leave the widget in the pending state if the user canceled the configure.
858 return;
859 }
860
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 // The pattern used here is that a user PICKs a specific application,
862 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700863
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
865 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700866 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700867 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
868 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800869 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700870 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800871 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700872 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700873 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
874 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 }
Adam Cohen00074722013-10-11 17:46:09 -0700876 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700877 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700878 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800881 }
882
Adam Cohendb364c32014-05-20 14:23:40 -0700883 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
884 appWidgetId, ItemInfo info) {
885 PendingAddArguments args = new PendingAddArguments();
886 args.requestCode = requestCode;
887 args.intent = data;
888 args.container = info.container;
889 args.screenId = info.screenId;
890 args.cellX = info.cellX;
891 args.cellY = info.cellY;
892 args.appWidgetId = appWidgetId;
893 return args;
894 }
895
896 /**
897 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
898 *
899 * @param screenId the screen id to check
900 * @return the new screen, or screenId if it exists
901 */
902 private long ensurePendingDropLayoutExists(long screenId) {
903 CellLayout dropLayout =
904 (CellLayout) mWorkspace.getScreenWithId(screenId);
905 if (dropLayout == null) {
906 // it's possible that the add screen was removed because it was
907 // empty and a re-bind occurred
908 mWorkspace.addExtraEmptyScreen();
909 return mWorkspace.commitExtraEmptyScreen();
910 } else {
911 return screenId;
912 }
913 }
914
Adam Cohened66b2b2012-01-23 17:28:51 -0800915 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700916 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700917 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800918 Runnable onCompleteRunnable = null;
919 int animationType = 0;
920
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700921 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800922 if (resultCode == RESULT_OK) {
923 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
924 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700925 mPendingAddWidgetInfo);
926 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 onCompleteRunnable = new Runnable() {
928 @Override
929 public void run() {
930 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700931 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700932 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
933 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800934 }
935 };
936 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800937 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800938 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800939 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700940 if (mDragLayer.getAnimatedView() != null) {
941 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
942 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
943 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700944 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700945 // The animated view may be null in the case of a rotation during widget configuration
946 onCompleteRunnable.run();
947 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 }
949
950 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700951 protected void onStop() {
952 super.onStop();
953 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700954
955 if (mLauncherCallbacks != null) {
956 mLauncherCallbacks.onStop();
957 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700958 }
959
960 @Override
961 protected void onStart() {
962 super.onStart();
963 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700964
965 if (mLauncherCallbacks != null) {
966 mLauncherCallbacks.onStart();
967 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700968 }
969
970 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200972 long startTime = 0;
973 if (DEBUG_RESUME_TIME) {
974 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400975 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200976 }
Adam Cohen9211d422014-10-07 18:14:53 -0700977
978 if (mLauncherCallbacks != null) {
979 mLauncherCallbacks.preOnResume();
980 }
981
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700983
Winson Chung4a2afa32012-07-19 14:53:05 -0700984 // Restore the previous launcher state
985 if (mOnResumeState == State.WORKSPACE) {
986 showWorkspace(false);
987 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c62013-11-06 15:49:51 -0800988 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700989 }
990 mOnResumeState = State.NONE;
991
Craig Mautner360310b2012-10-26 15:13:08 -0700992 // Background was set to gradient in onPause(), restore to black if in all apps.
993 setWorkspaceBackground(mState == State.WORKSPACE);
994
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800995 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700996 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700997 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500998 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400999 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001000 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001002 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001003 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1004 // execute them here
1005 long startTimeCallbacks = 0;
1006 if (DEBUG_RESUME_TIME) {
1007 startTimeCallbacks = System.currentTimeMillis();
1008 }
1009
1010 if (mAppsCustomizeContent != null) {
1011 mAppsCustomizeContent.setBulkBind(true);
1012 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001013 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1014 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001015 }
1016 if (mAppsCustomizeContent != null) {
1017 mAppsCustomizeContent.setBulkBind(false);
1018 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001019 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001020 if (DEBUG_RESUME_TIME) {
1021 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1022 (System.currentTimeMillis() - startTimeCallbacks));
1023 }
Michael Jurka447bf842013-05-15 14:52:15 +02001024 }
Michael Jurka54554252013-08-01 12:52:23 +02001025 if (mOnResumeCallbacks.size() > 0) {
1026 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1027 mOnResumeCallbacks.get(i).run();
1028 }
1029 mOnResumeCallbacks.clear();
1030 }
Winson Chunge4e50662012-01-23 14:45:13 -08001031
1032 // Reset the pressed state of icons that were locked in the press state while activities
1033 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001034 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001035 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001036 mWaitingForResume.setStayPressed(false);
1037 }
Winson Chung82963d52013-10-09 11:20:57 -07001038
Adam Cohen06dff352012-06-01 17:17:08 -07001039 // It is possible that widgets can receive updates while launcher is not in the foreground.
1040 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1041 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1042 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001043 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001044
Winson Chung780fe592013-09-26 14:48:44 -07001045 // Process any items that were added while Launcher was away.
1046 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1047
Michael Jurka447bf842013-05-15 14:52:15 +02001048 if (DEBUG_RESUME_TIME) {
1049 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1050 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001051
1052 if (mWorkspace.getCustomContentCallbacks() != null) {
1053 // If we are resuming and the custom content is the current page, we call onShow().
1054 // It is also poassible that onShow will instead be called slightly after first layout
1055 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1056 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001057 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001058 }
1059 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001060 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001061 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001062
1063 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001064
1065 if (mLauncherCallbacks != null) {
1066 mLauncherCallbacks.onResume();
1067 }
Adam Cohen06dff352012-06-01 17:17:08 -07001068 }
1069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001071 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001072 // Ensure that items added to Launcher are queued until Launcher returns
1073 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001074 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001075
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001076 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001077 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001078 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001079 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001080
1081 // We call onHide() aggressively. The custom content callbacks should be able to
1082 // debounce excess onHide calls.
1083 if (mWorkspace.getCustomContentCallbacks() != null) {
1084 mWorkspace.getCustomContentCallbacks().onHide();
1085 }
Romain Guycbb89e42009-06-08 15:52:54 -07001086
Adam Cohen9211d422014-10-07 18:14:53 -07001087 if (mLauncherCallbacks != null) {
1088 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001089 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001090 }
1091
1092 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001093 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1094 // by a onResume or by scrolling otherwise.
1095 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001096
1097 // Custom content is completely hidden
1098 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001099
1100 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1101 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001102
1103 // Indicates whether the user is allowed to scroll away from the custom content.
1104 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001105 }
1106
Adam Cohenc2d6e892014-10-16 09:49:24 -07001107 public interface LauncherOverlay {
1108
1109 /**
1110 * Touch interaction leading to overscroll has begun
1111 */
1112 public void onScrollInteractionBegin();
1113
1114 /**
1115 * Touch interaction related to overscroll has ended
1116 */
1117 public void onScrollInteractionEnd();
1118
1119 /**
1120 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1121 * screen (or in the case of RTL, the rightmost screen).
1122 */
1123 public void onScrollChange(int progress, boolean rtl);
1124
1125 /**
1126 * Screen has stopped scrolling
1127 */
1128 public void onScrollSettled();
1129
1130 /**
1131 * This method can be called by the Launcher in order to force the LauncherOverlay
1132 * to exit fully immersive mode.
1133 */
1134 public void forceExitFullImmersion();
1135 }
1136
1137 public interface LauncherOverlayCallbacks {
1138 /**
1139 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1140 * however it doesn't modify any state within the launcher.
1141 */
1142 public boolean canEnterFullImmersion();
1143
1144 /**
1145 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1146 * eg. by occupying the full screen and handling all touch events.
1147 *
1148 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1149 * case, Launcher will modify any necessary state and assumes the overlay is
1150 * handling all interaction. If false, the LauncherOverlay should cancel any
1151 *
1152 */
1153 public boolean enterFullImmersion();
1154
1155 /**
1156 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1157 * full control over UI and state.
1158 */
1159 public void exitFullImmersion();
1160 }
1161
1162 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1163
1164 @Override
1165 public boolean canEnterFullImmersion() {
1166 return mState == State.WORKSPACE;
1167 }
1168
1169 @Override
1170 public boolean enterFullImmersion() {
1171 if (mState == State.WORKSPACE) {
1172 // When fully immersed, disregard any touches which fall through.
1173 mDragLayer.setBlockTouch(true);
1174 return true;
1175 }
1176 return false;
1177 }
1178
1179 @Override
1180 public void exitFullImmersion() {
1181 mDragLayer.setBlockTouch(false);
1182 }
1183 }
1184
Jorim Jaggid017f882014-01-14 17:08:48 -08001185 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001186 if (mLauncherCallbacks != null) {
1187 return mLauncherCallbacks.hasSettings();
1188 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001189 return false;
1190 }
1191
Adam Cohenbffe7452013-07-22 18:21:45 -07001192
Adam Cohen9211d422014-10-07 18:14:53 -07001193 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001194 CustomContentCallbacks callbacks, String description) {
1195 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001196 }
1197
Adam Cohenedb40762013-07-18 16:45:45 -07001198 // The custom content needs to offset its content to account for the QSB
1199 public int getTopOffsetForCustomContent() {
1200 return mWorkspace.getPaddingTop();
1201 }
1202
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001203 @Override
1204 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001205 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001206 if (mModel.isCurrentCallbacks(this)) {
1207 mModel.stopLoader();
1208 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001209 if (mAppsCustomizeContent != null) {
1210 mAppsCustomizeContent.surrender();
1211 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001212 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001213 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001214
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001215 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001216 @Override
1217 public void onWindowFocusChanged(boolean hasFocus) {
1218 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001219 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001220
1221 if (mLauncherCallbacks != null) {
1222 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1223 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001224 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001225
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 private boolean acceptFilter() {
1227 final InputMethodManager inputManager = (InputMethodManager)
1228 getSystemService(Context.INPUT_METHOD_SERVICE);
1229 return !inputManager.isFullscreenMode();
1230 }
1231
1232 @Override
1233 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001234 final int uniChar = event.getUnicodeChar();
1235 final boolean handled = super.onKeyDown(keyCode, event);
1236 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1237 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1239 keyCode, event);
1240 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001241 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001242 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001243 // showSearchDialog()
1244 // If there are multiple keystrokes before the search dialog takes focus,
1245 // onSearchRequested() will be called for every keystroke,
1246 // but it is idempotent, so it's fine.
1247 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 }
1249 }
1250
Joe Onorato8a9625e2010-01-28 15:55:35 -08001251 // Eat the long press event so the keyboard doesn't come up.
1252 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1253 return true;
1254 }
1255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 return handled;
1257 }
1258
Karl Rosaen138a0412009-04-23 19:00:21 -07001259 private String getTypedText() {
1260 return mDefaultKeySsb.toString();
1261 }
1262
1263 private void clearTypedText() {
1264 mDefaultKeySsb.clear();
1265 mDefaultKeySsb.clearSpans();
1266 Selection.setSelection(mDefaultKeySsb, 0);
1267 }
1268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001270 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1271 * State
1272 */
1273 private static State intToState(int stateOrdinal) {
1274 State state = State.WORKSPACE;
1275 final State[] stateValues = State.values();
1276 for (int i = 0; i < stateValues.length; i++) {
1277 if (stateValues[i].ordinal() == stateOrdinal) {
1278 state = stateValues[i];
1279 break;
1280 }
1281 }
1282 return state;
1283 }
1284
1285 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 * Restores the previous state, if it exists.
1287 *
1288 * @param savedState The previous state.
1289 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001290 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 private void restoreState(Bundle savedState) {
1292 if (savedState == null) {
1293 return;
1294 }
1295
Michael Jurka883f55b2010-10-21 15:47:14 -07001296 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001297 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001298 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001299 }
1300
Adam Cohen21cd0022013-10-09 18:57:02 -07001301 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1302 PagedView.INVALID_RESTORE_PAGE);
1303 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001304 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 }
1306
Winson Chung3d503fb2011-07-13 17:25:49 -07001307 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001308 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001309
Winson Chung3d503fb2011-07-13 17:25:49 -07001310 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1311 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001312 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001313 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1314 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001315 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1316 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1317 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001318 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001319 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320 mRestoring = true;
1321 }
1322
1323 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1324 if (renameFolder) {
1325 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001326 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 mRestoring = true;
1328 }
Winson Chunga12a2502010-12-20 14:41:35 -08001329
Winson Chung785d2eb2011-04-14 16:08:02 -07001330 // Restore the AppsCustomize tab
1331 if (mAppsCustomizeTabHost != null) {
1332 String curTab = savedState.getString("apps_customize_currentTab");
1333 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001334 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001335 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001336 mAppsCustomizeContent.loadAssociatedPages(
1337 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001338 }
1339
Winson Chung5afbf7b2011-07-25 11:53:08 -07001340 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1341 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001342 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001343 mItemIdToViewId = (HashMap<Integer, Integer>)
1344 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346
1347 /**
1348 * Finds all the views we need and configure them properly.
1349 */
1350 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001351 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001352
Craig Mautner360310b2012-10-26 15:13:08 -07001353 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001354 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001355 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1356 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001357 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001358 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001359
John Spurlock77e1f472013-09-11 10:09:51 -04001360 mLauncherView.setSystemUiVisibility(
1361 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001362 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363
Winson Chung4c98d922011-05-31 16:50:48 -07001364 // Setup the drag layer
1365 mDragLayer.setup(this, dragController);
1366
Winson Chung3d503fb2011-07-13 17:25:49 -07001367 // Setup the hotseat
1368 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1369 if (mHotseat != null) {
1370 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001371 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001372 }
1373
Jorim Jaggid017f882014-01-14 17:08:48 -08001374 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001375 View widgetButton = findViewById(R.id.widget_button);
1376 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001377 @Override
1378 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001379 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001380 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001381 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001382 }
1383 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001384 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1385
1386 View wallpaperButton = findViewById(R.id.wallpaper_button);
1387 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001388 @Override
1389 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001390 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001391 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001392 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001393 }
1394 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001395 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1396
1397 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001398 if (hasSettings()) {
1399 settingsButton.setOnClickListener(new OnClickListener() {
1400 @Override
1401 public void onClick(View arg0) {
1402 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001403 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001404 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001405 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001406 });
1407 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1408 } else {
1409 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001410 }
1411
Adam Cohendbdff6b2013-09-18 19:09:15 -07001412 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001413
Winson Chung4c98d922011-05-31 16:50:48 -07001414 // Setup the workspace
1415 mWorkspace.setHapticFeedbackEnabled(false);
1416 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001417 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001418 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001419
Winson Chungf0ea4d32011-06-06 14:27:16 -07001420 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001421 mSearchDropTargetBar = (SearchDropTargetBar)
1422 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001423
Winson Chungf0ea4d32011-06-06 14:27:16 -07001424 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001425 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001426 mAppsCustomizeContent = (AppsCustomizePagedView)
1427 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1428 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001429
Winson Chung3d503fb2011-07-13 17:25:49 -07001430 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001431 dragController.setDragScoller(mWorkspace);
1432 dragController.setScrollView(mDragLayer);
1433 dragController.setMoveTarget(mWorkspace);
1434 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001435 if (mSearchDropTargetBar != null) {
1436 mSearchDropTargetBar.setup(this, dragController);
Sunny Goyal594d76d2014-11-06 10:12:54 -08001437 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001438 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001439
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001440 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001441 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001442 mWeightWatcher = new WeightWatcher(this);
1443 mWeightWatcher.setAlpha(0.5f);
1444 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001445 new FrameLayout.LayoutParams(
1446 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001447 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001448 Gravity.BOTTOM)
1449 );
Adam Cohen39a06042013-07-19 14:30:12 -07001450
1451 boolean show = shouldShowWeightWatcher();
1452 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001453 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 }
1455
1456 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001457 * Sets the all apps button. This method is called from {@link Hotseat}.
1458 */
1459 public void setAllAppsButton(View allAppsButton) {
1460 mAllAppsButton = allAppsButton;
1461 }
1462
1463 public View getAllAppsButton() {
1464 return mAllAppsButton;
1465 }
1466
1467 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 * Creates a view representing a shortcut.
1469 *
1470 * @param info The data structure describing the shortcut.
1471 *
1472 * @return A View inflated from R.layout.application.
1473 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001474 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001476 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 }
1478
1479 /**
1480 * Creates a view representing a shortcut inflated from the specified resource.
1481 *
1482 * @param layoutResId The id of the XML layout used to create the shortcut.
1483 * @param parent The group the shortcut belongs to.
1484 * @param info The data structure describing the shortcut.
1485 *
1486 * @return A View inflated from layoutResId.
1487 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001488 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001489 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001490 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001492 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 return favorite;
1494 }
1495
1496 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 * Add a shortcut to the workspace.
1498 *
1499 * @param data The intent describing the shortcut.
1500 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001502 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001503 int cellY) {
1504 int[] cellXY = mTmpAddItemCellCoordinates;
1505 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001506 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001507
Michael Jurkad3ef3062010-11-23 16:23:58 -08001508 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001509
Sunny Goyal2350bc92014-10-14 16:42:54 -07001510 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001511 if (info == null) {
1512 return;
1513 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001514 final View view = createShortcut(info);
1515
Adam Cohenfbba09b2011-07-18 21:43:05 -07001516 // First we check if we already know the exact location where we want to add this item.
1517 if (cellX >= 0 && cellY >= 0) {
1518 cellXY[0] = cellX;
1519 cellXY[1] = cellY;
1520 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001521
1522 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001523 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001524 true, null,null)) {
1525 return;
1526 }
1527 DragObject dragObject = new DragObject();
1528 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001529 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1530 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001531 return;
1532 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001533 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001534 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001535 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001536 foundCellSpan = (result != null);
1537 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001538 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001539 }
1540
1541 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001542 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001543 return;
1544 }
1545
Adam Cohendcd297f2013-06-18 13:13:40 -07001546 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547
1548 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001549 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001550 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 }
1552 }
1553
Adam Cohen2f093b62012-04-30 18:59:53 -07001554 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1555 int minHeight) {
1556 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001557 // We want to account for the extra amount of padding that we are adding to the widget
1558 // to ensure that it gets the full amount of space that it has requested
1559 int requiredWidth = minWidth + padding.left + padding.right;
1560 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001561 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001562 }
1563
Adam Cohen2f093b62012-04-30 18:59:53 -07001564 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1565 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001566 }
1567
Adam Cohen2f093b62012-04-30 18:59:53 -07001568 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1569 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001570 }
1571
Adam Cohen2f093b62012-04-30 18:59:53 -07001572 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1573 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001574 }
1575
Adam Cohen2f093b62012-04-30 18:59:53 -07001576 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1577 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001578 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001579 }
1580
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001582 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001584 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001585 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001587 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001588 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1589 if (appWidgetInfo == null) {
1590 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1591 }
Romain Guycbb89e42009-06-08 15:52:54 -07001592
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001593 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001594 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001595
Adam Cohen2f093b62012-04-30 18:59:53 -07001596 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1597 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001600 // We have saved the position to which the widget was dragged-- this really only matters
1601 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001602 int[] cellXY = mTmpAddItemCellCoordinates;
1603 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001604 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001605 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001606 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1607 cellXY[0] = mPendingAddInfo.cellX;
1608 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001609 spanXY[0] = mPendingAddInfo.spanX;
1610 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001611 foundCellSpan = true;
1612 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001613 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001614 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001615 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1616 spanXY[1], cellXY, finalSpan);
1617 spanXY[0] = finalSpan[0];
1618 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001619 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001620 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001621 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001622 }
1623
Michael Jurka0280c3b2010-09-17 15:00:07 -07001624 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001625 if (appWidgetId != -1) {
1626 // Deleting an app widget ID is a void call but writes to disk before returning
1627 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001628 new AsyncTask<Void, Void, Void>() {
1629 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001630 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001631 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001632 }
Michael Jurka43467462013-11-14 12:03:58 +01001633 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001634 }
Winson Chung93eef082012-03-23 15:59:27 -07001635 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001636 return;
1637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001639 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001640 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1641 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001642 launcherInfo.spanX = spanXY[0];
1643 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001644 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1645 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001646 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001649 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650
1651 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001652 if (hostView == null) {
1653 // Perform actual inflation because we're live
1654 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1655 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1656 } else {
1657 // The AppWidgetHostView has already been inflated and instantiated
1658 launcherInfo.hostView = hostView;
1659 }
Romain Guycbb89e42009-06-08 15:52:54 -07001660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001662 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001663 launcherInfo.notifyWidgetSizeChanged(this);
1664
Adam Cohendcd297f2013-06-18 13:13:40 -07001665 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001666 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001667
1668 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001670 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 }
Romain Guycbb89e42009-06-08 15:52:54 -07001672
Adam Cohended9f8d2010-11-03 13:25:16 -07001673 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1674 @Override
1675 public void onReceive(Context context, Intent intent) {
1676 final String action = intent.getAction();
1677 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1678 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001679 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001680 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001681
Winson Chungc100e8e2011-08-09 16:02:43 -07001682 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001683 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001684 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001685 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001686 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001687 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1688 mUserPresent = true;
1689 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001690 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1691 mModel.resetLoadedState(false, true);
1692 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1693 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1694 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1695 mModel.resetLoadedState(false, true);
1696 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1697 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1698 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001699 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1700 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1701 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001702 }
1703 }
1704 };
1705
1706 @Override
1707 public void onAttachedToWindow() {
1708 super.onAttachedToWindow();
1709
1710 // Listen for broadcasts related to user-presence
1711 final IntentFilter filter = new IntentFilter();
1712 filter.addAction(Intent.ACTION_SCREEN_OFF);
1713 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001714 // For handling managed profiles
1715 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1716 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001717 if (ENABLE_DEBUG_INTENTS) {
1718 filter.addAction(DebugIntents.DELETE_DATABASE);
1719 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1720 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001722 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001723 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001724 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 mVisible = true;
1726 }
1727
Adam Cohen0b395352014-06-09 22:54:36 +00001728 /**
1729 * Sets up transparent navigation and status bars in LMP.
1730 * This method is a no-op for other platform versions.
1731 */
1732 @TargetApi(19)
1733 private void setupTransparentSystemBarsForLmp() {
1734 // TODO(sansid): use the APIs directly when compiling against L sdk.
1735 // Currently we use reflection to access the flags and the API to set the transparency
1736 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001737 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001738 try {
1739 getWindow().getAttributes().systemUiVisibility |=
1740 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1741 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1742 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1743 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1744 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1745 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1746 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1747 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1748
1749 Method setStatusBarColorMethod =
1750 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1751 Method setNavigationBarColorMethod =
1752 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1753 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1754 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1755 } catch (NoSuchFieldException e) {
1756 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1757 } catch (NoSuchMethodException ex) {
1758 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1759 } catch (IllegalAccessException e) {
1760 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1761 } catch (IllegalArgumentException e) {
1762 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1763 } catch (InvocationTargetException e) {
1764 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1765 } finally {}
1766 }
1767 }
1768
Adam Cohended9f8d2010-11-03 13:25:16 -07001769 @Override
1770 public void onDetachedFromWindow() {
1771 super.onDetachedFromWindow();
1772 mVisible = false;
1773
Adam Cohend113e0c2010-11-11 10:48:05 -08001774 if (mAttached) {
1775 unregisterReceiver(mReceiver);
1776 mAttached = false;
1777 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001778 updateRunning();
1779 }
1780
1781 public void onWindowVisibilityChanged(int visibility) {
1782 mVisible = visibility == View.VISIBLE;
1783 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001784 // The following code used to be in onResume, but it turns out onResume is called when
1785 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1786 // is a more appropriate event to handle
1787 if (mVisible) {
1788 mAppsCustomizeTabHost.onWindowVisible();
1789 if (!mWorkspaceLoading) {
1790 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001791 // We want to let Launcher draw itself at least once before we force it to build
1792 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001793 // apps is nice and speedy.
1794 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001795 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001796 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001797 if (mStarted) return;
1798 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001799 // We delay the layer building a bit in order to give
1800 // other message processing a time to run. In particular
1801 // this avoids a delay in hiding the IME if it was
1802 // currently shown, because doing that may involve
1803 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001804 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001805 final ViewTreeObserver.OnDrawListener listener = this;
1806 mWorkspace.post(new Runnable() {
1807 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001808 if (mWorkspace != null &&
1809 mWorkspace.getViewTreeObserver() != null) {
1810 mWorkspace.getViewTreeObserver().
1811 removeOnDrawListener(listener);
1812 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001813 }
1814 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001815 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001816 }
1817 });
1818 }
1819 clearTypedText();
1820 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001821 }
1822
1823 private void sendAdvanceMessage(long delay) {
1824 mHandler.removeMessages(ADVANCE_MSG);
1825 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1826 mHandler.sendMessageDelayed(msg, delay);
1827 mAutoAdvanceSentTime = System.currentTimeMillis();
1828 }
1829
1830 private void updateRunning() {
1831 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1832 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1833 mAutoAdvanceRunning = autoAdvanceRunning;
1834 if (autoAdvanceRunning) {
1835 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1836 sendAdvanceMessage(delay);
1837 } else {
1838 if (!mWidgetsToAdvance.isEmpty()) {
1839 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1840 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1841 }
1842 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001843 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001844 }
1845 }
1846 }
1847
1848 private final Handler mHandler = new Handler() {
1849 @Override
1850 public void handleMessage(Message msg) {
1851 if (msg.what == ADVANCE_MSG) {
1852 int i = 0;
1853 for (View key: mWidgetsToAdvance.keySet()) {
1854 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1855 final int delay = mAdvanceStagger * i;
1856 if (v instanceof Advanceable) {
1857 postDelayed(new Runnable() {
1858 public void run() {
1859 ((Advanceable) v).advance();
1860 }
1861 }, delay);
1862 }
1863 i++;
1864 }
1865 sendAdvanceMessage(mAdvanceInterval);
1866 }
1867 }
1868 };
1869
1870 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001871 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001872 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1873 if (v instanceof Advanceable) {
1874 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001875 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001876 updateRunning();
1877 }
1878 }
1879
1880 void removeWidgetToAutoAdvance(View hostView) {
1881 if (mWidgetsToAdvance.containsKey(hostView)) {
1882 mWidgetsToAdvance.remove(hostView);
1883 updateRunning();
1884 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001885 }
1886
Joe Onorato9c1289c2009-08-17 11:03:03 -04001887 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001888 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001889 launcherInfo.hostView = null;
1890 }
1891
Winson Chung93eef082012-03-23 15:59:27 -07001892 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1893 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1894 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001895 }
1896
Winson Chunga6945242014-01-08 14:04:34 -08001897 public DragLayer getDragLayer() {
1898 return mDragLayer;
1899 }
1900
1901 public Workspace getWorkspace() {
1902 return mWorkspace;
1903 }
1904
1905 public Hotseat getHotseat() {
1906 return mHotseat;
1907 }
1908
Jorim Jaggid017f882014-01-14 17:08:48 -08001909 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001910 return mOverviewPanel;
1911 }
1912
1913 public SearchDropTargetBar getSearchBar() {
1914 return mSearchDropTargetBar;
1915 }
1916
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001917 public LauncherAppWidgetHost getAppWidgetHost() {
1918 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
Romain Guycbb89e42009-06-08 15:52:54 -07001920
Winson Chunga9abd0e2010-10-27 17:18:37 -07001921 public LauncherModel getModel() {
1922 return mModel;
1923 }
1924
Winson Chunga6945242014-01-08 14:04:34 -08001925 protected SharedPreferences getSharedPrefs() {
1926 return mSharedPrefs;
1927 }
1928
Bjorn Bringertc459e522013-06-07 19:36:01 +01001929 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001930 getWindow().closeAllPanels();
1931
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001932 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001933 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001934 }
1935
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 @Override
1937 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001938 long startTime = 0;
1939 if (DEBUG_RESUME_TIME) {
1940 startTime = System.currentTimeMillis();
1941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 super.onNewIntent(intent);
1943
1944 // Close the menu
1945 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001946 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001947 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001948
Adam Cohened307df2013-10-02 09:37:31 -07001949 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1950 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1951 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001952
Adam Cohen6fecd412013-10-02 17:41:50 -07001953 if (mWorkspace == null) {
1954 // Can be cases where mWorkspace is null, this prevents a NPE
1955 return;
1956 }
1957 Folder openFolder = mWorkspace.getOpenFolder();
1958 // In all these cases, only animate if we're already on home
1959 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001960
1961 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1962 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001963 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001964 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001965 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001966 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001967
Adam Cohen6fecd412013-10-02 17:41:50 -07001968 closeFolder();
1969 exitSpringLoadedDragMode();
1970
1971 // If we are already on home, then just animate back to the workspace,
1972 // otherwise, just wait until onResume to set the state back to Workspace
1973 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001974 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001975 } else {
1976 mOnResumeState = State.WORKSPACE;
1977 }
1978
1979 final View v = getWindow().peekDecorView();
1980 if (v != null && v.getWindowToken() != null) {
1981 InputMethodManager imm = (InputMethodManager)getSystemService(
1982 INPUT_METHOD_SERVICE);
1983 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1984 }
1985
1986 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001987 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001988 mAppsCustomizeTabHost.reset();
1989 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001990
Adam Cohen9211d422014-10-07 18:14:53 -07001991 if (mLauncherCallbacks != null) {
1992 mLauncherCallbacks.onHomeIntent();
1993 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 }
Adam Cohened307df2013-10-02 09:37:31 -07001995
Michael Jurka447bf842013-05-15 14:52:15 +02001996 if (DEBUG_RESUME_TIME) {
1997 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1998 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999
Adam Cohen9211d422014-10-07 18:14:53 -07002000 if (mLauncherCallbacks != null) {
2001 mLauncherCallbacks.onNewIntent(intent);
2002 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002003 }
2004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07002006 public void onRestoreInstanceState(Bundle state) {
2007 super.onRestoreInstanceState(state);
2008 for (int page: mSynchronouslyBoundPages) {
2009 mWorkspace.restoreInstanceStateForChild(page);
2010 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 }
2012
2013 @Override
2014 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002015 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002016 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2017 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002018 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002019 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020
Michael Jurka883f55b2010-10-21 15:47:14 -07002021 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002022 // We close any open folder since it will not be re-opened, and we need to make sure
2023 // this state is reflected.
2024 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025
Adam Cohendcd297f2013-06-18 13:13:40 -07002026 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002027 mWaitingForResult) {
2028 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002029 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002030 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2031 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002032 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2033 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2034 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002035 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 }
2037
2038 if (mFolderInfo != null && mWaitingForResult) {
2039 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2040 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2041 }
Michael Jurka946ad472010-07-09 18:05:18 -07002042
Winson Chung785d2eb2011-04-14 16:08:02 -07002043 // Save the current AppsCustomize tab
2044 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08002045 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2046 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002047 if (currentTabTag != null) {
2048 outState.putString("apps_customize_currentTab", currentTabTag);
2049 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002050 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2051 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002052 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002053 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002054
2055 if (mLauncherCallbacks != null) {
2056 mLauncherCallbacks.onSaveInstanceState(outState);
2057 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 }
2059
2060 @Override
2061 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002063
Winson Chunge7a03942011-08-05 15:05:12 -07002064 // Remove all pending runnables
2065 mHandler.removeMessages(ADVANCE_MSG);
2066 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002067 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002068
Winson Chungcd2b0142011-06-08 16:02:26 -07002069 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002070 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002071
2072 // It's possible to receive onDestroy after a new Launcher activity has
2073 // been created. In this case, don't interfere with the new Launcher.
2074 if (mModel.isCurrentCallbacks(this)) {
2075 mModel.stopLoader();
2076 app.setLauncher(null);
2077 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002078
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002080 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002082 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002084 mAppWidgetHost = null;
2085
2086 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087
2088 TextKeyListener.getInstance().release();
2089
Winson Chung81b52252012-08-27 15:34:29 -07002090 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2091 // to prevent leaking Launcher activities on orientation change.
2092 if (mModel != null) {
2093 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2094 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002095
2096 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002097 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002098
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002099 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002100 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002101 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002102 mWorkspace = null;
2103 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002104
Michael Jurka2ecf9952012-06-18 12:52:28 -07002105 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002106
2107 if (mLauncherCallbacks != null) {
2108 mLauncherCallbacks.onDestroy();
2109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 }
2111
Adam Cohena9cf38f2011-05-02 15:36:58 -07002112 public DragController getDragController() {
2113 return mDragController;
2114 }
2115
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 @Override
2117 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002118 if (requestCode >= 0) {
2119 setWaitingForResult(true);
2120 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 super.startActivityForResult(intent, requestCode);
2122 }
2123
Winson Chung70d72102011-08-12 11:24:35 -07002124 /**
2125 * Indicates that we want global search for this activity by setting the globalSearch
2126 * argument for {@link #startSearch} to true.
2127 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002129 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002131
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002132 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002133
Karl Rosaen138a0412009-04-23 19:00:21 -07002134 if (initialQuery == null) {
2135 // Use any text typed in the launcher as the initial query
2136 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 if (appSearchData == null) {
2139 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002140 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 }
Winson Chungadf0c182012-08-23 14:59:07 -07002142 Rect sourceBounds = new Rect();
2143 if (mSearchDropTargetBar != null) {
2144 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2145 }
Romain Guycbb89e42009-06-08 15:52:54 -07002146
Ian Parkinson047036e2014-09-01 15:40:53 +01002147 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002148 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002149 if (clearTextImmediately) {
2150 clearTypedText();
2151 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002152 }
2153
Ian Parkinson047036e2014-09-01 15:40:53 +01002154 /**
2155 * Start a text search.
2156 *
2157 * @return {@code true} if the search will start immediately, so any further keypresses
2158 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2159 * to buffer keypresses.
2160 */
2161 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002162 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002163 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2164 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2165 sourceBounds);
2166 }
2167
Michael Jurkaa33411c2012-06-14 16:18:21 -07002168 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002169 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002170 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002171 }
2172
2173 /**
2174 * Starts the global search activity. This code is a copied from SearchManager
2175 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002176 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002177 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002178 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002179 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2180 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2181 if (globalSearchActivity == null) {
2182 Log.w(TAG, "No global search activity found.");
2183 return;
2184 }
2185 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2186 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2187 intent.setComponent(globalSearchActivity);
2188 // Make sure that we have a Bundle to put source in
2189 if (appSearchData == null) {
2190 appSearchData = new Bundle();
2191 } else {
2192 appSearchData = new Bundle(appSearchData);
2193 }
Adam Cohen9211d422014-10-07 18:14:53 -07002194 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002195 if (!appSearchData.containsKey("source")) {
2196 appSearchData.putString("source", getPackageName());
2197 }
2198 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2199 if (!TextUtils.isEmpty(initialQuery)) {
2200 intent.putExtra(SearchManager.QUERY, initialQuery);
2201 }
2202 if (selectInitialQuery) {
2203 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2204 }
2205 intent.setSourceBounds(sourceBounds);
2206 try {
2207 startActivity(intent);
2208 } catch (ActivityNotFoundException ex) {
2209 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211 }
2212
Yura4f93ec62014-02-04 14:15:21 +00002213 public boolean isOnCustomContent() {
2214 return mWorkspace.isOnOrMovingToCustomContent();
2215 }
2216
Winson Chung70d72102011-08-12 11:24:35 -07002217 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002218 public boolean onPrepareOptionsMenu(Menu menu) {
2219 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002220 if (!isOnCustomContent()) {
2221 // Close any open folders
2222 closeFolder();
2223 // Stop resizing any widgets
2224 mWorkspace.exitWidgetResizeMode();
2225 if (!mWorkspace.isInOverviewMode()) {
2226 // Show the overview mode
2227 showOverviewMode(true);
2228 } else {
2229 showWorkspace(true);
2230 }
Winson Chunge0298742014-01-17 12:03:00 -08002231 }
Adam Cohen9211d422014-10-07 18:14:53 -07002232 if (mLauncherCallbacks != null) {
2233 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2234 }
2235
Michael Jurkab94f3f82013-09-11 18:08:54 +02002236 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002237 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002239 @Override
2240 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002241 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002242 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002243 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002244 }
2245
Joe Onorato9c1289c2009-08-17 11:03:03 -04002246 public boolean isWorkspaceLocked() {
2247 return mWorkspaceLoading || mWaitingForResult;
2248 }
2249
Adam Cohen517a7f52014-03-01 12:12:59 -08002250 public boolean isWorkspaceLoading() {
2251 return mWorkspaceLoading;
2252 }
2253
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002254 private void setWorkspaceLoading(boolean value) {
2255 boolean isLocked = isWorkspaceLocked();
2256 mWorkspaceLoading = value;
2257 if (isLocked != isWorkspaceLocked()) {
2258 onWorkspaceLockedChanged();
2259 }
2260 }
2261
2262 private void setWaitingForResult(boolean value) {
2263 boolean isLocked = isWorkspaceLocked();
2264 mWaitingForResult = value;
2265 if (isLocked != isWorkspaceLocked()) {
2266 onWorkspaceLockedChanged();
2267 }
2268 }
2269
Adam Cohen9211d422014-10-07 18:14:53 -07002270 protected void onWorkspaceLockedChanged() {
2271 if (mLauncherCallbacks != null) {
2272 mLauncherCallbacks.onWorkspaceLockedChanged();
2273 }
2274 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002275
Michael Jurka0280c3b2010-09-17 15:00:07 -07002276 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002277 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002278 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002279 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2280 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002281 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002282 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002283 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002284
Adam Cohenad4e15c2013-10-17 16:21:35 -07002285 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2286 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2287 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2288 }
2289
2290 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2291 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2292 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002293 if (appWidgetInfo.configure != null) {
2294 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002295 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002296
Bjorn Bringert7984c942009-12-09 15:38:25 +00002297 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002298 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2299 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002302 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002303 Runnable onComplete = new Runnable() {
2304 @Override
2305 public void run() {
2306 // Exit spring loaded mode if necessary after adding the widget
2307 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2308 null);
2309 }
2310 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002311 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002312 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002313 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002314 }
2315 }
Romain Guycbb89e42009-06-08 15:52:54 -07002316
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002317 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002318 // Close any folders that may be open.
2319 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002320 mWorkspace.moveToCustomContentScreen(animate);
2321 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002322 /**
2323 * Process a shortcut drop.
2324 *
2325 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002326 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002327 * @param cell The cell it should be added to, optional
2328 * @param position The location on the screen where it was dropped, optional
2329 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002330 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002331 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002332 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002334 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002335 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002336
2337 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002338 mPendingAddInfo.cellX = cell[0];
2339 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002340 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002341
2342 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2343 createShortcutIntent.setComponent(componentName);
2344 processShortcut(createShortcutIntent);
2345 }
2346
Adam Cohenfbba09b2011-07-18 21:43:05 -07002347 /**
2348 * Process a widget drop.
2349 *
2350 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002351 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002352 * @param cell The cell it should be added to, optional
2353 * @param position The location on the screen where it was dropped, optional
2354 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002355 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002356 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002357 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002358 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002359 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002360 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002361 mPendingAddInfo.minSpanX = info.minSpanX;
2362 mPendingAddInfo.minSpanY = info.minSpanY;
2363
Adam Cohenfbba09b2011-07-18 21:43:05 -07002364 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002365 mPendingAddInfo.cellX = cell[0];
2366 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002367 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002368 if (span != null) {
2369 mPendingAddInfo.spanX = span[0];
2370 mPendingAddInfo.spanY = span[1];
2371 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002372
Adam Cohened66b2b2012-01-23 17:28:51 -08002373 AppWidgetHostView hostView = info.boundWidget;
2374 int appWidgetId;
2375 if (hostView != null) {
2376 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002377 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002378 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002379 // In this case, we either need to start an activity to get permission to bind
2380 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002381 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002382 Bundle options = info.bindOptions;
2383
Sunny Goyalffe83f12014-08-14 17:39:34 -07002384 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2385 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002386 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002387 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002388 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002389 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002390 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2391 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2392 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002393 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2394 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002395 // TODO: we need to make sure that this accounts for the options bundle.
2396 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002397 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2398 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002399 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002400 }
2401
Joe Onoratodeb98af2010-02-19 14:59:39 -08002402 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002403 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 }
2405
Winson Chung24ab2f12010-09-16 14:10:47 -07002406 void processWallpaper(Intent intent) {
2407 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2408 }
2409
Adam Cohendcd297f2013-06-18 13:13:40 -07002410 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002411 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002412 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 folderInfo.title = getText(R.string.folder_name);
2414
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002416 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002417 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002418 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419
2420 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002421 FolderIcon newFolder =
2422 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002423 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002424 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002425 // Force measure the new folder icon
2426 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2427 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002428 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 }
Romain Guycbb89e42009-06-08 15:52:54 -07002430
Joe Onorato9c1289c2009-08-17 11:03:03 -04002431 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002432 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002433 }
2434
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002435 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002436 if (mLauncherCallbacks != null) {
2437 return mLauncherCallbacks.getWallpaperPickerComponent();
2438 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002439 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002440 }
2441
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002442 /**
2443 * Registers various content observers. The current implementation registers
2444 * only a favorites observer to keep track of the favorites applications.
2445 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002446 private void registerContentObservers() {
2447 ContentResolver resolver = getContentResolver();
2448 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2449 true, mWidgetObserver);
2450 }
2451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 @Override
2453 public boolean dispatchKeyEvent(KeyEvent event) {
2454 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2455 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002456 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002457 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002458 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002459 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002460 dumpState();
2461 return true;
2462 }
2463 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002464 }
2465 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2466 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002467 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 return true;
2469 }
2470 }
2471
2472 return super.dispatchKeyEvent(event);
2473 }
2474
Joe Onorato88ec0992009-11-19 13:16:06 -08002475 @Override
2476 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002477 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2478 return;
2479 }
2480
Winson Chungc93e5ae2012-07-23 20:48:26 -07002481 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002482 if (mAppsCustomizeContent.getContentType() ==
2483 AppsCustomizePagedView.ContentType.Applications) {
2484 showWorkspace(true);
2485 } else {
2486 showOverviewMode(true);
2487 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002488 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002489 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002490 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002491 Folder openFolder = mWorkspace.getOpenFolder();
2492 if (openFolder.isEditingName()) {
2493 openFolder.dismissEditingName();
2494 } else {
2495 closeFolder();
2496 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002497 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002498 mWorkspace.exitWidgetResizeMode();
2499
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002500 // Back button is a no-op here, but give at least some feedback for the button press
2501 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002502 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002503 }
2504
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002506 * Re-listen when widgets are reset.
2507 */
2508 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002509 if (mAppWidgetHost != null) {
2510 mAppWidgetHost.startListening();
2511 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002512 }
2513
2514 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 * Launches the intent referred by the clicked shortcut.
2516 *
2517 * @param v The view representing the clicked shortcut.
2518 */
2519 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002520 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2521 // view has detached (it's possible for this to happen if the view is removed mid touch).
2522 if (v.getWindowToken() == null) {
2523 return;
2524 }
2525
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002526 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002527 return;
2528 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002529
Adam Cohen1697b792013-09-17 19:08:21 -07002530 if (v instanceof Workspace) {
2531 if (mWorkspace.isInOverviewMode()) {
2532 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002533 }
2534 return;
2535 }
2536
2537 if (v instanceof CellLayout) {
2538 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002539 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002540 }
2541 }
2542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002544 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002545 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002546 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002547 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002548 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002549 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002550 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002551 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002552 } else if (tag instanceof AppInfo) {
2553 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002554 } else if (tag instanceof LauncherAppWidgetInfo) {
2555 if (v instanceof PendingAppWidgetHostView) {
2556 onClickPendingWidget((PendingAppWidgetHostView) v);
2557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 }
2559 }
2560
Sunny Goyal508da152014-08-14 10:53:27 -07002561 public void onClickPagedViewIcon(View v) {
2562 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002563 if (mLauncherCallbacks != null) {
2564 mLauncherCallbacks.onClickPagedViewIcon(v);
2565 }
Sunny Goyal508da152014-08-14 10:53:27 -07002566 }
2567
Michael Jurka0e260592010-06-30 17:07:39 -07002568 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002569 return false;
2570 }
2571
Michael Jurkaaf442092010-06-10 17:01:57 -07002572 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002573 * Event handler for the app widget view which has not fully restored.
2574 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002575 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002576 if (mIsSafeModeEnabled) {
2577 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2578 return;
2579 }
2580
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002581 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002582 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002583 int widgetId = info.appWidgetId;
2584 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2585 if (appWidgetInfo != null) {
2586 mPendingAddWidgetInfo = appWidgetInfo;
2587 mPendingAddInfo.copyFrom(info);
2588 mPendingAddWidgetId = widgetId;
2589
Sunny Goyalffe83f12014-08-14 17:39:34 -07002590 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2591 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002592 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002593 } else if (info.installProgress < 0) {
2594 // The install has not been queued
2595 final String packageName = info.providerName.getPackageName();
2596 showBrokenAppInstallDialog(packageName,
2597 new DialogInterface.OnClickListener() {
2598 public void onClick(DialogInterface dialog, int id) {
2599 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2600 }
2601 });
2602 } else {
2603 // Download has started.
2604 final String packageName = info.providerName.getPackageName();
2605 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002606 }
2607 }
2608
2609 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002610 * Event handler for the "grid" button that appears on the home screen, which
2611 * enters all apps mode.
2612 *
2613 * @param v The view that was clicked.
2614 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002615 protected void onClickAllAppsButton(View v) {
2616 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2617 if (isAllAppsVisible()) {
2618 showWorkspace(true);
2619 } else {
2620 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2621 }
Adam Cohen9211d422014-10-07 18:14:53 -07002622 if (mLauncherCallbacks != null) {
2623 mLauncherCallbacks.onClickAllAppsButton(v);
2624 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 }
2626
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002627 private void showBrokenAppInstallDialog(final String packageName,
2628 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002629 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002630 .setTitle(R.string.abandoned_promises_title)
2631 .setMessage(R.string.abandoned_promise_explanation)
2632 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2633 .setNeutralButton(R.string.abandoned_clean_this,
2634 new DialogInterface.OnClickListener() {
2635 public void onClick(DialogInterface dialog, int id) {
2636 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2637 mWorkspace.removeAbandonedPromise(packageName, user);
2638 }
2639 })
2640 .create().show();
2641 return;
2642 }
2643
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002644 /**
2645 * Event handler for an app shortcut click.
2646 *
2647 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2648 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002649 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002650 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2651 Object tag = v.getTag();
2652 if (!(tag instanceof ShortcutInfo)) {
2653 throw new IllegalArgumentException("Input must be a Shortcut");
2654 }
2655
2656 // Open shortcut
2657 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002658
2659 if (shortcut.isDisabled != 0) {
2660 int error = R.string.activity_not_available;
2661 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2662 error = R.string.safemode_shortcut_error;
2663 }
2664 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2665 return;
2666 }
2667
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002668 final Intent intent = shortcut.intent;
2669
2670 // Check for special shortcuts
2671 if (intent.getComponent() != null) {
2672 final String shortcutClass = intent.getComponent().getClassName();
2673
2674 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2675 MemoryDumpActivity.startDump(this);
2676 return;
2677 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2678 toggleShowWeightWatcher();
2679 return;
2680 }
2681 }
2682
Chris Wren40c5ed32014-06-24 18:24:23 -04002683 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002684 if ((v instanceof BubbleTextView)
2685 && shortcut.isPromise()
2686 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002687 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002688 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002689 new DialogInterface.OnClickListener() {
2690 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002691 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002692 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002693 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002694 return;
2695 }
2696
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002697 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002698 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002699
2700 if (mLauncherCallbacks != null) {
2701 mLauncherCallbacks.onClickAppShortcut(v);
2702 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002703 }
2704
Sunny Goyal508da152014-08-14 10:53:27 -07002705 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002706 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002707 final ShortcutInfo shortcut;
2708 final Intent intent;
2709 if (tag instanceof ShortcutInfo) {
2710 shortcut = (ShortcutInfo) tag;
2711 intent = shortcut.intent;
2712 int[] pos = new int[2];
2713 v.getLocationOnScreen(pos);
2714 intent.setSourceBounds(new Rect(pos[0], pos[1],
2715 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002716
Sunny Goyal508da152014-08-14 10:53:27 -07002717 } else if (tag instanceof AppInfo) {
2718 shortcut = null;
2719 intent = ((AppInfo) tag).intent;
2720 } else {
2721 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2722 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002723
2724 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002725 mStats.recordLaunch(intent, shortcut);
2726
2727 if (success && v instanceof BubbleTextView) {
2728 mWaitingForResume = (BubbleTextView) v;
2729 mWaitingForResume.setStayPressed(true);
2730 }
2731 }
2732
2733 /**
2734 * Event handler for a folder icon click.
2735 *
2736 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2737 */
2738 protected void onClickFolderIcon(View v) {
2739 if (LOGD) Log.d(TAG, "onClickFolder");
2740 if (!(v instanceof FolderIcon)){
2741 throw new IllegalArgumentException("Input must be a FolderIcon");
2742 }
2743
2744 FolderIcon folderIcon = (FolderIcon) v;
2745 final FolderInfo info = folderIcon.getFolderInfo();
2746 Folder openFolder = mWorkspace.getFolderForTag(info);
2747
2748 // If the folder info reports that the associated folder is open, then verify that
2749 // it is actually opened. There have been a few instances where this gets out of sync.
2750 if (info.opened && openFolder == null) {
2751 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2752 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2753 info.opened = false;
2754 }
2755
2756 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2757 // Close any open folder
2758 closeFolder();
2759 // Open the requested folder
2760 openFolder(folderIcon);
2761 } else {
2762 // Find the open folder...
2763 int folderScreen;
2764 if (openFolder != null) {
2765 folderScreen = mWorkspace.getPageForView(openFolder);
2766 // .. and close it
2767 closeFolder(openFolder);
2768 if (folderScreen != mWorkspace.getCurrentPage()) {
2769 // Close any folder open on the current screen
2770 closeFolder();
2771 // Pull the folder onto this screen
2772 openFolder(folderIcon);
2773 }
2774 }
2775 }
Adam Cohen9211d422014-10-07 18:14:53 -07002776
2777 if (mLauncherCallbacks != null) {
2778 mLauncherCallbacks.onClickFolderIcon(v);
2779 }
Michael Jurka5130e402011-10-13 04:55:35 -07002780 }
2781
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002782 /**
2783 * Event handler for the (Add) Widgets button that appears after a long press
2784 * on the home screen.
2785 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002786 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002787 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002788 if (mIsSafeModeEnabled) {
2789 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2790 } else {
2791 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2792 if (mLauncherCallbacks != null) {
2793 mLauncherCallbacks.onClickAddWidgetButton(view);
2794 }
Adam Cohen9211d422014-10-07 18:14:53 -07002795 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002796 }
2797
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002798 /**
2799 * Event handler for the wallpaper picker button that appears after a long press
2800 * on the home screen.
2801 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002802 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002803 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2804 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2805 pickWallpaper.setComponent(getWallpaperPickerComponent());
2806 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002807
2808 if (mLauncherCallbacks != null) {
2809 mLauncherCallbacks.onClickWallpaperPicker(v);
2810 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002811 }
2812
2813 /**
2814 * Event handler for a click on the settings button that appears after a long press
2815 * on the home screen.
2816 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002817 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002818 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002819 if (mLauncherCallbacks != null) {
2820 mLauncherCallbacks.onClickSettingsButton(v);
2821 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002822 }
2823
Michael Jurka5130e402011-10-13 04:55:35 -07002824 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002825 // Provide the same haptic feedback that the system offers for virtual keys.
2826 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002827 }
2828
Adam Cohen61f560d2013-09-30 15:58:20 -07002829 public void performHapticFeedbackOnTouchDown(View v) {
2830 // Provide the same haptic feedback that the system offers for virtual keys.
2831 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2832 }
2833
2834 public View.OnTouchListener getHapticFeedbackTouchListener() {
2835 if (mHapticFeedbackTouchListener == null) {
2836 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2837 @Override
2838 public boolean onTouch(View v, MotionEvent event) {
2839 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2840 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2841 }
2842 return false;
2843 }
2844 };
2845 }
2846 return mHapticFeedbackTouchListener;
2847 }
2848
Adam Cohen9211d422014-10-07 18:14:53 -07002849 public void onDragStarted(View view) {
2850 if (isOnCustomContent()) {
2851 // Custom content screen doesn't participate in drag and drop. If on custom
2852 // content screen, move to default.
2853 moveWorkspaceToDefaultScreen();
2854 }
2855
2856 if (mLauncherCallbacks != null) {
2857 mLauncherCallbacks.onDragStarted(view);
2858 }
2859 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002860
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002861 /**
2862 * Called when the user stops interacting with the launcher.
2863 * This implies that the user is now on the homescreen and is not doing housekeeping.
2864 */
Adam Cohen9211d422014-10-07 18:14:53 -07002865 protected void onInteractionEnd() {
2866 if (mLauncherCallbacks != null) {
2867 mLauncherCallbacks.onInteractionEnd();
2868 }
2869 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002870
2871 /**
2872 * Called when the user starts interacting with the launcher.
2873 * The possible interactions are:
2874 * - open all apps
2875 * - reorder an app shortcut, or a widget
2876 * - open the overview mode.
2877 * This is a good time to stop doing things that only make sense
2878 * when the user is on the homescreen and not doing housekeeping.
2879 */
Adam Cohen9211d422014-10-07 18:14:53 -07002880 protected void onInteractionBegin() {
2881 if (mLauncherCallbacks != null) {
2882 mLauncherCallbacks.onInteractionBegin();
2883 }
2884 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002885
Kenny Guyf07af7b2014-07-31 11:39:16 +01002886 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002887 try {
2888 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002889 launcherApps.showAppDetailsForProfile(componentName, user);
2890 } catch (SecurityException e) {
2891 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2892 Log.e(TAG, "Launcher does not have permission to launch settings");
2893 } catch (ActivityNotFoundException e) {
2894 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2895 Log.e(TAG, "Unable to launch settings");
2896 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002897 }
2898
Michael Jurka1e2f4652013-07-08 18:03:46 -07002899 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002900 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2901 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002902 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002903 // System applications cannot be installed. For now, show a toast explaining that.
2904 // We may give them the option of disabling apps this way.
2905 int messageId = R.string.uninstall_system_app_text;
2906 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002907 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002908 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002909 String packageName = componentName.getPackageName();
2910 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002911 Intent intent = new Intent(
2912 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002913 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2914 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002915 if (user != null) {
2916 user.addToIntent(intent, Intent.EXTRA_USER);
2917 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002918 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002919 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002920 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002921 }
2922
Michael Jurka86a720e2012-05-09 11:23:23 -07002923 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002924 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002925 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002926 // Only launch using the new animation if the shortcut has not opted out (this is a
2927 // private contract between launcher and may be ignored in the future).
2928 boolean useLaunchAnimation = (v != null) &&
2929 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002930 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2931 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002932
Kenny Guy1317e2d2014-05-08 18:52:50 +01002933 UserHandleCompat user = null;
2934 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2935 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2936 user = userManager.getUserForSerialNumber(serialNumber);
2937 }
2938
2939 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002940 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002941 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002942 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2943 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002944 optsBundle = opts.toBundle();
2945 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002946
2947 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2948 // Could be launching some bookkeeping activity
2949 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002950 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002951 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002952 launcherApps.startActivityForProfile(intent.getComponent(), user,
2953 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002954 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002955 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 } catch (SecurityException e) {
2957 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002958 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002960 "or use the exported attribute for this activity. "
2961 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002963 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002964 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002965
Michael Jurka86a720e2012-05-09 11:23:23 -07002966 boolean startActivitySafely(View v, Intent intent, Object tag) {
2967 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002968 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2969 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2970 return false;
2971 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002972 try {
2973 success = startActivity(v, intent, tag);
2974 } catch (ActivityNotFoundException e) {
2975 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2976 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2977 }
2978 return success;
2979 }
2980
Adam Cohen268c4752012-06-06 17:47:33 -07002981 /**
2982 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2983 * in the DragLayer in the exact absolute location of the original FolderIcon.
2984 */
2985 private void copyFolderIconToImage(FolderIcon fi) {
2986 final int width = fi.getMeasuredWidth();
2987 final int height = fi.getMeasuredHeight();
2988
2989 // Lazy load ImageView, Bitmap and Canvas
2990 if (mFolderIconImageView == null) {
2991 mFolderIconImageView = new ImageView(this);
2992 }
2993 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2994 mFolderIconBitmap.getHeight() != height) {
2995 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2996 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2997 }
2998
2999 DragLayer.LayoutParams lp;
3000 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3001 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3002 } else {
3003 lp = new DragLayer.LayoutParams(width, height);
3004 }
3005
Adam Cohen307fe232012-08-16 17:55:58 -07003006 // The layout from which the folder is being opened may be scaled, adjust the starting
3007 // view size by this scale factor.
3008 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003009 lp.customPosition = true;
3010 lp.x = mRectForFolderAnimation.left;
3011 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003012 lp.width = (int) (scale * width);
3013 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003014
3015 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3016 fi.draw(mFolderIconCanvas);
3017 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003018 if (fi.getFolder() != null) {
3019 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3020 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003021 }
Adam Cohen268c4752012-06-06 17:47:33 -07003022 // Just in case this image view is still in the drag layer from a previous animation,
3023 // we remove it and re-add it.
3024 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3025 mDragLayer.removeView(mFolderIconImageView);
3026 }
3027 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003028 if (fi.getFolder() != null) {
3029 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003030 }
Adam Cohen268c4752012-06-06 17:47:33 -07003031 }
3032
Adam Cohen2801caf2011-05-13 20:57:39 -07003033 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003034 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003035 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3036 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3037 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3038
Adam Cohenc51934b2011-07-26 21:07:43 -07003039 FolderInfo info = (FolderInfo) fi.getTag();
3040 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3041 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003042 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3043 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003044 }
3045
Adam Cohen268c4752012-06-06 17:47:33 -07003046 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3047 copyFolderIconToImage(fi);
3048 fi.setVisibility(View.INVISIBLE);
3049
Michael Jurka2ecf9952012-06-18 12:52:28 -07003050 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003051 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003052 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003053 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3054 }
3055 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003056 oa.start();
3057 }
3058
Adam Cohen268c4752012-06-06 17:47:33 -07003059 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003060 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003061 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3062 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3063 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3064
Adam Cohen268c4752012-06-06 17:47:33 -07003065 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003066
Adam Cohen268c4752012-06-06 17:47:33 -07003067 // We remove and re-draw the FolderIcon in-case it has changed
3068 mDragLayer.removeView(mFolderIconImageView);
3069 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003070 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003071 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003072 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003073 oa.addListener(new AnimatorListenerAdapter() {
3074 @Override
3075 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003076 if (cl != null) {
3077 cl.clearFolderLeaveBehind();
3078 // Remove the ImageView copy of the FolderIcon and make the original visible.
3079 mDragLayer.removeView(mFolderIconImageView);
3080 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003081 }
3082 }
3083 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003084 oa.start();
3085 }
3086
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003087 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003088 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003089 * is animated relative to the specified View. If the View is null, no animation
3090 * is played.
3091 *
3092 * @param folderInfo The FolderInfo describing the folder to open.
3093 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003094 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003095 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003096 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003097
Adam Cohena9cf38f2011-05-02 15:36:58 -07003098 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099
Adam Cohen4554ee12011-08-03 16:13:21 -07003100 // Just verify that the folder hasn't already been added to the DragLayer.
3101 // There was a one-off crash where the folder had a parent already.
3102 if (folder.getParent() == null) {
3103 mDragLayer.addView(folder);
3104 mDragController.addDropTarget((DropTarget) folder);
3105 } else {
3106 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3107 folder.getParent() + ").");
3108 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003109 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003110 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003111
3112 // Notify the accessibility manager that this folder "window" has appeared and occluded
3113 // the workspace items
3114 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3115 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003116 }
3117
3118 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003119 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003120 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003121 if (folder.isEditingName()) {
3122 folder.dismissEditingName();
3123 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003124 closeFolder(folder);
3125 }
3126 }
3127
3128 void closeFolder(Folder folder) {
3129 folder.getInfo().opened = false;
3130
3131 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3132 if (parent != null) {
3133 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3134 shrinkAndFadeInFolderIcon(fi);
3135 }
3136 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003137
3138 // Notify the accessibility manager that this folder "window" has disappeard and no
3139 // longer occludeds the workspace items
3140 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003141 }
3142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003144 if (!isDraggingEnabled()) return false;
3145 if (isWorkspaceLocked()) return false;
3146 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147
Adam Cohen1697b792013-09-17 19:08:21 -07003148 if (v instanceof Workspace) {
3149 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003150 if (mWorkspace.enterOverviewMode()) {
3151 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3152 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3153 return true;
3154 } else {
3155 return false;
3156 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003157 } else {
3158 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003159 }
Adam Cohen1697b792013-09-17 19:08:21 -07003160 }
3161
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003162 CellLayout.CellInfo longClickCellInfo = null;
3163 View itemUnderLongClick = null;
3164 if (v.getTag() instanceof ItemInfo) {
3165 ItemInfo info = (ItemInfo) v.getTag();
3166 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3167 itemUnderLongClick = longClickCellInfo.cell;
3168 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003169 }
3170
Winson Chung3d503fb2011-07-13 17:25:49 -07003171 // The hotseat touch handling does not go through Workspace, and we always allow long press
3172 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003173 final boolean inHotseat = isHotseatLayout(v);
3174 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003175 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003176 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003177 // User long pressed on empty space
3178 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3179 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003180 if (mWorkspace.isInOverviewMode()) {
3181 mWorkspace.startReordering(v);
3182 } else {
3183 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003184 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003185 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003186 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3187 mHotseat.getOrderInHotseat(
3188 longClickCellInfo.cellX,
3189 longClickCellInfo.cellY));
3190 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003191 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003192 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003193 }
3194 }
3195 }
3196 return true;
3197 }
3198
Winson Chung3d503fb2011-07-13 17:25:49 -07003199 boolean isHotseatLayout(View layout) {
3200 return mHotseat != null && layout != null &&
3201 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3202 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003203
Winson Chung3d503fb2011-07-13 17:25:49 -07003204 /**
3205 * Returns the CellLayout of the specified container at the specified screen.
3206 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003207 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003208 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3209 if (mHotseat != null) {
3210 return mHotseat.getLayout();
3211 } else {
3212 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003213 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003214 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003215 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003216 }
3217 }
3218
Daniel Sandler843e8602010-06-07 14:59:01 -04003219 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003220 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003221 }
3222
Craig Mautner360310b2012-10-26 15:13:08 -07003223 private void setWorkspaceBackground(boolean workspace) {
3224 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003225 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003226 }
3227
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003228 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003229 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3230 int curflags = getWindow().getAttributes().flags
3231 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3232 if (wpflags != curflags) {
3233 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3234 }
Craig Mautner360310b2012-10-26 15:13:08 -07003235 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003236 }
3237
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003238 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3239 if (v instanceof LauncherTransitionable) {
3240 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3241 }
3242 }
3243
Michael Jurkabed61d22012-02-14 22:51:29 -08003244 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3245 if (v instanceof LauncherTransitionable) {
3246 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3247 }
Winson Chung70442722012-02-10 15:43:22 -08003248
3249 // Update the workspace transition step as well
3250 dispatchOnLauncherTransitionStep(v, 0f);
3251 }
3252
3253 private void dispatchOnLauncherTransitionStep(View v, float t) {
3254 if (v instanceof LauncherTransitionable) {
3255 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3256 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003257 }
3258
3259 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3260 if (v instanceof LauncherTransitionable) {
3261 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3262 }
Winson Chung70442722012-02-10 15:43:22 -08003263
3264 // Update the workspace transition step as well
3265 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003266 }
3267
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003268 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003269 * Things to test when changing the following seven functions.
3270 * - Home from workspace
3271 * - from center screen
3272 * - from other screens
3273 * - Home from all apps
3274 * - from center screen
3275 * - from other screens
3276 * - Back from all apps
3277 * - from center screen
3278 * - from other screens
3279 * - Launch app from workspace and quit
3280 * - with back
3281 * - with home
3282 * - Launch app from all apps and quit
3283 * - with back
3284 * - with home
3285 * - Go to a screen that's not the default, then all
3286 * apps, and launch and app, and go back
3287 * - with back
3288 * -with home
3289 * - On workspace, long press power and go back
3290 * - with back
3291 * - with home
3292 * - On all apps, long press power and go back
3293 * - with back
3294 * - with home
3295 * - On workspace, power off
3296 * - On all apps, power off
3297 * - Launch an app and turn off the screen while in that app
3298 * - Go back with home key
3299 * - Go back with back key TODO: make this not go to workspace
3300 * - From all apps
3301 * - From workspace
3302 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3303 * - From all apps
3304 * - From the center workspace
3305 * - From another workspace
3306 */
3307
3308 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003309 * Zoom the camera out from the workspace to reveal 'toView'.
3310 * Assumes that the view to show is anchored at either the very top or very bottom
3311 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003312 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003313 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003314 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3315 showAppsCustomizeHelper(animated, springLoaded, contentType);
3316 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003317
Winson Chungc58497e2013-09-03 17:48:37 -07003318 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3319 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003320 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003321 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003322 mStateAnimation.cancel();
3323 mStateAnimation = null;
3324 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003325
Kenny Guyd794a3f2014-09-16 15:17:58 +01003326 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003327
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003328 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003329
Winson Chungf0ea4d32011-06-06 14:27:16 -07003330 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3331 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003332 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003333 final int itemsAlphaStagger =
3334 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003335
Winson Chungf0ea4d32011-06-06 14:27:16 -07003336 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003337 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003338 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003339
Adam Cohen2854d252014-08-27 16:04:07 -07003340 final ArrayList<View> layerViews = new ArrayList<View>();
3341
Adam Cohen6c5891a2014-07-09 23:53:15 -07003342 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3343 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003344 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003345 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003346 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003347 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3348 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003349 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3350 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003351
Adam Cohena38dc902014-09-07 17:48:55 +02003352 // If for some reason our views aren't initialized, don't animate
3353 boolean initialized = getAllAppsButton() != null;
3354
3355 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003356 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003357 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3358 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003359
Adam Cohen9bfdb762014-07-21 17:44:06 -07003360 final View page = content.getPageAt(content.getCurrentPage());
3361 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003362
Adam Cohen63f1ec02014-08-12 09:23:13 -07003363 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3364 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003365 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3366 } else {
3367 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3368 }
3369
Adam Cohen9bfdb762014-07-21 17:44:06 -07003370 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003371 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003372 revealView.setVisibility(View.VISIBLE);
3373 // We need to hide this view as the animation start will be posted.
3374 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003375
Adam Cohen9bfdb762014-07-21 17:44:06 -07003376 int width = revealView.getMeasuredWidth();
3377 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003378 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003379
Adam Cohen63f1ec02014-08-12 09:23:13 -07003380 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003381 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003382
Adam Cohen63f1ec02014-08-12 09:23:13 -07003383 // Get the y delta between the center of the page and the center of the all apps button
3384 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3385 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003386
Adam Cohen2854d252014-08-27 16:04:07 -07003387 float alpha = 0;
3388 float xDrift = 0;
3389 float yDrift = 0;
3390 if (material) {
3391 alpha = isWidgetTray ? 0.3f : 1f;
3392 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3393 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3394 } else {
3395 yDrift = 2 * height / 3;
3396 xDrift = 0;
3397 }
3398 final float initAlpha = alpha;
3399
Adam Cohen9bfdb762014-07-21 17:44:06 -07003400 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003401 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003402 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003403 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003404 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003405 PropertyValuesHolder panelDriftX =
3406 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003407
Adam Cohen2854d252014-08-27 16:04:07 -07003408 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3409 panelAlpha, panelDriftY, panelDriftX);
3410
Adam Cohen9bfdb762014-07-21 17:44:06 -07003411 panelAlphaAndDrift.setDuration(revealDuration);
3412 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003413
Adam Cohen9bfdb762014-07-21 17:44:06 -07003414 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003415
Adam Cohen4e243a22014-08-10 18:30:55 -07003416 if (page != null) {
3417 page.setVisibility(View.VISIBLE);
3418 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003419 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003420
Adam Cohen2854d252014-08-27 16:04:07 -07003421 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3422 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003423 pageDrift.setDuration(revealDuration);
3424 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003425 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003426 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003427
Adam Cohen63f1ec02014-08-12 09:23:13 -07003428 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003429 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003430 itemsAlpha.setDuration(revealDuration);
3431 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3432 itemsAlpha.setStartDelay(itemsAlphaStagger);
3433 mStateAnimation.play(itemsAlpha);
3434 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003435
Adam Cohen4e243a22014-08-10 18:30:55 -07003436 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3437 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003438 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003439 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003440 indicatorsAlpha.setDuration(revealDuration);
3441 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003442
Adam Cohen9bfdb762014-07-21 17:44:06 -07003443 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003444 final View allApps = getAllAppsButton();
3445 int allAppsButtonSize = LauncherAppState.getInstance().
3446 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3447 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003448 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003449 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003450 reveal.setDuration(revealDuration);
3451 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003452
3453 reveal.addListener(new AnimatorListenerAdapter() {
3454 public void onAnimationStart(Animator animation) {
3455 if (!isWidgetTray) {
3456 allApps.setVisibility(View.INVISIBLE);
3457 }
3458 }
3459 public void onAnimationEnd(Animator animation) {
3460 if (!isWidgetTray) {
3461 allApps.setVisibility(View.VISIBLE);
3462 }
3463 }
3464 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003465 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003466 }
Michael Jurka1899a362011-11-03 13:50:45 -07003467
Adam Cohen9bfdb762014-07-21 17:44:06 -07003468 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3469 @Override
3470 public void onAnimationEnd(Animator animation) {
3471 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3472 dispatchOnLauncherTransitionEnd(toView, animated, false);
3473
3474 revealView.setVisibility(View.INVISIBLE);
3475 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003476 if (page != null) {
3477 page.setLayerType(View.LAYER_TYPE_NONE, null);
3478 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003479 content.setPageBackgroundsVisible(true);
3480
3481 // Hide the search bar
3482 if (mSearchDropTargetBar != null) {
3483 mSearchDropTargetBar.hideSearchBar(false);
3484 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003485
3486 // This can hold unnecessary references to views.
3487 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003488 }
3489
Adam Cohen9bfdb762014-07-21 17:44:06 -07003490 });
3491
Adam Cohen6c5891a2014-07-09 23:53:15 -07003492 if (workspaceAnim != null) {
3493 mStateAnimation.play(workspaceAnim);
3494 }
Adam Cohen2854d252014-08-27 16:04:07 -07003495
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003496 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3497 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003498 final AnimatorSet stateAnimation = mStateAnimation;
3499 final Runnable startAnimRunnable = new Runnable() {
3500 public void run() {
3501 // Check that mStateAnimation hasn't changed while
3502 // we waited for a layout/draw pass
3503 if (mStateAnimation != stateAnimation)
3504 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003505 dispatchOnLauncherTransitionStart(fromView, animated, false);
3506 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003507
3508 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003509 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003510 for (int i = 0; i < layerViews.size(); i++) {
3511 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003512 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003513 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003514 }
Adam Cohen2854d252014-08-27 16:04:07 -07003515 }
3516 }
3517 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003518 }
3519 };
Adam Cohen2854d252014-08-27 16:04:07 -07003520 toView.bringToFront();
3521 toView.setVisibility(View.VISIBLE);
3522 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003523 } else {
3524 toView.setTranslationX(0.0f);
3525 toView.setTranslationY(0.0f);
3526 toView.setScaleX(1.0f);
3527 toView.setScaleY(1.0f);
3528 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003529 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003530
Daniel Sandlere4f98912013-06-25 15:13:26 -04003531 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003532 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003533 if (mSearchDropTargetBar != null) {
3534 mSearchDropTargetBar.hideSearchBar(false);
3535 }
Michael Jurkaabded662011-03-04 12:06:57 -08003536 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003537 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003538 dispatchOnLauncherTransitionStart(fromView, animated, false);
3539 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003540 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003541 dispatchOnLauncherTransitionStart(toView, animated, false);
3542 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003543 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003544 }
3545
3546 /**
3547 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003548 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003549 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003550 */
Adam Cohened307df2013-10-02 09:37:31 -07003551 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003552 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003553
Michael Jurkab3e22d92011-10-31 15:58:33 -07003554 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003555 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003556 mStateAnimation.cancel();
3557 mStateAnimation = null;
3558 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003559
Kenny Guyd794a3f2014-09-16 15:17:58 +01003560 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003561 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003562
Winson Chungf0ea4d32011-06-06 14:27:16 -07003563 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003564 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003565 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003566 final int itemsAlphaStagger =
3567 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003568
Winson Chungf0ea4d32011-06-06 14:27:16 -07003569 final float scaleFactor = (float)
3570 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3571 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003572 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003573 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003574 final ArrayList<View> layerViews = new ArrayList<View>();
3575
Adam Cohened307df2013-10-02 09:37:31 -07003576 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003577 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003578 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003579 } else if (toState == Workspace.State.SPRING_LOADED ||
3580 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003581 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003582 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003583 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003584
Adam Cohena38dc902014-09-07 17:48:55 +02003585 // If for some reason our views aren't initialized, don't animate
3586 boolean initialized = getAllAppsButton() != null;
3587
3588 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003589 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003590 if (workspaceAnim != null) {
3591 mStateAnimation.play(workspaceAnim);
3592 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003593
Adam Cohen9bfdb762014-07-21 17:44:06 -07003594 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3595 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003596
Adam Cohen9bfdb762014-07-21 17:44:06 -07003597 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003598
3599 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3600 int count = content.getChildCount();
3601 for (int i = 0; i < count; i++) {
3602 View child = content.getChildAt(i);
3603 if (child != page) {
3604 child.setVisibility(View.INVISIBLE);
3605 }
3606 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003607 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003608
Adam Cohen2854d252014-08-27 16:04:07 -07003609 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3610 // don't perform all these no-op animations. In particularly, this was causing
3611 // the all-apps button to pop in and out.
3612 if (fromView.getVisibility() == View.VISIBLE) {
3613 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3614 final boolean isWidgetTray =
3615 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003616
Adam Cohen2854d252014-08-27 16:04:07 -07003617 if (isWidgetTray) {
3618 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3619 } else {
3620 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003621 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003622
Adam Cohen2854d252014-08-27 16:04:07 -07003623 int width = revealView.getMeasuredWidth();
3624 int height = revealView.getMeasuredHeight();
3625 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3626
3627 // Hide the real page background, and swap in the fake one
3628 revealView.setVisibility(View.VISIBLE);
3629 content.setPageBackgroundsVisible(false);
3630
3631 final View allAppsButton = getAllAppsButton();
3632 revealView.setTranslationY(0);
3633 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3634 allAppsButton, null);
3635
3636 float xDrift = 0;
3637 float yDrift = 0;
3638 if (material) {
3639 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3640 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3641 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003642 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003643 xDrift = 0;
3644 }
3645
3646 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3647 TimeInterpolator decelerateInterpolator = material ?
3648 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003649 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003650
3651 // The vertical motion of the apps panel should be delayed by one frame
3652 // from the conceal animation in order to give the right feel. We correpsondingly
3653 // shorten the duration so that the slide and conceal end at the same time.
3654 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3655 0, yDrift);
3656 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3657 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3658 panelDriftY.setInterpolator(decelerateInterpolator);
3659 mStateAnimation.play(panelDriftY);
3660
3661 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3662 0, xDrift);
3663 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3664 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3665 panelDriftX.setInterpolator(decelerateInterpolator);
3666 mStateAnimation.play(panelDriftX);
3667
3668 if (isWidgetTray || !material) {
3669 float finalAlpha = material ? 0.4f : 0f;
3670 revealView.setAlpha(1f);
3671 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3672 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003673 panelAlpha.setDuration(material ? revealDuration : 150);
3674 panelAlpha.setInterpolator(decelerateInterpolator);
3675 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003676 mStateAnimation.play(panelAlpha);
3677 }
3678
3679 if (page != null) {
3680 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3681
3682 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3683 0, yDrift);
3684 page.setTranslationY(0);
3685 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3686 pageDrift.setInterpolator(decelerateInterpolator);
3687 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3688 mStateAnimation.play(pageDrift);
3689
3690 page.setAlpha(1f);
3691 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3692 itemsAlpha.setDuration(100);
3693 itemsAlpha.setInterpolator(decelerateInterpolator);
3694 mStateAnimation.play(itemsAlpha);
3695 }
3696
3697 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3698 pageIndicators.setAlpha(1f);
3699 ObjectAnimator indicatorsAlpha =
3700 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3701 indicatorsAlpha.setDuration(revealDuration);
3702 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3703 mStateAnimation.play(indicatorsAlpha);
3704
3705 width = revealView.getMeasuredWidth();
3706
3707 if (material) {
3708 if (!isWidgetTray) {
3709 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003710 }
Adam Cohen2854d252014-08-27 16:04:07 -07003711 int allAppsButtonSize = LauncherAppState.getInstance().
3712 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3713 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3714 Animator reveal =
3715 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3716 height / 2, revealRadius, finalRadius);
3717 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3718 reveal.setDuration(revealDuration);
3719 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003720
Adam Cohen2854d252014-08-27 16:04:07 -07003721 reveal.addListener(new AnimatorListenerAdapter() {
3722 public void onAnimationEnd(Animator animation) {
3723 revealView.setVisibility(View.INVISIBLE);
3724 if (!isWidgetTray) {
3725 allAppsButton.setVisibility(View.VISIBLE);
3726 }
3727 }
3728 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003729
Adam Cohen2854d252014-08-27 16:04:07 -07003730 mStateAnimation.play(reveal);
3731 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003732
Adam Cohen2854d252014-08-27 16:04:07 -07003733 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3734 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3735 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003736 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003737
3738 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003739 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003740 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003741 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003742 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3743 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003744 if (onCompleteRunnable != null) {
3745 onCompleteRunnable.run();
3746 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003747
3748 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003749 if (page != null) {
3750 page.setLayerType(View.LAYER_TYPE_NONE, null);
3751 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003752 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003753 // Unhide side pages
3754 int count = content.getChildCount();
3755 for (int i = 0; i < count; i++) {
3756 View child = content.getChildAt(i);
3757 child.setVisibility(View.VISIBLE);
3758 }
3759
3760 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003761 if (page != null) {
3762 page.setTranslationX(0);
3763 page.setTranslationY(0);
3764 page.setAlpha(1);
3765 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003766 content.setCurrentPage(content.getNextPage());
3767
Chet Haasebc2f0822012-10-26 17:59:53 -07003768 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003769
3770 // This can hold unnecessary references to views.
3771 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003772 }
3773 });
3774
Adam Cohen2854d252014-08-27 16:04:07 -07003775 final AnimatorSet stateAnimation = mStateAnimation;
3776 final Runnable startAnimRunnable = new Runnable() {
3777 public void run() {
3778 // Check that mStateAnimation hasn't changed while
3779 // we waited for a layout/draw pass
3780 if (mStateAnimation != stateAnimation)
3781 return;
3782 dispatchOnLauncherTransitionStart(fromView, animated, false);
3783 dispatchOnLauncherTransitionStart(toView, animated, false);
3784
Kenny Guyd794a3f2014-09-16 15:17:58 +01003785 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003786 for (int i = 0; i < layerViews.size(); i++) {
3787 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003788 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003789 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003790 }
Adam Cohen2854d252014-08-27 16:04:07 -07003791 }
3792 }
3793 mStateAnimation.start();
3794 }
3795 };
3796 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003797 } else {
3798 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003799 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003800 dispatchOnLauncherTransitionStart(fromView, animated, true);
3801 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003802 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003803 dispatchOnLauncherTransitionStart(toView, animated, true);
3804 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003805 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003806 }
3807
Michael Jurkae326f182011-11-21 14:05:46 -08003808 @Override
3809 public void onTrimMemory(int level) {
3810 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003811 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3812 // The widget preview db can result in holding onto over
3813 // 3MB of memory for caching which isn't necessary.
3814 SQLiteDatabase.releaseMemory();
3815
3816 // We reset the apps customize tray in order to
3817 // to free all the memory associated with widget previews
3818 if (mAppsCustomizeTabHost != null) {
3819 mAppsCustomizeTabHost.reset();
3820 }
Michael Jurkae326f182011-11-21 14:05:46 -08003821 }
3822 }
3823
Adam Cohened307df2013-10-02 09:37:31 -07003824 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003825 showWorkspace(animated, null);
3826 }
3827
Adam Cohened307df2013-10-02 09:37:31 -07003828 protected void showWorkspace() {
3829 showWorkspace(true);
3830 }
3831
Adam Cohened66b2b2012-01-23 17:28:51 -08003832 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003833 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003834 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003835 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003836 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003837
Winson Chungc7d2b602012-05-16 17:02:20 -07003838 // Show the search bar (only animate if we were showing the drop target bar in spring
3839 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003840 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003841 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003842 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003843
Michael Jurkab3e22d92011-10-31 15:58:33 -07003844 // Set focus to the AppsCustomize button
3845 if (mAllAppsButton != null) {
3846 mAllAppsButton.requestFocus();
3847 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003848 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003849
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003850 // Change the state *after* we've called all the transition code
3851 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003852
Winson Chung5fb63472011-02-02 17:03:37 -08003853 // Resume the auto-advance of widgets
3854 mUserPresent = true;
3855 updateRunning();
3856
alanv1d4fde62012-10-17 13:15:47 -07003857 // Send an accessibility event to announce the context change
3858 getWindow().getDecorView()
3859 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003860
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003861 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003862 }
3863
Adam Cohened307df2013-10-02 09:37:31 -07003864 void showOverviewMode(boolean animated) {
3865 mWorkspace.setVisibility(View.VISIBLE);
3866 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3867 mState = State.WORKSPACE;
3868 onWorkspaceShown(animated);
3869 }
3870
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003871 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003872 }
3873
Winson Chung82963d52013-10-09 11:20:57 -07003874 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3875 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003876 if (mState != State.WORKSPACE) return;
3877
Winson Chung82963d52013-10-09 11:20:57 -07003878 if (resetPageToZero) {
3879 mAppsCustomizeTabHost.reset();
3880 }
Winson Chungc58497e2013-09-03 17:48:37 -07003881 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003882 mAppsCustomizeTabHost.post(new Runnable() {
3883 @Override
3884 public void run() {
3885 // We post this in-case the all apps view isn't yet constructed.
3886 mAppsCustomizeTabHost.requestFocus();
3887 }
3888 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003889
Michael Jurkab3e22d92011-10-31 15:58:33 -07003890 // Change the state *after* we've called all the transition code
3891 mState = State.APPS_CUSTOMIZE;
3892
3893 // Pause the auto-advance of widgets until we are out of AllApps
3894 mUserPresent = false;
3895 updateRunning();
3896 closeFolder();
3897
3898 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003899 getWindow().getDecorView()
3900 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003901 }
3902
Adam Cohen7777d962011-08-18 18:58:38 -07003903 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003904 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003905 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003906 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003907 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003908 }
Adam Cohen7777d962011-08-18 18:58:38 -07003909
Adam Cohenad4e15c2013-10-17 16:21:35 -07003910 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003911 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003912 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3913
Winson Chunge7a03942011-08-05 15:05:12 -07003914 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003915 @Override
3916 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003917 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003918 // Before we show workspace, hide all apps again because
3919 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3920 // clean up our state transition functions
3921 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003922 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003923 } else {
3924 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003925 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003926 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003927 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003928 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003929
Michael Jurkad3ef3062010-11-23 16:23:58 -08003930 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003931 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003932 final boolean animated = true;
3933 final boolean springLoaded = true;
3934 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003935 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003936 }
3937 // Otherwise, we are not in spring loaded mode, so don't do anything.
3938 }
3939
Michael Jurkab3e22d92011-10-31 15:58:33 -07003940 void lockAllApps() {
3941 // TODO
3942 }
3943
3944 void unlockAllApps() {
3945 // TODO
3946 }
3947
Sunny Goyal594d76d2014-11-06 10:12:54 -08003948 protected void disableVoiceButtonProxy(boolean disable) {
3949 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003950 }
3951
Cristina Stancu476493b2013-08-07 17:20:14 +01003952 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003953 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3954 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003955 }
3956
Adam Cohen24ce0b32014-01-14 16:18:14 -08003957 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003958 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3959 if (searchProvider == null) {
3960 return null;
3961 }
3962
3963 Bundle opts = new Bundle();
3964 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
3965 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
3966
3967 SharedPreferences sp = getSharedPreferences(
3968 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3969 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
3970 if (!searchProvider.provider.flattenToString().equals(
3971 sp.getString(QSB_WIDGET_PROVIDER, null))
3972 || (widgetId == -1)
3973 || !mAppWidgetManager.getAppWidgetInfo(widgetId).provider
3974 .equals(searchProvider.provider)) {
3975 // A valid widget is not already bound.
3976 if (widgetId > -1) {
3977 mAppWidgetHost.deleteAppWidgetId(widgetId);
3978 widgetId = -1;
3979 }
3980
3981 // Try to bind a new widget
3982 widgetId = mAppWidgetHost.allocateAppWidgetId();
3983
3984 if (!AppWidgetManagerCompat.getInstance(this)
3985 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3986 mAppWidgetHost.deleteAppWidgetId(widgetId);
3987 widgetId = -1;
3988 }
3989
3990 sp.edit()
3991 .putInt(QSB_WIDGET_ID, widgetId)
3992 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3993 .commit();
3994 }
3995
3996 if (widgetId != -1) {
3997 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3998 mQsb.updateAppWidgetOptions(opts);
3999 mQsb.setPadding(0, 0, 0, 0);
4000 mSearchDropTargetBar.addView(mQsb);
4001 }
Cristina Stancu476493b2013-08-07 17:20:14 +01004002 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08004003 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01004004 }
4005
Michael Jurkad7c28052012-04-27 15:43:36 -07004006 @Override
4007 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004008 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004009 final List<CharSequence> text = event.getText();
4010 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004011 // Populate event with a fake title based on the current state.
4012 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004013 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004014 } else {
4015 text.add(getString(R.string.all_apps_home_button_label));
4016 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004017 return result;
4018 }
4019
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004020 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004021 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004022 */
4023 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4024 @Override
4025 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004026 closeSystemDialogs();
4027 }
4028 }
4029
4030 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004031 * Receives notifications whenever the appwidgets are reset.
4032 */
4033 private class AppWidgetResetObserver extends ContentObserver {
4034 public AppWidgetResetObserver() {
4035 super(new Handler());
4036 }
4037
4038 @Override
4039 public void onChange(boolean selfChange) {
4040 onAppWidgetReset();
4041 }
4042 }
4043
4044 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004045 * If the activity is currently paused, signal that we need to run the passed Runnable
4046 * in onResume.
4047 *
4048 * This needs to be called from incoming places where resources might have been loaded
4049 * while we are paused. That is becaues the Configuration might be wrong
4050 * when we're not running, and if it comes back to what it was when we
4051 * were paused, we are not restarted.
4052 *
4053 * Implementation of the method from LauncherModel.Callbacks.
4054 *
4055 * @return true if we are currently paused. The caller might be able to
4056 * skip some work in that case since we will come back again.
4057 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004058 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004059 if (mPaused) {
4060 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004061 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004062 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004063 }
4064 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004065 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004066 return true;
4067 } else {
4068 return false;
4069 }
4070 }
4071
Michael Jurkac402cd92013-05-20 15:49:32 +02004072 private boolean waitUntilResume(Runnable run) {
4073 return waitUntilResume(run, false);
4074 }
4075
Michael Jurka1e2f4652013-07-08 18:03:46 -07004076 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004077 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004078 }
4079
Michael Jurka7607c2f2013-04-03 14:33:19 -07004080 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004081 * If the activity is currently paused, signal that we need to re-run the loader
4082 * in onResume.
4083 *
4084 * This needs to be called from incoming places where resources might have been loaded
4085 * while we are paused. That is becaues the Configuration might be wrong
4086 * when we're not running, and if it comes back to what it was when we
4087 * were paused, we are not restarted.
4088 *
4089 * Implementation of the method from LauncherModel.Callbacks.
4090 *
4091 * @return true if we are currently paused. The caller might be able to
4092 * skip some work in that case since we will come back again.
4093 */
4094 public boolean setLoadOnResume() {
4095 if (mPaused) {
4096 Log.i(TAG, "setLoadOnResume");
4097 mOnResumeNeedsLoad = true;
4098 return true;
4099 } else {
4100 return false;
4101 }
4102 }
4103
4104 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004105 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004106 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004107 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004108 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004109 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004110 } else {
4111 return SCREEN_COUNT / 2;
4112 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004113 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004114
Joe Onorato9c1289c2009-08-17 11:03:03 -04004115 /**
4116 * Refreshes the shortcuts shown on the workspace.
4117 *
4118 * Implementation of the method from LauncherModel.Callbacks.
4119 */
4120 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004121 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004122
Michael Jurka7607c2f2013-04-03 14:33:19 -07004123 // If we're starting binding all over again, clear any bind calls we'd postponed in
4124 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4125 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004126 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004127
Winson Chungd64d1762013-08-20 14:37:16 -07004128 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004129 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004130 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004131
Michael Jurka05bf644e2011-11-30 20:28:53 -08004132 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004133 if (mHotseat != null) {
4134 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004135 }
4136 }
4137
Adam Cohendcd297f2013-06-18 13:13:40 -07004138 @Override
4139 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004140 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004141
Adam Cohen5084cba2013-09-03 12:01:16 -07004142 // If there are no screens, we need to have an empty screen
4143 if (orderedScreenIds.size() == 0) {
4144 mWorkspace.addExtraEmptyScreen();
4145 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004146
4147 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004148 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004149 if (hasCustomContentToLeft()) {
4150 mWorkspace.createCustomContentContainer();
4151 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004152 }
Winson Chung64359a52013-07-08 17:17:08 -07004153 }
4154
4155 @Override
4156 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004157 // Log to disk
4158 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4159 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4160 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004161 int count = orderedScreenIds.size();
4162 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004163 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004164 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004165 }
4166
Adam Cohen39a06042013-07-19 14:30:12 -07004167 private boolean shouldShowWeightWatcher() {
4168 String spKey = LauncherAppState.getSharedPreferencesKey();
4169 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07004170 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004171
4172 return show;
4173 }
4174
4175 private void toggleShowWeightWatcher() {
4176 String spKey = LauncherAppState.getSharedPreferencesKey();
4177 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4178 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4179
4180 show = !show;
4181
4182 SharedPreferences.Editor editor = sp.edit();
4183 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4184 editor.commit();
4185
4186 if (mWeightWatcher != null) {
4187 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4188 }
4189 }
4190
Winson Chungd64d1762013-08-20 14:37:16 -07004191 public void bindAppsAdded(final ArrayList<Long> newScreens,
4192 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004193 final ArrayList<ItemInfo> addAnimated,
4194 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004195 Runnable r = new Runnable() {
4196 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004197 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004198 }
4199 };
4200 if (waitUntilResume(r)) {
4201 return;
4202 }
4203
Winson Chungd64d1762013-08-20 14:37:16 -07004204 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004205 if (newScreens != null) {
4206 bindAddScreens(newScreens);
4207 }
Winson Chungd64d1762013-08-20 14:37:16 -07004208
4209 // We add the items without animation on non-visible pages, and with
4210 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004211 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004212 bindItems(addNotAnimated, 0,
4213 addNotAnimated.size(), false);
4214 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004215 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004216 bindItems(addAnimated, 0,
4217 addAnimated.size(), true);
4218 }
Winson Chungc58497e2013-09-03 17:48:37 -07004219
Winson Chung87412982013-10-03 18:34:14 -07004220 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004221 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004222
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004223 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004224 addedApps != null && mAppsCustomizeContent != null) {
4225 mAppsCustomizeContent.addApps(addedApps);
4226 }
Winson Chungd64d1762013-08-20 14:37:16 -07004227 }
4228
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004229 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004230 * Bind the items start-end from the list.
4231 *
4232 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004233 */
Winson Chung64359a52013-07-08 17:17:08 -07004234 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4235 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004236 Runnable r = new Runnable() {
4237 public void run() {
4238 bindItems(shortcuts, start, end, forceAnimateIcons);
4239 }
4240 };
4241 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004242 return;
4243 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004244
Winson Chungf0c6ae02012-03-21 16:10:31 -07004245 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004246 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4247 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004248 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004249 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004250 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004251 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004252 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004253
4254 // Short circuit if we are loading dock items for a configuration which has no dock
4255 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4256 mHotseat == null) {
4257 continue;
4258 }
4259
Joe Onorato9c1289c2009-08-17 11:03:03 -04004260 switch (item.itemType) {
4261 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4262 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004263 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004264 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004265
Winson Chung64359a52013-07-08 17:17:08 -07004266 /*
4267 * TODO: FIX collision case
4268 */
Winson Chungce376632013-07-11 16:12:41 -07004269 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4270 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4271 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004272 View v = cl.getChildAt(item.cellX, item.cellY);
4273 Object tag = v.getTag();
4274 String desc = "Collision while binding workspace item: " + item
4275 + ". Collides with " + tag;
4276 if (LauncherAppState.isDogfoodBuild()) {
4277 throw (new RuntimeException(desc));
4278 } else {
4279 Log.d(TAG, desc);
4280 }
Winson Chungce376632013-07-11 16:12:41 -07004281 }
Winson Chung64359a52013-07-08 17:17:08 -07004282 }
4283
Adam Cohendcd297f2013-06-18 13:13:40 -07004284 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4285 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004286 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004287 // Animate all the applications up now
4288 shortcut.setAlpha(0f);
4289 shortcut.setScaleX(0f);
4290 shortcut.setScaleY(0f);
4291 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004292 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004293 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004294 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004295 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004296 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004297 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004298 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004299 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4300 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004301 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004302 default:
4303 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004304 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004305 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004306
Winson Chung997a9232013-07-24 15:33:46 -07004307 if (animateIcons) {
4308 // Animate to the correct page
4309 if (newShortcutsScreenId > -1) {
4310 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004311 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004312 final Runnable startBounceAnimRunnable = new Runnable() {
4313 public void run() {
4314 anim.playTogether(bounceAnims);
4315 anim.start();
4316 }
4317 };
Winson Chung997a9232013-07-24 15:33:46 -07004318 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004319 // We post the animation slightly delayed to prevent slowdowns
4320 // when we are loading right after we return to launcher.
4321 mWorkspace.postDelayed(new Runnable() {
4322 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004323 if (mWorkspace != null) {
4324 mWorkspace.snapToPage(newScreenIndex);
4325 mWorkspace.postDelayed(startBounceAnimRunnable,
4326 NEW_APPS_ANIMATION_DELAY);
4327 }
Winson Chung94d67682013-09-25 16:29:40 -07004328 }
4329 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004330 } else {
4331 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004332 }
4333 }
Winson Chung64359a52013-07-08 17:17:08 -07004334 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004335 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004336 }
4337
Joe Onorato9c1289c2009-08-17 11:03:03 -04004338 /**
4339 * Implementation of the method from LauncherModel.Callbacks.
4340 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004341 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004342 Runnable r = new Runnable() {
4343 public void run() {
4344 bindFolders(folders);
4345 }
4346 };
4347 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004348 return;
4349 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004350 sFolders.clear();
4351 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004352 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004353
4354 /**
4355 * Add the views for a widget to the workspace.
4356 *
4357 * Implementation of the method from LauncherModel.Callbacks.
4358 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004359 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004360 Runnable r = new Runnable() {
4361 public void run() {
4362 bindAppWidget(item);
4363 }
4364 };
4365 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004366 return;
4367 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004368
Daniel Sandler843e8602010-06-07 14:59:01 -04004369 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4370 if (DEBUG_WIDGETS) {
4371 Log.d(TAG, "bindAppWidget: " + item);
4372 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004373 final Workspace workspace = mWorkspace;
4374
Sunny Goyalff572272014-07-23 13:58:07 -07004375 AppWidgetProviderInfo appWidgetInfo;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004376 if (!mIsSafeModeEnabled
4377 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4378 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004379
4380 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4381 if (appWidgetInfo == null) {
4382 if (DEBUG_WIDGETS) {
4383 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4384 + " belongs to component " + item.providerName
4385 + ", as the povider is null");
4386 }
4387 LauncherModel.deleteItemFromDatabase(this, item);
4388 return;
4389 }
4390 // Note: This assumes that the id remap broadcast is received before this step.
4391 // If that is not the case, the id remap will be ignored and user may see the
4392 // click to setup view.
4393 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4394 pendingInfo.spanX = item.spanX;
4395 pendingInfo.spanY = item.spanY;
4396 pendingInfo.minSpanX = item.minSpanX;
4397 pendingInfo.minSpanY = item.minSpanY;
4398 Bundle options =
4399 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4400
Sunny Goyalff572272014-07-23 13:58:07 -07004401 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004402 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4403 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004404
4405 // TODO consider showing a permission dialog when the widget is clicked.
4406 if (!success) {
4407 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4408 if (DEBUG_WIDGETS) {
4409 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4410 + " belongs to component " + item.providerName
4411 + ", as the launcher is unable to bing a new widget id");
4412 }
4413 LauncherModel.deleteItemFromDatabase(this, item);
4414 return;
4415 }
4416
4417 item.appWidgetId = newWidgetId;
4418
4419 // If the widget has a configure activity, it is still needs to set it up, otherwise
4420 // the widget is ready to go.
4421 item.restoreStatus = (appWidgetInfo.configure == null)
4422 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4423 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4424
4425 LauncherModel.updateItemInDatabase(this, item);
4426 }
4427
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004428 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004429 final int appWidgetId = item.appWidgetId;
4430 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4431 if (DEBUG_WIDGETS) {
4432 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4433 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004434
Sunny Goyal651077b2014-06-30 14:15:31 -07004435 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4436 } else {
4437 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004438 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4439 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004440 view.updateIcon(mIconCache);
4441 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004442 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004443 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004444 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004445
Joe Onorato9c1289c2009-08-17 11:03:03 -04004446 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004447 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004448
Adam Cohendcd297f2013-06-18 13:13:40 -07004449 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004450 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004451 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4452
Joe Onorato9c1289c2009-08-17 11:03:03 -04004453 workspace.requestLayout();
4454
Daniel Sandler843e8602010-06-07 14:59:01 -04004455 if (DEBUG_WIDGETS) {
4456 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4457 + (SystemClock.uptimeMillis()-start) + "ms");
4458 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004459 }
4460
Sunny Goyalff572272014-07-23 13:58:07 -07004461 /**
4462 * Restores a pending widget.
4463 *
4464 * @param appWidgetId The app widget id
4465 * @param cellInfo The position on screen where to create the widget.
4466 */
4467 private void completeRestoreAppWidget(final int appWidgetId) {
4468 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4469 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4470 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4471 return;
4472 }
4473
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004474 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004475 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4476
4477 mWorkspace.reinflateWidgetsIfNecessary();
4478 LauncherModel.updateItemInDatabase(this, info);
4479 }
4480
Adam Cohen1462de32012-07-24 22:34:36 -07004481 public void onPageBoundSynchronously(int page) {
4482 mSynchronouslyBoundPages.add(page);
4483 }
4484
Joe Onorato9c1289c2009-08-17 11:03:03 -04004485 /**
4486 * Callback saying that there aren't any more items to bind.
4487 *
4488 * Implementation of the method from LauncherModel.Callbacks.
4489 */
Adam Cohene25af792013-06-06 23:08:25 -07004490 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004491 Runnable r = new Runnable() {
4492 public void run() {
4493 finishBindingItems(upgradePath);
4494 }
4495 };
4496 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004497 return;
4498 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004499 if (mSavedState != null) {
4500 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004501 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004502 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004503 mSavedState = null;
4504 }
4505
Adam Cohen1462de32012-07-24 22:34:36 -07004506 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004507
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004508 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004509 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004510
4511 // If we received the result of any pending adds while the loader was running (e.g. the
4512 // widget configuration forced an orientation change), process them now.
4513 if (sPendingAddItem != null) {
4514 final long screenId = completeAdd(sPendingAddItem);
4515
4516 // TODO: this moves the user to the page where the pending item was added. Ideally,
4517 // the screen would be guaranteed to exist after bind, and the page would be set through
4518 // the workspace restore process.
4519 mWorkspace.post(new Runnable() {
4520 @Override
4521 public void run() {
4522 mWorkspace.snapToScreenId(screenId);
4523 }
4524 });
4525 sPendingAddItem = null;
4526 }
4527
Adam Cohene25af792013-06-06 23:08:25 -07004528 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004529 mWorkspace.getUniqueComponents(true, null);
4530 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004531 }
Sunny Goyale755d462014-07-22 13:48:29 -07004532 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004533
4534 if (mLauncherCallbacks != null) {
4535 mLauncherCallbacks.finishBindingItems(upgradePath);
4536 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004537 }
4538
Adam Cohen3ed316a2014-07-23 18:21:20 -07004539 private void sendLoadingCompleteBroadcastIfNecessary() {
4540 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4541 String permission =
4542 getResources().getString(R.string.receive_first_load_broadcast_permission);
4543 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4544 sendBroadcast(intent, permission);
4545 SharedPreferences.Editor editor = mSharedPrefs.edit();
4546 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4547 editor.apply();
4548 }
4549 }
4550
Winson Chunga0b7e862013-09-05 16:03:15 -07004551 public boolean isAllAppsButtonRank(int rank) {
4552 if (mHotseat != null) {
4553 return mHotseat.isAllAppsButtonRank(rank);
4554 }
4555 return false;
4556 }
4557
Winson Chunga2413752012-04-03 14:22:34 -07004558 private boolean canRunNewAppsAnimation() {
4559 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4560 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4561 }
4562
Winson Chungc9168342013-06-26 14:54:55 -07004563 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4564 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4565 PropertyValuesHolder.ofFloat("alpha", 1f),
4566 PropertyValuesHolder.ofFloat("scaleX", 1f),
4567 PropertyValuesHolder.ofFloat("scaleY", 1f));
4568 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4569 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4570 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4571 return bounceAnim;
4572 }
4573
Adam Cohen27772732013-11-11 14:00:56 +00004574 public boolean useVerticalBarLayout() {
4575 return LauncherAppState.getInstance().getDynamicGrid().
4576 getDeviceProfile().isVerticalBarLayout();
4577 }
4578
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004579 protected Rect getSearchBarBounds() {
4580 return LauncherAppState.getInstance().getDynamicGrid().
4581 getDeviceProfile().getSearchBarBounds();
4582 }
4583
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004584 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004585 if (mSearchDropTargetBar == null) {
4586 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004587 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004588 if (mQsb != null) {
4589 mSearchDropTargetBar.removeView(mQsb);
4590 mQsb = null;
4591 }
4592 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004593 }
4594
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004595 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004596 * Add the icons for all apps.
4597 *
4598 * Implementation of the method from LauncherModel.Callbacks.
4599 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004600 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004601 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004602 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4603 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4604 getHotseat().addAllAppsFolder(mIconCache, apps,
4605 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4606 }
4607 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004608 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004609 if (mAppsCustomizeContent != null) {
4610 mAppsCustomizeContent.onPackagesUpdated(
4611 LauncherModel.getSortedWidgetsAndShortcuts(this));
4612 }
Winson Chungc58497e2013-09-03 17:48:37 -07004613 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004614 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004615 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004616 mAppsCustomizeContent.onPackagesUpdated(
4617 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004618 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004619 }
Adam Cohen9211d422014-10-07 18:14:53 -07004620 if (mLauncherCallbacks != null) {
4621 mLauncherCallbacks.bindAllApplications(apps);
4622 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004623 }
4624
4625 /**
4626 * A package was updated.
4627 *
4628 * Implementation of the method from LauncherModel.Callbacks.
4629 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004630 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004631 Runnable r = new Runnable() {
4632 public void run() {
4633 bindAppsUpdated(apps);
4634 }
4635 };
4636 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004637 return;
4638 }
4639
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004640 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004641 mAppsCustomizeContent != null) {
4642 mAppsCustomizeContent.updateApps(apps);
4643 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004644 }
4645
Sunny Goyal4390ace2014-10-13 11:33:11 -07004646 @Override
4647 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4648 Runnable r = new Runnable() {
4649 public void run() {
4650 bindWidgetsRestored(widgets);
4651 }
4652 };
4653 if (waitUntilResume(r)) {
4654 return;
4655 }
4656 mWorkspace.widgetsRestored(widgets);
4657 }
4658
Joe Onorato9c1289c2009-08-17 11:03:03 -04004659 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004660 * Some shortcuts were updated in the background.
4661 *
4662 * Implementation of the method from LauncherModel.Callbacks.
4663 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004664 @Override
4665 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4666 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004667 Runnable r = new Runnable() {
4668 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004669 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004670 }
4671 };
4672 if (waitUntilResume(r)) {
4673 return;
4674 }
4675
Sunny Goyal4390ace2014-10-13 11:33:11 -07004676 if (!updated.isEmpty()) {
4677 mWorkspace.updateShortcuts(updated);
4678 }
4679
4680 if (!removed.isEmpty()) {
4681 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4682 for (ShortcutInfo si : removed) {
4683 removedComponents.add(si.getTargetComponent());
4684 }
4685 mWorkspace.removeItemsByComponentName(removedComponents, user);
4686 // Notify the drag controller
4687 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004688 }
4689 }
4690
4691 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004692 * Update the state of a package, typically related to install state.
4693 *
4694 * Implementation of the method from LauncherModel.Callbacks.
4695 */
Sunny Goyale755d462014-07-22 13:48:29 -07004696 @Override
4697 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004698 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004699 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004700 }
4701 }
4702
4703 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004704 * Update the label and icon of all the icons in a package
4705 *
4706 * Implementation of the method from LauncherModel.Callbacks.
4707 */
4708 @Override
4709 public void updatePackageBadge(String packageName) {
4710 if (mWorkspace != null) {
4711 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4712 }
4713 }
4714
4715 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004716 * A package was uninstalled. We take both the super set of packageNames
4717 * in addition to specific applications to remove, the reason being that
4718 * this can be called when a package is updated as well. In that scenario,
4719 * we only remove specific components from the workspace, where as
4720 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004721 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004722 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004723 * Implementation of the method from LauncherModel.Callbacks.
4724 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004725 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004726 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004727 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004728 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004729 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004730 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004731 }
Winson Chungd64d1762013-08-20 14:37:16 -07004732 };
4733 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004734 return;
4735 }
4736
Sunny Goyal1a745e82014-10-02 15:58:31 -07004737 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004738 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4739 for (AppInfo info : appInfos) {
4740 removedComponents.add(info.componentName);
4741 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004742 if (!packageNames.isEmpty()) {
4743 mWorkspace.removeItemsByPackageName(packageNames, user);
4744 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004745 if (!removedComponents.isEmpty()) {
4746 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004747 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004748 // Notify the drag controller
4749 mDragController.onAppsRemoved(packageNames, removedComponents);
4750
Sunny Goyal1a745e82014-10-02 15:58:31 -07004751 } else {
4752 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004753 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004754
Winson Chungdf95eb12013-10-16 14:57:07 -07004755 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004756 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004757 mAppsCustomizeContent != null) {
4758 mAppsCustomizeContent.removeApps(appInfos);
4759 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004760 }
Joe Onoratobe386092009-11-17 17:32:16 -08004761
4762 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004763 * A number of packages were updated.
4764 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004765 private ArrayList<Object> mWidgetsAndShortcuts;
4766 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004767 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004768 bindPackagesUpdated(mWidgetsAndShortcuts);
4769 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004770 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004771 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004772 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4773 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4774 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004775 return;
4776 }
4777
Winson Chung64359a52013-07-08 17:17:08 -07004778 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004779 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004780 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004781 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004782 }
4783
Winson Chung400438b2011-01-16 17:53:48 -08004784 private int mapConfigurationOriActivityInfoOri(int configOri) {
4785 final Display d = getWindowManager().getDefaultDisplay();
4786 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4787 switch (d.getRotation()) {
4788 case Surface.ROTATION_0:
4789 case Surface.ROTATION_180:
4790 // We are currently in the same basic orientation as the natural orientation
4791 naturalOri = configOri;
4792 break;
4793 case Surface.ROTATION_90:
4794 case Surface.ROTATION_270:
4795 // We are currently in the other basic orientation to the natural orientation
4796 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4797 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4798 break;
4799 }
4800
4801 int[] oriMap = {
4802 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4803 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4804 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4805 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4806 };
4807 // Since the map starts at portrait, we need to offset if this device's natural orientation
4808 // is landscape.
4809 int indexOffset = 0;
4810 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4811 indexOffset = 1;
4812 }
4813 return oriMap[(d.getRotation() + indexOffset) % 4];
4814 }
Adam Cohen446e9402011-09-15 18:21:21 -07004815
Winson Chung4b919f82012-05-01 10:44:08 -07004816 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004817 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004818 getResources().getBoolean(R.bool.allow_rotation);
4819 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004820 }
Winson Chung4b919f82012-05-01 10:44:08 -07004821 public void lockScreenOrientation() {
4822 if (isRotationEnabled()) {
4823 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4824 .getConfiguration().orientation));
4825 }
4826 }
4827 public void unlockScreenOrientation(boolean immediate) {
4828 if (isRotationEnabled()) {
4829 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004830 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004831 } else {
4832 mHandler.postDelayed(new Runnable() {
4833 public void run() {
4834 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4835 }
4836 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004837 }
Winson Chung4b919f82012-05-01 10:44:08 -07004838 }
Winson Chung400438b2011-01-16 17:53:48 -08004839 }
4840
Winson Chunge43a1e72014-01-15 10:33:02 -08004841 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004842 if (mLauncherCallbacks != null) {
4843 return mLauncherCallbacks.isLauncherPreinstalled();
4844 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004845 PackageManager pm = getPackageManager();
4846 try {
4847 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4848 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4849 return true;
4850 } else {
4851 return false;
4852 }
4853 } catch (NameNotFoundException e) {
4854 e.printStackTrace();
4855 return false;
4856 }
4857 }
4858
Adam Cohenea90f832014-05-21 15:03:34 -07004859 /**
4860 * This method indicates whether or not we should suggest default wallpaper dimensions
4861 * when our wallpaper cropper was not yet used to set a wallpaper.
4862 */
4863 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004864 if (mLauncherCallbacks != null) {
4865 return mLauncherCallbacks.overrideWallpaperDimensions();
4866 }
Adam Cohenea90f832014-05-21 15:03:34 -07004867 return true;
4868 }
4869
Adam Cohen432609a2014-03-13 17:03:22 -07004870 /**
4871 * To be overridden by subclasses to indicate that there is an activity to launch
4872 * before showing the standard launcher experience.
4873 */
4874 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004875 if (mLauncherCallbacks != null) {
4876 return mLauncherCallbacks.hasFirstRunActivity();
4877 }
Adam Cohen432609a2014-03-13 17:03:22 -07004878 return false;
4879 }
4880
4881 /**
4882 * To be overridden by subclasses to launch any first run activity
4883 */
4884 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004885 if (mLauncherCallbacks != null) {
4886 return mLauncherCallbacks.getFirstRunActivity();
4887 }
Adam Cohen432609a2014-03-13 17:03:22 -07004888 return null;
4889 }
4890
Winson Chunga6945242014-01-08 14:04:34 -08004891 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004892 return !ActivityManager.isRunningInTestHarness() &&
4893 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004894 }
4895
Adam Cohen4a9423d2014-03-28 14:22:31 -07004896 protected boolean hasRunFirstRunActivity() {
4897 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4898 }
4899
Adam Cohen432609a2014-03-13 17:03:22 -07004900 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004901 if (shouldRunFirstRunActivity() &&
4902 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004903 Intent firstRunIntent = getFirstRunActivity();
4904 if (firstRunIntent != null) {
4905 startActivity(firstRunIntent);
4906 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004907 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004908 }
4909 }
Adam Cohen432609a2014-03-13 17:03:22 -07004910 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004911 }
4912
4913 private void markFirstRunActivityShown() {
4914 SharedPreferences.Editor editor = mSharedPrefs.edit();
4915 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4916 editor.apply();
4917 }
4918
Adam Cohen432609a2014-03-13 17:03:22 -07004919 /**
4920 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4921 * screen that must be displayed and dismissed.
4922 */
4923 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004924 if (mLauncherCallbacks != null) {
4925 return mLauncherCallbacks.hasDismissableIntroScreen();
4926 }
Adam Cohen432609a2014-03-13 17:03:22 -07004927 return false;
4928 }
4929
4930 /**
4931 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4932 */
4933 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004934 if (mLauncherCallbacks != null) {
4935 return mLauncherCallbacks.getIntroScreen();
4936 }
Adam Cohen432609a2014-03-13 17:03:22 -07004937 return null;
4938 }
4939
4940 /**
4941 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4942 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4943 */
4944 private boolean shouldShowIntroScreen() {
4945 return hasDismissableIntroScreen() &&
4946 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4947 }
4948
4949 protected void showIntroScreen() {
4950 View introScreen = getIntroScreen();
4951 changeWallpaperVisiblity(false);
4952 if (introScreen != null) {
4953 mDragLayer.showOverlayView(introScreen);
4954 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004955 if (mLauncherOverlayContainer != null) {
4956 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4957 }
Adam Cohen432609a2014-03-13 17:03:22 -07004958 }
4959
4960 public void dismissIntroScreen() {
4961 markIntroScreenDismissed();
4962 if (showFirstRunActivity()) {
4963 // We delay hiding the intro view until the first run activity is showing. This
4964 // avoids a blip.
4965 mWorkspace.postDelayed(new Runnable() {
4966 @Override
4967 public void run() {
4968 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004969 if (mLauncherOverlayContainer != null) {
4970 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4971 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004972 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004973 }
4974 }, ACTIVITY_START_DELAY);
4975 } else {
4976 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004977 if (mLauncherOverlayContainer != null) {
4978 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4979 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004980 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004981 }
4982 changeWallpaperVisiblity(true);
4983 }
4984
4985 private void markIntroScreenDismissed() {
4986 SharedPreferences.Editor editor = mSharedPrefs.edit();
4987 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4988 editor.apply();
4989 }
4990
Sunny Goyal88d60f12014-09-08 03:47:29 -07004991 private void showFirstRunClings() {
4992 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4993 // on the device, then we always show the first run cling experience (or if there is no
4994 // launcher2). Otherwise, we prompt the user upon started for migration
4995 LauncherClings launcherClings = new LauncherClings(this);
4996 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4997 if (mModel.canMigrateFromOldLauncherDb(this)) {
4998 launcherClings.showMigrationCling();
4999 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005000 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005001 }
5002 }
5003 }
5004
Winson Chunga6945242014-01-08 14:04:34 -08005005 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005006 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5007 if (mHotseat != null) mHotseat.setAlpha(1f);
5008 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5009 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005010 }
5011
5012 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005013 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5014 if (mHotseat != null) mHotseat.setAlpha(0f);
5015 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5016 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005017 }
5018
Mathew Inwood72fbec12013-11-19 15:45:07 +00005019 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005020 // Called from search suggestion, not supported in other profiles.
5021 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5022 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5023 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5024 myUser);
5025 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005026 return null;
5027 }
Kenny Guyed131872014-04-30 03:02:21 +01005028 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005029 }
5030
5031 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5032 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005033 // Called from search suggestion, not supported in other profiles.
5034 return createShortcutDragInfo(shortcutIntent, caption, icon,
5035 UserHandleCompat.myUserHandle());
5036 }
5037
5038 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5039 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005040 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005041 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005042 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005043 }
5044
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005045 protected void moveWorkspaceToDefaultScreen() {
5046 mWorkspace.moveToDefaultScreen(false);
5047 }
5048
Mathew Inwood72fbec12013-11-19 15:45:07 +00005049 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5050 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005051 mWorkspace.onExternalDragStartedWithItem(dragView);
5052 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005053 }
5054
Anjali Koppalf5d29132014-02-28 13:33:27 -08005055 @Override
5056 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005057 if (mLauncherCallbacks != null) {
5058 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5059 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005060 }
5061
Winson Chung80baf5a2010-08-09 16:03:15 -07005062 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005063 * Prints out out state for debugging.
5064 */
5065 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005066 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005067 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005068 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5069 Log.d(TAG, "mRestoring=" + mRestoring);
5070 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5071 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005072 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005073 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005074
Winson Chung785d2eb2011-04-14 16:08:02 -07005075 if (mAppsCustomizeContent != null) {
5076 mAppsCustomizeContent.dumpState();
5077 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005078 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005079 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005080
5081 @Override
5082 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5083 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005084 synchronized (sDumpLogs) {
5085 writer.println(" ");
5086 writer.println("Debug logs: ");
5087 for (int i = 0; i < sDumpLogs.size(); i++) {
5088 writer.println(" " + sDumpLogs.get(i));
5089 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005090 }
Adam Cohen9211d422014-10-07 18:14:53 -07005091 if (mLauncherCallbacks != null) {
5092 mLauncherCallbacks.dump(prefix, fd, writer, args);
5093 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005094 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005095
5096 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005097 if (DEBUG_DUMP_LOG) {
5098 synchronized (sDumpLogs) {
5099 Log.d(TAG, "");
5100 Log.d(TAG, "*********************");
5101 Log.d(TAG, "Launcher debug logs: ");
5102 for (int i = 0; i < sDumpLogs.size(); i++) {
5103 Log.d(TAG, " " + sDumpLogs.get(i));
5104 }
5105 Log.d(TAG, "*********************");
5106 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005107 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005108 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005109 }
5110
5111 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005112 addDumpLog(tag, log, null, debugLog);
5113 }
5114
5115 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005116 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005117 if (e != null) {
5118 Log.d(tag, log, e);
5119 } else {
5120 Log.d(tag, log);
5121 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005122 }
Winson Chungede41292013-09-19 16:27:36 -07005123 if (DEBUG_DUMP_LOG) {
5124 sDateStamp.setTime(System.currentTimeMillis());
5125 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005126 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5127 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005128 }
Winson Chungede41292013-09-19 16:27:36 -07005129 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005130 }
5131
Winson Chungede41292013-09-19 16:27:36 -07005132 public void dumpLogsToLocalData() {
5133 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005134 new AsyncTask<Void, Void, Void>() {
5135 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005136 boolean success = false;
5137 sDateStamp.setTime(sRunStart);
5138 String FILENAME = sDateStamp.getMonth() + "-"
5139 + sDateStamp.getDay() + "_"
5140 + sDateStamp.getHours() + "-"
5141 + sDateStamp.getMinutes() + "_"
5142 + sDateStamp.getSeconds() + ".txt";
5143
5144 FileOutputStream fos = null;
5145 File outFile = null;
5146 try {
5147 outFile = new File(getFilesDir(), FILENAME);
5148 outFile.createNewFile();
5149 fos = new FileOutputStream(outFile);
5150 } catch (Exception e) {
5151 e.printStackTrace();
5152 }
5153 if (fos != null) {
5154 PrintWriter writer = new PrintWriter(fos);
5155
5156 writer.println(" ");
5157 writer.println("Debug logs: ");
5158 synchronized (sDumpLogs) {
5159 for (int i = 0; i < sDumpLogs.size(); i++) {
5160 writer.println(" " + sDumpLogs.get(i));
5161 }
5162 }
5163 writer.close();
5164 }
5165 try {
5166 if (fos != null) {
5167 fos.close();
5168 success = true;
5169 }
5170 } catch (IOException e) {
5171 e.printStackTrace();
5172 }
Michael Jurka43467462013-11-14 12:03:58 +01005173 return null;
Winson Chungede41292013-09-19 16:27:36 -07005174 }
Michael Jurka43467462013-11-14 12:03:58 +01005175 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005176 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005177 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005178}
Michael Jurka2763be32011-02-24 11:19:57 -08005179
Michael Jurkaabded662011-03-04 12:06:57 -08005180interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005181 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005182 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005183 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005184 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005185 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005186}
Dan Sandlerd5024042014-01-09 15:01:33 -05005187
5188interface DebugIntents {
5189 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5190 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005191}