blob: bb7265e44693fc0991c523f2078e8b8740664ce9 [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 Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040059import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020061import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080068import android.speech.RecognizerIntent;
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;
98import android.widget.TextView;
99import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700100
Sunny Goyal651077b2014-06-30 14:15:31 -0700101import com.android.launcher3.DropTarget.DragObject;
102import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700103import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100104import com.android.launcher3.compat.LauncherActivityInfoCompat;
105import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700106import com.android.launcher3.compat.PackageInstallerCompat;
107import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100108import com.android.launcher3.compat.UserHandleCompat;
109import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700110
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.io.DataInputStream;
112import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700113import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700114import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700117import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700118import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000119import java.lang.reflect.Field;
120import java.lang.reflect.InvocationTargetException;
121import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700122import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700123import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700124import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700127import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000129import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131/**
132 * Default launcher application.
133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100134public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800137 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700138 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Daniel Sandlerf061f822013-06-27 13:59:36 -0400140 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400141 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700142 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400143 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700144 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700145
Dan Sandlerd5024042014-01-09 15:01:33 -0500146 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700152 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Michael Jurka8b805b12012-04-18 14:23:14 -0700154 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700155 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700156
Mathew Inwood876a8462013-06-14 14:12:41 +0100157 /**
158 * IntentStarter uses request codes starting with this. This must be greater than all activity
159 * request codes used internally.
160 */
161 protected static final int REQUEST_LAST = 100;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
164
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800165 static final int SCREEN_COUNT = 5;
166 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 // To turn on these properties, type
169 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
170 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
171 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800172 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Winson Chung2672ff92012-05-04 16:22:30 -0700174 // The Intent extra that defines whether to ignore the launch animation
175 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400176 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: int
179 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700180 // Type: int
181 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
184 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700189 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: boolean
191 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
192 // Type: long
193 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700194 // Type: int
195 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
196 // Type: int
197 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
198 // Type: parcelable
199 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000200 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800201 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000202 // Type: int[]
203 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
Adam Cohen432609a2014-03-13 17:03:22 -0700205 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800206 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
207
Adam Cohen3ed316a2014-07-23 18:21:20 -0700208 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
209 static final String ACTION_FIRST_LOAD_COMPLETE =
210 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
211
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100212 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700213 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100214 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700215 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100216 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700217
Adam Cohen39a06042013-07-19 14:30:12 -0700218 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700219 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700220
Winson Chunga6945242014-01-08 14:04:34 -0800221 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
222
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700223 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700224 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700225 private State mState = State.WORKSPACE;
226 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700227
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700228 private boolean mIsSafeModeEnabled;
229
Adam Cohenc2d6e892014-10-16 09:49:24 -0700230 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
231 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700232 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700233
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700235 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
236 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700237 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800240 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000242 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
243 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
244
Winson Chunga2413752012-04-03 14:22:34 -0700245 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700246 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
247 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700248 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700249 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700250
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800251 private final BroadcastReceiver mCloseSystemDialogsReceiver
252 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800253 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 private LayoutInflater mInflater;
256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700258 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800259 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800260 private DragLayer mDragLayer;
261 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700262 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700263
Sunny Goyalffe83f12014-08-14 17:39:34 -0700264 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700265 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700266
Michael Jurkac9d95c52011-08-29 14:03:34 -0700267 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700268 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800269 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700270
Michael Jurka0280c3b2010-09-17 15:00:07 -0700271 private int[] mTmpAddItemCellCoordinates = new int[2];
272
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 private FolderInfo mFolderInfo;
274
Winson Chung3d503fb2011-07-13 17:25:49 -0700275 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800276 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700277
Michael Jurka838a4ca2011-02-07 13:33:06 -0800278 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700279
Winson Chungc51db6a2011-10-05 11:44:49 -0700280 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700281 private AppsCustomizeTabHost mAppsCustomizeTabHost;
282 private AppsCustomizePagedView mAppsCustomizeContent;
283 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800284 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700287 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
288 // scroll issues (because the workspace may not have been measured yet) and extra work.
289 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
290 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291
292 private SpannableStringBuilder mDefaultKeySsb = null;
293
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294 private boolean mWorkspaceLoading = true;
295
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800296 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 private boolean mRestoring;
298 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700299 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300
Michael Jurka1e2f4652013-07-08 18:03:46 -0700301 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700302 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 private Bundle mSavedInstanceState;
305
Joe Onorato9c1289c2009-08-17 11:03:03 -0400306 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800307 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800308 private boolean mUserPresent = true;
309 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700310 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800311 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400312
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700313 private static LocaleConfiguration sLocaleConfiguration = null;
314
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700315 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700316
Adam Cohen61f560d2013-09-30 15:58:20 -0700317 private View.OnTouchListener mHapticFeedbackTouchListener;
318
Adam Cohended9f8d2010-11-03 13:25:16 -0700319 // Related to the auto-advancing of widgets
320 private final int ADVANCE_MSG = 1;
321 private final int mAdvanceInterval = 20000;
322 private final int mAdvanceStagger = 250;
323 private long mAutoAdvanceSentTime;
324 private long mAutoAdvanceTimeLeft = -1;
325 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
326 new HashMap<View, AppWidgetProviderInfo>();
327
Winson Chung400438b2011-01-16 17:53:48 -0800328 // Determines how long to wait after a rotation before restoring the screen orientation to
329 // match the sensor state.
330 private final int mRestoreScreenOrientationDelay = 500;
331
Michael Jurka4ef207b2010-11-29 17:05:45 -0800332 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700333 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
334 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
Winson Chungd64a6662013-09-30 11:06:59 -0700335
Craig Mautner360310b2012-10-26 15:13:08 -0700336 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700337
Adam Cohen1462de32012-07-24 22:34:36 -0700338 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700339 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700340
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700341 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700342 static Date sDateStamp = new Date();
343 static DateFormat sDateFormat =
344 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
345 static long sRunStart = System.currentTimeMillis();
346 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700347
Winson Chung46353de2012-02-16 14:05:10 -0800348 // We only want to get the SharedPreferences once since it does an FS stat each time we get
349 // it from the context.
350 private SharedPreferences mSharedPrefs;
351
Adam Cohene25af792013-06-06 23:08:25 -0700352 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
353
Winson Chungf0c6ae02012-03-21 16:10:31 -0700354 // Holds the page that we need to animate to, and the icon views that we need to animate up
355 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700356 private ImageView mFolderIconImageView;
357 private Bitmap mFolderIconBitmap;
358 private Canvas mFolderIconCanvas;
359 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700360
Michael Jurkaddd62e92011-02-16 17:49:14 -0800361 private BubbleTextView mWaitingForResume;
362
Michael Jurkac1f5d262011-09-30 19:32:27 -0700363 private Runnable mBuildLayersRunnable = new Runnable() {
364 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700365 if (mWorkspace != null) {
366 mWorkspace.buildPageHardwareLayers();
367 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700368 }
369 };
370
Adam Cohendb364c32014-05-20 14:23:40 -0700371 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800372
Michael Jurka7267fa52013-09-26 15:29:57 -0700373 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800374
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800375 private static class PendingAddArguments {
376 int requestCode;
377 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700378 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700379 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800380 int cellX;
381 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700382 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800383 }
384
Daniel Sandlerff02d492013-08-05 02:12:05 -0400385 private Stats mStats;
386
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700387 FocusIndicatorView mFocusHandler;
388
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800389 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800390 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700391 }
392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 @Override
394 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700395 if (DEBUG_STRICT_MODE) {
396 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
397 .detectDiskReads()
398 .detectDiskWrites()
399 .detectNetwork() // or .detectAll() for all detectable problems
400 .penaltyLog()
401 .build());
402 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
403 .detectLeakedSqlLiteObjects()
404 .detectLeakedClosableObjects()
405 .penaltyLog()
406 .penaltyDeath()
407 .build());
408 }
409
Adam Cohen9211d422014-10-07 18:14:53 -0700410 if (mLauncherCallbacks != null) {
411 mLauncherCallbacks.preOnCreate();
412 }
413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400415
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400416 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400417 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700418 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700419
Winson Chung5f8afe62013-08-12 16:19:28 -0700420 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700421 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700422
423 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400424 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700425 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700426 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800427 mModel = app.setLauncher(this);
428 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700429 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400430 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700432
Daniel Sandlerff02d492013-08-05 02:12:05 -0400433 mStats = new Stats(this);
434
Sunny Goyalffe83f12014-08-14 17:39:34 -0700435 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700436
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700437 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
438 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700439
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700440 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
441 // this also ensures that any synchronous binding below doesn't re-trigger another
442 // LauncherModel load.
443 mPaused = false;
444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200446 android.os.Debug.startMethodTracing(
447 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 }
449
450 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100454 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800456 registerContentObservers();
457
Joe Onorato7c312c12009-08-13 21:36:53 -0700458 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 mSavedState = savedInstanceState;
461 restoreState(mSavedState);
462
463 if (PROFILE_STARTUP) {
464 android.os.Debug.stopMethodTracing();
465 }
466
467 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700468 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 // If the user leaves launcher, then we should just load items asynchronously when
470 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500471 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700472 } else {
473 // We only load the page synchronously if the user rotates (or triggers a
474 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800475 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 }
478
479 // For handling default keys
480 mDefaultKeySsb = new SpannableStringBuilder();
481 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800482
483 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
484 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800485
Adam Cohenf9426d52012-06-04 17:26:21 -0700486 updateGlobalIcons();
487
488 // On large interfaces, we want the screen to auto-rotate based on the current orientation
489 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700490
Adam Cohen432609a2014-03-13 17:03:22 -0700491 if (shouldShowIntroScreen()) {
492 showIntroScreen();
493 } else {
494 showFirstRunActivity();
Sunny Goyal88d60f12014-09-08 03:47:29 -0700495 showFirstRunClings();
Winson Chunga6945242014-01-08 14:04:34 -0800496 }
Adam Cohen9211d422014-10-07 18:14:53 -0700497
498 if (mLauncherCallbacks != null) {
499 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700500 if (mLauncherCallbacks.hasLauncherOverlay()) {
501 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700502 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
503 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
504 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700505 mWorkspace.setLauncherOverlay(mLauncherOverlay);
506 }
Adam Cohen9211d422014-10-07 18:14:53 -0700507 }
508 }
509
510 private LauncherCallbacks mLauncherCallbacks;
511
512 public void onPostCreate(Bundle savedInstanceState) {
513 super.onPostCreate(savedInstanceState);
514 if (mLauncherCallbacks != null) {
515 mLauncherCallbacks.onPostCreate(savedInstanceState);
516 }
517 }
518
519 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
520 mLauncherCallbacks = callbacks;
521 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700522 }
523
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700524 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700525 public void onLauncherProviderChange() {
526 if (mLauncherCallbacks != null) {
527 mLauncherCallbacks.onLauncherProviderChange();
528 }
529 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700530
Adam Cohen9211d422014-10-07 18:14:53 -0700531 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700532 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700533 if (mLauncherCallbacks != null) {
534 return mLauncherCallbacks.hasCustomContentToLeft();
535 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700536 return false;
537 }
538
Dave Hawkeya8881582013-09-17 15:55:33 -0600539 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500540 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600541 * {@link #addToCustomContentPage}. This will only be invoked if
542 * {@link #hasCustomContentToLeft()} is {@code true}.
543 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500544 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700545 if (mLauncherCallbacks != null) {
546 mLauncherCallbacks.populateCustomContentContainer();
547 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600548 }
549
550 /**
551 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
552 * ensure the custom content page is added or removed if necessary.
553 */
554 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600555 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600556 // Not bound yet, wait for bindScreens to be called.
557 return;
558 }
559
560 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
561 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500562 mWorkspace.createCustomContentContainer();
563 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600564 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
565 mWorkspace.removeCustomContentPage();
566 }
567 }
568
Adam Cohenf9426d52012-06-04 17:26:21 -0700569 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700570 boolean searchVisible = false;
571 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800572 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700573 int coi = getCurrentOrientationIndexForGlobalIcons();
Adam Cohena00673c2014-08-14 12:57:28 -0700574 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700575 searchVisible = updateGlobalSearchIcon();
576 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700577 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700578 if (sGlobalSearchIcon[coi] != null) {
579 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700580 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700581 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700582 if (sVoiceSearchIcon[coi] != null) {
583 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700584 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700585 }
Winson Chungadf0c182012-08-23 14:59:07 -0700586 if (mSearchDropTargetBar != null) {
587 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
588 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 }
Romain Guycbb89e42009-06-08 15:52:54 -0700590
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700592 if (sLocaleConfiguration == null) {
593 new AsyncTask<Void, Void, LocaleConfiguration>() {
594 @Override
595 protected LocaleConfiguration doInBackground(Void... unused) {
596 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
597 readConfiguration(Launcher.this, localeConfiguration);
598 return localeConfiguration;
599 }
600
601 @Override
602 protected void onPostExecute(LocaleConfiguration result) {
603 sLocaleConfiguration = result;
604 checkForLocaleChange(); // recursive, but now with a locale configuration
605 }
606 }.execute();
607 return;
608 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700609
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 final Configuration configuration = getResources().getConfiguration();
611
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700612 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 final String locale = configuration.locale.toString();
614
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700615 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 final int mcc = configuration.mcc;
617
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700618 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 final int mnc = configuration.mnc;
620
Romain Guy84f296c2009-11-04 15:00:44 -0800621 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622
Romain Guy84f296c2009-11-04 15:00:44 -0800623 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700624 sLocaleConfiguration.locale = locale;
625 sLocaleConfiguration.mcc = mcc;
626 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700627
Joe Onorato0589f0f2010-02-08 13:44:00 -0800628 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700629
630 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100631 new AsyncTask<Void, Void, Void>() {
632 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700633 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100634 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700635 }
Michael Jurka43467462013-11-14 12:03:58 +0100636 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700637 }
638 }
639
640 private static class LocaleConfiguration {
641 public String locale;
642 public int mcc = -1;
643 public int mnc = -1;
644 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700645
Romain Guy98d01652009-06-30 16:21:04 -0700646 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
647 DataInputStream in = null;
648 try {
Helena Josol28db2802014-10-09 17:04:09 +0100649 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700650 configuration.locale = in.readUTF();
651 configuration.mcc = in.readInt();
652 configuration.mnc = in.readInt();
653 } catch (FileNotFoundException e) {
654 // Ignore
655 } catch (IOException e) {
656 // Ignore
657 } finally {
658 if (in != null) {
659 try {
660 in.close();
661 } catch (IOException e) {
662 // Ignore
663 }
664 }
665 }
666 }
667
668 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
669 DataOutputStream out = null;
670 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100671 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100672 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700673 out.writeUTF(configuration.locale);
674 out.writeInt(configuration.mcc);
675 out.writeInt(configuration.mnc);
676 out.flush();
677 } catch (FileNotFoundException e) {
678 // Ignore
679 } catch (IOException e) {
680 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100681 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700682 } finally {
683 if (out != null) {
684 try {
685 out.close();
686 } catch (IOException e) {
687 // Ignore
688 }
689 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 }
691 }
692
Anton Hanssona2f665f2013-09-26 12:18:32 +0100693 public Stats getStats() {
694 return mStats;
695 }
696
Bjorn Bringertc459e522013-06-07 19:36:01 +0100697 public LayoutInflater getInflater() {
698 return mInflater;
699 }
700
Winson Chung36a62fe2012-05-06 18:04:42 -0700701 boolean isDraggingEnabled() {
702 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
703 // that is subsequently removed from the workspace in startBinding().
704 return !mModel.isLoadingWorkspace();
705 }
706
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 static int getScreen() {
708 synchronized (sLock) {
709 return sScreen;
710 }
711 }
712
713 static void setScreen(int screen) {
714 synchronized (sLock) {
715 sScreen = screen;
716 }
717 }
718
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000719 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100720 if (Build.VERSION.SDK_INT >= 17) {
721 return View.generateViewId();
722 } else {
723 // View.generateViewId() is not available. The following fallback logic is a copy
724 // of its implementation.
725 for (;;) {
726 final int result = sNextGeneratedId.get();
727 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
728 int newValue = result + 1;
729 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
730 if (sNextGeneratedId.compareAndSet(result, newValue)) {
731 return result;
732 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000733 }
734 }
735 }
736
737 public int getViewIdForItem(ItemInfo info) {
738 // This cast is safe given the > 2B range for int.
739 int itemId = (int) info.id;
740 if (mItemIdToViewId.containsKey(itemId)) {
741 return mItemIdToViewId.get(itemId);
742 }
743 int viewId = generateViewId();
744 mItemIdToViewId.put(itemId, viewId);
745 return viewId;
746 }
747
748 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700749 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
750 * a configuration step, this allows the proper animations to run after other transitions.
751 */
Adam Cohendb364c32014-05-20 14:23:40 -0700752 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700753 long screenId = args.screenId;
754 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
755 // When the screen id represents an actual screen (as opposed to a rank) we make sure
756 // that the drop page actually exists.
757 screenId = ensurePendingDropLayoutExists(args.screenId);
758 }
Adam Cohendb364c32014-05-20 14:23:40 -0700759
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800760 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800761 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700762 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700763 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700764 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800765 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700766 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700767 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700768 case REQUEST_RECONFIGURE_APPWIDGET:
769 completeRestoreAppWidget(args.appWidgetId);
770 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800771 }
Michael Jurka27614d22012-04-02 06:40:22 -0700772 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
773 // if you turned the screen off and then back while in All Apps, Launcher would not
774 // return to the workspace. Clearing mAddInfo.container here fixes this issue
775 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700776 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800777 }
778
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700780 protected void onActivityResult(
781 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700782 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700783 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700784 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800785 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700786
Adam Cohenad4e15c2013-10-17 16:21:35 -0700787 Runnable exitSpringLoaded = new Runnable() {
788 @Override
789 public void run() {
790 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
791 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
792 }
793 };
794
Michael Jurka8b805b12012-04-18 14:23:14 -0700795 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700796 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700797 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
798 if (resultCode == RESULT_CANCELED) {
799 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700800 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700801 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700802 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700803 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
804 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700805 }
806 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200807 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
808 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
809 mWorkspace.exitOverviewMode(false);
810 }
811 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700812 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200813
Adam Cohened66b2b2012-01-23 17:28:51 -0800814 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
815 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700816
Adam Cohendb364c32014-05-20 14:23:40 -0700817 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800818 // We have special handling for widgets
819 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800820 final int appWidgetId;
821 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
822 : -1;
823 if (widgetId < 0) {
824 appWidgetId = pendingAddWidgetId;
825 } else {
826 appWidgetId = widgetId;
827 }
828
Adam Cohenad4e15c2013-10-17 16:21:35 -0700829 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800830 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700831 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
832 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700833 result = RESULT_CANCELED;
834 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700835 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700836 @Override
837 public void run() {
838 exitSpringLoadedDragModeDelayed(false, 0, null);
839 }
840 };
Adam Cohendb364c32014-05-20 14:23:40 -0700841 if (workspaceLocked) {
842 // No need to remove the empty screen if we're mid-binding, as the
843 // the bind will not add the empty screen.
844 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
845 } else {
846 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
847 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
848 }
Winson Chung5aaab772012-04-27 15:24:02 -0700849 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700850 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700851 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
852 // When the screen id represents an actual screen (as opposed to a rank)
853 // we make sure that the drop page actually exists.
854 mPendingAddInfo.screenId =
855 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
856 }
Adam Cohendb364c32014-05-20 14:23:40 -0700857 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
858
859 dropLayout.setDropPending(true);
860 final Runnable onComplete = new Runnable() {
861 @Override
862 public void run() {
863 completeTwoStageWidgetDrop(resultCode, appWidgetId);
864 dropLayout.setDropPending(false);
865 }
866 };
867 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
868 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
869 } else {
870 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
871 mPendingAddInfo);
872 sPendingAddItem = args;
873 }
Winson Chung5aaab772012-04-27 15:24:02 -0700874 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 return;
876 }
877
Sunny Goyalff572272014-07-23 13:58:07 -0700878 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
879 if (resultCode == RESULT_OK) {
880 // Update the widget view.
881 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
882 pendingAddWidgetId, mPendingAddInfo);
883 if (workspaceLocked) {
884 sPendingAddItem = args;
885 } else {
886 completeAdd(args);
887 }
888 }
889 // Leave the widget in the pending state if the user canceled the configure.
890 return;
891 }
892
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 // The pattern used here is that a user PICKs a specific application,
894 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700895
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
897 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700898 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700899 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
900 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800901 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700902 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800903 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700904 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700905 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
906 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 }
Adam Cohen00074722013-10-11 17:46:09 -0700908 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700909 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700910 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800912 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 }
914
Adam Cohendb364c32014-05-20 14:23:40 -0700915 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
916 appWidgetId, ItemInfo info) {
917 PendingAddArguments args = new PendingAddArguments();
918 args.requestCode = requestCode;
919 args.intent = data;
920 args.container = info.container;
921 args.screenId = info.screenId;
922 args.cellX = info.cellX;
923 args.cellY = info.cellY;
924 args.appWidgetId = appWidgetId;
925 return args;
926 }
927
928 /**
929 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
930 *
931 * @param screenId the screen id to check
932 * @return the new screen, or screenId if it exists
933 */
934 private long ensurePendingDropLayoutExists(long screenId) {
935 CellLayout dropLayout =
936 (CellLayout) mWorkspace.getScreenWithId(screenId);
937 if (dropLayout == null) {
938 // it's possible that the add screen was removed because it was
939 // empty and a re-bind occurred
940 mWorkspace.addExtraEmptyScreen();
941 return mWorkspace.commitExtraEmptyScreen();
942 } else {
943 return screenId;
944 }
945 }
946
Adam Cohened66b2b2012-01-23 17:28:51 -0800947 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700948 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700949 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800950 Runnable onCompleteRunnable = null;
951 int animationType = 0;
952
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700953 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800954 if (resultCode == RESULT_OK) {
955 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
956 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700957 mPendingAddWidgetInfo);
958 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800959 onCompleteRunnable = new Runnable() {
960 @Override
961 public void run() {
962 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700963 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700964 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
965 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800966 }
967 };
968 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800969 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800970 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800971 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700972 if (mDragLayer.getAnimatedView() != null) {
973 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
974 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
975 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700976 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700977 // The animated view may be null in the case of a rotation during widget configuration
978 onCompleteRunnable.run();
979 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 }
981
982 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700983 protected void onStop() {
984 super.onStop();
985 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700986
987 if (mLauncherCallbacks != null) {
988 mLauncherCallbacks.onStop();
989 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700990 }
991
992 @Override
993 protected void onStart() {
994 super.onStart();
995 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700996
997 if (mLauncherCallbacks != null) {
998 mLauncherCallbacks.onStart();
999 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001000 }
1001
1002 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001004 long startTime = 0;
1005 if (DEBUG_RESUME_TIME) {
1006 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001007 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001008 }
Adam Cohen9211d422014-10-07 18:14:53 -07001009
1010 if (mLauncherCallbacks != null) {
1011 mLauncherCallbacks.preOnResume();
1012 }
1013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001015
Winson Chung4a2afa32012-07-19 14:53:05 -07001016 // Restore the previous launcher state
1017 if (mOnResumeState == State.WORKSPACE) {
1018 showWorkspace(false);
1019 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c62013-11-06 15:49:51 -08001020 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001021 }
1022 mOnResumeState = State.NONE;
1023
Craig Mautner360310b2012-10-26 15:13:08 -07001024 // Background was set to gradient in onPause(), restore to black if in all apps.
1025 setWorkspaceBackground(mState == State.WORKSPACE);
1026
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001027 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001028 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001029 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001030 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001031 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001032 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001034 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001035 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1036 // execute them here
1037 long startTimeCallbacks = 0;
1038 if (DEBUG_RESUME_TIME) {
1039 startTimeCallbacks = System.currentTimeMillis();
1040 }
1041
1042 if (mAppsCustomizeContent != null) {
1043 mAppsCustomizeContent.setBulkBind(true);
1044 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001045 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1046 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001047 }
1048 if (mAppsCustomizeContent != null) {
1049 mAppsCustomizeContent.setBulkBind(false);
1050 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001051 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001052 if (DEBUG_RESUME_TIME) {
1053 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1054 (System.currentTimeMillis() - startTimeCallbacks));
1055 }
Michael Jurka447bf842013-05-15 14:52:15 +02001056 }
Michael Jurka54554252013-08-01 12:52:23 +02001057 if (mOnResumeCallbacks.size() > 0) {
1058 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1059 mOnResumeCallbacks.get(i).run();
1060 }
1061 mOnResumeCallbacks.clear();
1062 }
Winson Chunge4e50662012-01-23 14:45:13 -08001063
1064 // Reset the pressed state of icons that were locked in the press state while activities
1065 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001066 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001067 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001068 mWaitingForResume.setStayPressed(false);
1069 }
Winson Chung82963d52013-10-09 11:20:57 -07001070
Adam Cohen06dff352012-06-01 17:17:08 -07001071 // It is possible that widgets can receive updates while launcher is not in the foreground.
1072 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1073 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1074 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001075 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001076
Winson Chung780fe592013-09-26 14:48:44 -07001077 // Process any items that were added while Launcher was away.
1078 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1079
Winson Chung5841efa2013-09-30 18:06:44 -07001080 // Update the voice search button proxy
1081 updateVoiceButtonProxyVisible(false);
1082
Adam Cohenf9426d52012-06-04 17:26:21 -07001083 // Again, as with the above scenario, it's possible that one or more of the global icons
1084 // were updated in the wrong orientation.
1085 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001086 if (DEBUG_RESUME_TIME) {
1087 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1088 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001089
1090 if (mWorkspace.getCustomContentCallbacks() != null) {
1091 // If we are resuming and the custom content is the current page, we call onShow().
1092 // It is also poassible that onShow will instead be called slightly after first layout
1093 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1094 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001095 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001096 }
1097 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001098 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001099 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001100
1101 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001102
1103 if (mLauncherCallbacks != null) {
1104 mLauncherCallbacks.onResume();
1105 }
Adam Cohen06dff352012-06-01 17:17:08 -07001106 }
1107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001109 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001110 // Ensure that items added to Launcher are queued until Launcher returns
1111 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001112 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001113
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001114 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001115 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001116 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001117 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001118
1119 // We call onHide() aggressively. The custom content callbacks should be able to
1120 // debounce excess onHide calls.
1121 if (mWorkspace.getCustomContentCallbacks() != null) {
1122 mWorkspace.getCustomContentCallbacks().onHide();
1123 }
Romain Guycbb89e42009-06-08 15:52:54 -07001124
Adam Cohen9211d422014-10-07 18:14:53 -07001125 if (mLauncherCallbacks != null) {
1126 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001127 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001128 }
1129
1130 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001131 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1132 // by a onResume or by scrolling otherwise.
1133 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001134
1135 // Custom content is completely hidden
1136 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001137
1138 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1139 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001140
1141 // Indicates whether the user is allowed to scroll away from the custom content.
1142 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001143 }
1144
Adam Cohenc2d6e892014-10-16 09:49:24 -07001145 public interface LauncherOverlay {
1146
1147 /**
1148 * Touch interaction leading to overscroll has begun
1149 */
1150 public void onScrollInteractionBegin();
1151
1152 /**
1153 * Touch interaction related to overscroll has ended
1154 */
1155 public void onScrollInteractionEnd();
1156
1157 /**
1158 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1159 * screen (or in the case of RTL, the rightmost screen).
1160 */
1161 public void onScrollChange(int progress, boolean rtl);
1162
1163 /**
1164 * Screen has stopped scrolling
1165 */
1166 public void onScrollSettled();
1167
1168 /**
1169 * This method can be called by the Launcher in order to force the LauncherOverlay
1170 * to exit fully immersive mode.
1171 */
1172 public void forceExitFullImmersion();
1173 }
1174
1175 public interface LauncherOverlayCallbacks {
1176 /**
1177 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1178 * however it doesn't modify any state within the launcher.
1179 */
1180 public boolean canEnterFullImmersion();
1181
1182 /**
1183 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1184 * eg. by occupying the full screen and handling all touch events.
1185 *
1186 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1187 * case, Launcher will modify any necessary state and assumes the overlay is
1188 * handling all interaction. If false, the LauncherOverlay should cancel any
1189 *
1190 */
1191 public boolean enterFullImmersion();
1192
1193 /**
1194 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1195 * full control over UI and state.
1196 */
1197 public void exitFullImmersion();
1198 }
1199
1200 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1201
1202 @Override
1203 public boolean canEnterFullImmersion() {
1204 return mState == State.WORKSPACE;
1205 }
1206
1207 @Override
1208 public boolean enterFullImmersion() {
1209 if (mState == State.WORKSPACE) {
1210 // When fully immersed, disregard any touches which fall through.
1211 mDragLayer.setBlockTouch(true);
1212 return true;
1213 }
1214 return false;
1215 }
1216
1217 @Override
1218 public void exitFullImmersion() {
1219 mDragLayer.setBlockTouch(false);
1220 }
1221 }
1222
Jorim Jaggid017f882014-01-14 17:08:48 -08001223 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001224 if (mLauncherCallbacks != null) {
1225 return mLauncherCallbacks.hasSettings();
1226 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001227 return false;
1228 }
1229
Adam Cohenbffe7452013-07-22 18:21:45 -07001230
Adam Cohen9211d422014-10-07 18:14:53 -07001231 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001232 CustomContentCallbacks callbacks, String description) {
1233 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001234 }
1235
Adam Cohenedb40762013-07-18 16:45:45 -07001236 // The custom content needs to offset its content to account for the QSB
1237 public int getTopOffsetForCustomContent() {
1238 return mWorkspace.getPaddingTop();
1239 }
1240
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001241 @Override
1242 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001243 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001244 if (mModel.isCurrentCallbacks(this)) {
1245 mModel.stopLoader();
1246 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001247 if (mAppsCustomizeContent != null) {
1248 mAppsCustomizeContent.surrender();
1249 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001250 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001251 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001252
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001253 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001254 @Override
1255 public void onWindowFocusChanged(boolean hasFocus) {
1256 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001257 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001258
1259 if (mLauncherCallbacks != null) {
1260 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1261 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001262 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 private boolean acceptFilter() {
1265 final InputMethodManager inputManager = (InputMethodManager)
1266 getSystemService(Context.INPUT_METHOD_SERVICE);
1267 return !inputManager.isFullscreenMode();
1268 }
1269
1270 @Override
1271 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001272 final int uniChar = event.getUnicodeChar();
1273 final boolean handled = super.onKeyDown(keyCode, event);
1274 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1275 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1277 keyCode, event);
1278 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001279 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001280 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001281 // showSearchDialog()
1282 // If there are multiple keystrokes before the search dialog takes focus,
1283 // onSearchRequested() will be called for every keystroke,
1284 // but it is idempotent, so it's fine.
1285 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 }
1287 }
1288
Joe Onorato8a9625e2010-01-28 15:55:35 -08001289 // Eat the long press event so the keyboard doesn't come up.
1290 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1291 return true;
1292 }
1293
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 return handled;
1295 }
1296
Karl Rosaen138a0412009-04-23 19:00:21 -07001297 private String getTypedText() {
1298 return mDefaultKeySsb.toString();
1299 }
1300
1301 private void clearTypedText() {
1302 mDefaultKeySsb.clear();
1303 mDefaultKeySsb.clearSpans();
1304 Selection.setSelection(mDefaultKeySsb, 0);
1305 }
1306
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001308 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1309 * State
1310 */
1311 private static State intToState(int stateOrdinal) {
1312 State state = State.WORKSPACE;
1313 final State[] stateValues = State.values();
1314 for (int i = 0; i < stateValues.length; i++) {
1315 if (stateValues[i].ordinal() == stateOrdinal) {
1316 state = stateValues[i];
1317 break;
1318 }
1319 }
1320 return state;
1321 }
1322
1323 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 * Restores the previous state, if it exists.
1325 *
1326 * @param savedState The previous state.
1327 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001328 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 private void restoreState(Bundle savedState) {
1330 if (savedState == null) {
1331 return;
1332 }
1333
Michael Jurka883f55b2010-10-21 15:47:14 -07001334 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001335 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001336 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001337 }
1338
Adam Cohen21cd0022013-10-09 18:57:02 -07001339 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1340 PagedView.INVALID_RESTORE_PAGE);
1341 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001342 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
1344
Winson Chung3d503fb2011-07-13 17:25:49 -07001345 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001346 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001347
Winson Chung3d503fb2011-07-13 17:25:49 -07001348 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1349 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001350 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001351 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1352 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001353 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1354 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1355 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001356 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001357 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 mRestoring = true;
1359 }
1360
1361 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1362 if (renameFolder) {
1363 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001364 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 mRestoring = true;
1366 }
Winson Chunga12a2502010-12-20 14:41:35 -08001367
Winson Chung785d2eb2011-04-14 16:08:02 -07001368 // Restore the AppsCustomize tab
1369 if (mAppsCustomizeTabHost != null) {
1370 String curTab = savedState.getString("apps_customize_currentTab");
1371 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001372 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001373 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001374 mAppsCustomizeContent.loadAssociatedPages(
1375 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001376 }
1377
Winson Chung5afbf7b2011-07-25 11:53:08 -07001378 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1379 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001380 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001381 mItemIdToViewId = (HashMap<Integer, Integer>)
1382 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 }
1384
1385 /**
1386 * Finds all the views we need and configure them properly.
1387 */
1388 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001389 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001390
Craig Mautner360310b2012-10-26 15:13:08 -07001391 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001392 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001393 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1394 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001395 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001396 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001397
John Spurlock77e1f472013-09-11 10:09:51 -04001398 mLauncherView.setSystemUiVisibility(
1399 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001400 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401
Winson Chung4c98d922011-05-31 16:50:48 -07001402 // Setup the drag layer
1403 mDragLayer.setup(this, dragController);
1404
Winson Chung3d503fb2011-07-13 17:25:49 -07001405 // Setup the hotseat
1406 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1407 if (mHotseat != null) {
1408 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001409 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001410 }
1411
Jorim Jaggid017f882014-01-14 17:08:48 -08001412 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001413 View widgetButton = findViewById(R.id.widget_button);
1414 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001415 @Override
1416 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001417 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001418 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001419 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001420 }
1421 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001422 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1423
1424 View wallpaperButton = findViewById(R.id.wallpaper_button);
1425 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001426 @Override
1427 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001428 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001429 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001430 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001431 }
1432 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001433 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1434
1435 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001436 if (hasSettings()) {
1437 settingsButton.setOnClickListener(new OnClickListener() {
1438 @Override
1439 public void onClick(View arg0) {
1440 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001441 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001442 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001443 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001444 });
1445 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1446 } else {
1447 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001448 }
1449
Adam Cohendbdff6b2013-09-18 19:09:15 -07001450 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001451
Winson Chung4c98d922011-05-31 16:50:48 -07001452 // Setup the workspace
1453 mWorkspace.setHapticFeedbackEnabled(false);
1454 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001455 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001456 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001457
Winson Chungf0ea4d32011-06-06 14:27:16 -07001458 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001459 mSearchDropTargetBar = (SearchDropTargetBar)
1460 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001461
Winson Chungf0ea4d32011-06-06 14:27:16 -07001462 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001463 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001464 mAppsCustomizeContent = (AppsCustomizePagedView)
1465 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1466 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001467
Winson Chung3d503fb2011-07-13 17:25:49 -07001468 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001469 dragController.setDragScoller(mWorkspace);
1470 dragController.setScrollView(mDragLayer);
1471 dragController.setMoveTarget(mWorkspace);
1472 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001473 if (mSearchDropTargetBar != null) {
1474 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001475 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001476
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001477 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001478 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001479 mWeightWatcher = new WeightWatcher(this);
1480 mWeightWatcher.setAlpha(0.5f);
1481 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001482 new FrameLayout.LayoutParams(
1483 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001484 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001485 Gravity.BOTTOM)
1486 );
Adam Cohen39a06042013-07-19 14:30:12 -07001487
1488 boolean show = shouldShowWeightWatcher();
1489 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001490 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
1492
1493 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001494 * Sets the all apps button. This method is called from {@link Hotseat}.
1495 */
1496 public void setAllAppsButton(View allAppsButton) {
1497 mAllAppsButton = allAppsButton;
1498 }
1499
1500 public View getAllAppsButton() {
1501 return mAllAppsButton;
1502 }
1503
1504 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 * Creates a view representing a shortcut.
1506 *
1507 * @param info The data structure describing the shortcut.
1508 *
1509 * @return A View inflated from R.layout.application.
1510 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001511 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001513 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 }
1515
1516 /**
1517 * Creates a view representing a shortcut inflated from the specified resource.
1518 *
1519 * @param layoutResId The id of the XML layout used to create the shortcut.
1520 * @param parent The group the shortcut belongs to.
1521 * @param info The data structure describing the shortcut.
1522 *
1523 * @return A View inflated from layoutResId.
1524 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001525 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001526 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001527 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001529 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 return favorite;
1531 }
1532
1533 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 * Add a shortcut to the workspace.
1535 *
1536 * @param data The intent describing the shortcut.
1537 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001539 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001540 int cellY) {
1541 int[] cellXY = mTmpAddItemCellCoordinates;
1542 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001543 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001544
Michael Jurkad3ef3062010-11-23 16:23:58 -08001545 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001546
Sunny Goyal2350bc92014-10-14 16:42:54 -07001547 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001548 if (info == null) {
1549 return;
1550 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001551 final View view = createShortcut(info);
1552
Adam Cohenfbba09b2011-07-18 21:43:05 -07001553 // First we check if we already know the exact location where we want to add this item.
1554 if (cellX >= 0 && cellY >= 0) {
1555 cellXY[0] = cellX;
1556 cellXY[1] = cellY;
1557 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001558
1559 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001560 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001561 true, null,null)) {
1562 return;
1563 }
1564 DragObject dragObject = new DragObject();
1565 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001566 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1567 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001568 return;
1569 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001570 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001571 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001572 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001573 foundCellSpan = (result != null);
1574 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001575 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001576 }
1577
1578 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001579 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001580 return;
1581 }
1582
Adam Cohendcd297f2013-06-18 13:13:40 -07001583 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584
1585 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001586 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001587 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 }
1589 }
1590
Adam Cohen2f093b62012-04-30 18:59:53 -07001591 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1592 int minHeight) {
1593 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001594 // We want to account for the extra amount of padding that we are adding to the widget
1595 // to ensure that it gets the full amount of space that it has requested
1596 int requiredWidth = minWidth + padding.left + padding.right;
1597 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001598 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001599 }
1600
Adam Cohen2f093b62012-04-30 18:59:53 -07001601 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1602 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001603 }
1604
Adam Cohen2f093b62012-04-30 18:59:53 -07001605 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1606 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001607 }
1608
Adam Cohen2f093b62012-04-30 18:59:53 -07001609 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1610 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001611 }
1612
Adam Cohen2f093b62012-04-30 18:59:53 -07001613 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1614 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001615 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001616 }
1617
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001619 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001621 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001622 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001624 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001625 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1626 if (appWidgetInfo == null) {
1627 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1628 }
Romain Guycbb89e42009-06-08 15:52:54 -07001629
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001630 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001631 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001632
Adam Cohen2f093b62012-04-30 18:59:53 -07001633 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1634 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001635
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001637 // We have saved the position to which the widget was dragged-- this really only matters
1638 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001639 int[] cellXY = mTmpAddItemCellCoordinates;
1640 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001641 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001642 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001643 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1644 cellXY[0] = mPendingAddInfo.cellX;
1645 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001646 spanXY[0] = mPendingAddInfo.spanX;
1647 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001648 foundCellSpan = true;
1649 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001650 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001651 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001652 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1653 spanXY[1], cellXY, finalSpan);
1654 spanXY[0] = finalSpan[0];
1655 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001656 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001657 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001658 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001659 }
1660
Michael Jurka0280c3b2010-09-17 15:00:07 -07001661 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001662 if (appWidgetId != -1) {
1663 // Deleting an app widget ID is a void call but writes to disk before returning
1664 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001665 new AsyncTask<Void, Void, Void>() {
1666 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001667 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001668 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001669 }
Michael Jurka43467462013-11-14 12:03:58 +01001670 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001671 }
Winson Chung93eef082012-03-23 15:59:27 -07001672 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001673 return;
1674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001676 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001677 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1678 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001679 launcherInfo.spanX = spanXY[0];
1680 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001681 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1682 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001683 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001684
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001686 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687
1688 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001689 if (hostView == null) {
1690 // Perform actual inflation because we're live
1691 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1692 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1693 } else {
1694 // The AppWidgetHostView has already been inflated and instantiated
1695 launcherInfo.hostView = hostView;
1696 }
Romain Guycbb89e42009-06-08 15:52:54 -07001697
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001699 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001700 launcherInfo.notifyWidgetSizeChanged(this);
1701
Adam Cohendcd297f2013-06-18 13:13:40 -07001702 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001703 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001704
1705 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001707 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 }
Romain Guycbb89e42009-06-08 15:52:54 -07001709
Adam Cohended9f8d2010-11-03 13:25:16 -07001710 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1711 @Override
1712 public void onReceive(Context context, Intent intent) {
1713 final String action = intent.getAction();
1714 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1715 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001716 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001718
Winson Chungc100e8e2011-08-09 16:02:43 -07001719 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001720 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001721 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001722 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001723 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001724 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1725 mUserPresent = true;
1726 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001727 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1728 mModel.resetLoadedState(false, true);
1729 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1730 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1731 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1732 mModel.resetLoadedState(false, true);
1733 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1734 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1735 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001736 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1737 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1738 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 }
1740 }
1741 };
1742
1743 @Override
1744 public void onAttachedToWindow() {
1745 super.onAttachedToWindow();
1746
1747 // Listen for broadcasts related to user-presence
1748 final IntentFilter filter = new IntentFilter();
1749 filter.addAction(Intent.ACTION_SCREEN_OFF);
1750 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001751 // For handling managed profiles
1752 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1753 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001754 if (ENABLE_DEBUG_INTENTS) {
1755 filter.addAction(DebugIntents.DELETE_DATABASE);
1756 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1757 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001759 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001760 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001761 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 mVisible = true;
1763 }
1764
Adam Cohen0b395352014-06-09 22:54:36 +00001765 /**
1766 * Sets up transparent navigation and status bars in LMP.
1767 * This method is a no-op for other platform versions.
1768 */
1769 @TargetApi(19)
1770 private void setupTransparentSystemBarsForLmp() {
1771 // TODO(sansid): use the APIs directly when compiling against L sdk.
1772 // Currently we use reflection to access the flags and the API to set the transparency
1773 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001774 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001775 try {
1776 getWindow().getAttributes().systemUiVisibility |=
1777 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1778 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1779 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1780 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1781 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1782 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1783 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1784 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1785
1786 Method setStatusBarColorMethod =
1787 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1788 Method setNavigationBarColorMethod =
1789 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1790 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1791 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1792 } catch (NoSuchFieldException e) {
1793 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1794 } catch (NoSuchMethodException ex) {
1795 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1796 } catch (IllegalAccessException e) {
1797 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1798 } catch (IllegalArgumentException e) {
1799 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1800 } catch (InvocationTargetException e) {
1801 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1802 } finally {}
1803 }
1804 }
1805
Adam Cohended9f8d2010-11-03 13:25:16 -07001806 @Override
1807 public void onDetachedFromWindow() {
1808 super.onDetachedFromWindow();
1809 mVisible = false;
1810
Adam Cohend113e0c2010-11-11 10:48:05 -08001811 if (mAttached) {
1812 unregisterReceiver(mReceiver);
1813 mAttached = false;
1814 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001815 updateRunning();
1816 }
1817
1818 public void onWindowVisibilityChanged(int visibility) {
1819 mVisible = visibility == View.VISIBLE;
1820 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001821 // The following code used to be in onResume, but it turns out onResume is called when
1822 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1823 // is a more appropriate event to handle
1824 if (mVisible) {
1825 mAppsCustomizeTabHost.onWindowVisible();
1826 if (!mWorkspaceLoading) {
1827 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001828 // We want to let Launcher draw itself at least once before we force it to build
1829 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001830 // apps is nice and speedy.
1831 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001832 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001833 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001834 if (mStarted) return;
1835 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001836 // We delay the layer building a bit in order to give
1837 // other message processing a time to run. In particular
1838 // this avoids a delay in hiding the IME if it was
1839 // currently shown, because doing that may involve
1840 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001841 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001842 final ViewTreeObserver.OnDrawListener listener = this;
1843 mWorkspace.post(new Runnable() {
1844 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001845 if (mWorkspace != null &&
1846 mWorkspace.getViewTreeObserver() != null) {
1847 mWorkspace.getViewTreeObserver().
1848 removeOnDrawListener(listener);
1849 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001850 }
1851 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001852 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001853 }
1854 });
1855 }
1856 clearTypedText();
1857 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001858 }
1859
1860 private void sendAdvanceMessage(long delay) {
1861 mHandler.removeMessages(ADVANCE_MSG);
1862 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1863 mHandler.sendMessageDelayed(msg, delay);
1864 mAutoAdvanceSentTime = System.currentTimeMillis();
1865 }
1866
1867 private void updateRunning() {
1868 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1869 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1870 mAutoAdvanceRunning = autoAdvanceRunning;
1871 if (autoAdvanceRunning) {
1872 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1873 sendAdvanceMessage(delay);
1874 } else {
1875 if (!mWidgetsToAdvance.isEmpty()) {
1876 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1877 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1878 }
1879 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001880 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001881 }
1882 }
1883 }
1884
1885 private final Handler mHandler = new Handler() {
1886 @Override
1887 public void handleMessage(Message msg) {
1888 if (msg.what == ADVANCE_MSG) {
1889 int i = 0;
1890 for (View key: mWidgetsToAdvance.keySet()) {
1891 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1892 final int delay = mAdvanceStagger * i;
1893 if (v instanceof Advanceable) {
1894 postDelayed(new Runnable() {
1895 public void run() {
1896 ((Advanceable) v).advance();
1897 }
1898 }, delay);
1899 }
1900 i++;
1901 }
1902 sendAdvanceMessage(mAdvanceInterval);
1903 }
1904 }
1905 };
1906
1907 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001908 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001909 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1910 if (v instanceof Advanceable) {
1911 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001912 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001913 updateRunning();
1914 }
1915 }
1916
1917 void removeWidgetToAutoAdvance(View hostView) {
1918 if (mWidgetsToAdvance.containsKey(hostView)) {
1919 mWidgetsToAdvance.remove(hostView);
1920 updateRunning();
1921 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001922 }
1923
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001925 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001926 launcherInfo.hostView = null;
1927 }
1928
Winson Chung93eef082012-03-23 15:59:27 -07001929 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1930 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1931 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001932 }
1933
Winson Chunga6945242014-01-08 14:04:34 -08001934 public DragLayer getDragLayer() {
1935 return mDragLayer;
1936 }
1937
1938 public Workspace getWorkspace() {
1939 return mWorkspace;
1940 }
1941
1942 public Hotseat getHotseat() {
1943 return mHotseat;
1944 }
1945
Jorim Jaggid017f882014-01-14 17:08:48 -08001946 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001947 return mOverviewPanel;
1948 }
1949
1950 public SearchDropTargetBar getSearchBar() {
1951 return mSearchDropTargetBar;
1952 }
1953
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001954 public LauncherAppWidgetHost getAppWidgetHost() {
1955 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 }
Romain Guycbb89e42009-06-08 15:52:54 -07001957
Winson Chunga9abd0e2010-10-27 17:18:37 -07001958 public LauncherModel getModel() {
1959 return mModel;
1960 }
1961
Winson Chunga6945242014-01-08 14:04:34 -08001962 protected SharedPreferences getSharedPrefs() {
1963 return mSharedPrefs;
1964 }
1965
Bjorn Bringertc459e522013-06-07 19:36:01 +01001966 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001967 getWindow().closeAllPanels();
1968
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001969 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001970 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001971 }
1972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 @Override
1974 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001975 long startTime = 0;
1976 if (DEBUG_RESUME_TIME) {
1977 startTime = System.currentTimeMillis();
1978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 super.onNewIntent(intent);
1980
1981 // Close the menu
1982 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001983 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001984 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001985
Adam Cohened307df2013-10-02 09:37:31 -07001986 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1987 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1988 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001989
Adam Cohen6fecd412013-10-02 17:41:50 -07001990 if (mWorkspace == null) {
1991 // Can be cases where mWorkspace is null, this prevents a NPE
1992 return;
1993 }
1994 Folder openFolder = mWorkspace.getOpenFolder();
1995 // In all these cases, only animate if we're already on home
1996 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001997
1998 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1999 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07002000 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07002001 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07002002 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07002003 }
Romain Guy1dd3a072009-07-16 13:21:01 -07002004
Adam Cohen6fecd412013-10-02 17:41:50 -07002005 closeFolder();
2006 exitSpringLoadedDragMode();
2007
2008 // If we are already on home, then just animate back to the workspace,
2009 // otherwise, just wait until onResume to set the state back to Workspace
2010 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07002011 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07002012 } else {
2013 mOnResumeState = State.WORKSPACE;
2014 }
2015
2016 final View v = getWindow().peekDecorView();
2017 if (v != null && v.getWindowToken() != null) {
2018 InputMethodManager imm = (InputMethodManager)getSystemService(
2019 INPUT_METHOD_SERVICE);
2020 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
2021 }
2022
2023 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08002024 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07002025 mAppsCustomizeTabHost.reset();
2026 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002027
Adam Cohen9211d422014-10-07 18:14:53 -07002028 if (mLauncherCallbacks != null) {
2029 mLauncherCallbacks.onHomeIntent();
2030 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 }
Adam Cohened307df2013-10-02 09:37:31 -07002032
Michael Jurka447bf842013-05-15 14:52:15 +02002033 if (DEBUG_RESUME_TIME) {
2034 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
2035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036
Adam Cohen9211d422014-10-07 18:14:53 -07002037 if (mLauncherCallbacks != null) {
2038 mLauncherCallbacks.onNewIntent(intent);
2039 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002040 }
2041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07002043 public void onRestoreInstanceState(Bundle state) {
2044 super.onRestoreInstanceState(state);
2045 for (int page: mSynchronouslyBoundPages) {
2046 mWorkspace.restoreInstanceStateForChild(page);
2047 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 }
2049
2050 @Override
2051 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002052 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002053 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2054 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002055 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002056 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057
Michael Jurka883f55b2010-10-21 15:47:14 -07002058 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002059 // We close any open folder since it will not be re-opened, and we need to make sure
2060 // this state is reflected.
2061 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062
Adam Cohendcd297f2013-06-18 13:13:40 -07002063 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002064 mWaitingForResult) {
2065 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002066 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002067 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2068 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002069 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2070 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2071 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002072 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 }
2074
2075 if (mFolderInfo != null && mWaitingForResult) {
2076 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2077 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2078 }
Michael Jurka946ad472010-07-09 18:05:18 -07002079
Winson Chung785d2eb2011-04-14 16:08:02 -07002080 // Save the current AppsCustomize tab
2081 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08002082 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2083 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002084 if (currentTabTag != null) {
2085 outState.putString("apps_customize_currentTab", currentTabTag);
2086 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002087 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2088 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002089 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002090 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002091
2092 if (mLauncherCallbacks != null) {
2093 mLauncherCallbacks.onSaveInstanceState(outState);
2094 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 }
2096
2097 @Override
2098 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002100
Winson Chunge7a03942011-08-05 15:05:12 -07002101 // Remove all pending runnables
2102 mHandler.removeMessages(ADVANCE_MSG);
2103 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002104 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002105
Winson Chungcd2b0142011-06-08 16:02:26 -07002106 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002107 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002108
2109 // It's possible to receive onDestroy after a new Launcher activity has
2110 // been created. In this case, don't interfere with the new Launcher.
2111 if (mModel.isCurrentCallbacks(this)) {
2112 mModel.stopLoader();
2113 app.setLauncher(null);
2114 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002115
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002117 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002119 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002121 mAppWidgetHost = null;
2122
2123 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124
2125 TextKeyListener.getInstance().release();
2126
Winson Chung81b52252012-08-27 15:34:29 -07002127 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2128 // to prevent leaking Launcher activities on orientation change.
2129 if (mModel != null) {
2130 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2131 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002132
2133 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002134 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002135
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002136 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002137 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002138 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002139 mWorkspace = null;
2140 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002141
Michael Jurka2ecf9952012-06-18 12:52:28 -07002142 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002143
2144 if (mLauncherCallbacks != null) {
2145 mLauncherCallbacks.onDestroy();
2146 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 }
2148
Adam Cohena9cf38f2011-05-02 15:36:58 -07002149 public DragController getDragController() {
2150 return mDragController;
2151 }
2152
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 @Override
2154 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002155 if (requestCode >= 0) {
2156 setWaitingForResult(true);
2157 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 super.startActivityForResult(intent, requestCode);
2159 }
2160
Winson Chung70d72102011-08-12 11:24:35 -07002161 /**
2162 * Indicates that we want global search for this activity by setting the globalSearch
2163 * argument for {@link #startSearch} to true.
2164 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002166 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002168
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002169 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002170
Karl Rosaen138a0412009-04-23 19:00:21 -07002171 if (initialQuery == null) {
2172 // Use any text typed in the launcher as the initial query
2173 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002174 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 if (appSearchData == null) {
2176 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002177 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 }
Winson Chungadf0c182012-08-23 14:59:07 -07002179 Rect sourceBounds = new Rect();
2180 if (mSearchDropTargetBar != null) {
2181 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2182 }
Romain Guycbb89e42009-06-08 15:52:54 -07002183
Ian Parkinson047036e2014-09-01 15:40:53 +01002184 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002185 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002186 if (clearTextImmediately) {
2187 clearTypedText();
2188 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002189 }
2190
Ian Parkinson047036e2014-09-01 15:40:53 +01002191 /**
2192 * Start a text search.
2193 *
2194 * @return {@code true} if the search will start immediately, so any further keypresses
2195 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2196 * to buffer keypresses.
2197 */
2198 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002199 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002200 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2201 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2202 sourceBounds);
2203 }
2204
Michael Jurkaa33411c2012-06-14 16:18:21 -07002205 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002206 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002207 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002208 }
2209
2210 /**
2211 * Starts the global search activity. This code is a copied from SearchManager
2212 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002213 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002214 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002215 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002216 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2217 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2218 if (globalSearchActivity == null) {
2219 Log.w(TAG, "No global search activity found.");
2220 return;
2221 }
2222 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2223 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2224 intent.setComponent(globalSearchActivity);
2225 // Make sure that we have a Bundle to put source in
2226 if (appSearchData == null) {
2227 appSearchData = new Bundle();
2228 } else {
2229 appSearchData = new Bundle(appSearchData);
2230 }
Adam Cohen9211d422014-10-07 18:14:53 -07002231 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002232 if (!appSearchData.containsKey("source")) {
2233 appSearchData.putString("source", getPackageName());
2234 }
2235 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2236 if (!TextUtils.isEmpty(initialQuery)) {
2237 intent.putExtra(SearchManager.QUERY, initialQuery);
2238 }
2239 if (selectInitialQuery) {
2240 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2241 }
2242 intent.setSourceBounds(sourceBounds);
2243 try {
2244 startActivity(intent);
2245 } catch (ActivityNotFoundException ex) {
2246 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2247 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 }
2249
Yura4f93ec62014-02-04 14:15:21 +00002250 public boolean isOnCustomContent() {
2251 return mWorkspace.isOnOrMovingToCustomContent();
2252 }
2253
Winson Chung70d72102011-08-12 11:24:35 -07002254 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002255 public boolean onPrepareOptionsMenu(Menu menu) {
2256 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002257 if (!isOnCustomContent()) {
2258 // Close any open folders
2259 closeFolder();
2260 // Stop resizing any widgets
2261 mWorkspace.exitWidgetResizeMode();
2262 if (!mWorkspace.isInOverviewMode()) {
2263 // Show the overview mode
2264 showOverviewMode(true);
2265 } else {
2266 showWorkspace(true);
2267 }
Winson Chunge0298742014-01-17 12:03:00 -08002268 }
Adam Cohen9211d422014-10-07 18:14:53 -07002269 if (mLauncherCallbacks != null) {
2270 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2271 }
2272
Michael Jurkab94f3f82013-09-11 18:08:54 +02002273 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002276 @Override
2277 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002278 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002279 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002280 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002281 }
2282
Joe Onorato9c1289c2009-08-17 11:03:03 -04002283 public boolean isWorkspaceLocked() {
2284 return mWorkspaceLoading || mWaitingForResult;
2285 }
2286
Adam Cohen517a7f52014-03-01 12:12:59 -08002287 public boolean isWorkspaceLoading() {
2288 return mWorkspaceLoading;
2289 }
2290
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002291 private void setWorkspaceLoading(boolean value) {
2292 boolean isLocked = isWorkspaceLocked();
2293 mWorkspaceLoading = value;
2294 if (isLocked != isWorkspaceLocked()) {
2295 onWorkspaceLockedChanged();
2296 }
2297 }
2298
2299 private void setWaitingForResult(boolean value) {
2300 boolean isLocked = isWorkspaceLocked();
2301 mWaitingForResult = value;
2302 if (isLocked != isWorkspaceLocked()) {
2303 onWorkspaceLockedChanged();
2304 }
2305 }
2306
Adam Cohen9211d422014-10-07 18:14:53 -07002307 protected void onWorkspaceLockedChanged() {
2308 if (mLauncherCallbacks != null) {
2309 mLauncherCallbacks.onWorkspaceLockedChanged();
2310 }
2311 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002312
Michael Jurka0280c3b2010-09-17 15:00:07 -07002313 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002314 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002315 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002316 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2317 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002318 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002319 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002320 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002321
Adam Cohenad4e15c2013-10-17 16:21:35 -07002322 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2323 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2324 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2325 }
2326
2327 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2328 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2329 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002330 if (appWidgetInfo.configure != null) {
2331 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002332 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002333
Bjorn Bringert7984c942009-12-09 15:38:25 +00002334 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002335 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2336 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2337
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002338 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002339 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002340 Runnable onComplete = new Runnable() {
2341 @Override
2342 public void run() {
2343 // Exit spring loaded mode if necessary after adding the widget
2344 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2345 null);
2346 }
2347 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002348 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002349 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002350 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 }
2352 }
Romain Guycbb89e42009-06-08 15:52:54 -07002353
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002354 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002355 // Close any folders that may be open.
2356 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002357 mWorkspace.moveToCustomContentScreen(animate);
2358 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002359 /**
2360 * Process a shortcut drop.
2361 *
2362 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002363 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002364 * @param cell The cell it should be added to, optional
2365 * @param position The location on the screen where it was dropped, optional
2366 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002367 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002368 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002369 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002370 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002371 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002372 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002373
2374 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002375 mPendingAddInfo.cellX = cell[0];
2376 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002377 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002378
2379 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2380 createShortcutIntent.setComponent(componentName);
2381 processShortcut(createShortcutIntent);
2382 }
2383
Adam Cohenfbba09b2011-07-18 21:43:05 -07002384 /**
2385 * Process a widget drop.
2386 *
2387 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002388 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002389 * @param cell The cell it should be added to, optional
2390 * @param position The location on the screen where it was dropped, optional
2391 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002392 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002393 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002394 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002395 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002396 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002397 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002398 mPendingAddInfo.minSpanX = info.minSpanX;
2399 mPendingAddInfo.minSpanY = info.minSpanY;
2400
Adam Cohenfbba09b2011-07-18 21:43:05 -07002401 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002402 mPendingAddInfo.cellX = cell[0];
2403 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002404 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002405 if (span != null) {
2406 mPendingAddInfo.spanX = span[0];
2407 mPendingAddInfo.spanY = span[1];
2408 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002409
Adam Cohened66b2b2012-01-23 17:28:51 -08002410 AppWidgetHostView hostView = info.boundWidget;
2411 int appWidgetId;
2412 if (hostView != null) {
2413 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002414 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002415 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002416 // In this case, we either need to start an activity to get permission to bind
2417 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002418 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002419 Bundle options = info.bindOptions;
2420
Sunny Goyalffe83f12014-08-14 17:39:34 -07002421 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2422 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002423 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002424 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002425 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002426 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002427 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2428 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2429 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002430 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2431 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002432 // TODO: we need to make sure that this accounts for the options bundle.
2433 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002434 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2435 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002436 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002437 }
2438
Joe Onoratodeb98af2010-02-19 14:59:39 -08002439 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002440 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 }
2442
Winson Chung24ab2f12010-09-16 14:10:47 -07002443 void processWallpaper(Intent intent) {
2444 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2445 }
2446
Adam Cohendcd297f2013-06-18 13:13:40 -07002447 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002448 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002449 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 folderInfo.title = getText(R.string.folder_name);
2451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002453 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002454 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002455 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002456
2457 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002458 FolderIcon newFolder =
2459 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002460 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002461 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002462 // Force measure the new folder icon
2463 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2464 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002465 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 }
Romain Guycbb89e42009-06-08 15:52:54 -07002467
Joe Onorato9c1289c2009-08-17 11:03:03 -04002468 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002469 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002470 }
2471
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002472 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002473 if (mLauncherCallbacks != null) {
2474 return mLauncherCallbacks.getWallpaperPickerComponent();
2475 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002476 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002477 }
2478
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002479 /**
2480 * Registers various content observers. The current implementation registers
2481 * only a favorites observer to keep track of the favorites applications.
2482 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002483 private void registerContentObservers() {
2484 ContentResolver resolver = getContentResolver();
2485 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2486 true, mWidgetObserver);
2487 }
2488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 @Override
2490 public boolean dispatchKeyEvent(KeyEvent event) {
2491 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2492 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002494 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002495 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002496 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002497 dumpState();
2498 return true;
2499 }
2500 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002501 }
2502 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2503 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002504 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 return true;
2506 }
2507 }
2508
2509 return super.dispatchKeyEvent(event);
2510 }
2511
Joe Onorato88ec0992009-11-19 13:16:06 -08002512 @Override
2513 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002514 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2515 return;
2516 }
2517
Winson Chungc93e5ae2012-07-23 20:48:26 -07002518 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002519 if (mAppsCustomizeContent.getContentType() ==
2520 AppsCustomizePagedView.ContentType.Applications) {
2521 showWorkspace(true);
2522 } else {
2523 showOverviewMode(true);
2524 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002525 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002526 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002527 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002528 Folder openFolder = mWorkspace.getOpenFolder();
2529 if (openFolder.isEditingName()) {
2530 openFolder.dismissEditingName();
2531 } else {
2532 closeFolder();
2533 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002534 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002535 mWorkspace.exitWidgetResizeMode();
2536
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002537 // Back button is a no-op here, but give at least some feedback for the button press
2538 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002539 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002540 }
2541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002542 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002543 * Re-listen when widgets are reset.
2544 */
2545 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002546 if (mAppWidgetHost != null) {
2547 mAppWidgetHost.startListening();
2548 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002549 }
2550
2551 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 * Launches the intent referred by the clicked shortcut.
2553 *
2554 * @param v The view representing the clicked shortcut.
2555 */
2556 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002557 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2558 // view has detached (it's possible for this to happen if the view is removed mid touch).
2559 if (v.getWindowToken() == null) {
2560 return;
2561 }
2562
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002563 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002564 return;
2565 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002566
Adam Cohen1697b792013-09-17 19:08:21 -07002567 if (v instanceof Workspace) {
2568 if (mWorkspace.isInOverviewMode()) {
2569 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002570 }
2571 return;
2572 }
2573
2574 if (v instanceof CellLayout) {
2575 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002576 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002577 }
2578 }
2579
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002580 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002581 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002582 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002583 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002584 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002585 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002586 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002587 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002588 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002589 } else if (tag instanceof AppInfo) {
2590 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002591 } else if (tag instanceof LauncherAppWidgetInfo) {
2592 if (v instanceof PendingAppWidgetHostView) {
2593 onClickPendingWidget((PendingAppWidgetHostView) v);
2594 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002595 }
2596 }
2597
Sunny Goyal508da152014-08-14 10:53:27 -07002598 public void onClickPagedViewIcon(View v) {
2599 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002600 if (mLauncherCallbacks != null) {
2601 mLauncherCallbacks.onClickPagedViewIcon(v);
2602 }
Sunny Goyal508da152014-08-14 10:53:27 -07002603 }
2604
Michael Jurka0e260592010-06-30 17:07:39 -07002605 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002606 return false;
2607 }
2608
Michael Jurkaaf442092010-06-10 17:01:57 -07002609 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002610 * Event handler for the app widget view which has not fully restored.
2611 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002612 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002613 if (mIsSafeModeEnabled) {
2614 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2615 return;
2616 }
2617
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002618 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002619 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002620 int widgetId = info.appWidgetId;
2621 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2622 if (appWidgetInfo != null) {
2623 mPendingAddWidgetInfo = appWidgetInfo;
2624 mPendingAddInfo.copyFrom(info);
2625 mPendingAddWidgetId = widgetId;
2626
Sunny Goyalffe83f12014-08-14 17:39:34 -07002627 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2628 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002629 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002630 } else if (info.installProgress < 0) {
2631 // The install has not been queued
2632 final String packageName = info.providerName.getPackageName();
2633 showBrokenAppInstallDialog(packageName,
2634 new DialogInterface.OnClickListener() {
2635 public void onClick(DialogInterface dialog, int id) {
2636 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2637 }
2638 });
2639 } else {
2640 // Download has started.
2641 final String packageName = info.providerName.getPackageName();
2642 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002643 }
2644 }
2645
2646 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002647 * Event handler for the search button
2648 *
2649 * @param v The view that was clicked.
2650 */
2651 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002652 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2653
Amith Yamasania135ba82011-08-09 17:42:01 -07002654 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002655 }
2656
2657 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002658 * Event handler for the voice button
2659 *
2660 * @param v The view that was clicked.
2661 */
2662 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002663 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002664
Bjorn Bringertc459e522013-06-07 19:36:01 +01002665 startVoice();
2666 }
2667
2668 public void startVoice() {
Adam Cohen9211d422014-10-07 18:14:53 -07002669 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2670 mLauncherCallbacks.startVoice();
2671 return;
2672 }
2673
Michael Jurkaae65ee32012-04-30 11:45:54 -07002674 try {
2675 final SearchManager searchManager =
2676 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2677 ComponentName activityName = searchManager.getGlobalSearchActivity();
2678 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002679 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002680 if (activityName != null) {
2681 intent.setPackage(activityName.getPackageName());
2682 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002683 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002684 } catch (ActivityNotFoundException e) {
2685 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002686 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002687 startActivitySafely(null, intent, "onClickVoiceButton");
2688 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002689 }
2690
2691 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002692 * Event handler for the "grid" button that appears on the home screen, which
2693 * enters all apps mode.
2694 *
2695 * @param v The view that was clicked.
2696 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002697 protected void onClickAllAppsButton(View v) {
2698 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2699 if (isAllAppsVisible()) {
2700 showWorkspace(true);
2701 } else {
2702 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2703 }
Adam Cohen9211d422014-10-07 18:14:53 -07002704 if (mLauncherCallbacks != null) {
2705 mLauncherCallbacks.onClickAllAppsButton(v);
2706 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002707 }
2708
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002709 private void showBrokenAppInstallDialog(final String packageName,
2710 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002711 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002712 .setTitle(R.string.abandoned_promises_title)
2713 .setMessage(R.string.abandoned_promise_explanation)
2714 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2715 .setNeutralButton(R.string.abandoned_clean_this,
2716 new DialogInterface.OnClickListener() {
2717 public void onClick(DialogInterface dialog, int id) {
2718 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2719 mWorkspace.removeAbandonedPromise(packageName, user);
2720 }
2721 })
2722 .create().show();
2723 return;
2724 }
2725
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002726 /**
2727 * Event handler for an app shortcut click.
2728 *
2729 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2730 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002731 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002732 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2733 Object tag = v.getTag();
2734 if (!(tag instanceof ShortcutInfo)) {
2735 throw new IllegalArgumentException("Input must be a Shortcut");
2736 }
2737
2738 // Open shortcut
2739 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002740
2741 if (shortcut.isDisabled != 0) {
2742 int error = R.string.activity_not_available;
2743 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2744 error = R.string.safemode_shortcut_error;
2745 }
2746 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2747 return;
2748 }
2749
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002750 final Intent intent = shortcut.intent;
2751
2752 // Check for special shortcuts
2753 if (intent.getComponent() != null) {
2754 final String shortcutClass = intent.getComponent().getClassName();
2755
2756 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2757 MemoryDumpActivity.startDump(this);
2758 return;
2759 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2760 toggleShowWeightWatcher();
2761 return;
2762 }
2763 }
2764
Chris Wren40c5ed32014-06-24 18:24:23 -04002765 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002766 if ((v instanceof BubbleTextView)
2767 && shortcut.isPromise()
2768 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002769 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002770 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002771 new DialogInterface.OnClickListener() {
2772 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002773 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002774 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002775 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002776 return;
2777 }
2778
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002779 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002780 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002781
2782 if (mLauncherCallbacks != null) {
2783 mLauncherCallbacks.onClickAppShortcut(v);
2784 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002785 }
2786
Sunny Goyal508da152014-08-14 10:53:27 -07002787 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002788 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002789 final ShortcutInfo shortcut;
2790 final Intent intent;
2791 if (tag instanceof ShortcutInfo) {
2792 shortcut = (ShortcutInfo) tag;
2793 intent = shortcut.intent;
2794 int[] pos = new int[2];
2795 v.getLocationOnScreen(pos);
2796 intent.setSourceBounds(new Rect(pos[0], pos[1],
2797 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002798
Sunny Goyal508da152014-08-14 10:53:27 -07002799 } else if (tag instanceof AppInfo) {
2800 shortcut = null;
2801 intent = ((AppInfo) tag).intent;
2802 } else {
2803 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2804 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002805
2806 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002807 mStats.recordLaunch(intent, shortcut);
2808
2809 if (success && v instanceof BubbleTextView) {
2810 mWaitingForResume = (BubbleTextView) v;
2811 mWaitingForResume.setStayPressed(true);
2812 }
2813 }
2814
2815 /**
2816 * Event handler for a folder icon click.
2817 *
2818 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2819 */
2820 protected void onClickFolderIcon(View v) {
2821 if (LOGD) Log.d(TAG, "onClickFolder");
2822 if (!(v instanceof FolderIcon)){
2823 throw new IllegalArgumentException("Input must be a FolderIcon");
2824 }
2825
2826 FolderIcon folderIcon = (FolderIcon) v;
2827 final FolderInfo info = folderIcon.getFolderInfo();
2828 Folder openFolder = mWorkspace.getFolderForTag(info);
2829
2830 // If the folder info reports that the associated folder is open, then verify that
2831 // it is actually opened. There have been a few instances where this gets out of sync.
2832 if (info.opened && openFolder == null) {
2833 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2834 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2835 info.opened = false;
2836 }
2837
2838 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2839 // Close any open folder
2840 closeFolder();
2841 // Open the requested folder
2842 openFolder(folderIcon);
2843 } else {
2844 // Find the open folder...
2845 int folderScreen;
2846 if (openFolder != null) {
2847 folderScreen = mWorkspace.getPageForView(openFolder);
2848 // .. and close it
2849 closeFolder(openFolder);
2850 if (folderScreen != mWorkspace.getCurrentPage()) {
2851 // Close any folder open on the current screen
2852 closeFolder();
2853 // Pull the folder onto this screen
2854 openFolder(folderIcon);
2855 }
2856 }
2857 }
Adam Cohen9211d422014-10-07 18:14:53 -07002858
2859 if (mLauncherCallbacks != null) {
2860 mLauncherCallbacks.onClickFolderIcon(v);
2861 }
Michael Jurka5130e402011-10-13 04:55:35 -07002862 }
2863
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002864 /**
2865 * Event handler for the (Add) Widgets button that appears after a long press
2866 * on the home screen.
2867 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002868 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002869 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002870 if (mIsSafeModeEnabled) {
2871 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2872 } else {
2873 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2874 if (mLauncherCallbacks != null) {
2875 mLauncherCallbacks.onClickAddWidgetButton(view);
2876 }
Adam Cohen9211d422014-10-07 18:14:53 -07002877 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002878 }
2879
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002880 /**
2881 * Event handler for the wallpaper picker button that appears after a long press
2882 * on the home screen.
2883 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002884 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002885 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2886 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2887 pickWallpaper.setComponent(getWallpaperPickerComponent());
2888 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002889
2890 if (mLauncherCallbacks != null) {
2891 mLauncherCallbacks.onClickWallpaperPicker(v);
2892 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002893 }
2894
2895 /**
2896 * Event handler for a click on the settings button that appears after a long press
2897 * on the home screen.
2898 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002899 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002900 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002901 if (mLauncherCallbacks != null) {
2902 mLauncherCallbacks.onClickSettingsButton(v);
2903 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002904 }
2905
Michael Jurka5130e402011-10-13 04:55:35 -07002906 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002907 // Provide the same haptic feedback that the system offers for virtual keys.
2908 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002909 }
2910
Adam Cohen61f560d2013-09-30 15:58:20 -07002911 public void performHapticFeedbackOnTouchDown(View v) {
2912 // Provide the same haptic feedback that the system offers for virtual keys.
2913 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2914 }
2915
2916 public View.OnTouchListener getHapticFeedbackTouchListener() {
2917 if (mHapticFeedbackTouchListener == null) {
2918 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2919 @Override
2920 public boolean onTouch(View v, MotionEvent event) {
2921 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2922 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2923 }
2924 return false;
2925 }
2926 };
2927 }
2928 return mHapticFeedbackTouchListener;
2929 }
2930
Adam Cohen9211d422014-10-07 18:14:53 -07002931 public void onDragStarted(View view) {
2932 if (isOnCustomContent()) {
2933 // Custom content screen doesn't participate in drag and drop. If on custom
2934 // content screen, move to default.
2935 moveWorkspaceToDefaultScreen();
2936 }
2937
2938 if (mLauncherCallbacks != null) {
2939 mLauncherCallbacks.onDragStarted(view);
2940 }
2941 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002942
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002943 /**
2944 * Called when the user stops interacting with the launcher.
2945 * This implies that the user is now on the homescreen and is not doing housekeeping.
2946 */
Adam Cohen9211d422014-10-07 18:14:53 -07002947 protected void onInteractionEnd() {
2948 if (mLauncherCallbacks != null) {
2949 mLauncherCallbacks.onInteractionEnd();
2950 }
2951 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002952
2953 /**
2954 * Called when the user starts interacting with the launcher.
2955 * The possible interactions are:
2956 * - open all apps
2957 * - reorder an app shortcut, or a widget
2958 * - open the overview mode.
2959 * This is a good time to stop doing things that only make sense
2960 * when the user is on the homescreen and not doing housekeeping.
2961 */
Adam Cohen9211d422014-10-07 18:14:53 -07002962 protected void onInteractionBegin() {
2963 if (mLauncherCallbacks != null) {
2964 mLauncherCallbacks.onInteractionBegin();
2965 }
2966 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002967
Kenny Guyf07af7b2014-07-31 11:39:16 +01002968 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002969 String packageName = componentName.getPackageName();
Kenny Guyf07af7b2014-07-31 11:39:16 +01002970 try {
2971 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2972 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2973 launcherApps.showAppDetailsForProfile(componentName, user);
2974 } catch (SecurityException e) {
2975 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2976 Log.e(TAG, "Launcher does not have permission to launch settings");
2977 } catch (ActivityNotFoundException e) {
2978 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2979 Log.e(TAG, "Unable to launch settings");
2980 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002981 }
2982
Michael Jurka1e2f4652013-07-08 18:03:46 -07002983 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002984 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2985 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002986 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002987 // System applications cannot be installed. For now, show a toast explaining that.
2988 // We may give them the option of disabling apps this way.
2989 int messageId = R.string.uninstall_system_app_text;
2990 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002991 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002992 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002993 String packageName = componentName.getPackageName();
2994 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002995 Intent intent = new Intent(
2996 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002997 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2998 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002999 if (user != null) {
3000 user.addToIntent(intent, Intent.EXTRA_USER);
3001 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07003002 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003003 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07003004 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07003005 }
3006
Michael Jurka86a720e2012-05-09 11:23:23 -07003007 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01003008 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003009 try {
Winson Chung2672ff92012-05-04 16:22:30 -07003010 // Only launch using the new animation if the shortcut has not opted out (this is a
3011 // private contract between launcher and may be ignored in the future).
3012 boolean useLaunchAnimation = (v != null) &&
3013 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01003014 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
3015 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01003016
Kenny Guy1317e2d2014-05-08 18:52:50 +01003017 UserHandleCompat user = null;
3018 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
3019 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
3020 user = userManager.getUserForSerialNumber(serialNumber);
3021 }
3022
3023 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07003024 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01003025 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07003026 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
3027 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07003028 optsBundle = opts.toBundle();
3029 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01003030
3031 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
3032 // Could be launching some bookkeeping activity
3033 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003034 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003035 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003036 launcherApps.startActivityForProfile(intent.getComponent(), user,
3037 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003038 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003039 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040 } catch (SecurityException e) {
3041 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003042 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003043 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003044 "or use the exported attribute for this activity. "
3045 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003046 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003047 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003048 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003049
Michael Jurka86a720e2012-05-09 11:23:23 -07003050 boolean startActivitySafely(View v, Intent intent, Object tag) {
3051 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003052 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3053 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3054 return false;
3055 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003056 try {
3057 success = startActivity(v, intent, tag);
3058 } catch (ActivityNotFoundException e) {
3059 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3060 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3061 }
3062 return success;
3063 }
3064
Adam Cohen268c4752012-06-06 17:47:33 -07003065 /**
3066 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3067 * in the DragLayer in the exact absolute location of the original FolderIcon.
3068 */
3069 private void copyFolderIconToImage(FolderIcon fi) {
3070 final int width = fi.getMeasuredWidth();
3071 final int height = fi.getMeasuredHeight();
3072
3073 // Lazy load ImageView, Bitmap and Canvas
3074 if (mFolderIconImageView == null) {
3075 mFolderIconImageView = new ImageView(this);
3076 }
3077 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3078 mFolderIconBitmap.getHeight() != height) {
3079 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3080 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3081 }
3082
3083 DragLayer.LayoutParams lp;
3084 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3085 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3086 } else {
3087 lp = new DragLayer.LayoutParams(width, height);
3088 }
3089
Adam Cohen307fe232012-08-16 17:55:58 -07003090 // The layout from which the folder is being opened may be scaled, adjust the starting
3091 // view size by this scale factor.
3092 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003093 lp.customPosition = true;
3094 lp.x = mRectForFolderAnimation.left;
3095 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003096 lp.width = (int) (scale * width);
3097 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003098
3099 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3100 fi.draw(mFolderIconCanvas);
3101 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003102 if (fi.getFolder() != null) {
3103 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3104 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003105 }
Adam Cohen268c4752012-06-06 17:47:33 -07003106 // Just in case this image view is still in the drag layer from a previous animation,
3107 // we remove it and re-add it.
3108 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3109 mDragLayer.removeView(mFolderIconImageView);
3110 }
3111 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003112 if (fi.getFolder() != null) {
3113 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003114 }
Adam Cohen268c4752012-06-06 17:47:33 -07003115 }
3116
Adam Cohen2801caf2011-05-13 20:57:39 -07003117 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003118 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003119 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3120 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3121 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3122
Adam Cohenc51934b2011-07-26 21:07:43 -07003123 FolderInfo info = (FolderInfo) fi.getTag();
3124 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3125 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003126 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3127 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003128 }
3129
Adam Cohen268c4752012-06-06 17:47:33 -07003130 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3131 copyFolderIconToImage(fi);
3132 fi.setVisibility(View.INVISIBLE);
3133
Michael Jurka2ecf9952012-06-18 12:52:28 -07003134 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003135 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003136 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003137 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3138 }
3139 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003140 oa.start();
3141 }
3142
Adam Cohen268c4752012-06-06 17:47:33 -07003143 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003144 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003145 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3146 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3147 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3148
Adam Cohen268c4752012-06-06 17:47:33 -07003149 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003150
Adam Cohen268c4752012-06-06 17:47:33 -07003151 // We remove and re-draw the FolderIcon in-case it has changed
3152 mDragLayer.removeView(mFolderIconImageView);
3153 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003154 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003155 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003156 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003157 oa.addListener(new AnimatorListenerAdapter() {
3158 @Override
3159 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003160 if (cl != null) {
3161 cl.clearFolderLeaveBehind();
3162 // Remove the ImageView copy of the FolderIcon and make the original visible.
3163 mDragLayer.removeView(mFolderIconImageView);
3164 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003165 }
3166 }
3167 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003168 oa.start();
3169 }
3170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003172 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173 * is animated relative to the specified View. If the View is null, no animation
3174 * is played.
3175 *
3176 * @param folderInfo The FolderInfo describing the folder to open.
3177 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003178 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003179 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003180 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181
Adam Cohena9cf38f2011-05-02 15:36:58 -07003182 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003183
Adam Cohen4554ee12011-08-03 16:13:21 -07003184 // Just verify that the folder hasn't already been added to the DragLayer.
3185 // There was a one-off crash where the folder had a parent already.
3186 if (folder.getParent() == null) {
3187 mDragLayer.addView(folder);
3188 mDragController.addDropTarget((DropTarget) folder);
3189 } else {
3190 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3191 folder.getParent() + ").");
3192 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003193 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003194 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003195
3196 // Notify the accessibility manager that this folder "window" has appeared and occluded
3197 // the workspace items
3198 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3199 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003200 }
3201
3202 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003203 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003204 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003205 if (folder.isEditingName()) {
3206 folder.dismissEditingName();
3207 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003208 closeFolder(folder);
3209 }
3210 }
3211
3212 void closeFolder(Folder folder) {
3213 folder.getInfo().opened = false;
3214
3215 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3216 if (parent != null) {
3217 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3218 shrinkAndFadeInFolderIcon(fi);
3219 }
3220 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003221
3222 // Notify the accessibility manager that this folder "window" has disappeard and no
3223 // longer occludeds the workspace items
3224 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 }
3226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003227 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003228 if (!isDraggingEnabled()) return false;
3229 if (isWorkspaceLocked()) return false;
3230 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003231
Adam Cohen1697b792013-09-17 19:08:21 -07003232 if (v instanceof Workspace) {
3233 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003234 if (mWorkspace.enterOverviewMode()) {
3235 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3236 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3237 return true;
3238 } else {
3239 return false;
3240 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003241 } else {
3242 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003243 }
Adam Cohen1697b792013-09-17 19:08:21 -07003244 }
3245
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003246 CellLayout.CellInfo longClickCellInfo = null;
3247 View itemUnderLongClick = null;
3248 if (v.getTag() instanceof ItemInfo) {
3249 ItemInfo info = (ItemInfo) v.getTag();
3250 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3251 itemUnderLongClick = longClickCellInfo.cell;
3252 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003253 }
3254
Winson Chung3d503fb2011-07-13 17:25:49 -07003255 // The hotseat touch handling does not go through Workspace, and we always allow long press
3256 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003257 final boolean inHotseat = isHotseatLayout(v);
3258 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003259 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003260 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003261 // User long pressed on empty space
3262 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3263 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003264 if (mWorkspace.isInOverviewMode()) {
3265 mWorkspace.startReordering(v);
3266 } else {
3267 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003268 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003269 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003270 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3271 mHotseat.getOrderInHotseat(
3272 longClickCellInfo.cellX,
3273 longClickCellInfo.cellY));
3274 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003275 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003276 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003277 }
3278 }
3279 }
3280 return true;
3281 }
3282
Winson Chung3d503fb2011-07-13 17:25:49 -07003283 boolean isHotseatLayout(View layout) {
3284 return mHotseat != null && layout != null &&
3285 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3286 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003287
Winson Chung3d503fb2011-07-13 17:25:49 -07003288 /**
3289 * Returns the CellLayout of the specified container at the specified screen.
3290 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003291 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003292 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3293 if (mHotseat != null) {
3294 return mHotseat.getLayout();
3295 } else {
3296 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003297 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003298 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003299 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003300 }
3301 }
3302
Daniel Sandler843e8602010-06-07 14:59:01 -04003303 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003304 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003305 }
3306
Craig Mautner360310b2012-10-26 15:13:08 -07003307 private void setWorkspaceBackground(boolean workspace) {
3308 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003309 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003310 }
3311
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003312 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003313 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3314 int curflags = getWindow().getAttributes().flags
3315 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3316 if (wpflags != curflags) {
3317 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3318 }
Craig Mautner360310b2012-10-26 15:13:08 -07003319 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003320 }
3321
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003322 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3323 if (v instanceof LauncherTransitionable) {
3324 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3325 }
3326 }
3327
Michael Jurkabed61d22012-02-14 22:51:29 -08003328 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3329 if (v instanceof LauncherTransitionable) {
3330 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3331 }
Winson Chung70442722012-02-10 15:43:22 -08003332
3333 // Update the workspace transition step as well
3334 dispatchOnLauncherTransitionStep(v, 0f);
3335 }
3336
3337 private void dispatchOnLauncherTransitionStep(View v, float t) {
3338 if (v instanceof LauncherTransitionable) {
3339 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3340 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003341 }
3342
3343 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3344 if (v instanceof LauncherTransitionable) {
3345 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3346 }
Winson Chung70442722012-02-10 15:43:22 -08003347
3348 // Update the workspace transition step as well
3349 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003350 }
3351
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003352 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003353 * Things to test when changing the following seven functions.
3354 * - Home from workspace
3355 * - from center screen
3356 * - from other screens
3357 * - Home from all apps
3358 * - from center screen
3359 * - from other screens
3360 * - Back from all apps
3361 * - from center screen
3362 * - from other screens
3363 * - Launch app from workspace and quit
3364 * - with back
3365 * - with home
3366 * - Launch app from all apps and quit
3367 * - with back
3368 * - with home
3369 * - Go to a screen that's not the default, then all
3370 * apps, and launch and app, and go back
3371 * - with back
3372 * -with home
3373 * - On workspace, long press power and go back
3374 * - with back
3375 * - with home
3376 * - On all apps, long press power and go back
3377 * - with back
3378 * - with home
3379 * - On workspace, power off
3380 * - On all apps, power off
3381 * - Launch an app and turn off the screen while in that app
3382 * - Go back with home key
3383 * - Go back with back key TODO: make this not go to workspace
3384 * - From all apps
3385 * - From workspace
3386 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3387 * - From all apps
3388 * - From the center workspace
3389 * - From another workspace
3390 */
3391
3392 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003393 * Zoom the camera out from the workspace to reveal 'toView'.
3394 * Assumes that the view to show is anchored at either the very top or very bottom
3395 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003396 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003397 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003398 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3399 showAppsCustomizeHelper(animated, springLoaded, contentType);
3400 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003401
Winson Chungc58497e2013-09-03 17:48:37 -07003402 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3403 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003404 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003405 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003406 mStateAnimation.cancel();
3407 mStateAnimation = null;
3408 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003409
Kenny Guyd794a3f2014-09-16 15:17:58 +01003410 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003411
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003412 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003413
Winson Chungf0ea4d32011-06-06 14:27:16 -07003414 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3415 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003416 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003417 final int itemsAlphaStagger =
3418 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003419
Winson Chungf0ea4d32011-06-06 14:27:16 -07003420 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003421 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003422 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003423
Adam Cohen2854d252014-08-27 16:04:07 -07003424 final ArrayList<View> layerViews = new ArrayList<View>();
3425
Adam Cohen6c5891a2014-07-09 23:53:15 -07003426 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3427 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003428 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003429 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003430 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003431 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3432 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003433 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3434 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003435
Adam Cohena38dc902014-09-07 17:48:55 +02003436 // If for some reason our views aren't initialized, don't animate
3437 boolean initialized = getAllAppsButton() != null;
3438
3439 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003440 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003441 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3442 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003443
Adam Cohen9bfdb762014-07-21 17:44:06 -07003444 final View page = content.getPageAt(content.getCurrentPage());
3445 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003446
Adam Cohen63f1ec02014-08-12 09:23:13 -07003447 final float initialPanelAlpha = 1f;
3448
3449 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3450 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003451 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3452 } else {
3453 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3454 }
3455
Adam Cohen9bfdb762014-07-21 17:44:06 -07003456 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003457 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003458 revealView.setVisibility(View.VISIBLE);
3459 // We need to hide this view as the animation start will be posted.
3460 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003461
Adam Cohen9bfdb762014-07-21 17:44:06 -07003462 int width = revealView.getMeasuredWidth();
3463 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003464 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003465
Adam Cohen63f1ec02014-08-12 09:23:13 -07003466 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003467 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003468
Adam Cohen63f1ec02014-08-12 09:23:13 -07003469 // Get the y delta between the center of the page and the center of the all apps button
3470 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3471 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003472
Adam Cohen2854d252014-08-27 16:04:07 -07003473 float alpha = 0;
3474 float xDrift = 0;
3475 float yDrift = 0;
3476 if (material) {
3477 alpha = isWidgetTray ? 0.3f : 1f;
3478 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3479 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3480 } else {
3481 yDrift = 2 * height / 3;
3482 xDrift = 0;
3483 }
3484 final float initAlpha = alpha;
3485
Adam Cohen9bfdb762014-07-21 17:44:06 -07003486 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003487 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003488 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003489 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003490 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003491 PropertyValuesHolder panelDriftX =
3492 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003493
Adam Cohen2854d252014-08-27 16:04:07 -07003494 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3495 panelAlpha, panelDriftY, panelDriftX);
3496
Adam Cohen9bfdb762014-07-21 17:44:06 -07003497 panelAlphaAndDrift.setDuration(revealDuration);
3498 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003499
Adam Cohen9bfdb762014-07-21 17:44:06 -07003500 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003501
Adam Cohen4e243a22014-08-10 18:30:55 -07003502 if (page != null) {
3503 page.setVisibility(View.VISIBLE);
3504 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003505 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003506
Adam Cohen2854d252014-08-27 16:04:07 -07003507 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3508 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003509 pageDrift.setDuration(revealDuration);
3510 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003511 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003512 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003513
Adam Cohen63f1ec02014-08-12 09:23:13 -07003514 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003515 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003516 itemsAlpha.setDuration(revealDuration);
3517 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3518 itemsAlpha.setStartDelay(itemsAlphaStagger);
3519 mStateAnimation.play(itemsAlpha);
3520 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003521
Adam Cohen4e243a22014-08-10 18:30:55 -07003522 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3523 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003524 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003525 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003526 indicatorsAlpha.setDuration(revealDuration);
3527 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003528
Adam Cohen9bfdb762014-07-21 17:44:06 -07003529 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003530 final View allApps = getAllAppsButton();
3531 int allAppsButtonSize = LauncherAppState.getInstance().
3532 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3533 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003534 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003535 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003536 reveal.setDuration(revealDuration);
3537 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003538
3539 reveal.addListener(new AnimatorListenerAdapter() {
3540 public void onAnimationStart(Animator animation) {
3541 if (!isWidgetTray) {
3542 allApps.setVisibility(View.INVISIBLE);
3543 }
3544 }
3545 public void onAnimationEnd(Animator animation) {
3546 if (!isWidgetTray) {
3547 allApps.setVisibility(View.VISIBLE);
3548 }
3549 }
3550 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003551 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003552 }
Michael Jurka1899a362011-11-03 13:50:45 -07003553
Adam Cohen9bfdb762014-07-21 17:44:06 -07003554 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3555 @Override
3556 public void onAnimationEnd(Animator animation) {
3557 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3558 dispatchOnLauncherTransitionEnd(toView, animated, false);
3559
3560 revealView.setVisibility(View.INVISIBLE);
3561 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003562 if (page != null) {
3563 page.setLayerType(View.LAYER_TYPE_NONE, null);
3564 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003565 content.setPageBackgroundsVisible(true);
3566
3567 // Hide the search bar
3568 if (mSearchDropTargetBar != null) {
3569 mSearchDropTargetBar.hideSearchBar(false);
3570 }
3571 }
3572
Adam Cohen9bfdb762014-07-21 17:44:06 -07003573 });
3574
Adam Cohen6c5891a2014-07-09 23:53:15 -07003575 if (workspaceAnim != null) {
3576 mStateAnimation.play(workspaceAnim);
3577 }
Adam Cohen2854d252014-08-27 16:04:07 -07003578
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003579 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3580 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003581 final AnimatorSet stateAnimation = mStateAnimation;
3582 final Runnable startAnimRunnable = new Runnable() {
3583 public void run() {
3584 // Check that mStateAnimation hasn't changed while
3585 // we waited for a layout/draw pass
3586 if (mStateAnimation != stateAnimation)
3587 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003588 dispatchOnLauncherTransitionStart(fromView, animated, false);
3589 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003590
3591 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003592 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003593 for (int i = 0; i < layerViews.size(); i++) {
3594 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003595 if (v != null) {
3596 boolean attached = true;
3597 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3598 attached = v.isAttachedToWindow();
3599 }
3600 if (attached) v.buildLayer();
3601 }
Adam Cohen2854d252014-08-27 16:04:07 -07003602 }
3603 }
3604 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003605 }
3606 };
Adam Cohen2854d252014-08-27 16:04:07 -07003607 toView.bringToFront();
3608 toView.setVisibility(View.VISIBLE);
3609 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003610 } else {
3611 toView.setTranslationX(0.0f);
3612 toView.setTranslationY(0.0f);
3613 toView.setScaleX(1.0f);
3614 toView.setScaleY(1.0f);
3615 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003616 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003617
Daniel Sandlere4f98912013-06-25 15:13:26 -04003618 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003619 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003620 if (mSearchDropTargetBar != null) {
3621 mSearchDropTargetBar.hideSearchBar(false);
3622 }
Michael Jurkaabded662011-03-04 12:06:57 -08003623 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003624 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003625 dispatchOnLauncherTransitionStart(fromView, animated, false);
3626 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003627 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003628 dispatchOnLauncherTransitionStart(toView, animated, false);
3629 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003630 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003631 }
3632
3633 /**
3634 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003635 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003636 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003637 */
Adam Cohened307df2013-10-02 09:37:31 -07003638 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003639 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003640
Michael Jurkab3e22d92011-10-31 15:58:33 -07003641 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003642 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003643 mStateAnimation.cancel();
3644 mStateAnimation = null;
3645 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003646
Kenny Guyd794a3f2014-09-16 15:17:58 +01003647 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003648 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003649
Winson Chungf0ea4d32011-06-06 14:27:16 -07003650 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003651 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003652 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003653 final int itemsAlphaStagger =
3654 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003655
Winson Chungf0ea4d32011-06-06 14:27:16 -07003656 final float scaleFactor = (float)
3657 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3658 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003659 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003660 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003661 final ArrayList<View> layerViews = new ArrayList<View>();
3662
Adam Cohened307df2013-10-02 09:37:31 -07003663 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003664 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003665 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003666 } else if (toState == Workspace.State.SPRING_LOADED ||
3667 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003668 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003669 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003670 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003671
Adam Cohena38dc902014-09-07 17:48:55 +02003672 // If for some reason our views aren't initialized, don't animate
3673 boolean initialized = getAllAppsButton() != null;
3674
3675 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003676 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003677 if (workspaceAnim != null) {
3678 mStateAnimation.play(workspaceAnim);
3679 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003680
Adam Cohen9bfdb762014-07-21 17:44:06 -07003681 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3682 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003683
Adam Cohen9bfdb762014-07-21 17:44:06 -07003684 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003685
3686 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3687 int count = content.getChildCount();
3688 for (int i = 0; i < count; i++) {
3689 View child = content.getChildAt(i);
3690 if (child != page) {
3691 child.setVisibility(View.INVISIBLE);
3692 }
3693 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003694 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003695
Adam Cohen2854d252014-08-27 16:04:07 -07003696 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3697 // don't perform all these no-op animations. In particularly, this was causing
3698 // the all-apps button to pop in and out.
3699 if (fromView.getVisibility() == View.VISIBLE) {
3700 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3701 final boolean isWidgetTray =
3702 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003703
Adam Cohen2854d252014-08-27 16:04:07 -07003704 if (isWidgetTray) {
3705 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3706 } else {
3707 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003708 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003709
Adam Cohen2854d252014-08-27 16:04:07 -07003710 int width = revealView.getMeasuredWidth();
3711 int height = revealView.getMeasuredHeight();
3712 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3713
3714 // Hide the real page background, and swap in the fake one
3715 revealView.setVisibility(View.VISIBLE);
3716 content.setPageBackgroundsVisible(false);
3717
3718 final View allAppsButton = getAllAppsButton();
3719 revealView.setTranslationY(0);
3720 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3721 allAppsButton, null);
3722
3723 float xDrift = 0;
3724 float yDrift = 0;
3725 if (material) {
3726 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3727 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3728 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003729 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003730 xDrift = 0;
3731 }
3732
3733 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3734 TimeInterpolator decelerateInterpolator = material ?
3735 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003736 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003737
3738 // The vertical motion of the apps panel should be delayed by one frame
3739 // from the conceal animation in order to give the right feel. We correpsondingly
3740 // shorten the duration so that the slide and conceal end at the same time.
3741 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3742 0, yDrift);
3743 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3744 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3745 panelDriftY.setInterpolator(decelerateInterpolator);
3746 mStateAnimation.play(panelDriftY);
3747
3748 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3749 0, xDrift);
3750 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3751 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3752 panelDriftX.setInterpolator(decelerateInterpolator);
3753 mStateAnimation.play(panelDriftX);
3754
3755 if (isWidgetTray || !material) {
3756 float finalAlpha = material ? 0.4f : 0f;
3757 revealView.setAlpha(1f);
3758 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3759 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003760 panelAlpha.setDuration(material ? revealDuration : 150);
3761 panelAlpha.setInterpolator(decelerateInterpolator);
3762 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003763 mStateAnimation.play(panelAlpha);
3764 }
3765
3766 if (page != null) {
3767 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3768
3769 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3770 0, yDrift);
3771 page.setTranslationY(0);
3772 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3773 pageDrift.setInterpolator(decelerateInterpolator);
3774 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3775 mStateAnimation.play(pageDrift);
3776
3777 page.setAlpha(1f);
3778 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3779 itemsAlpha.setDuration(100);
3780 itemsAlpha.setInterpolator(decelerateInterpolator);
3781 mStateAnimation.play(itemsAlpha);
3782 }
3783
3784 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3785 pageIndicators.setAlpha(1f);
3786 ObjectAnimator indicatorsAlpha =
3787 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3788 indicatorsAlpha.setDuration(revealDuration);
3789 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3790 mStateAnimation.play(indicatorsAlpha);
3791
3792 width = revealView.getMeasuredWidth();
3793
3794 if (material) {
3795 if (!isWidgetTray) {
3796 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003797 }
Adam Cohen2854d252014-08-27 16:04:07 -07003798 int allAppsButtonSize = LauncherAppState.getInstance().
3799 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3800 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3801 Animator reveal =
3802 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3803 height / 2, revealRadius, finalRadius);
3804 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3805 reveal.setDuration(revealDuration);
3806 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003807
Adam Cohen2854d252014-08-27 16:04:07 -07003808 reveal.addListener(new AnimatorListenerAdapter() {
3809 public void onAnimationEnd(Animator animation) {
3810 revealView.setVisibility(View.INVISIBLE);
3811 if (!isWidgetTray) {
3812 allAppsButton.setVisibility(View.VISIBLE);
3813 }
3814 }
3815 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003816
Adam Cohen2854d252014-08-27 16:04:07 -07003817 mStateAnimation.play(reveal);
3818 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003819
Adam Cohen2854d252014-08-27 16:04:07 -07003820 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3821 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3822 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003823 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003824
3825 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003826 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003827 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003828 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003829 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3830 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003831 if (onCompleteRunnable != null) {
3832 onCompleteRunnable.run();
3833 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003834
3835 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003836 if (page != null) {
3837 page.setLayerType(View.LAYER_TYPE_NONE, null);
3838 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003839 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003840 // Unhide side pages
3841 int count = content.getChildCount();
3842 for (int i = 0; i < count; i++) {
3843 View child = content.getChildAt(i);
3844 child.setVisibility(View.VISIBLE);
3845 }
3846
3847 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003848 if (page != null) {
3849 page.setTranslationX(0);
3850 page.setTranslationY(0);
3851 page.setAlpha(1);
3852 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003853 content.setCurrentPage(content.getNextPage());
3854
Chet Haasebc2f0822012-10-26 17:59:53 -07003855 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003856 }
3857 });
3858
Adam Cohen2854d252014-08-27 16:04:07 -07003859 final AnimatorSet stateAnimation = mStateAnimation;
3860 final Runnable startAnimRunnable = new Runnable() {
3861 public void run() {
3862 // Check that mStateAnimation hasn't changed while
3863 // we waited for a layout/draw pass
3864 if (mStateAnimation != stateAnimation)
3865 return;
3866 dispatchOnLauncherTransitionStart(fromView, animated, false);
3867 dispatchOnLauncherTransitionStart(toView, animated, false);
3868
Kenny Guyd794a3f2014-09-16 15:17:58 +01003869 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003870 for (int i = 0; i < layerViews.size(); i++) {
3871 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003872 if (v != null) {
3873 boolean attached = true;
3874 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3875 attached = v.isAttachedToWindow();
3876 }
3877 if (attached) v.buildLayer();
3878 }
Adam Cohen2854d252014-08-27 16:04:07 -07003879 }
3880 }
3881 mStateAnimation.start();
3882 }
3883 };
3884 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003885 } else {
3886 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003887 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003888 dispatchOnLauncherTransitionStart(fromView, animated, true);
3889 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003890 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003891 dispatchOnLauncherTransitionStart(toView, animated, true);
3892 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003893 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003894 }
3895
Michael Jurkae326f182011-11-21 14:05:46 -08003896 @Override
3897 public void onTrimMemory(int level) {
3898 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003899 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003900 mAppsCustomizeTabHost.onTrimMemory();
3901 }
3902 }
3903
Adam Cohened307df2013-10-02 09:37:31 -07003904 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003905 showWorkspace(animated, null);
3906 }
3907
Adam Cohened307df2013-10-02 09:37:31 -07003908 protected void showWorkspace() {
3909 showWorkspace(true);
3910 }
3911
Adam Cohened66b2b2012-01-23 17:28:51 -08003912 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003913 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003914 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003915 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003916 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003917
Winson Chungc7d2b602012-05-16 17:02:20 -07003918 // Show the search bar (only animate if we were showing the drop target bar in spring
3919 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003920 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003921 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003922 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003923
Michael Jurkab3e22d92011-10-31 15:58:33 -07003924 // Set focus to the AppsCustomize button
3925 if (mAllAppsButton != null) {
3926 mAllAppsButton.requestFocus();
3927 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003928 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003929
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003930 // Change the state *after* we've called all the transition code
3931 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003932
Winson Chung5fb63472011-02-02 17:03:37 -08003933 // Resume the auto-advance of widgets
3934 mUserPresent = true;
3935 updateRunning();
3936
alanv1d4fde62012-10-17 13:15:47 -07003937 // Send an accessibility event to announce the context change
3938 getWindow().getDecorView()
3939 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003940
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003941 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003942 }
3943
Adam Cohened307df2013-10-02 09:37:31 -07003944 void showOverviewMode(boolean animated) {
3945 mWorkspace.setVisibility(View.VISIBLE);
3946 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3947 mState = State.WORKSPACE;
3948 onWorkspaceShown(animated);
3949 }
3950
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003951 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003952 }
3953
Winson Chung82963d52013-10-09 11:20:57 -07003954 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3955 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003956 if (mState != State.WORKSPACE) return;
3957
Winson Chung82963d52013-10-09 11:20:57 -07003958 if (resetPageToZero) {
3959 mAppsCustomizeTabHost.reset();
3960 }
Winson Chungc58497e2013-09-03 17:48:37 -07003961 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003962 mAppsCustomizeTabHost.post(new Runnable() {
3963 @Override
3964 public void run() {
3965 // We post this in-case the all apps view isn't yet constructed.
3966 mAppsCustomizeTabHost.requestFocus();
3967 }
3968 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003969
Michael Jurkab3e22d92011-10-31 15:58:33 -07003970 // Change the state *after* we've called all the transition code
3971 mState = State.APPS_CUSTOMIZE;
3972
3973 // Pause the auto-advance of widgets until we are out of AllApps
3974 mUserPresent = false;
3975 updateRunning();
3976 closeFolder();
3977
3978 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003979 getWindow().getDecorView()
3980 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003981 }
3982
Adam Cohen7777d962011-08-18 18:58:38 -07003983 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003984 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003985 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003986 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003987 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003988 }
Adam Cohen7777d962011-08-18 18:58:38 -07003989
Adam Cohenad4e15c2013-10-17 16:21:35 -07003990 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003991 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003992 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3993
Winson Chunge7a03942011-08-05 15:05:12 -07003994 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003995 @Override
3996 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003997 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003998 // Before we show workspace, hide all apps again because
3999 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
4000 // clean up our state transition functions
4001 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08004002 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07004003 } else {
4004 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07004005 }
Winson Chung557d6ed2011-07-08 15:34:52 -07004006 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07004007 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07004008 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07004009
Michael Jurkad3ef3062010-11-23 16:23:58 -08004010 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07004011 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07004012 final boolean animated = true;
4013 final boolean springLoaded = true;
4014 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07004015 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07004016 }
4017 // Otherwise, we are not in spring loaded mode, so don't do anything.
4018 }
4019
Michael Jurkab3e22d92011-10-31 15:58:33 -07004020 void lockAllApps() {
4021 // TODO
4022 }
4023
4024 void unlockAllApps() {
4025 // TODO
4026 }
4027
Winson Chungf0ea4d32011-06-06 14:27:16 -07004028 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07004029 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07004030 */
Winson Chung3d503fb2011-07-13 17:25:49 -07004031 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04004032 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07004033 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08004034 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07004035 int duration = 0;
4036 if (mSearchDropTargetBar != null) {
4037 duration = mSearchDropTargetBar.getTransitionOutDuration();
4038 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08004039 mHotseat.animate().alpha(0f).setDuration(duration);
4040 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004041 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07004042 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07004043 }
Winson Chungb26f3d62011-06-02 10:49:29 -07004044 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08004045 }
4046
Patrick Dubroy5f445422011-02-18 14:35:21 -08004047 /**
4048 * Add an item from all apps or customize onto the given workspace screen.
4049 * If layout is null, add to the current screen.
4050 */
4051 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07004052 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07004053 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07004054 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07004055 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07004056
Winson Chungdff8ebb2011-09-08 17:25:31 -07004057 /** Maps the current orientation to an index for referencing orientation correct global icons */
4058 private int getCurrentOrientationIndexForGlobalIcons() {
4059 // default - 0, landscape - 1
4060 switch (getResources().getConfiguration().orientation) {
4061 case Configuration.ORIENTATION_LANDSCAPE:
4062 return 1;
4063 default:
4064 return 0;
4065 }
4066 }
4067
Michael Jurkaae65ee32012-04-30 11:45:54 -07004068 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07004069 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004070 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07004071 // Look for the toolbar icon specified in the activity meta-data
4072 Bundle metaData = packageManager.getActivityInfo(
4073 activityName, PackageManager.GET_META_DATA).metaData;
4074 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07004075 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004076 if (iconResId != 0) {
4077 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004078 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004079 }
4080 }
4081 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07004082 // This can happen if the activity defines an invalid drawable
4083 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
4084 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01004085 } catch (Resources.NotFoundException nfe) {
4086 // This can happen if the activity defines an invalid drawable
4087 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
4088 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004089 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004090 return null;
4091 }
4092
4093 // if successful in getting icon, return it; otherwise, set button to use default drawable
4094 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004095 int buttonId, ComponentName activityName, int fallbackDrawableId,
4096 String toolbarResourceName) {
4097 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07004098 Resources r = getResources();
4099 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
4100 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004101
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004102 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004103 // If we were unable to find the icon via the meta-data, use a generic one
4104 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07004105 toolbarIcon = r.getDrawable(fallbackDrawableId);
4106 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004107 if (button != null) {
4108 button.setCompoundDrawables(toolbarIcon, null, null, null);
4109 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004110 return null;
4111 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07004112 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004113 if (button != null) {
4114 button.setCompoundDrawables(toolbarIcon, null, null, null);
4115 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004116 return toolbarIcon.getConstantState();
4117 }
4118 }
4119
4120 // if successful in getting icon, return it; otherwise, set button to use default drawable
4121 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004122 int buttonId, ComponentName activityName, int fallbackDrawableId,
4123 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004124 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07004125 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004126
Michael Jurka19e0fc52011-07-22 18:00:21 -07004127 if (button != null) {
4128 // If we were unable to find the icon via the meta-data, use a
4129 // generic one
4130 if (toolbarIcon == null) {
4131 button.setImageResource(fallbackDrawableId);
4132 } else {
4133 button.setImageDrawable(toolbarIcon);
4134 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004135 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07004136
4137 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
4138
Michael Jurka0423dcf2010-10-05 14:56:18 -07004139 }
4140
Winson Chung1b884092012-06-08 17:13:02 -07004141 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004142 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07004143 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004144 }
4145
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004146 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08004147 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004148 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08004149 }
4150
Winson Chungbb185bd2011-11-21 12:31:42 -08004151 private void invalidatePressedFocusedStates(View container, View button) {
4152 if (container instanceof HolographicLinearLayout) {
4153 HolographicLinearLayout layout = (HolographicLinearLayout) container;
4154 layout.invalidatePressedFocusedStates();
4155 } else if (button instanceof HolographicImageView) {
4156 HolographicImageView view = (HolographicImageView) button;
4157 view.invalidatePressedFocusedStates();
4158 }
4159 }
4160
Cristina Stancu476493b2013-08-07 17:20:14 +01004161 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07004162 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4163 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07004164 }
4165
Adam Cohen24ce0b32014-01-14 16:18:14 -08004166 if (mQsb == null) {
4167 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
4168 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01004169 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08004170 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01004171 }
4172
4173 protected boolean updateGlobalSearchIcon() {
Adam Cohen9211d422014-10-07 18:14:53 -07004174 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4175 return true;
4176 }
4177
Winson Chungc51db6a2011-10-05 11:44:49 -07004178 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004179 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07004180 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004181 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004182
Winson Chung1cad91e2011-05-25 17:41:01 -07004183 final SearchManager searchManager =
4184 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4185 ComponentName activityName = searchManager.getGlobalSearchActivity();
4186 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004187 int coi = getCurrentOrientationIndexForGlobalIcons();
4188 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004189 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
4190 TOOLBAR_SEARCH_ICON_METADATA_NAME);
4191 if (sGlobalSearchIcon[coi] == null) {
4192 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4193 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
4194 TOOLBAR_ICON_METADATA_NAME);
4195 }
4196
Winson Chungc51db6a2011-10-05 11:44:49 -07004197 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
4198 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08004199 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004200 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004201 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004202 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07004203 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
4204 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004205 if (searchButton != null) searchButton.setVisibility(View.GONE);
4206 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004207 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004208 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004209 }
4210 }
4211
Cristina Stancu476493b2013-08-07 17:20:14 +01004212 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Adam Cohen9211d422014-10-07 18:14:53 -07004213 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) return;
Winson Chungbb185bd2011-11-21 12:31:42 -08004214 final View searchButtonContainer = findViewById(R.id.search_button_container);
4215 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004216 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004217 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004218 }
4219
Cristina Stancu476493b2013-08-07 17:20:14 +01004220 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Adam Cohen9211d422014-10-07 18:14:53 -07004221 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4222 return true;
4223 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004224 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004225 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004226
Winson Chungc51db6a2011-10-05 11:44:49 -07004227 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07004228 final SearchManager searchManager =
4229 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4230 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
4231
4232 ComponentName activityName = null;
4233 if (globalSearchActivity != null) {
4234 // Check if the global search activity handles voice search
4235 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4236 intent.setPackage(globalSearchActivity.getPackageName());
4237 activityName = intent.resolveActivity(getPackageManager());
4238 }
4239
4240 if (activityName == null) {
4241 // Fallback: check if an activity other than the global search activity
4242 // resolves this
4243 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4244 activityName = intent.resolveActivity(getPackageManager());
4245 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004246 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004247 int coi = getCurrentOrientationIndexForGlobalIcons();
4248 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004249 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4250 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
4251 if (sVoiceSearchIcon[coi] == null) {
4252 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4253 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4254 TOOLBAR_ICON_METADATA_NAME);
4255 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004256 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07004257 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07004258 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08004259 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004260 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004261 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07004262 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004263 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004264 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004265 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004266 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07004267 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004268
Cristina Stancu476493b2013-08-07 17:20:14 +01004269 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Adam Cohen9211d422014-10-07 18:14:53 -07004270 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4271 return;
4272 }
4273
Winson Chungbb185bd2011-11-21 12:31:42 -08004274 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
4275 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004276 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004277 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004278 }
4279
Winson Chung5841efa2013-09-30 18:06:44 -07004280 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Adam Cohen9211d422014-10-07 18:14:53 -07004281 if (mLauncherCallbacks != null) {
4282 forceDisableVoiceButtonProxy |= mLauncherCallbacks.forceDisableVoiceButtonProxy();
4283 }
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004284 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
4285 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07004286 boolean visible = !forceDisableVoiceButtonProxy &&
4287 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004288 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004289 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004290 }
4291 }
Winson Chung5841efa2013-09-30 18:06:44 -07004292
4293 /**
4294 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
4295 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
4296 */
4297 public void disableVoiceButtonProxy(boolean disabled) {
4298 updateVoiceButtonProxyVisible(disabled);
4299 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004300
Michael Jurkad7c28052012-04-27 15:43:36 -07004301 @Override
4302 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004303 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004304 final List<CharSequence> text = event.getText();
4305 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004306 // Populate event with a fake title based on the current state.
4307 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004308 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004309 } else {
4310 text.add(getString(R.string.all_apps_home_button_label));
4311 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004312 return result;
4313 }
4314
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004315 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004316 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004317 */
4318 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4319 @Override
4320 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004321 closeSystemDialogs();
4322 }
4323 }
4324
4325 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004326 * Receives notifications whenever the appwidgets are reset.
4327 */
4328 private class AppWidgetResetObserver extends ContentObserver {
4329 public AppWidgetResetObserver() {
4330 super(new Handler());
4331 }
4332
4333 @Override
4334 public void onChange(boolean selfChange) {
4335 onAppWidgetReset();
4336 }
4337 }
4338
4339 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004340 * If the activity is currently paused, signal that we need to run the passed Runnable
4341 * in onResume.
4342 *
4343 * This needs to be called from incoming places where resources might have been loaded
4344 * while we are paused. That is becaues the Configuration might be wrong
4345 * when we're not running, and if it comes back to what it was when we
4346 * were paused, we are not restarted.
4347 *
4348 * Implementation of the method from LauncherModel.Callbacks.
4349 *
4350 * @return true if we are currently paused. The caller might be able to
4351 * skip some work in that case since we will come back again.
4352 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004353 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004354 if (mPaused) {
4355 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004356 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004357 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004358 }
4359 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004360 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004361 return true;
4362 } else {
4363 return false;
4364 }
4365 }
4366
Michael Jurkac402cd92013-05-20 15:49:32 +02004367 private boolean waitUntilResume(Runnable run) {
4368 return waitUntilResume(run, false);
4369 }
4370
Michael Jurka1e2f4652013-07-08 18:03:46 -07004371 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004372 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004373 }
4374
Michael Jurka7607c2f2013-04-03 14:33:19 -07004375 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004376 * If the activity is currently paused, signal that we need to re-run the loader
4377 * in onResume.
4378 *
4379 * This needs to be called from incoming places where resources might have been loaded
4380 * while we are paused. That is becaues the Configuration might be wrong
4381 * when we're not running, and if it comes back to what it was when we
4382 * were paused, we are not restarted.
4383 *
4384 * Implementation of the method from LauncherModel.Callbacks.
4385 *
4386 * @return true if we are currently paused. The caller might be able to
4387 * skip some work in that case since we will come back again.
4388 */
4389 public boolean setLoadOnResume() {
4390 if (mPaused) {
4391 Log.i(TAG, "setLoadOnResume");
4392 mOnResumeNeedsLoad = true;
4393 return true;
4394 } else {
4395 return false;
4396 }
4397 }
4398
4399 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004400 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004401 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004402 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004403 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004404 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004405 } else {
4406 return SCREEN_COUNT / 2;
4407 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004408 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004409
Joe Onorato9c1289c2009-08-17 11:03:03 -04004410 /**
4411 * Refreshes the shortcuts shown on the workspace.
4412 *
4413 * Implementation of the method from LauncherModel.Callbacks.
4414 */
4415 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004416 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004417
Michael Jurka7607c2f2013-04-03 14:33:19 -07004418 // If we're starting binding all over again, clear any bind calls we'd postponed in
4419 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4420 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004421 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004422
Winson Chungd64d1762013-08-20 14:37:16 -07004423 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004424 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004425 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004426
Michael Jurka05bf644e2011-11-30 20:28:53 -08004427 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004428 if (mHotseat != null) {
4429 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004430 }
4431 }
4432
Adam Cohendcd297f2013-06-18 13:13:40 -07004433 @Override
4434 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004435 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004436
Adam Cohen5084cba2013-09-03 12:01:16 -07004437 // If there are no screens, we need to have an empty screen
4438 if (orderedScreenIds.size() == 0) {
4439 mWorkspace.addExtraEmptyScreen();
4440 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004441
4442 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004443 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004444 if (hasCustomContentToLeft()) {
4445 mWorkspace.createCustomContentContainer();
4446 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004447 }
Winson Chung64359a52013-07-08 17:17:08 -07004448 }
4449
4450 @Override
4451 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004452 // Log to disk
4453 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4454 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4455 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004456 int count = orderedScreenIds.size();
4457 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004458 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004459 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004460 }
4461
Adam Cohen39a06042013-07-19 14:30:12 -07004462 private boolean shouldShowWeightWatcher() {
4463 String spKey = LauncherAppState.getSharedPreferencesKey();
4464 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07004465 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004466
4467 return show;
4468 }
4469
4470 private void toggleShowWeightWatcher() {
4471 String spKey = LauncherAppState.getSharedPreferencesKey();
4472 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4473 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4474
4475 show = !show;
4476
4477 SharedPreferences.Editor editor = sp.edit();
4478 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4479 editor.commit();
4480
4481 if (mWeightWatcher != null) {
4482 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4483 }
4484 }
4485
Winson Chungd64d1762013-08-20 14:37:16 -07004486 public void bindAppsAdded(final ArrayList<Long> newScreens,
4487 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004488 final ArrayList<ItemInfo> addAnimated,
4489 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004490 Runnable r = new Runnable() {
4491 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004492 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004493 }
4494 };
4495 if (waitUntilResume(r)) {
4496 return;
4497 }
4498
Winson Chungd64d1762013-08-20 14:37:16 -07004499 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004500 if (newScreens != null) {
4501 bindAddScreens(newScreens);
4502 }
Winson Chungd64d1762013-08-20 14:37:16 -07004503
4504 // We add the items without animation on non-visible pages, and with
4505 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004506 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004507 bindItems(addNotAnimated, 0,
4508 addNotAnimated.size(), false);
4509 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004510 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004511 bindItems(addAnimated, 0,
4512 addAnimated.size(), true);
4513 }
Winson Chungc58497e2013-09-03 17:48:37 -07004514
Winson Chung87412982013-10-03 18:34:14 -07004515 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004516 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004517
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004518 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004519 addedApps != null && mAppsCustomizeContent != null) {
4520 mAppsCustomizeContent.addApps(addedApps);
4521 }
Winson Chungd64d1762013-08-20 14:37:16 -07004522 }
4523
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004524 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004525 * Bind the items start-end from the list.
4526 *
4527 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004528 */
Winson Chung64359a52013-07-08 17:17:08 -07004529 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4530 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004531 Runnable r = new Runnable() {
4532 public void run() {
4533 bindItems(shortcuts, start, end, forceAnimateIcons);
4534 }
4535 };
4536 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004537 return;
4538 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004539
Winson Chungf0c6ae02012-03-21 16:10:31 -07004540 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004541 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4542 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004543 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004544 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004545 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004546 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004547 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004548
4549 // Short circuit if we are loading dock items for a configuration which has no dock
4550 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4551 mHotseat == null) {
4552 continue;
4553 }
4554
Joe Onorato9c1289c2009-08-17 11:03:03 -04004555 switch (item.itemType) {
4556 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4557 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004558 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004559 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004560
Winson Chung64359a52013-07-08 17:17:08 -07004561 /*
4562 * TODO: FIX collision case
4563 */
Winson Chungce376632013-07-11 16:12:41 -07004564 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4565 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4566 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004567 View v = cl.getChildAt(item.cellX, item.cellY);
4568 Object tag = v.getTag();
4569 String desc = "Collision while binding workspace item: " + item
4570 + ". Collides with " + tag;
4571 if (LauncherAppState.isDogfoodBuild()) {
4572 throw (new RuntimeException(desc));
4573 } else {
4574 Log.d(TAG, desc);
4575 }
Winson Chungce376632013-07-11 16:12:41 -07004576 }
Winson Chung64359a52013-07-08 17:17:08 -07004577 }
4578
Adam Cohendcd297f2013-06-18 13:13:40 -07004579 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4580 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004581 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004582 // Animate all the applications up now
4583 shortcut.setAlpha(0f);
4584 shortcut.setScaleX(0f);
4585 shortcut.setScaleY(0f);
4586 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004587 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004588 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004589 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004590 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004591 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004592 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004593 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004594 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4595 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004596 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004597 default:
4598 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004600 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004601
Winson Chung997a9232013-07-24 15:33:46 -07004602 if (animateIcons) {
4603 // Animate to the correct page
4604 if (newShortcutsScreenId > -1) {
4605 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004606 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004607 final Runnable startBounceAnimRunnable = new Runnable() {
4608 public void run() {
4609 anim.playTogether(bounceAnims);
4610 anim.start();
4611 }
4612 };
Winson Chung997a9232013-07-24 15:33:46 -07004613 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004614 // We post the animation slightly delayed to prevent slowdowns
4615 // when we are loading right after we return to launcher.
4616 mWorkspace.postDelayed(new Runnable() {
4617 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004618 if (mWorkspace != null) {
4619 mWorkspace.snapToPage(newScreenIndex);
4620 mWorkspace.postDelayed(startBounceAnimRunnable,
4621 NEW_APPS_ANIMATION_DELAY);
4622 }
Winson Chung94d67682013-09-25 16:29:40 -07004623 }
4624 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004625 } else {
4626 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004627 }
4628 }
Winson Chung64359a52013-07-08 17:17:08 -07004629 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004630 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004631 }
4632
Joe Onorato9c1289c2009-08-17 11:03:03 -04004633 /**
4634 * Implementation of the method from LauncherModel.Callbacks.
4635 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004636 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004637 Runnable r = new Runnable() {
4638 public void run() {
4639 bindFolders(folders);
4640 }
4641 };
4642 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004643 return;
4644 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004645 sFolders.clear();
4646 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004647 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004648
4649 /**
4650 * Add the views for a widget to the workspace.
4651 *
4652 * Implementation of the method from LauncherModel.Callbacks.
4653 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004654 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004655 Runnable r = new Runnable() {
4656 public void run() {
4657 bindAppWidget(item);
4658 }
4659 };
4660 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004661 return;
4662 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004663
Daniel Sandler843e8602010-06-07 14:59:01 -04004664 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4665 if (DEBUG_WIDGETS) {
4666 Log.d(TAG, "bindAppWidget: " + item);
4667 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004668 final Workspace workspace = mWorkspace;
4669
Sunny Goyalff572272014-07-23 13:58:07 -07004670 AppWidgetProviderInfo appWidgetInfo;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004671 if (!mIsSafeModeEnabled
4672 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4673 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004674
4675 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4676 if (appWidgetInfo == null) {
4677 if (DEBUG_WIDGETS) {
4678 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4679 + " belongs to component " + item.providerName
4680 + ", as the povider is null");
4681 }
4682 LauncherModel.deleteItemFromDatabase(this, item);
4683 return;
4684 }
4685 // Note: This assumes that the id remap broadcast is received before this step.
4686 // If that is not the case, the id remap will be ignored and user may see the
4687 // click to setup view.
4688 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4689 pendingInfo.spanX = item.spanX;
4690 pendingInfo.spanY = item.spanY;
4691 pendingInfo.minSpanX = item.minSpanX;
4692 pendingInfo.minSpanY = item.minSpanY;
4693 Bundle options =
4694 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4695
Sunny Goyalff572272014-07-23 13:58:07 -07004696 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004697 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4698 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004699
4700 // TODO consider showing a permission dialog when the widget is clicked.
4701 if (!success) {
4702 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4703 if (DEBUG_WIDGETS) {
4704 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4705 + " belongs to component " + item.providerName
4706 + ", as the launcher is unable to bing a new widget id");
4707 }
4708 LauncherModel.deleteItemFromDatabase(this, item);
4709 return;
4710 }
4711
4712 item.appWidgetId = newWidgetId;
4713
4714 // If the widget has a configure activity, it is still needs to set it up, otherwise
4715 // the widget is ready to go.
4716 item.restoreStatus = (appWidgetInfo.configure == null)
4717 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4718 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4719
4720 LauncherModel.updateItemInDatabase(this, item);
4721 }
4722
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004723 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004724 final int appWidgetId = item.appWidgetId;
4725 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4726 if (DEBUG_WIDGETS) {
4727 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4728 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004729
Sunny Goyal651077b2014-06-30 14:15:31 -07004730 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4731 } else {
4732 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004733 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4734 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004735 view.updateIcon(mIconCache);
4736 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004737 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004738 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004739 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004740
Joe Onorato9c1289c2009-08-17 11:03:03 -04004741 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004742 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004743
Adam Cohendcd297f2013-06-18 13:13:40 -07004744 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004745 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004746 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4747
Joe Onorato9c1289c2009-08-17 11:03:03 -04004748 workspace.requestLayout();
4749
Daniel Sandler843e8602010-06-07 14:59:01 -04004750 if (DEBUG_WIDGETS) {
4751 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4752 + (SystemClock.uptimeMillis()-start) + "ms");
4753 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004754 }
4755
Sunny Goyalff572272014-07-23 13:58:07 -07004756 /**
4757 * Restores a pending widget.
4758 *
4759 * @param appWidgetId The app widget id
4760 * @param cellInfo The position on screen where to create the widget.
4761 */
4762 private void completeRestoreAppWidget(final int appWidgetId) {
4763 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4764 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4765 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4766 return;
4767 }
4768
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004769 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004770 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4771
4772 mWorkspace.reinflateWidgetsIfNecessary();
4773 LauncherModel.updateItemInDatabase(this, info);
4774 }
4775
Adam Cohen1462de32012-07-24 22:34:36 -07004776 public void onPageBoundSynchronously(int page) {
4777 mSynchronouslyBoundPages.add(page);
4778 }
4779
Joe Onorato9c1289c2009-08-17 11:03:03 -04004780 /**
4781 * Callback saying that there aren't any more items to bind.
4782 *
4783 * Implementation of the method from LauncherModel.Callbacks.
4784 */
Adam Cohene25af792013-06-06 23:08:25 -07004785 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004786 Runnable r = new Runnable() {
4787 public void run() {
4788 finishBindingItems(upgradePath);
4789 }
4790 };
4791 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004792 return;
4793 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004794 if (mSavedState != null) {
4795 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004796 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004797 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004798 mSavedState = null;
4799 }
4800
Adam Cohen1462de32012-07-24 22:34:36 -07004801 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004802
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004803 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004804 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004805
4806 // If we received the result of any pending adds while the loader was running (e.g. the
4807 // widget configuration forced an orientation change), process them now.
4808 if (sPendingAddItem != null) {
4809 final long screenId = completeAdd(sPendingAddItem);
4810
4811 // TODO: this moves the user to the page where the pending item was added. Ideally,
4812 // the screen would be guaranteed to exist after bind, and the page would be set through
4813 // the workspace restore process.
4814 mWorkspace.post(new Runnable() {
4815 @Override
4816 public void run() {
4817 mWorkspace.snapToScreenId(screenId);
4818 }
4819 });
4820 sPendingAddItem = null;
4821 }
4822
Adam Cohene25af792013-06-06 23:08:25 -07004823 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004824 mWorkspace.getUniqueComponents(true, null);
4825 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004826 }
Sunny Goyale755d462014-07-22 13:48:29 -07004827 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004828
4829 if (mLauncherCallbacks != null) {
4830 mLauncherCallbacks.finishBindingItems(upgradePath);
4831 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004832 }
4833
Adam Cohen3ed316a2014-07-23 18:21:20 -07004834 private void sendLoadingCompleteBroadcastIfNecessary() {
4835 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4836 String permission =
4837 getResources().getString(R.string.receive_first_load_broadcast_permission);
4838 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4839 sendBroadcast(intent, permission);
4840 SharedPreferences.Editor editor = mSharedPrefs.edit();
4841 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4842 editor.apply();
4843 }
4844 }
4845
Winson Chunga0b7e862013-09-05 16:03:15 -07004846 public boolean isAllAppsButtonRank(int rank) {
4847 if (mHotseat != null) {
4848 return mHotseat.isAllAppsButtonRank(rank);
4849 }
4850 return false;
4851 }
4852
Winson Chunga2413752012-04-03 14:22:34 -07004853 private boolean canRunNewAppsAnimation() {
4854 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4855 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4856 }
4857
Winson Chungc9168342013-06-26 14:54:55 -07004858 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4859 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4860 PropertyValuesHolder.ofFloat("alpha", 1f),
4861 PropertyValuesHolder.ofFloat("scaleX", 1f),
4862 PropertyValuesHolder.ofFloat("scaleY", 1f));
4863 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4864 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4865 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4866 return bounceAnim;
4867 }
4868
Adam Cohen27772732013-11-11 14:00:56 +00004869 public boolean useVerticalBarLayout() {
4870 return LauncherAppState.getInstance().getDynamicGrid().
4871 getDeviceProfile().isVerticalBarLayout();
4872 }
4873
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004874 protected Rect getSearchBarBounds() {
4875 return LauncherAppState.getInstance().getDynamicGrid().
4876 getDeviceProfile().getSearchBarBounds();
4877 }
4878
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004879 @Override
4880 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004881 boolean searchVisible = updateGlobalSearchIcon();
4882 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004883 if (mSearchDropTargetBar != null) {
4884 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4885 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004886 }
4887
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004888 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004889 * Add the icons for all apps.
4890 *
4891 * Implementation of the method from LauncherModel.Callbacks.
4892 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004893 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004894 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004895 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4896 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4897 getHotseat().addAllAppsFolder(mIconCache, apps,
4898 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4899 }
4900 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004901 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004902 if (mAppsCustomizeContent != null) {
4903 mAppsCustomizeContent.onPackagesUpdated(
4904 LauncherModel.getSortedWidgetsAndShortcuts(this));
4905 }
Winson Chungc58497e2013-09-03 17:48:37 -07004906 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004907 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004908 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004909 mAppsCustomizeContent.onPackagesUpdated(
4910 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004911 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004912 }
Adam Cohen9211d422014-10-07 18:14:53 -07004913 if (mLauncherCallbacks != null) {
4914 mLauncherCallbacks.bindAllApplications(apps);
4915 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004916 }
4917
4918 /**
4919 * A package was updated.
4920 *
4921 * Implementation of the method from LauncherModel.Callbacks.
4922 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004923 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004924 Runnable r = new Runnable() {
4925 public void run() {
4926 bindAppsUpdated(apps);
4927 }
4928 };
4929 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004930 return;
4931 }
4932
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004933 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004934 mAppsCustomizeContent != null) {
4935 mAppsCustomizeContent.updateApps(apps);
4936 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004937 }
4938
Sunny Goyal4390ace2014-10-13 11:33:11 -07004939 @Override
4940 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4941 Runnable r = new Runnable() {
4942 public void run() {
4943 bindWidgetsRestored(widgets);
4944 }
4945 };
4946 if (waitUntilResume(r)) {
4947 return;
4948 }
4949 mWorkspace.widgetsRestored(widgets);
4950 }
4951
Joe Onorato9c1289c2009-08-17 11:03:03 -04004952 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004953 * Some shortcuts were updated in the background.
4954 *
4955 * Implementation of the method from LauncherModel.Callbacks.
4956 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004957 @Override
4958 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4959 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004960 Runnable r = new Runnable() {
4961 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004962 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004963 }
4964 };
4965 if (waitUntilResume(r)) {
4966 return;
4967 }
4968
Sunny Goyal4390ace2014-10-13 11:33:11 -07004969 if (!updated.isEmpty()) {
4970 mWorkspace.updateShortcuts(updated);
4971 }
4972
4973 if (!removed.isEmpty()) {
4974 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4975 for (ShortcutInfo si : removed) {
4976 removedComponents.add(si.getTargetComponent());
4977 }
4978 mWorkspace.removeItemsByComponentName(removedComponents, user);
4979 // Notify the drag controller
4980 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004981 }
4982 }
4983
4984 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004985 * Update the state of a package, typically related to install state.
4986 *
4987 * Implementation of the method from LauncherModel.Callbacks.
4988 */
Sunny Goyale755d462014-07-22 13:48:29 -07004989 @Override
4990 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004991 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004992 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004993 }
4994 }
4995
4996 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004997 * Update the label and icon of all the icons in a package
4998 *
4999 * Implementation of the method from LauncherModel.Callbacks.
5000 */
5001 @Override
5002 public void updatePackageBadge(String packageName) {
5003 if (mWorkspace != null) {
5004 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
5005 }
5006 }
5007
5008 /**
Winson Chung83892cc2013-05-01 16:53:33 -07005009 * A package was uninstalled. We take both the super set of packageNames
5010 * in addition to specific applications to remove, the reason being that
5011 * this can be called when a package is updated as well. In that scenario,
5012 * we only remove specific components from the workspace, where as
5013 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04005014 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07005015 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04005016 * Implementation of the method from LauncherModel.Callbacks.
5017 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07005018 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07005019 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07005020 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07005021 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07005022 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07005023 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07005024 }
Winson Chungd64d1762013-08-20 14:37:16 -07005025 };
5026 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07005027 return;
5028 }
5029
Sunny Goyal1a745e82014-10-02 15:58:31 -07005030 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07005031 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
5032 for (AppInfo info : appInfos) {
5033 removedComponents.add(info.componentName);
5034 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07005035 if (!packageNames.isEmpty()) {
5036 mWorkspace.removeItemsByPackageName(packageNames, user);
5037 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07005038 if (!removedComponents.isEmpty()) {
5039 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07005040 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07005041 // Notify the drag controller
5042 mDragController.onAppsRemoved(packageNames, removedComponents);
5043
Sunny Goyal1a745e82014-10-02 15:58:31 -07005044 } else {
5045 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04005046 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07005047
Winson Chungdf95eb12013-10-16 14:57:07 -07005048 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08005049 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07005050 mAppsCustomizeContent != null) {
5051 mAppsCustomizeContent.removeApps(appInfos);
5052 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04005053 }
Joe Onoratobe386092009-11-17 17:32:16 -08005054
5055 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07005056 * A number of packages were updated.
5057 */
Michael Jurkac402cd92013-05-20 15:49:32 +02005058 private ArrayList<Object> mWidgetsAndShortcuts;
5059 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07005060 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02005061 bindPackagesUpdated(mWidgetsAndShortcuts);
5062 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07005063 }
Michael Jurkac402cd92013-05-20 15:49:32 +02005064 };
Michael Jurkac402cd92013-05-20 15:49:32 +02005065 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
5066 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
5067 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07005068 return;
5069 }
5070
Winson Chung64359a52013-07-08 17:17:08 -07005071 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08005072 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02005073 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07005074 }
Winson Chung80baf5a2010-08-09 16:03:15 -07005075 }
5076
Winson Chung400438b2011-01-16 17:53:48 -08005077 private int mapConfigurationOriActivityInfoOri(int configOri) {
5078 final Display d = getWindowManager().getDefaultDisplay();
5079 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
5080 switch (d.getRotation()) {
5081 case Surface.ROTATION_0:
5082 case Surface.ROTATION_180:
5083 // We are currently in the same basic orientation as the natural orientation
5084 naturalOri = configOri;
5085 break;
5086 case Surface.ROTATION_90:
5087 case Surface.ROTATION_270:
5088 // We are currently in the other basic orientation to the natural orientation
5089 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
5090 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
5091 break;
5092 }
5093
5094 int[] oriMap = {
5095 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
5096 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
5097 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
5098 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
5099 };
5100 // Since the map starts at portrait, we need to offset if this device's natural orientation
5101 // is landscape.
5102 int indexOffset = 0;
5103 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
5104 indexOffset = 1;
5105 }
5106 return oriMap[(d.getRotation() + indexOffset) % 4];
5107 }
Adam Cohen446e9402011-09-15 18:21:21 -07005108
Winson Chung4b919f82012-05-01 10:44:08 -07005109 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08005110 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07005111 getResources().getBoolean(R.bool.allow_rotation);
5112 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08005113 }
Winson Chung4b919f82012-05-01 10:44:08 -07005114 public void lockScreenOrientation() {
5115 if (isRotationEnabled()) {
5116 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
5117 .getConfiguration().orientation));
5118 }
5119 }
5120 public void unlockScreenOrientation(boolean immediate) {
5121 if (isRotationEnabled()) {
5122 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07005123 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07005124 } else {
5125 mHandler.postDelayed(new Runnable() {
5126 public void run() {
5127 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
5128 }
5129 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07005130 }
Winson Chung4b919f82012-05-01 10:44:08 -07005131 }
Winson Chung400438b2011-01-16 17:53:48 -08005132 }
5133
Winson Chunge43a1e72014-01-15 10:33:02 -08005134 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07005135 if (mLauncherCallbacks != null) {
5136 return mLauncherCallbacks.isLauncherPreinstalled();
5137 }
Winson Chunge43a1e72014-01-15 10:33:02 -08005138 PackageManager pm = getPackageManager();
5139 try {
5140 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
5141 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
5142 return true;
5143 } else {
5144 return false;
5145 }
5146 } catch (NameNotFoundException e) {
5147 e.printStackTrace();
5148 return false;
5149 }
5150 }
5151
Adam Cohenea90f832014-05-21 15:03:34 -07005152 /**
5153 * This method indicates whether or not we should suggest default wallpaper dimensions
5154 * when our wallpaper cropper was not yet used to set a wallpaper.
5155 */
5156 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07005157 if (mLauncherCallbacks != null) {
5158 return mLauncherCallbacks.overrideWallpaperDimensions();
5159 }
Adam Cohenea90f832014-05-21 15:03:34 -07005160 return true;
5161 }
5162
Adam Cohen432609a2014-03-13 17:03:22 -07005163 /**
5164 * To be overridden by subclasses to indicate that there is an activity to launch
5165 * before showing the standard launcher experience.
5166 */
5167 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07005168 if (mLauncherCallbacks != null) {
5169 return mLauncherCallbacks.hasFirstRunActivity();
5170 }
Adam Cohen432609a2014-03-13 17:03:22 -07005171 return false;
5172 }
5173
5174 /**
5175 * To be overridden by subclasses to launch any first run activity
5176 */
5177 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07005178 if (mLauncherCallbacks != null) {
5179 return mLauncherCallbacks.getFirstRunActivity();
5180 }
Adam Cohen432609a2014-03-13 17:03:22 -07005181 return null;
5182 }
5183
Winson Chunga6945242014-01-08 14:04:34 -08005184 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08005185 return !ActivityManager.isRunningInTestHarness() &&
5186 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08005187 }
5188
Adam Cohen4a9423d2014-03-28 14:22:31 -07005189 protected boolean hasRunFirstRunActivity() {
5190 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
5191 }
5192
Adam Cohen432609a2014-03-13 17:03:22 -07005193 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08005194 if (shouldRunFirstRunActivity() &&
5195 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08005196 Intent firstRunIntent = getFirstRunActivity();
5197 if (firstRunIntent != null) {
5198 startActivity(firstRunIntent);
5199 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07005200 return true;
Winson Chunga6945242014-01-08 14:04:34 -08005201 }
5202 }
Adam Cohen432609a2014-03-13 17:03:22 -07005203 return false;
Winson Chunga6945242014-01-08 14:04:34 -08005204 }
5205
5206 private void markFirstRunActivityShown() {
5207 SharedPreferences.Editor editor = mSharedPrefs.edit();
5208 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
5209 editor.apply();
5210 }
5211
Adam Cohen432609a2014-03-13 17:03:22 -07005212 /**
5213 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
5214 * screen that must be displayed and dismissed.
5215 */
5216 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07005217 if (mLauncherCallbacks != null) {
5218 return mLauncherCallbacks.hasDismissableIntroScreen();
5219 }
Adam Cohen432609a2014-03-13 17:03:22 -07005220 return false;
5221 }
5222
5223 /**
5224 * Full screen intro screen to be shown and dismissed before the launcher can be used.
5225 */
5226 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07005227 if (mLauncherCallbacks != null) {
5228 return mLauncherCallbacks.getIntroScreen();
5229 }
Adam Cohen432609a2014-03-13 17:03:22 -07005230 return null;
5231 }
5232
5233 /**
5234 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
5235 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
5236 */
5237 private boolean shouldShowIntroScreen() {
5238 return hasDismissableIntroScreen() &&
5239 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
5240 }
5241
5242 protected void showIntroScreen() {
5243 View introScreen = getIntroScreen();
5244 changeWallpaperVisiblity(false);
5245 if (introScreen != null) {
5246 mDragLayer.showOverlayView(introScreen);
5247 }
5248 }
5249
5250 public void dismissIntroScreen() {
5251 markIntroScreenDismissed();
5252 if (showFirstRunActivity()) {
5253 // We delay hiding the intro view until the first run activity is showing. This
5254 // avoids a blip.
5255 mWorkspace.postDelayed(new Runnable() {
5256 @Override
5257 public void run() {
5258 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005259 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005260 }
5261 }, ACTIVITY_START_DELAY);
5262 } else {
5263 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005264 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005265 }
5266 changeWallpaperVisiblity(true);
5267 }
5268
5269 private void markIntroScreenDismissed() {
5270 SharedPreferences.Editor editor = mSharedPrefs.edit();
5271 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
5272 editor.apply();
5273 }
5274
Sunny Goyal88d60f12014-09-08 03:47:29 -07005275 private void showFirstRunClings() {
5276 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
5277 // on the device, then we always show the first run cling experience (or if there is no
5278 // launcher2). Otherwise, we prompt the user upon started for migration
5279 LauncherClings launcherClings = new LauncherClings(this);
5280 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
5281 if (mModel.canMigrateFromOldLauncherDb(this)) {
5282 launcherClings.showMigrationCling();
5283 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005284 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005285 }
5286 }
5287 }
5288
Winson Chunga6945242014-01-08 14:04:34 -08005289 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005290 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5291 if (mHotseat != null) mHotseat.setAlpha(1f);
5292 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5293 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005294 }
5295
5296 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005297 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5298 if (mHotseat != null) mHotseat.setAlpha(0f);
5299 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5300 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005301 }
5302
Mathew Inwood72fbec12013-11-19 15:45:07 +00005303 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005304 // Called from search suggestion, not supported in other profiles.
5305 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5306 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5307 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5308 myUser);
5309 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005310 return null;
5311 }
Kenny Guyed131872014-04-30 03:02:21 +01005312 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005313 }
5314
5315 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5316 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005317 // Called from search suggestion, not supported in other profiles.
5318 return createShortcutDragInfo(shortcutIntent, caption, icon,
5319 UserHandleCompat.myUserHandle());
5320 }
5321
5322 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5323 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005324 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005325 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005326 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005327 }
5328
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005329 protected void moveWorkspaceToDefaultScreen() {
5330 mWorkspace.moveToDefaultScreen(false);
5331 }
5332
Mathew Inwood72fbec12013-11-19 15:45:07 +00005333 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5334 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005335 mWorkspace.onExternalDragStartedWithItem(dragView);
5336 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005337 }
5338
Anjali Koppalf5d29132014-02-28 13:33:27 -08005339 @Override
5340 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005341 if (mLauncherCallbacks != null) {
5342 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5343 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005344 }
5345
Winson Chung80baf5a2010-08-09 16:03:15 -07005346 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005347 * Prints out out state for debugging.
5348 */
5349 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005350 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005351 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005352 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5353 Log.d(TAG, "mRestoring=" + mRestoring);
5354 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5355 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005356 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005357 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005358
Winson Chung785d2eb2011-04-14 16:08:02 -07005359 if (mAppsCustomizeContent != null) {
5360 mAppsCustomizeContent.dumpState();
5361 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005362 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005363 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005364
5365 @Override
5366 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5367 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005368 synchronized (sDumpLogs) {
5369 writer.println(" ");
5370 writer.println("Debug logs: ");
5371 for (int i = 0; i < sDumpLogs.size(); i++) {
5372 writer.println(" " + sDumpLogs.get(i));
5373 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005374 }
Adam Cohen9211d422014-10-07 18:14:53 -07005375 if (mLauncherCallbacks != null) {
5376 mLauncherCallbacks.dump(prefix, fd, writer, args);
5377 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005378 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005379
5380 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005381 if (DEBUG_DUMP_LOG) {
5382 synchronized (sDumpLogs) {
5383 Log.d(TAG, "");
5384 Log.d(TAG, "*********************");
5385 Log.d(TAG, "Launcher debug logs: ");
5386 for (int i = 0; i < sDumpLogs.size(); i++) {
5387 Log.d(TAG, " " + sDumpLogs.get(i));
5388 }
5389 Log.d(TAG, "*********************");
5390 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005391 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005392 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005393 }
5394
5395 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005396 addDumpLog(tag, log, null, debugLog);
5397 }
5398
5399 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005400 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005401 if (e != null) {
5402 Log.d(tag, log, e);
5403 } else {
5404 Log.d(tag, log);
5405 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005406 }
Winson Chungede41292013-09-19 16:27:36 -07005407 if (DEBUG_DUMP_LOG) {
5408 sDateStamp.setTime(System.currentTimeMillis());
5409 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005410 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5411 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005412 }
Winson Chungede41292013-09-19 16:27:36 -07005413 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005414 }
5415
Winson Chungede41292013-09-19 16:27:36 -07005416 public void dumpLogsToLocalData() {
5417 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005418 new AsyncTask<Void, Void, Void>() {
5419 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005420 boolean success = false;
5421 sDateStamp.setTime(sRunStart);
5422 String FILENAME = sDateStamp.getMonth() + "-"
5423 + sDateStamp.getDay() + "_"
5424 + sDateStamp.getHours() + "-"
5425 + sDateStamp.getMinutes() + "_"
5426 + sDateStamp.getSeconds() + ".txt";
5427
5428 FileOutputStream fos = null;
5429 File outFile = null;
5430 try {
5431 outFile = new File(getFilesDir(), FILENAME);
5432 outFile.createNewFile();
5433 fos = new FileOutputStream(outFile);
5434 } catch (Exception e) {
5435 e.printStackTrace();
5436 }
5437 if (fos != null) {
5438 PrintWriter writer = new PrintWriter(fos);
5439
5440 writer.println(" ");
5441 writer.println("Debug logs: ");
5442 synchronized (sDumpLogs) {
5443 for (int i = 0; i < sDumpLogs.size(); i++) {
5444 writer.println(" " + sDumpLogs.get(i));
5445 }
5446 }
5447 writer.close();
5448 }
5449 try {
5450 if (fos != null) {
5451 fos.close();
5452 success = true;
5453 }
5454 } catch (IOException e) {
5455 e.printStackTrace();
5456 }
Michael Jurka43467462013-11-14 12:03:58 +01005457 return null;
Winson Chungede41292013-09-19 16:27:36 -07005458 }
Michael Jurka43467462013-11-14 12:03:58 +01005459 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005460 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005462}
Michael Jurka2763be32011-02-24 11:19:57 -08005463
Michael Jurkaabded662011-03-04 12:06:57 -08005464interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005465 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005466 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005467 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005468 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005469 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005470}
Dan Sandlerd5024042014-01-09 15:01:33 -05005471
5472interface DebugIntents {
5473 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5474 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005475}