blob: a5ca2981cabb462e61a9d40ead269ba77df7a3de [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;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
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;
Winson Chung5f8afe62013-08-12 16:19:28 -070056import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
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;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070068import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080069import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070074import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070075import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Display;
77import android.view.Gravity;
78import android.view.HapticFeedbackConstants;
79import android.view.KeyEvent;
80import android.view.LayoutInflater;
81import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.MotionEvent;
83import android.view.Surface;
84import android.view.View;
Adam Cohen0b395352014-06-09 22:54:36 +000085import android.view.Window;
Adam Cohenf358a4b2013-07-23 16:47:31 -070086import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080087import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.ViewGroup;
89import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080090import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080092import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070093import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080094import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070096import android.widget.Advanceable;
97import android.widget.FrameLayout;
98import android.widget.ImageView;
99import android.widget.TextView;
100import android.widget.Toast;
Kenny Guyed131872014-04-30 03:02:21 +0100101import com.android.launcher3.compat.LauncherActivityInfoCompat;
102import com.android.launcher3.compat.LauncherAppsCompat;
103import com.android.launcher3.compat.UserHandleCompat;
104import com.android.launcher3.compat.UserManagerCompat;
Daniel Sandler325dc232013-06-05 22:57:57 -0400105import com.android.launcher3.DropTarget.DragObject;
Anjali Koppalf5d29132014-02-28 13:33:27 -0800106import com.android.launcher3.PagedView.PageSwitchListener;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.io.DataInputStream;
108import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700109import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700110import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700114import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000115import java.lang.reflect.Field;
116import java.lang.reflect.InvocationTargetException;
117import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700118import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700120import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700121import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700122import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700123import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000124import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700125
Winson Chunga6945242014-01-08 14:04:34 -0800126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127/**
128 * Default launcher application.
129 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100130public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700131 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700132 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800133 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700134 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Daniel Sandlerf061f822013-06-27 13:59:36 -0400136 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400137 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700138 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700140 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700141
Dan Sandlerd5024042014-01-09 15:01:33 -0500142 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700145 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700148 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Michael Jurka8b805b12012-04-18 14:23:14 -0700150 private static final int REQUEST_BIND_APPWIDGET = 11;
151
Mathew Inwood876a8462013-06-14 14:12:41 +0100152 /**
153 * IntentStarter uses request codes starting with this. This must be greater than all activity
154 * request codes used internally.
155 */
156 protected static final int REQUEST_LAST = 100;
157
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
159
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800160 static final int SCREEN_COUNT = 5;
161 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Romain Guy98d01652009-06-30 16:21:04 -0700163 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800164 // To turn on these properties, type
165 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
166 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
167 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800168 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Winson Chung2672ff92012-05-04 16:22:30 -0700170 // The Intent extra that defines whether to ignore the launch animation
171 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400172 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 // Type: int
175 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700176 // Type: int
177 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700179 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
180 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
182 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700185 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: boolean
187 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
188 // Type: long
189 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
192 // Type: int
193 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
194 // Type: parcelable
195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000198 // Type: int[]
199 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Adam Cohenb54a5982014-01-08 15:21:04 -0800201
Adam Cohen432609a2014-03-13 17:03:22 -0700202 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800203 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
204
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100205 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700206 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100207 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700208 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100209 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700210
Adam Cohen39a06042013-07-19 14:30:12 -0700211 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700212 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700213
Winson Chunga6945242014-01-08 14:04:34 -0800214 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
215
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700216 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700217 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700218 private State mState = State.WORKSPACE;
219 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700220
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700221 private boolean mIsSafeModeEnabled;
222
Joe Onorato9c1289c2009-08-17 11:03:03 -0400223 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700224 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
225 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700226 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800229 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000231 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
232 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
233
Winson Chunga2413752012-04-03 14:22:34 -0700234 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700235 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
236 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700237 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700238
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800239 private final BroadcastReceiver mCloseSystemDialogsReceiver
240 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800241 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 private LayoutInflater mInflater;
244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700246 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800247 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800248 private DragLayer mDragLayer;
249 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700250 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800251 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700252
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700253 private AppWidgetManager mAppWidgetManager;
254 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700255
Michael Jurkac9d95c52011-08-29 14:03:34 -0700256 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700257 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800258 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700259
Michael Jurka0280c3b2010-09-17 15:00:07 -0700260 private int[] mTmpAddItemCellCoordinates = new int[2];
261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 private FolderInfo mFolderInfo;
263
Winson Chung3d503fb2011-07-13 17:25:49 -0700264 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800265 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700266
Michael Jurka838a4ca2011-02-07 13:33:06 -0800267 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700268
Winson Chungc51db6a2011-10-05 11:44:49 -0700269 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700270 private AppsCustomizeTabHost mAppsCustomizeTabHost;
271 private AppsCustomizePagedView mAppsCustomizeContent;
272 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800273 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700276 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
277 // scroll issues (because the workspace may not have been measured yet) and extra work.
278 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
279 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280
281 private SpannableStringBuilder mDefaultKeySsb = null;
282
Joe Onorato9c1289c2009-08-17 11:03:03 -0400283 private boolean mWorkspaceLoading = true;
284
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800285 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 private boolean mRestoring;
287 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700288 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289
Michael Jurka1e2f4652013-07-08 18:03:46 -0700290 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700291 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 private Bundle mSavedInstanceState;
294
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800296 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800297 private boolean mUserPresent = true;
298 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700299 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800300 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400301
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700302 private static LocaleConfiguration sLocaleConfiguration = null;
303
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700304 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700305
Adam Cohen61f560d2013-09-30 15:58:20 -0700306 private View.OnTouchListener mHapticFeedbackTouchListener;
307
Adam Cohended9f8d2010-11-03 13:25:16 -0700308 // Related to the auto-advancing of widgets
309 private final int ADVANCE_MSG = 1;
310 private final int mAdvanceInterval = 20000;
311 private final int mAdvanceStagger = 250;
312 private long mAutoAdvanceSentTime;
313 private long mAutoAdvanceTimeLeft = -1;
314 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
315 new HashMap<View, AppWidgetProviderInfo>();
316
Winson Chung400438b2011-01-16 17:53:48 -0800317 // Determines how long to wait after a rotation before restoring the screen orientation to
318 // match the sensor state.
319 private final int mRestoreScreenOrientationDelay = 500;
320
Michael Jurka4ef207b2010-11-29 17:05:45 -0800321 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700322 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
323 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
324 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800325
Winson Chungd64a6662013-09-30 11:06:59 -0700326 private Intent mAppMarketIntent = null;
327 private static final boolean DISABLE_MARKET_BUTTON = true;
328
Craig Mautner360310b2012-10-26 15:13:08 -0700329 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700330
Adam Cohen1462de32012-07-24 22:34:36 -0700331 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700332 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700333
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700334 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700335 static Date sDateStamp = new Date();
336 static DateFormat sDateFormat =
337 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
338 static long sRunStart = System.currentTimeMillis();
339 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700340
Winson Chung46353de2012-02-16 14:05:10 -0800341 // We only want to get the SharedPreferences once since it does an FS stat each time we get
342 // it from the context.
343 private SharedPreferences mSharedPrefs;
344
Adam Cohene25af792013-06-06 23:08:25 -0700345 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
346
Winson Chungf0c6ae02012-03-21 16:10:31 -0700347 // Holds the page that we need to animate to, and the icon views that we need to animate up
348 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700349 private ImageView mFolderIconImageView;
350 private Bitmap mFolderIconBitmap;
351 private Canvas mFolderIconCanvas;
352 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700353
Michael Jurkaddd62e92011-02-16 17:49:14 -0800354 private BubbleTextView mWaitingForResume;
355
Michael Jurkac1f5d262011-09-30 19:32:27 -0700356 private Runnable mBuildLayersRunnable = new Runnable() {
357 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700358 if (mWorkspace != null) {
359 mWorkspace.buildPageHardwareLayers();
360 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700361 }
362 };
363
Adam Cohendb364c32014-05-20 14:23:40 -0700364 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800365
Michael Jurka7267fa52013-09-26 15:29:57 -0700366 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800367
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800368 private static class PendingAddArguments {
369 int requestCode;
370 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700371 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700372 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800373 int cellX;
374 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700375 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800376 }
377
Daniel Sandlerff02d492013-08-05 02:12:05 -0400378 private Stats mStats;
379
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800380 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800381 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700382 }
383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 @Override
385 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700386 if (DEBUG_STRICT_MODE) {
387 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
388 .detectDiskReads()
389 .detectDiskWrites()
390 .detectNetwork() // or .detectAll() for all detectable problems
391 .penaltyLog()
392 .build());
393 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
394 .detectLeakedSqlLiteObjects()
395 .detectLeakedClosableObjects()
396 .penaltyLog()
397 .penaltyDeath()
398 .build());
399 }
400
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400402
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400403 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400404 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700405 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700406 // Determine the dynamic grid properties
407 Point smallestSize = new Point();
408 Point largestSize = new Point();
409 Point realSize = new Point();
410 Display display = getWindowManager().getDefaultDisplay();
411 display.getCurrentSizeRange(smallestSize, largestSize);
412 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700413 DisplayMetrics dm = new DisplayMetrics();
414 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700415
Winson Chung5f8afe62013-08-12 16:19:28 -0700416 // Lazy-initialize the dynamic grid
417 DeviceProfile grid = app.initDynamicGrid(this,
418 Math.min(smallestSize.x, smallestSize.y),
419 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700420 realSize.x, realSize.y,
421 dm.widthPixels, dm.heightPixels);
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);
Winson Chunga6945242014-01-08 14:04:34 -0800431 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700433
Daniel Sandlerff02d492013-08-05 02:12:05 -0400434 mStats = new Stats(this);
435
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700436 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700437
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700438 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
439 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700440
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700441 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
442 // this also ensures that any synchronous binding below doesn't re-trigger another
443 // LauncherModel load.
444 mPaused = false;
445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200447 android.os.Debug.startMethodTracing(
448 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 }
450
Adam Cohen53805212013-10-01 10:39:23 -0700451
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700454
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100456 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800458 registerContentObservers();
459
Joe Onorato7c312c12009-08-13 21:36:53 -0700460 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 mSavedState = savedInstanceState;
463 restoreState(mSavedState);
464
465 if (PROFILE_STARTUP) {
466 android.os.Debug.stopMethodTracing();
467 }
468
469 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700470 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700471 // If the user leaves launcher, then we should just load items asynchronously when
472 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500473 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700474 } else {
475 // We only load the page synchronously if the user rotates (or triggers a
476 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800477 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 }
480
481 // For handling default keys
482 mDefaultKeySsb = new SpannableStringBuilder();
483 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800484
485 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
486 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800487
Adam Cohenf9426d52012-06-04 17:26:21 -0700488 updateGlobalIcons();
489
490 // On large interfaces, we want the screen to auto-rotate based on the current orientation
491 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700492
Adam Cohen432609a2014-03-13 17:03:22 -0700493 if (shouldShowIntroScreen()) {
494 showIntroScreen();
495 } else {
496 showFirstRunActivity();
497 }
498
Winson Chunge43a1e72014-01-15 10:33:02 -0800499 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
500 // on the device, then we always show the first run cling experience (or if there is no
501 // launcher2). Otherwise, we prompt the user upon started for migration
Winson Chunge43a1e72014-01-15 10:33:02 -0800502 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
503 if (mModel.canMigrateFromOldLauncherDb(this)) {
504 mLauncherClings.showMigrationCling();
505 } else {
506 mLauncherClings.showFirstRunCling();
507 }
Winson Chunga6945242014-01-08 14:04:34 -0800508 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800509 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800510 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700511 }
512
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700513 @Override
514 public void onLauncherProviderChange() { }
515
Winson Chung98ca0f72013-07-29 12:58:51 -0700516 /** To be overriden by subclasses to hint to Launcher that we have custom content */
517 protected boolean hasCustomContentToLeft() {
518 return false;
519 }
520
Dave Hawkeya8881582013-09-17 15:55:33 -0600521 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500522 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600523 * {@link #addToCustomContentPage}. This will only be invoked if
524 * {@link #hasCustomContentToLeft()} is {@code true}.
525 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500526 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600527 }
528
529 /**
530 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
531 * ensure the custom content page is added or removed if necessary.
532 */
533 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600534 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600535 // Not bound yet, wait for bindScreens to be called.
536 return;
537 }
538
539 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
540 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500541 mWorkspace.createCustomContentContainer();
542 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600543 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
544 mWorkspace.removeCustomContentPage();
545 }
546 }
547
Adam Cohenf9426d52012-06-04 17:26:21 -0700548 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700549 boolean searchVisible = false;
550 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800551 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700552 int coi = getCurrentOrientationIndexForGlobalIcons();
553 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
554 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700555 if (!DISABLE_MARKET_BUTTON) {
556 updateAppMarketIcon();
557 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700558 searchVisible = updateGlobalSearchIcon();
559 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700560 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700561 if (sGlobalSearchIcon[coi] != null) {
562 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700563 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700564 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700565 if (sVoiceSearchIcon[coi] != null) {
566 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700567 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700568 }
Winson Chungd64a6662013-09-30 11:06:59 -0700569 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700570 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800571 }
Winson Chungadf0c182012-08-23 14:59:07 -0700572 if (mSearchDropTargetBar != null) {
573 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
574 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 }
Romain Guycbb89e42009-06-08 15:52:54 -0700576
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700578 if (sLocaleConfiguration == null) {
579 new AsyncTask<Void, Void, LocaleConfiguration>() {
580 @Override
581 protected LocaleConfiguration doInBackground(Void... unused) {
582 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
583 readConfiguration(Launcher.this, localeConfiguration);
584 return localeConfiguration;
585 }
586
587 @Override
588 protected void onPostExecute(LocaleConfiguration result) {
589 sLocaleConfiguration = result;
590 checkForLocaleChange(); // recursive, but now with a locale configuration
591 }
592 }.execute();
593 return;
594 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700595
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 final Configuration configuration = getResources().getConfiguration();
597
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700598 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 final String locale = configuration.locale.toString();
600
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700601 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 final int mcc = configuration.mcc;
603
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 final int mnc = configuration.mnc;
606
Romain Guy84f296c2009-11-04 15:00:44 -0800607 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608
Romain Guy84f296c2009-11-04 15:00:44 -0800609 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700610 sLocaleConfiguration.locale = locale;
611 sLocaleConfiguration.mcc = mcc;
612 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700613
Joe Onorato0589f0f2010-02-08 13:44:00 -0800614 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700615
616 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100617 new AsyncTask<Void, Void, Void>() {
618 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700619 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100620 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700621 }
Michael Jurka43467462013-11-14 12:03:58 +0100622 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700623 }
624 }
625
626 private static class LocaleConfiguration {
627 public String locale;
628 public int mcc = -1;
629 public int mnc = -1;
630 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700631
Romain Guy98d01652009-06-30 16:21:04 -0700632 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
633 DataInputStream in = null;
634 try {
635 in = new DataInputStream(context.openFileInput(PREFERENCES));
636 configuration.locale = in.readUTF();
637 configuration.mcc = in.readInt();
638 configuration.mnc = in.readInt();
639 } catch (FileNotFoundException e) {
640 // Ignore
641 } catch (IOException e) {
642 // Ignore
643 } finally {
644 if (in != null) {
645 try {
646 in.close();
647 } catch (IOException e) {
648 // Ignore
649 }
650 }
651 }
652 }
653
654 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
655 DataOutputStream out = null;
656 try {
657 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
658 out.writeUTF(configuration.locale);
659 out.writeInt(configuration.mcc);
660 out.writeInt(configuration.mnc);
661 out.flush();
662 } catch (FileNotFoundException e) {
663 // Ignore
664 } catch (IOException e) {
665 //noinspection ResultOfMethodCallIgnored
666 context.getFileStreamPath(PREFERENCES).delete();
667 } finally {
668 if (out != null) {
669 try {
670 out.close();
671 } catch (IOException e) {
672 // Ignore
673 }
674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
676 }
677
Anton Hanssona2f665f2013-09-26 12:18:32 +0100678 public Stats getStats() {
679 return mStats;
680 }
681
Bjorn Bringertc459e522013-06-07 19:36:01 +0100682 public LayoutInflater getInflater() {
683 return mInflater;
684 }
685
Winson Chung36a62fe2012-05-06 18:04:42 -0700686 boolean isDraggingEnabled() {
687 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
688 // that is subsequently removed from the workspace in startBinding().
689 return !mModel.isLoadingWorkspace();
690 }
691
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 static int getScreen() {
693 synchronized (sLock) {
694 return sScreen;
695 }
696 }
697
698 static void setScreen(int screen) {
699 synchronized (sLock) {
700 sScreen = screen;
701 }
702 }
703
Winson Chung557d6ed2011-07-08 15:34:52 -0700704 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000705 * Copied from View -- the View version of the method isn't called
706 * anywhere else in our process and only exists for API level 17+,
707 * so it's ok to keep our own version with no API requirement.
708 */
709 public static int generateViewId() {
710 for (;;) {
711 final int result = sNextGeneratedId.get();
712 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
713 int newValue = result + 1;
714 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
715 if (sNextGeneratedId.compareAndSet(result, newValue)) {
716 return result;
717 }
718 }
719 }
720
721 public int getViewIdForItem(ItemInfo info) {
722 // This cast is safe given the > 2B range for int.
723 int itemId = (int) info.id;
724 if (mItemIdToViewId.containsKey(itemId)) {
725 return mItemIdToViewId.get(itemId);
726 }
727 int viewId = generateViewId();
728 mItemIdToViewId.put(itemId, viewId);
729 return viewId;
730 }
731
732 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700733 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
734 * a configuration step, this allows the proper animations to run after other transitions.
735 */
Adam Cohendb364c32014-05-20 14:23:40 -0700736 private long completeAdd(PendingAddArguments args) {
737
738 long screenId = ensurePendingDropLayoutExists(args.screenId);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800740 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700741 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700742 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700743 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800744 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700745 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700746 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800747 }
Michael Jurka27614d22012-04-02 06:40:22 -0700748 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
749 // if you turned the screen off and then back while in All Apps, Launcher would not
750 // return to the workspace. Clearing mAddInfo.container here fixes this issue
751 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700752 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800753 }
754
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700756 protected void onActivityResult(
757 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700758 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700759 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700760 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800761 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700762
Adam Cohenad4e15c2013-10-17 16:21:35 -0700763 Runnable exitSpringLoaded = new Runnable() {
764 @Override
765 public void run() {
766 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
767 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
768 }
769 };
770
Michael Jurka8b805b12012-04-18 14:23:14 -0700771 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700772 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700773 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
774 if (resultCode == RESULT_CANCELED) {
775 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700776 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700777 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700778 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700779 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
780 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700781 }
782 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200783 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
784 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
785 mWorkspace.exitOverviewMode(false);
786 }
787 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700788 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200789
Adam Cohened66b2b2012-01-23 17:28:51 -0800790 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
791 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700792
Adam Cohendb364c32014-05-20 14:23:40 -0700793 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800794 // We have special handling for widgets
795 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800796 final int appWidgetId;
797 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
798 : -1;
799 if (widgetId < 0) {
800 appWidgetId = pendingAddWidgetId;
801 } else {
802 appWidgetId = widgetId;
803 }
804
Adam Cohenad4e15c2013-10-17 16:21:35 -0700805 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800806 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700807 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
808 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700809 result = RESULT_CANCELED;
810 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700811 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700812 @Override
813 public void run() {
814 exitSpringLoadedDragModeDelayed(false, 0, null);
815 }
816 };
Adam Cohendb364c32014-05-20 14:23:40 -0700817 if (workspaceLocked) {
818 // No need to remove the empty screen if we're mid-binding, as the
819 // the bind will not add the empty screen.
820 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
821 } else {
822 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
823 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
824 }
Winson Chung5aaab772012-04-27 15:24:02 -0700825 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700826 if (!workspaceLocked) {
827 mPendingAddInfo.screenId = ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
828 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
829
830 dropLayout.setDropPending(true);
831 final Runnable onComplete = new Runnable() {
832 @Override
833 public void run() {
834 completeTwoStageWidgetDrop(resultCode, appWidgetId);
835 dropLayout.setDropPending(false);
836 }
837 };
838 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
839 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
840 } else {
841 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
842 mPendingAddInfo);
843 sPendingAddItem = args;
844 }
Winson Chung5aaab772012-04-27 15:24:02 -0700845 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800846 return;
847 }
848
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 // The pattern used here is that a user PICKs a specific application,
850 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700851
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
853 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700854 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700855 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
856 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800857 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700858 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800859 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700860 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700861 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
862 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 }
Adam Cohen00074722013-10-11 17:46:09 -0700864 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700865 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700866 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800868 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 }
870
Adam Cohendb364c32014-05-20 14:23:40 -0700871 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
872 appWidgetId, ItemInfo info) {
873 PendingAddArguments args = new PendingAddArguments();
874 args.requestCode = requestCode;
875 args.intent = data;
876 args.container = info.container;
877 args.screenId = info.screenId;
878 args.cellX = info.cellX;
879 args.cellY = info.cellY;
880 args.appWidgetId = appWidgetId;
881 return args;
882 }
883
884 /**
885 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
886 *
887 * @param screenId the screen id to check
888 * @return the new screen, or screenId if it exists
889 */
890 private long ensurePendingDropLayoutExists(long screenId) {
891 CellLayout dropLayout =
892 (CellLayout) mWorkspace.getScreenWithId(screenId);
893 if (dropLayout == null) {
894 // it's possible that the add screen was removed because it was
895 // empty and a re-bind occurred
896 mWorkspace.addExtraEmptyScreen();
897 return mWorkspace.commitExtraEmptyScreen();
898 } else {
899 return screenId;
900 }
901 }
902
Adam Cohened66b2b2012-01-23 17:28:51 -0800903 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700904 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700905 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800906 Runnable onCompleteRunnable = null;
907 int animationType = 0;
908
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700909 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800910 if (resultCode == RESULT_OK) {
911 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
912 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700913 mPendingAddWidgetInfo);
914 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800915 onCompleteRunnable = new Runnable() {
916 @Override
917 public void run() {
918 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700919 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700920 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
921 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800922 }
923 };
924 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800925 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700928 if (mDragLayer.getAnimatedView() != null) {
929 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
930 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
931 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700932 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700933 // The animated view may be null in the case of a rotation during widget configuration
934 onCompleteRunnable.run();
935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
937
938 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700939 protected void onStop() {
940 super.onStop();
941 FirstFrameAnimatorHelper.setIsVisible(false);
942 }
943
944 @Override
945 protected void onStart() {
946 super.onStart();
947 FirstFrameAnimatorHelper.setIsVisible(true);
948 }
949
950 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200952 long startTime = 0;
953 if (DEBUG_RESUME_TIME) {
954 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400955 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200956 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700958
Winson Chung4a2afa32012-07-19 14:53:05 -0700959 // Restore the previous launcher state
960 if (mOnResumeState == State.WORKSPACE) {
961 showWorkspace(false);
962 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800963 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700964 }
965 mOnResumeState = State.NONE;
966
Craig Mautner360310b2012-10-26 15:13:08 -0700967 // Background was set to gradient in onPause(), restore to black if in all apps.
968 setWorkspaceBackground(mState == State.WORKSPACE);
969
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800970 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700971 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700972 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500973 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400974 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700975 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700977 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200978 // We might have postponed some bind calls until onResume (see waitUntilResume) --
979 // execute them here
980 long startTimeCallbacks = 0;
981 if (DEBUG_RESUME_TIME) {
982 startTimeCallbacks = System.currentTimeMillis();
983 }
984
985 if (mAppsCustomizeContent != null) {
986 mAppsCustomizeContent.setBulkBind(true);
987 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700988 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
989 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200990 }
991 if (mAppsCustomizeContent != null) {
992 mAppsCustomizeContent.setBulkBind(false);
993 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700994 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200995 if (DEBUG_RESUME_TIME) {
996 Log.d(TAG, "Time spent processing callbacks in onResume: " +
997 (System.currentTimeMillis() - startTimeCallbacks));
998 }
Michael Jurka447bf842013-05-15 14:52:15 +0200999 }
Michael Jurka54554252013-08-01 12:52:23 +02001000 if (mOnResumeCallbacks.size() > 0) {
1001 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1002 mOnResumeCallbacks.get(i).run();
1003 }
1004 mOnResumeCallbacks.clear();
1005 }
Winson Chunge4e50662012-01-23 14:45:13 -08001006
1007 // Reset the pressed state of icons that were locked in the press state while activities
1008 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001009 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001010 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001011 mWaitingForResume.setStayPressed(false);
1012 }
Winson Chunge4e50662012-01-23 14:45:13 -08001013 if (mAppsCustomizeContent != null) {
1014 // Resets the previous all apps icon press state
1015 mAppsCustomizeContent.resetDrawableState();
1016 }
Winson Chung82963d52013-10-09 11:20:57 -07001017
Adam Cohen06dff352012-06-01 17:17:08 -07001018 // It is possible that widgets can receive updates while launcher is not in the foreground.
1019 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1020 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1021 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001022 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001023
Winson Chung780fe592013-09-26 14:48:44 -07001024 // Process any items that were added while Launcher was away.
1025 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1026
Winson Chung5841efa2013-09-30 18:06:44 -07001027 // Update the voice search button proxy
1028 updateVoiceButtonProxyVisible(false);
1029
Adam Cohenf9426d52012-06-04 17:26:21 -07001030 // Again, as with the above scenario, it's possible that one or more of the global icons
1031 // were updated in the wrong orientation.
1032 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001033 if (DEBUG_RESUME_TIME) {
1034 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1035 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001036
1037 if (mWorkspace.getCustomContentCallbacks() != null) {
1038 // If we are resuming and the custom content is the current page, we call onShow().
1039 // It is also poassible that onShow will instead be called slightly after first layout
1040 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1041 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001042 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001043 }
1044 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001045 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001046 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001047 }
1048
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001050 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001051 // Ensure that items added to Launcher are queued until Launcher returns
1052 InstallShortcutReceiver.enableInstallQueue();
1053
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001054 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001055 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001056 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001057 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001058
1059 // We call onHide() aggressively. The custom content callbacks should be able to
1060 // debounce excess onHide calls.
1061 if (mWorkspace.getCustomContentCallbacks() != null) {
1062 mWorkspace.getCustomContentCallbacks().onHide();
1063 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001064 }
Romain Guycbb89e42009-06-08 15:52:54 -07001065
Adam Cohenbffe7452013-07-22 18:21:45 -07001066 QSBScroller mQsbScroller = new QSBScroller() {
1067 int scrollY = 0;
1068
1069 @Override
1070 public void setScrollY(int scroll) {
1071 scrollY = scroll;
1072
1073 if (mWorkspace.isOnOrMovingToCustomContent()) {
1074 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001075 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001076 }
1077 }
1078 };
1079
1080 public void resetQSBScroll() {
1081 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001082 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001083 }
1084
1085 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001086 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1087 // by a onResume or by scrolling otherwise.
1088 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001089
1090 // Custom content is completely hidden
1091 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001092
1093 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1094 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001095
1096 // Indicates whether the user is allowed to scroll away from the custom content.
1097 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001098 }
1099
Jorim Jaggid017f882014-01-14 17:08:48 -08001100 protected boolean hasSettings() {
1101 return false;
1102 }
1103
Adam Cohenbffe7452013-07-22 18:21:45 -07001104 public interface QSBScroller {
1105 public void setScrollY(int scrollY);
1106 }
1107
Winson Chung98ca0f72013-07-29 12:58:51 -07001108 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001109 CustomContentCallbacks callbacks, String description) {
1110 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001111 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001112 }
1113
Adam Cohenedb40762013-07-18 16:45:45 -07001114 // The custom content needs to offset its content to account for the QSB
1115 public int getTopOffsetForCustomContent() {
1116 return mWorkspace.getPaddingTop();
1117 }
1118
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001119 @Override
1120 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001121 // Flag the loader to stop early before switching
1122 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001123 if (mAppsCustomizeContent != null) {
1124 mAppsCustomizeContent.surrender();
1125 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001126 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001127 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001128
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001129 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001130 @Override
1131 public void onWindowFocusChanged(boolean hasFocus) {
1132 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001133 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001134 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 private boolean acceptFilter() {
1137 final InputMethodManager inputManager = (InputMethodManager)
1138 getSystemService(Context.INPUT_METHOD_SERVICE);
1139 return !inputManager.isFullscreenMode();
1140 }
1141
1142 @Override
1143 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001144 final int uniChar = event.getUnicodeChar();
1145 final boolean handled = super.onKeyDown(keyCode, event);
1146 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1147 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1149 keyCode, event);
1150 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001151 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001152 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001153 // showSearchDialog()
1154 // If there are multiple keystrokes before the search dialog takes focus,
1155 // onSearchRequested() will be called for every keystroke,
1156 // but it is idempotent, so it's fine.
1157 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 }
1159 }
1160
Joe Onorato8a9625e2010-01-28 15:55:35 -08001161 // Eat the long press event so the keyboard doesn't come up.
1162 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1163 return true;
1164 }
1165
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 return handled;
1167 }
1168
Karl Rosaen138a0412009-04-23 19:00:21 -07001169 private String getTypedText() {
1170 return mDefaultKeySsb.toString();
1171 }
1172
1173 private void clearTypedText() {
1174 mDefaultKeySsb.clear();
1175 mDefaultKeySsb.clearSpans();
1176 Selection.setSelection(mDefaultKeySsb, 0);
1177 }
1178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001180 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1181 * State
1182 */
1183 private static State intToState(int stateOrdinal) {
1184 State state = State.WORKSPACE;
1185 final State[] stateValues = State.values();
1186 for (int i = 0; i < stateValues.length; i++) {
1187 if (stateValues[i].ordinal() == stateOrdinal) {
1188 state = stateValues[i];
1189 break;
1190 }
1191 }
1192 return state;
1193 }
1194
1195 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 * Restores the previous state, if it exists.
1197 *
1198 * @param savedState The previous state.
1199 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001200 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 private void restoreState(Bundle savedState) {
1202 if (savedState == null) {
1203 return;
1204 }
1205
Michael Jurka883f55b2010-10-21 15:47:14 -07001206 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001207 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001208 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001209 }
1210
Adam Cohen21cd0022013-10-09 18:57:02 -07001211 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1212 PagedView.INVALID_RESTORE_PAGE);
1213 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001214 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 }
1216
Winson Chung3d503fb2011-07-13 17:25:49 -07001217 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001218 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001219
Winson Chung3d503fb2011-07-13 17:25:49 -07001220 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1221 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001222 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001223 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1224 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001225 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1226 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1227 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001228 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001229 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 mRestoring = true;
1231 }
1232
1233 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1234 if (renameFolder) {
1235 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001236 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 mRestoring = true;
1238 }
Winson Chunga12a2502010-12-20 14:41:35 -08001239
Winson Chung785d2eb2011-04-14 16:08:02 -07001240 // Restore the AppsCustomize tab
1241 if (mAppsCustomizeTabHost != null) {
1242 String curTab = savedState.getString("apps_customize_currentTab");
1243 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001244 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001245 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001246 mAppsCustomizeContent.loadAssociatedPages(
1247 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001248 }
1249
Winson Chung5afbf7b2011-07-25 11:53:08 -07001250 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1251 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001252 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001253 mItemIdToViewId = (HashMap<Integer, Integer>)
1254 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 }
1256
1257 /**
1258 * Finds all the views we need and configure them properly.
1259 */
1260 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001261 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001262
Craig Mautner360310b2012-10-26 15:13:08 -07001263 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001264 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1265 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001266 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001267 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001268
John Spurlock77e1f472013-09-11 10:09:51 -04001269 mLauncherView.setSystemUiVisibility(
1270 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001271 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272
Winson Chung4c98d922011-05-31 16:50:48 -07001273 // Setup the drag layer
1274 mDragLayer.setup(this, dragController);
1275
Winson Chung3d503fb2011-07-13 17:25:49 -07001276 // Setup the hotseat
1277 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1278 if (mHotseat != null) {
1279 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001280 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001281 }
1282
Jorim Jaggid017f882014-01-14 17:08:48 -08001283 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001284 View widgetButton = findViewById(R.id.widget_button);
1285 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001286 @Override
1287 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001288 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001289 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001290 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001291 }
1292 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001293 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1294
1295 View wallpaperButton = findViewById(R.id.wallpaper_button);
1296 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001297 @Override
1298 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001299 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001300 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001301 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001302 }
1303 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001304 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1305
1306 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001307 if (hasSettings()) {
1308 settingsButton.setOnClickListener(new OnClickListener() {
1309 @Override
1310 public void onClick(View arg0) {
1311 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001312 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001313 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001314 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001315 });
1316 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1317 } else {
1318 settingsButton.setVisibility(View.GONE);
1319 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1320 lp.gravity = Gravity.END | Gravity.TOP;
1321 widgetButton.requestLayout();
1322 }
1323
Adam Cohendbdff6b2013-09-18 19:09:15 -07001324 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001325
Winson Chung4c98d922011-05-31 16:50:48 -07001326 // Setup the workspace
1327 mWorkspace.setHapticFeedbackEnabled(false);
1328 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001329 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001330 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001331
Winson Chungf0ea4d32011-06-06 14:27:16 -07001332 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001333 mSearchDropTargetBar = (SearchDropTargetBar)
1334 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001335
Winson Chungf0ea4d32011-06-06 14:27:16 -07001336 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001337 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001338 mAppsCustomizeContent = (AppsCustomizePagedView)
1339 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1340 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001341
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001343 dragController.setDragScoller(mWorkspace);
1344 dragController.setScrollView(mDragLayer);
1345 dragController.setMoveTarget(mWorkspace);
1346 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001347 if (mSearchDropTargetBar != null) {
1348 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001349 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001350
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001351 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001352 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001353 mWeightWatcher = new WeightWatcher(this);
1354 mWeightWatcher.setAlpha(0.5f);
1355 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001356 new FrameLayout.LayoutParams(
1357 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001358 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001359 Gravity.BOTTOM)
1360 );
Adam Cohen39a06042013-07-19 14:30:12 -07001361
1362 boolean show = shouldShowWeightWatcher();
1363 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001364 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
1366
1367 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001368 * Sets the all apps button. This method is called from {@link Hotseat}.
1369 */
1370 public void setAllAppsButton(View allAppsButton) {
1371 mAllAppsButton = allAppsButton;
1372 }
1373
1374 public View getAllAppsButton() {
1375 return mAllAppsButton;
1376 }
1377
1378 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 * Creates a view representing a shortcut.
1380 *
1381 * @param info The data structure describing the shortcut.
1382 *
1383 * @return A View inflated from R.layout.application.
1384 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001385 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001387 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 }
1389
1390 /**
1391 * Creates a view representing a shortcut inflated from the specified resource.
1392 *
1393 * @param layoutResId The id of the XML layout used to create the shortcut.
1394 * @param parent The group the shortcut belongs to.
1395 * @param info The data structure describing the shortcut.
1396 *
1397 * @return A View inflated from layoutResId.
1398 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001399 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001400 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001401 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 return favorite;
1404 }
1405
1406 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 * Add a shortcut to the workspace.
1408 *
1409 * @param data The intent describing the shortcut.
1410 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001412 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001413 int cellY) {
1414 int[] cellXY = mTmpAddItemCellCoordinates;
1415 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001416 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001417
Michael Jurkad3ef3062010-11-23 16:23:58 -08001418 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001419
Adam Cohen558baaf2011-08-15 15:22:57 -07001420 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001421 if (info == null) {
1422 return;
1423 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001424 final View view = createShortcut(info);
1425
Adam Cohenfbba09b2011-07-18 21:43:05 -07001426 // First we check if we already know the exact location where we want to add this item.
1427 if (cellX >= 0 && cellY >= 0) {
1428 cellXY[0] = cellX;
1429 cellXY[1] = cellY;
1430 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001431
1432 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001433 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001434 true, null,null)) {
1435 return;
1436 }
1437 DragObject dragObject = new DragObject();
1438 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001439 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1440 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001441 return;
1442 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001443 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001444 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001445 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001446 foundCellSpan = (result != null);
1447 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001448 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001449 }
1450
1451 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001452 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001453 return;
1454 }
1455
Adam Cohendcd297f2013-06-18 13:13:40 -07001456 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457
1458 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001459 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001460 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 }
1462 }
1463
Adam Cohen2f093b62012-04-30 18:59:53 -07001464 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1465 int minHeight) {
1466 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001467 // We want to account for the extra amount of padding that we are adding to the widget
1468 // to ensure that it gets the full amount of space that it has requested
1469 int requiredWidth = minWidth + padding.left + padding.right;
1470 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001471 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001472 }
1473
Adam Cohen2f093b62012-04-30 18:59:53 -07001474 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1475 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001476 }
1477
Adam Cohen2f093b62012-04-30 18:59:53 -07001478 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1479 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001480 }
1481
Adam Cohen2f093b62012-04-30 18:59:53 -07001482 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1483 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001484 }
1485
Adam Cohen2f093b62012-04-30 18:59:53 -07001486 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1487 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001488 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001489 }
1490
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001492 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001494 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001495 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001497 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001498 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1499 if (appWidgetInfo == null) {
1500 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1501 }
Romain Guycbb89e42009-06-08 15:52:54 -07001502
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001503 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001504 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001505
Adam Cohen2f093b62012-04-30 18:59:53 -07001506 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1507 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001508
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001510 // We have saved the position to which the widget was dragged-- this really only matters
1511 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001512 int[] cellXY = mTmpAddItemCellCoordinates;
1513 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001514 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001515 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001516 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1517 cellXY[0] = mPendingAddInfo.cellX;
1518 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001519 spanXY[0] = mPendingAddInfo.spanX;
1520 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001521 foundCellSpan = true;
1522 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001523 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001524 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001525 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1526 spanXY[1], cellXY, finalSpan);
1527 spanXY[0] = finalSpan[0];
1528 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001529 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001530 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001531 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001532 }
1533
Michael Jurka0280c3b2010-09-17 15:00:07 -07001534 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001535 if (appWidgetId != -1) {
1536 // Deleting an app widget ID is a void call but writes to disk before returning
1537 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001538 new AsyncTask<Void, Void, Void>() {
1539 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001540 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001541 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001542 }
Michael Jurka43467462013-11-14 12:03:58 +01001543 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001544 }
Winson Chung93eef082012-03-23 15:59:27 -07001545 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001546 return;
1547 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001549 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001550 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1551 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001552 launcherInfo.spanX = spanXY[0];
1553 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001554 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1555 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559
1560 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001561 if (hostView == null) {
1562 // Perform actual inflation because we're live
1563 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1564 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1565 } else {
1566 // The AppWidgetHostView has already been inflated and instantiated
1567 launcherInfo.hostView = hostView;
1568 }
Romain Guycbb89e42009-06-08 15:52:54 -07001569
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001571 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001572 launcherInfo.notifyWidgetSizeChanged(this);
1573
Adam Cohendcd297f2013-06-18 13:13:40 -07001574 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001575 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001576
1577 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001579 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
Romain Guycbb89e42009-06-08 15:52:54 -07001581
Adam Cohended9f8d2010-11-03 13:25:16 -07001582 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1583 @Override
1584 public void onReceive(Context context, Intent intent) {
1585 final String action = intent.getAction();
1586 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1587 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001588 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001589 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001590
Winson Chungc100e8e2011-08-09 16:02:43 -07001591 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001592 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001593 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001594 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001595 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001596 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1597 mUserPresent = true;
1598 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001599 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1600 mModel.resetLoadedState(false, true);
1601 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1602 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1603 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1604 mModel.resetLoadedState(false, true);
1605 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1606 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1607 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001608 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1609 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1610 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001611 }
1612 }
1613 };
1614
1615 @Override
1616 public void onAttachedToWindow() {
1617 super.onAttachedToWindow();
1618
1619 // Listen for broadcasts related to user-presence
1620 final IntentFilter filter = new IntentFilter();
1621 filter.addAction(Intent.ACTION_SCREEN_OFF);
1622 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001623 // For handling managed profiles
1624 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1625 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001626 if (ENABLE_DEBUG_INTENTS) {
1627 filter.addAction(DebugIntents.DELETE_DATABASE);
1628 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1629 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001630 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001631 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001632 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001633 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001634 mVisible = true;
1635 }
1636
Adam Cohen0b395352014-06-09 22:54:36 +00001637 /**
1638 * Sets up transparent navigation and status bars in LMP.
1639 * This method is a no-op for other platform versions.
1640 */
1641 @TargetApi(19)
1642 private void setupTransparentSystemBarsForLmp() {
1643 // TODO(sansid): use the APIs directly when compiling against L sdk.
1644 // Currently we use reflection to access the flags and the API to set the transparency
1645 // on the System bars.
1646 if (Utilities.isLmp()) {
1647 try {
1648 getWindow().getAttributes().systemUiVisibility |=
1649 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1650 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1651 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1652 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1653 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1654 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1655 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1656 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1657
1658 Method setStatusBarColorMethod =
1659 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1660 Method setNavigationBarColorMethod =
1661 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1662 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1663 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1664 } catch (NoSuchFieldException e) {
1665 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1666 } catch (NoSuchMethodException ex) {
1667 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1668 } catch (IllegalAccessException e) {
1669 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1670 } catch (IllegalArgumentException e) {
1671 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1672 } catch (InvocationTargetException e) {
1673 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1674 } finally {}
1675 }
1676 }
1677
Adam Cohended9f8d2010-11-03 13:25:16 -07001678 @Override
1679 public void onDetachedFromWindow() {
1680 super.onDetachedFromWindow();
1681 mVisible = false;
1682
Adam Cohend113e0c2010-11-11 10:48:05 -08001683 if (mAttached) {
1684 unregisterReceiver(mReceiver);
1685 mAttached = false;
1686 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001687 updateRunning();
1688 }
1689
1690 public void onWindowVisibilityChanged(int visibility) {
1691 mVisible = visibility == View.VISIBLE;
1692 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001693 // The following code used to be in onResume, but it turns out onResume is called when
1694 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1695 // is a more appropriate event to handle
1696 if (mVisible) {
1697 mAppsCustomizeTabHost.onWindowVisible();
1698 if (!mWorkspaceLoading) {
1699 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001700 // We want to let Launcher draw itself at least once before we force it to build
1701 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001702 // apps is nice and speedy.
1703 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001704 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001705 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001706 if (mStarted) return;
1707 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001708 // We delay the layer building a bit in order to give
1709 // other message processing a time to run. In particular
1710 // this avoids a delay in hiding the IME if it was
1711 // currently shown, because doing that may involve
1712 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001713 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001714 final ViewTreeObserver.OnDrawListener listener = this;
1715 mWorkspace.post(new Runnable() {
1716 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001717 if (mWorkspace != null &&
1718 mWorkspace.getViewTreeObserver() != null) {
1719 mWorkspace.getViewTreeObserver().
1720 removeOnDrawListener(listener);
1721 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001722 }
1723 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001724 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001725 }
1726 });
1727 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001728 // When Launcher comes back to foreground, a different Activity might be responsible for
1729 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001730 if (!DISABLE_MARKET_BUTTON) {
1731 updateAppMarketIcon();
1732 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001733 clearTypedText();
1734 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001735 }
1736
1737 private void sendAdvanceMessage(long delay) {
1738 mHandler.removeMessages(ADVANCE_MSG);
1739 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1740 mHandler.sendMessageDelayed(msg, delay);
1741 mAutoAdvanceSentTime = System.currentTimeMillis();
1742 }
1743
1744 private void updateRunning() {
1745 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1746 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1747 mAutoAdvanceRunning = autoAdvanceRunning;
1748 if (autoAdvanceRunning) {
1749 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1750 sendAdvanceMessage(delay);
1751 } else {
1752 if (!mWidgetsToAdvance.isEmpty()) {
1753 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1754 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1755 }
1756 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001757 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 }
1759 }
1760 }
1761
1762 private final Handler mHandler = new Handler() {
1763 @Override
1764 public void handleMessage(Message msg) {
1765 if (msg.what == ADVANCE_MSG) {
1766 int i = 0;
1767 for (View key: mWidgetsToAdvance.keySet()) {
1768 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1769 final int delay = mAdvanceStagger * i;
1770 if (v instanceof Advanceable) {
1771 postDelayed(new Runnable() {
1772 public void run() {
1773 ((Advanceable) v).advance();
1774 }
1775 }, delay);
1776 }
1777 i++;
1778 }
1779 sendAdvanceMessage(mAdvanceInterval);
1780 }
1781 }
1782 };
1783
1784 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001785 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001786 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1787 if (v instanceof Advanceable) {
1788 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001789 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001790 updateRunning();
1791 }
1792 }
1793
1794 void removeWidgetToAutoAdvance(View hostView) {
1795 if (mWidgetsToAdvance.containsKey(hostView)) {
1796 mWidgetsToAdvance.remove(hostView);
1797 updateRunning();
1798 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001799 }
1800
Joe Onorato9c1289c2009-08-17 11:03:03 -04001801 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001802 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001803 launcherInfo.hostView = null;
1804 }
1805
Winson Chung93eef082012-03-23 15:59:27 -07001806 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1807 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1808 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001809 }
1810
Winson Chunga6945242014-01-08 14:04:34 -08001811 public DragLayer getDragLayer() {
1812 return mDragLayer;
1813 }
1814
1815 public Workspace getWorkspace() {
1816 return mWorkspace;
1817 }
1818
1819 public Hotseat getHotseat() {
1820 return mHotseat;
1821 }
1822
Jorim Jaggid017f882014-01-14 17:08:48 -08001823 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001824 return mOverviewPanel;
1825 }
1826
1827 public SearchDropTargetBar getSearchBar() {
1828 return mSearchDropTargetBar;
1829 }
1830
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001831 public LauncherAppWidgetHost getAppWidgetHost() {
1832 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 }
Romain Guycbb89e42009-06-08 15:52:54 -07001834
Winson Chunga9abd0e2010-10-27 17:18:37 -07001835 public LauncherModel getModel() {
1836 return mModel;
1837 }
1838
Winson Chunga6945242014-01-08 14:04:34 -08001839 public LauncherClings getLauncherClings() {
1840 return mLauncherClings;
1841 }
1842
1843 protected SharedPreferences getSharedPrefs() {
1844 return mSharedPrefs;
1845 }
1846
Bjorn Bringertc459e522013-06-07 19:36:01 +01001847 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001848 getWindow().closeAllPanels();
1849
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001850 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001851 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001852 }
1853
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 @Override
1855 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001856 long startTime = 0;
1857 if (DEBUG_RESUME_TIME) {
1858 startTime = System.currentTimeMillis();
1859 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 super.onNewIntent(intent);
1861
1862 // Close the menu
1863 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001864 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001865 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001866
Adam Cohened307df2013-10-02 09:37:31 -07001867 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1868 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1869 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001870
Adam Cohen6fecd412013-10-02 17:41:50 -07001871 if (mWorkspace == null) {
1872 // Can be cases where mWorkspace is null, this prevents a NPE
1873 return;
1874 }
1875 Folder openFolder = mWorkspace.getOpenFolder();
1876 // In all these cases, only animate if we're already on home
1877 mWorkspace.exitWidgetResizeMode();
1878 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001879 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001880 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001881 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001882
Adam Cohen6fecd412013-10-02 17:41:50 -07001883 closeFolder();
1884 exitSpringLoadedDragMode();
1885
1886 // If we are already on home, then just animate back to the workspace,
1887 // otherwise, just wait until onResume to set the state back to Workspace
1888 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001889 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001890 } else {
1891 mOnResumeState = State.WORKSPACE;
1892 }
1893
1894 final View v = getWindow().peekDecorView();
1895 if (v != null && v.getWindowToken() != null) {
1896 InputMethodManager imm = (InputMethodManager)getSystemService(
1897 INPUT_METHOD_SERVICE);
1898 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1899 }
1900
1901 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001902 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001903 mAppsCustomizeTabHost.reset();
1904 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001905
1906 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
Adam Cohened307df2013-10-02 09:37:31 -07001908
Michael Jurka447bf842013-05-15 14:52:15 +02001909 if (DEBUG_RESUME_TIME) {
1910 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
1913
Adam Coppa120b8e2013-11-12 16:26:04 +00001914 /**
1915 * Override point for subclasses to prevent movement to the default screen when the home
1916 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1917 */
1918 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1919 return true;
1920 }
1921
1922 /**
1923 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1924 */
1925 protected void onHomeIntent() {
1926 // Do nothing
1927 }
1928
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001930 public void onRestoreInstanceState(Bundle state) {
1931 super.onRestoreInstanceState(state);
1932 for (int page: mSynchronouslyBoundPages) {
1933 mWorkspace.restoreInstanceStateForChild(page);
1934 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 }
1936
1937 @Override
1938 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001939 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001940 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1941 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001942 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001943 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944
Michael Jurka883f55b2010-10-21 15:47:14 -07001945 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001946 // We close any open folder since it will not be re-opened, and we need to make sure
1947 // this state is reflected.
1948 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949
Adam Cohendcd297f2013-06-18 13:13:40 -07001950 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001951 mWaitingForResult) {
1952 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001953 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001954 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1955 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001956 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1957 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1958 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001959 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
1961
1962 if (mFolderInfo != null && mWaitingForResult) {
1963 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1964 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1965 }
Michael Jurka946ad472010-07-09 18:05:18 -07001966
Winson Chung785d2eb2011-04-14 16:08:02 -07001967 // Save the current AppsCustomize tab
1968 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001969 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1970 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001971 if (currentTabTag != null) {
1972 outState.putString("apps_customize_currentTab", currentTabTag);
1973 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001974 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1975 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001976 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001977 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 }
1979
1980 @Override
1981 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001983
Winson Chunge7a03942011-08-05 15:05:12 -07001984 // Remove all pending runnables
1985 mHandler.removeMessages(ADVANCE_MSG);
1986 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001987 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001988
Winson Chungcd2b0142011-06-08 16:02:26 -07001989 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001990 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001991 mModel.stopLoader();
1992 app.setLauncher(null);
1993
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001995 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001997 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001999 mAppWidgetHost = null;
2000
2001 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002
2003 TextKeyListener.getInstance().release();
2004
Winson Chung81b52252012-08-27 15:34:29 -07002005 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2006 // to prevent leaking Launcher activities on orientation change.
2007 if (mModel != null) {
2008 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2009 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002010
2011 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002012 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002013
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002014 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002015 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002016 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002017 mWorkspace = null;
2018 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002019
Michael Jurka2ecf9952012-06-18 12:52:28 -07002020 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 }
2022
Adam Cohena9cf38f2011-05-02 15:36:58 -07002023 public DragController getDragController() {
2024 return mDragController;
2025 }
2026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 @Override
2028 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002029 if (requestCode >= 0) {
2030 setWaitingForResult(true);
2031 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 super.startActivityForResult(intent, requestCode);
2033 }
2034
Winson Chung70d72102011-08-12 11:24:35 -07002035 /**
2036 * Indicates that we want global search for this activity by setting the globalSearch
2037 * argument for {@link #startSearch} to true.
2038 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002040 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002042
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002043 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002044
Karl Rosaen138a0412009-04-23 19:00:21 -07002045 if (initialQuery == null) {
2046 // Use any text typed in the launcher as the initial query
2047 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 if (appSearchData == null) {
2050 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002051 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 }
Winson Chungadf0c182012-08-23 14:59:07 -07002053 Rect sourceBounds = new Rect();
2054 if (mSearchDropTargetBar != null) {
2055 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2056 }
Romain Guycbb89e42009-06-08 15:52:54 -07002057
Bjorn Bringertc459e522013-06-07 19:36:01 +01002058 startSearch(initialQuery, selectInitialQuery,
2059 appSearchData, sourceBounds);
2060 }
2061
2062 public void startSearch(String initialQuery,
2063 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002064 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002065 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002066 }
2067
2068 /**
2069 * Starts the global search activity. This code is a copied from SearchManager
2070 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002071 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002072 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002073 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002074 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2075 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2076 if (globalSearchActivity == null) {
2077 Log.w(TAG, "No global search activity found.");
2078 return;
2079 }
2080 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2081 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2082 intent.setComponent(globalSearchActivity);
2083 // Make sure that we have a Bundle to put source in
2084 if (appSearchData == null) {
2085 appSearchData = new Bundle();
2086 } else {
2087 appSearchData = new Bundle(appSearchData);
2088 }
2089 // Set source to package name of app that starts global search, if not set already.
2090 if (!appSearchData.containsKey("source")) {
2091 appSearchData.putString("source", getPackageName());
2092 }
2093 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2094 if (!TextUtils.isEmpty(initialQuery)) {
2095 intent.putExtra(SearchManager.QUERY, initialQuery);
2096 }
2097 if (selectInitialQuery) {
2098 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2099 }
2100 intent.setSourceBounds(sourceBounds);
2101 try {
2102 startActivity(intent);
2103 } catch (ActivityNotFoundException ex) {
2104 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 }
2107
Yura4f93ec62014-02-04 14:15:21 +00002108 public boolean isOnCustomContent() {
2109 return mWorkspace.isOnOrMovingToCustomContent();
2110 }
2111
Winson Chung70d72102011-08-12 11:24:35 -07002112 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002113 public boolean onPrepareOptionsMenu(Menu menu) {
2114 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002115 if (!isOnCustomContent()) {
2116 // Close any open folders
2117 closeFolder();
2118 // Stop resizing any widgets
2119 mWorkspace.exitWidgetResizeMode();
2120 if (!mWorkspace.isInOverviewMode()) {
2121 // Show the overview mode
2122 showOverviewMode(true);
2123 } else {
2124 showWorkspace(true);
2125 }
Winson Chunge0298742014-01-17 12:03:00 -08002126 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002127 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002128 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002130 @Override
2131 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002132 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002133 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002134 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002135 }
2136
Joe Onorato9c1289c2009-08-17 11:03:03 -04002137 public boolean isWorkspaceLocked() {
2138 return mWorkspaceLoading || mWaitingForResult;
2139 }
2140
Adam Cohen517a7f52014-03-01 12:12:59 -08002141 public boolean isWorkspaceLoading() {
2142 return mWorkspaceLoading;
2143 }
2144
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002145 private void setWorkspaceLoading(boolean value) {
2146 boolean isLocked = isWorkspaceLocked();
2147 mWorkspaceLoading = value;
2148 if (isLocked != isWorkspaceLocked()) {
2149 onWorkspaceLockedChanged();
2150 }
2151 }
2152
2153 private void setWaitingForResult(boolean value) {
2154 boolean isLocked = isWorkspaceLocked();
2155 mWaitingForResult = value;
2156 if (isLocked != isWorkspaceLocked()) {
2157 onWorkspaceLockedChanged();
2158 }
2159 }
2160
2161 protected void onWorkspaceLockedChanged() { }
2162
Michael Jurka0280c3b2010-09-17 15:00:07 -07002163 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002164 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002165 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002166 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2167 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002168 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002169 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002170 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002171
Adam Cohenad4e15c2013-10-17 16:21:35 -07002172 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2173 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2174 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2175 }
2176
2177 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2178 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2179 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002180 if (appWidgetInfo.configure != null) {
2181 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002182 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002183
Bjorn Bringert7984c942009-12-09 15:38:25 +00002184 // Launch over to configure widget, if needed
2185 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002186 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002187 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002188 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002190 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002191 Runnable onComplete = new Runnable() {
2192 @Override
2193 public void run() {
2194 // Exit spring loaded mode if necessary after adding the widget
2195 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2196 null);
2197 }
2198 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002199 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002200 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002201 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 }
2203 }
Romain Guycbb89e42009-06-08 15:52:54 -07002204
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002205 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002206 // Close any folders that may be open.
2207 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002208 mWorkspace.moveToCustomContentScreen(animate);
2209 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002210 /**
2211 * Process a shortcut drop.
2212 *
2213 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002214 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002215 * @param cell The cell it should be added to, optional
2216 * @param position The location on the screen where it was dropped, optional
2217 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002218 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002219 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002220 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002221 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002222 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002223 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002224
2225 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002226 mPendingAddInfo.cellX = cell[0];
2227 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002228 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002229
2230 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2231 createShortcutIntent.setComponent(componentName);
2232 processShortcut(createShortcutIntent);
2233 }
2234
Adam Cohenfbba09b2011-07-18 21:43:05 -07002235 /**
2236 * Process a widget drop.
2237 *
2238 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002239 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002240 * @param cell The cell it should be added to, optional
2241 * @param position The location on the screen where it was dropped, optional
2242 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002244 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002245 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002246 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002247 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002248 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002249 mPendingAddInfo.minSpanX = info.minSpanX;
2250 mPendingAddInfo.minSpanY = info.minSpanY;
2251
Adam Cohenfbba09b2011-07-18 21:43:05 -07002252 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002253 mPendingAddInfo.cellX = cell[0];
2254 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002255 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002256 if (span != null) {
2257 mPendingAddInfo.spanX = span[0];
2258 mPendingAddInfo.spanY = span[1];
2259 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002260
Adam Cohened66b2b2012-01-23 17:28:51 -08002261 AppWidgetHostView hostView = info.boundWidget;
2262 int appWidgetId;
2263 if (hostView != null) {
2264 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002265 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002266 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002267 // In this case, we either need to start an activity to get permission to bind
2268 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002269 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002270 Bundle options = info.bindOptions;
2271
2272 boolean success = false;
2273 if (options != null) {
2274 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2275 info.componentName, options);
2276 } else {
2277 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2278 info.componentName);
2279 }
2280 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002281 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002282 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002283 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002284 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2285 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2286 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002287 // TODO: we need to make sure that this accounts for the options bundle.
2288 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002289 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2290 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002291 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002292 }
2293
Joe Onoratodeb98af2010-02-19 14:59:39 -08002294 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002295 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002296 }
2297
Winson Chung24ab2f12010-09-16 14:10:47 -07002298 void processWallpaper(Intent intent) {
2299 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2300 }
2301
Adam Cohendcd297f2013-06-18 13:13:40 -07002302 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002303 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002304 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002305 folderInfo.title = getText(R.string.folder_name);
2306
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002307 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002308 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002309 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002310 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311
2312 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002313 FolderIcon newFolder =
2314 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002315 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002316 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002317 // Force measure the new folder icon
2318 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2319 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002320 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 }
Romain Guycbb89e42009-06-08 15:52:54 -07002322
Joe Onorato9c1289c2009-08-17 11:03:03 -04002323 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002324 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002325 }
2326
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002327 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002328 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002329 }
2330
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002331 /**
2332 * Registers various content observers. The current implementation registers
2333 * only a favorites observer to keep track of the favorites applications.
2334 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002335 private void registerContentObservers() {
2336 ContentResolver resolver = getContentResolver();
2337 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2338 true, mWidgetObserver);
2339 }
2340
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002341 @Override
2342 public boolean dispatchKeyEvent(KeyEvent event) {
2343 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2344 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002345 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002346 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002347 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002348 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002349 dumpState();
2350 return true;
2351 }
2352 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002353 }
2354 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2355 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002356 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002357 return true;
2358 }
2359 }
2360
2361 return super.dispatchKeyEvent(event);
2362 }
2363
Joe Onorato88ec0992009-11-19 13:16:06 -08002364 @Override
2365 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002366 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002367 if (mAppsCustomizeContent.getContentType() ==
2368 AppsCustomizePagedView.ContentType.Applications) {
2369 showWorkspace(true);
2370 } else {
2371 showOverviewMode(true);
2372 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002373 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002374 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002375 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002376 Folder openFolder = mWorkspace.getOpenFolder();
2377 if (openFolder.isEditingName()) {
2378 openFolder.dismissEditingName();
2379 } else {
2380 closeFolder();
2381 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002382 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002383 mWorkspace.exitWidgetResizeMode();
2384
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002385 // Back button is a no-op here, but give at least some feedback for the button press
2386 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002387 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002388 }
2389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002391 * Re-listen when widgets are reset.
2392 */
2393 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002394 if (mAppWidgetHost != null) {
2395 mAppWidgetHost.startListening();
2396 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002397 }
2398
2399 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 * Launches the intent referred by the clicked shortcut.
2401 *
2402 * @param v The view representing the clicked shortcut.
2403 */
2404 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002405 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2406 // view has detached (it's possible for this to happen if the view is removed mid touch).
2407 if (v.getWindowToken() == null) {
2408 return;
2409 }
2410
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002411 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002412 return;
2413 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002414
Adam Cohen1697b792013-09-17 19:08:21 -07002415 if (v instanceof Workspace) {
2416 if (mWorkspace.isInOverviewMode()) {
2417 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002418 }
2419 return;
2420 }
2421
2422 if (v instanceof CellLayout) {
2423 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002424 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002425 }
2426 }
2427
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002429 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002430 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002432 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002433 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002434 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002435 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002436 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437 }
2438 }
2439
Michael Jurka0e260592010-06-30 17:07:39 -07002440 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002441 return false;
2442 }
2443
Michael Jurkaaf442092010-06-10 17:01:57 -07002444 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002445 * Event handler for the search button
2446 *
2447 * @param v The view that was clicked.
2448 */
2449 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002450 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2451
Amith Yamasania135ba82011-08-09 17:42:01 -07002452 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002453 }
2454
2455 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002456 * Event handler for the voice button
2457 *
2458 * @param v The view that was clicked.
2459 */
2460 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002461 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002462
Bjorn Bringertc459e522013-06-07 19:36:01 +01002463 startVoice();
2464 }
2465
2466 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002467 try {
2468 final SearchManager searchManager =
2469 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2470 ComponentName activityName = searchManager.getGlobalSearchActivity();
2471 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002472 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002473 if (activityName != null) {
2474 intent.setPackage(activityName.getPackageName());
2475 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002476 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002477 } catch (ActivityNotFoundException e) {
2478 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002479 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002480 startActivitySafely(null, intent, "onClickVoiceButton");
2481 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002482 }
2483
2484 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002485 * Event handler for the "grid" button that appears on the home screen, which
2486 * enters all apps mode.
2487 *
2488 * @param v The view that was clicked.
2489 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002490 protected void onClickAllAppsButton(View v) {
2491 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2492 if (isAllAppsVisible()) {
2493 showWorkspace(true);
2494 } else {
2495 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2496 }
2497 }
2498
2499 /**
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -07002500 * Event handler for a paged view icon click.
2501 * @param v The view that was clicked.
2502 * @param appInfo The {link AppInfo} of the view.
2503 */
2504 public void onClickPagedViewIcon(View v, AppInfo appInfo) {
2505 if (LOGD) Log.d(TAG, "onClickPagedViewIcon");
2506 startActivitySafely(v, appInfo.intent, appInfo);
2507 getStats().recordLaunch(appInfo.intent);
2508 }
2509
2510 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002511 * Event handler for an app shortcut click.
2512 *
2513 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2514 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002515 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002516 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2517 Object tag = v.getTag();
2518 if (!(tag instanceof ShortcutInfo)) {
2519 throw new IllegalArgumentException("Input must be a Shortcut");
2520 }
2521
2522 // Open shortcut
2523 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2524 final Intent intent = shortcut.intent;
2525
2526 // Check for special shortcuts
2527 if (intent.getComponent() != null) {
2528 final String shortcutClass = intent.getComponent().getClassName();
2529
2530 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2531 MemoryDumpActivity.startDump(this);
2532 return;
2533 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2534 toggleShowWeightWatcher();
2535 return;
2536 }
2537 }
2538
Chris Wren40c5ed32014-06-24 18:24:23 -04002539 // Check for abandoned promise
2540 if (shortcut.isAbandoned() && v instanceof BubbleTextView) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002541 AlertDialog.Builder builder = new AlertDialog.Builder(this);
2542 builder.setTitle(R.string.abandoned_promises_title);
Chris Wren803a4be2014-07-01 16:52:49 -04002543 builder.setMessage(R.string.abandoned_promise_explanation);
Chris Wren40c5ed32014-06-24 18:24:23 -04002544 builder.setPositiveButton(R.string.abandoned_search,
2545 new DialogInterface.OnClickListener() {
2546 public void onClick(DialogInterface dialog, int id) {
2547 startAppShortcutActivity(v);
2548 }
2549 }
2550 );
Chris Wren40c5ed32014-06-24 18:24:23 -04002551 builder.setNeutralButton(R.string.abandoned_clean_this,
2552 new DialogInterface.OnClickListener() {
2553 public void onClick(DialogInterface dialog, int id) {
2554 final BubbleTextView bubble = (BubbleTextView) v;
2555 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2556 mWorkspace.removeAbandonedPromise(bubble, user);
2557 }
2558 });
2559 builder.create().show();
2560 return;
2561 }
2562
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002563 // Start activities
Chris Wren40c5ed32014-06-24 18:24:23 -04002564 startAppShortcutActivity(v);
2565 }
2566
2567 private void startAppShortcutActivity(View v) {
2568 Object tag = v.getTag();
2569 if (!(tag instanceof ShortcutInfo)) {
2570 throw new IllegalArgumentException("Input must be a Shortcut");
2571 }
2572 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2573 final Intent intent = shortcut.intent;
2574
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002575 int[] pos = new int[2];
2576 v.getLocationOnScreen(pos);
2577 intent.setSourceBounds(new Rect(pos[0], pos[1],
2578 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2579
2580 boolean success = startActivitySafely(v, intent, tag);
2581
2582 mStats.recordLaunch(intent, shortcut);
2583
2584 if (success && v instanceof BubbleTextView) {
2585 mWaitingForResume = (BubbleTextView) v;
2586 mWaitingForResume.setStayPressed(true);
2587 }
2588 }
2589
2590 /**
2591 * Event handler for a folder icon click.
2592 *
2593 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2594 */
2595 protected void onClickFolderIcon(View v) {
2596 if (LOGD) Log.d(TAG, "onClickFolder");
2597 if (!(v instanceof FolderIcon)){
2598 throw new IllegalArgumentException("Input must be a FolderIcon");
2599 }
2600
2601 FolderIcon folderIcon = (FolderIcon) v;
2602 final FolderInfo info = folderIcon.getFolderInfo();
2603 Folder openFolder = mWorkspace.getFolderForTag(info);
2604
2605 // If the folder info reports that the associated folder is open, then verify that
2606 // it is actually opened. There have been a few instances where this gets out of sync.
2607 if (info.opened && openFolder == null) {
2608 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2609 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2610 info.opened = false;
2611 }
2612
2613 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2614 // Close any open folder
2615 closeFolder();
2616 // Open the requested folder
2617 openFolder(folderIcon);
2618 } else {
2619 // Find the open folder...
2620 int folderScreen;
2621 if (openFolder != null) {
2622 folderScreen = mWorkspace.getPageForView(openFolder);
2623 // .. and close it
2624 closeFolder(openFolder);
2625 if (folderScreen != mWorkspace.getCurrentPage()) {
2626 // Close any folder open on the current screen
2627 closeFolder();
2628 // Pull the folder onto this screen
2629 openFolder(folderIcon);
2630 }
2631 }
2632 }
Michael Jurka5130e402011-10-13 04:55:35 -07002633 }
2634
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002635 /**
2636 * Event handler for the (Add) Widgets button that appears after a long press
2637 * on the home screen.
2638 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002639 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002640 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002641 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2642 }
2643
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002644 /**
2645 * Event handler for the wallpaper picker button that appears after a long press
2646 * on the home screen.
2647 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002648 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002649 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2650 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2651 pickWallpaper.setComponent(getWallpaperPickerComponent());
2652 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2653 }
2654
2655 /**
2656 * Event handler for a click on the settings button that appears after a long press
2657 * on the home screen.
2658 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002659 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002660 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2661 }
2662
Michael Jurka5130e402011-10-13 04:55:35 -07002663 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002664 // Provide the same haptic feedback that the system offers for virtual keys.
2665 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002666 }
2667
Adam Cohen61f560d2013-09-30 15:58:20 -07002668 public void performHapticFeedbackOnTouchDown(View v) {
2669 // Provide the same haptic feedback that the system offers for virtual keys.
2670 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2671 }
2672
2673 public View.OnTouchListener getHapticFeedbackTouchListener() {
2674 if (mHapticFeedbackTouchListener == null) {
2675 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2676 @Override
2677 public boolean onTouch(View v, MotionEvent event) {
2678 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2679 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2680 }
2681 return false;
2682 }
2683 };
2684 }
2685 return mHapticFeedbackTouchListener;
2686 }
2687
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002688 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002689 if (!DISABLE_MARKET_BUTTON) {
2690 if (mAppMarketIntent != null) {
2691 startActivitySafely(v, mAppMarketIntent, "app market");
2692 } else {
2693 Log.e(TAG, "Invalid app market intent.");
2694 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002695 }
2696 }
2697
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002698 public void onDragStarted(View view) {}
2699
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002700 /**
2701 * Called when the user stops interacting with the launcher.
2702 * This implies that the user is now on the homescreen and is not doing housekeeping.
2703 */
2704 protected void onInteractionEnd() {}
2705
2706 /**
2707 * Called when the user starts interacting with the launcher.
2708 * The possible interactions are:
2709 * - open all apps
2710 * - reorder an app shortcut, or a widget
2711 * - open the overview mode.
2712 * This is a good time to stop doing things that only make sense
2713 * when the user is on the homescreen and not doing housekeeping.
2714 */
2715 protected void onInteractionBegin() {}
2716
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002717 void startApplicationDetailsActivity(ComponentName componentName) {
2718 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002719 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2720 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002721 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2722 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002723 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002724 }
2725
Michael Jurka1e2f4652013-07-08 18:03:46 -07002726 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002727 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2728 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002729 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002730 // System applications cannot be installed. For now, show a toast explaining that.
2731 // We may give them the option of disabling apps this way.
2732 int messageId = R.string.uninstall_system_app_text;
2733 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002734 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002735 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002736 String packageName = componentName.getPackageName();
2737 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002738 Intent intent = new Intent(
2739 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002740 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2741 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002742 if (user != null) {
2743 user.addToIntent(intent, Intent.EXTRA_USER);
2744 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002745 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002746 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002747 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002748 }
2749
Michael Jurka86a720e2012-05-09 11:23:23 -07002750 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002751 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002752 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002753 // Only launch using the new animation if the shortcut has not opted out (this is a
2754 // private contract between launcher and may be ignored in the future).
2755 boolean useLaunchAnimation = (v != null) &&
2756 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002757 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2758 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002759
Kenny Guy1317e2d2014-05-08 18:52:50 +01002760 UserHandleCompat user = null;
2761 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2762 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2763 user = userManager.getUserForSerialNumber(serialNumber);
2764 }
2765
2766 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002767 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002768 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2769 v.getMeasuredWidth(), v.getMeasuredHeight());
Kenny Guy1317e2d2014-05-08 18:52:50 +01002770 optsBundle = opts.toBundle();
2771 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002772 if (useLaunchAnimation && Utilities.isLmp()) {
2773 ActivityOptions opts = ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim);
2774 optsBundle = opts.toBundle();
2775 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002776
2777 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2778 // Could be launching some bookkeeping activity
2779 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002780 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002781 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002782 launcherApps.startActivityForProfile(intent.getComponent(), user,
2783 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002784 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002785 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002786 } catch (SecurityException e) {
2787 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002788 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002790 "or use the exported attribute for this activity. "
2791 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002792 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002793 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002794 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002795
Michael Jurka86a720e2012-05-09 11:23:23 -07002796 boolean startActivitySafely(View v, Intent intent, Object tag) {
2797 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002798 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2799 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2800 return false;
2801 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002802 try {
2803 success = startActivity(v, intent, tag);
2804 } catch (ActivityNotFoundException e) {
2805 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2806 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2807 }
2808 return success;
2809 }
2810
Adam Cohen268c4752012-06-06 17:47:33 -07002811 /**
2812 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2813 * in the DragLayer in the exact absolute location of the original FolderIcon.
2814 */
2815 private void copyFolderIconToImage(FolderIcon fi) {
2816 final int width = fi.getMeasuredWidth();
2817 final int height = fi.getMeasuredHeight();
2818
2819 // Lazy load ImageView, Bitmap and Canvas
2820 if (mFolderIconImageView == null) {
2821 mFolderIconImageView = new ImageView(this);
2822 }
2823 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2824 mFolderIconBitmap.getHeight() != height) {
2825 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2826 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2827 }
2828
2829 DragLayer.LayoutParams lp;
2830 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2831 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2832 } else {
2833 lp = new DragLayer.LayoutParams(width, height);
2834 }
2835
Adam Cohen307fe232012-08-16 17:55:58 -07002836 // The layout from which the folder is being opened may be scaled, adjust the starting
2837 // view size by this scale factor.
2838 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002839 lp.customPosition = true;
2840 lp.x = mRectForFolderAnimation.left;
2841 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002842 lp.width = (int) (scale * width);
2843 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002844
2845 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2846 fi.draw(mFolderIconCanvas);
2847 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002848 if (fi.getFolder() != null) {
2849 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2850 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002851 }
Adam Cohen268c4752012-06-06 17:47:33 -07002852 // Just in case this image view is still in the drag layer from a previous animation,
2853 // we remove it and re-add it.
2854 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2855 mDragLayer.removeView(mFolderIconImageView);
2856 }
2857 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002858 if (fi.getFolder() != null) {
2859 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002860 }
Adam Cohen268c4752012-06-06 17:47:33 -07002861 }
2862
Adam Cohen2801caf2011-05-13 20:57:39 -07002863 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002864 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002865 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2866 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2867 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2868
Adam Cohenc51934b2011-07-26 21:07:43 -07002869 FolderInfo info = (FolderInfo) fi.getTag();
2870 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2871 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002872 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2873 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002874 }
2875
Adam Cohen268c4752012-06-06 17:47:33 -07002876 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2877 copyFolderIconToImage(fi);
2878 fi.setVisibility(View.INVISIBLE);
2879
Michael Jurka2ecf9952012-06-18 12:52:28 -07002880 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002881 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002882 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2883 oa.start();
2884 }
2885
Adam Cohen268c4752012-06-06 17:47:33 -07002886 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002887 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002888 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2889 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2890 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2891
Adam Cohen268c4752012-06-06 17:47:33 -07002892 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002893
Adam Cohen268c4752012-06-06 17:47:33 -07002894 // We remove and re-draw the FolderIcon in-case it has changed
2895 mDragLayer.removeView(mFolderIconImageView);
2896 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002897 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002898 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002899 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002900 oa.addListener(new AnimatorListenerAdapter() {
2901 @Override
2902 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002903 if (cl != null) {
2904 cl.clearFolderLeaveBehind();
2905 // Remove the ImageView copy of the FolderIcon and make the original visible.
2906 mDragLayer.removeView(mFolderIconImageView);
2907 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002908 }
2909 }
2910 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002911 oa.start();
2912 }
2913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002914 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002915 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002916 * is animated relative to the specified View. If the View is null, no animation
2917 * is played.
2918 *
2919 * @param folderInfo The FolderInfo describing the folder to open.
2920 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002921 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002922 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002923 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002924
Adam Cohena9cf38f2011-05-02 15:36:58 -07002925 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002926
Adam Cohen4554ee12011-08-03 16:13:21 -07002927 // Just verify that the folder hasn't already been added to the DragLayer.
2928 // There was a one-off crash where the folder had a parent already.
2929 if (folder.getParent() == null) {
2930 mDragLayer.addView(folder);
2931 mDragController.addDropTarget((DropTarget) folder);
2932 } else {
2933 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2934 folder.getParent() + ").");
2935 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002936 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002937 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002938
2939 // Notify the accessibility manager that this folder "window" has appeared and occluded
2940 // the workspace items
2941 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2942 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002943 }
2944
2945 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002946 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002947 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002948 if (folder.isEditingName()) {
2949 folder.dismissEditingName();
2950 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002951 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002952
2953 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002954 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002955 }
2956 }
2957
2958 void closeFolder(Folder folder) {
2959 folder.getInfo().opened = false;
2960
2961 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2962 if (parent != null) {
2963 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2964 shrinkAndFadeInFolderIcon(fi);
2965 }
2966 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002967
2968 // Notify the accessibility manager that this folder "window" has disappeard and no
2969 // longer occludeds the workspace items
2970 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 }
2972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002974 if (!isDraggingEnabled()) return false;
2975 if (isWorkspaceLocked()) return false;
2976 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977
Adam Cohen1697b792013-09-17 19:08:21 -07002978 if (v instanceof Workspace) {
2979 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002980 if (mWorkspace.enterOverviewMode()) {
2981 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2982 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2983 return true;
2984 } else {
2985 return false;
2986 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002987 } else {
2988 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002989 }
Adam Cohen1697b792013-09-17 19:08:21 -07002990 }
2991
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002992 CellLayout.CellInfo longClickCellInfo = null;
2993 View itemUnderLongClick = null;
2994 if (v.getTag() instanceof ItemInfo) {
2995 ItemInfo info = (ItemInfo) v.getTag();
2996 longClickCellInfo = new CellLayout.CellInfo(v, info);;
2997 itemUnderLongClick = longClickCellInfo.cell;
2998 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002999 }
3000
Winson Chung3d503fb2011-07-13 17:25:49 -07003001 // The hotseat touch handling does not go through Workspace, and we always allow long press
3002 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003003 final boolean inHotseat = isHotseatLayout(v);
3004 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003005 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003006 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003007 // User long pressed on empty space
3008 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3009 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003010 if (mWorkspace.isInOverviewMode()) {
3011 mWorkspace.startReordering(v);
3012 } else {
3013 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003014 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003016 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3017 mHotseat.getOrderInHotseat(
3018 longClickCellInfo.cellX,
3019 longClickCellInfo.cellY));
3020 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003021 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003022 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003023 }
3024 }
3025 }
3026 return true;
3027 }
3028
Winson Chung3d503fb2011-07-13 17:25:49 -07003029 boolean isHotseatLayout(View layout) {
3030 return mHotseat != null && layout != null &&
3031 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3032 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003033
Winson Chung3d503fb2011-07-13 17:25:49 -07003034 /**
3035 * Returns the CellLayout of the specified container at the specified screen.
3036 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003037 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003038 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3039 if (mHotseat != null) {
3040 return mHotseat.getLayout();
3041 } else {
3042 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003043 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003044 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003045 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003046 }
3047 }
3048
Daniel Sandler843e8602010-06-07 14:59:01 -04003049 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003050 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003051 }
3052
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003053 /**
3054 * Helper method for the cameraZoomIn/cameraZoomOut animations
3055 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003056 * @param scaleFactor The scale factor used for the zoom
3057 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07003058 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003059 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07003060 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003061 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003062
Craig Mautner360310b2012-10-26 15:13:08 -07003063 private void setWorkspaceBackground(boolean workspace) {
3064 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003065 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003066 }
3067
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003068 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003069 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3070 int curflags = getWindow().getAttributes().flags
3071 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3072 if (wpflags != curflags) {
3073 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3074 }
Craig Mautner360310b2012-10-26 15:13:08 -07003075 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003076 }
3077
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003078 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3079 if (v instanceof LauncherTransitionable) {
3080 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3081 }
3082 }
3083
Michael Jurkabed61d22012-02-14 22:51:29 -08003084 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3085 if (v instanceof LauncherTransitionable) {
3086 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3087 }
Winson Chung70442722012-02-10 15:43:22 -08003088
3089 // Update the workspace transition step as well
3090 dispatchOnLauncherTransitionStep(v, 0f);
3091 }
3092
3093 private void dispatchOnLauncherTransitionStep(View v, float t) {
3094 if (v instanceof LauncherTransitionable) {
3095 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3096 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003097 }
3098
3099 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3100 if (v instanceof LauncherTransitionable) {
3101 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3102 }
Winson Chung70442722012-02-10 15:43:22 -08003103
3104 // Update the workspace transition step as well
3105 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003106 }
3107
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003108 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003109 * Things to test when changing the following seven functions.
3110 * - Home from workspace
3111 * - from center screen
3112 * - from other screens
3113 * - Home from all apps
3114 * - from center screen
3115 * - from other screens
3116 * - Back from all apps
3117 * - from center screen
3118 * - from other screens
3119 * - Launch app from workspace and quit
3120 * - with back
3121 * - with home
3122 * - Launch app from all apps and quit
3123 * - with back
3124 * - with home
3125 * - Go to a screen that's not the default, then all
3126 * apps, and launch and app, and go back
3127 * - with back
3128 * -with home
3129 * - On workspace, long press power and go back
3130 * - with back
3131 * - with home
3132 * - On all apps, long press power and go back
3133 * - with back
3134 * - with home
3135 * - On workspace, power off
3136 * - On all apps, power off
3137 * - Launch an app and turn off the screen while in that app
3138 * - Go back with home key
3139 * - Go back with back key TODO: make this not go to workspace
3140 * - From all apps
3141 * - From workspace
3142 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3143 * - From all apps
3144 * - From the center workspace
3145 * - From another workspace
3146 */
3147
3148 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003149 * Zoom the camera out from the workspace to reveal 'toView'.
3150 * Assumes that the view to show is anchored at either the very top or very bottom
3151 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003152 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003153 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003154 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3155 showAppsCustomizeHelper(animated, springLoaded, contentType);
3156 }
3157 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3158 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003159 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003160 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003161 mStateAnimation.cancel();
3162 mStateAnimation = null;
3163 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003164 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003165
Winson Chungf0ea4d32011-06-06 14:27:16 -07003166 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3167 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
3168 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003169 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003170 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003171 final int startDelay =
3172 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003173
Michael Jurkab3e22d92011-10-31 15:58:33 -07003174 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003175
Michael Jurkad74c9842011-07-10 12:44:21 -07003176 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003177 Animator workspaceAnim =
3178 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003179 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003180 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3181 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003182 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3183 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003184
3185 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003186 toView.setScaleX(scale);
3187 toView.setScaleY(scale);
3188 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3189 scaleAnim.
3190 scaleX(1f).scaleY(1f).
3191 setDuration(duration).
3192 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003193
Winson Chungf0ea4d32011-06-06 14:27:16 -07003194 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003195 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003196 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003197 .ofFloat(toView, "alpha", 0f, 1f)
3198 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003199 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003200 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3201 @Override
3202 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003203 if (animation == null) {
3204 throw new RuntimeException("animation is null");
3205 }
Winson Chung70442722012-02-10 15:43:22 -08003206 float t = (Float) animation.getAnimatedValue();
3207 dispatchOnLauncherTransitionStep(fromView, t);
3208 dispatchOnLauncherTransitionStep(toView, t);
3209 }
3210 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003211
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003212 // toView should appear right at the end of the workspace shrink
3213 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003214 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003215 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003216 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003217
3218 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003219 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003220 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003221 // Prepare the position
3222 toView.setTranslationX(0.0f);
3223 toView.setTranslationY(0.0f);
3224 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003225 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003226 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003227 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003228 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003229 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3230 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003231
Winson Chungc7d2b602012-05-16 17:02:20 -07003232 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003233 if (mSearchDropTargetBar != null) {
3234 mSearchDropTargetBar.hideSearchBar(false);
3235 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003236 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003237 });
3238
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003239 if (workspaceAnim != null) {
3240 mStateAnimation.play(workspaceAnim);
3241 }
Michael Jurka1899a362011-11-03 13:50:45 -07003242
3243 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003244
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003245 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3246 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003247
3248 // If any of the objects being animated haven't been measured/laid out
3249 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003250 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003251 (mWorkspace.getMeasuredWidth() == 0) ||
3252 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003253 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003254 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003255
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003256 final AnimatorSet stateAnimation = mStateAnimation;
3257 final Runnable startAnimRunnable = new Runnable() {
3258 public void run() {
3259 // Check that mStateAnimation hasn't changed while
3260 // we waited for a layout/draw pass
3261 if (mStateAnimation != stateAnimation)
3262 return;
3263 setPivotsForZoom(toView, scale);
3264 dispatchOnLauncherTransitionStart(fromView, animated, false);
3265 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003266 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003267 }
3268 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003269 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003270 final ViewTreeObserver observer = toView.getViewTreeObserver();
3271 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3272 public void onGlobalLayout() {
3273 startAnimRunnable.run();
3274 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3275 }
3276 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003277 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003278 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003279 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003280 } else {
3281 toView.setTranslationX(0.0f);
3282 toView.setTranslationY(0.0f);
3283 toView.setScaleX(1.0f);
3284 toView.setScaleY(1.0f);
3285 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003286 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003287
Daniel Sandlere4f98912013-06-25 15:13:26 -04003288 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003289 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003290 if (mSearchDropTargetBar != null) {
3291 mSearchDropTargetBar.hideSearchBar(false);
3292 }
Michael Jurkaabded662011-03-04 12:06:57 -08003293 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003294 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003295 dispatchOnLauncherTransitionStart(fromView, animated, false);
3296 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003297 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003298 dispatchOnLauncherTransitionStart(toView, animated, false);
3299 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003300 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003301 }
3302
3303 /**
3304 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003305 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003306 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003307 */
Adam Cohened307df2013-10-02 09:37:31 -07003308 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003309 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003310
Michael Jurkab3e22d92011-10-31 15:58:33 -07003311 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003312 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003313 mStateAnimation.cancel();
3314 mStateAnimation = null;
3315 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003316 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003317
Winson Chungf0ea4d32011-06-06 14:27:16 -07003318 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003319 final int fadeOutDuration =
3320 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003321 final float scaleFactor = (float)
3322 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3323 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003324 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003325 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003326 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003327 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3328 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003329 toState, animated, stagger, -1);
3330 } else if (toState == Workspace.State.SPRING_LOADED ||
3331 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003332 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003333 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003334 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003335
Michael Jurkab3e22d92011-10-31 15:58:33 -07003336 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003337 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003338 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003339 final LauncherViewPropertyAnimator scaleAnim =
3340 new LauncherViewPropertyAnimator(fromView);
3341 scaleAnim.
3342 scaleX(scaleFactor).scaleY(scaleFactor).
3343 setDuration(duration).
3344 setInterpolator(new Workspace.ZoomInInterpolator());
3345
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003346 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003347 .ofFloat(fromView, "alpha", 1f, 0f)
3348 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003349 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003350 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3351 @Override
3352 public void onAnimationUpdate(ValueAnimator animation) {
3353 float t = 1f - (Float) animation.getAnimatedValue();
3354 dispatchOnLauncherTransitionStep(fromView, t);
3355 dispatchOnLauncherTransitionStep(toView, t);
3356 }
3357 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003358
Michael Jurka2ecf9952012-06-18 12:52:28 -07003359 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003360
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003361 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3362 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003363 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003364
3365 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003366 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003367 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003368 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003369 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3370 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003371 if (onCompleteRunnable != null) {
3372 onCompleteRunnable.run();
3373 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003374 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003375 }
3376 });
3377
Winson Chungf0ea4d32011-06-06 14:27:16 -07003378 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003379 if (workspaceAnim != null) {
3380 mStateAnimation.play(workspaceAnim);
3381 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003382 dispatchOnLauncherTransitionStart(fromView, animated, true);
3383 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003384 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003385 } else {
3386 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003387 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003388 dispatchOnLauncherTransitionStart(fromView, animated, true);
3389 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003390 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003391 dispatchOnLauncherTransitionStart(toView, animated, true);
3392 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003393 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003394 }
3395
Michael Jurkae326f182011-11-21 14:05:46 -08003396 @Override
3397 public void onTrimMemory(int level) {
3398 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003399 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003400 mAppsCustomizeTabHost.onTrimMemory();
3401 }
3402 }
3403
Adam Cohened307df2013-10-02 09:37:31 -07003404 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003405 showWorkspace(animated, null);
3406 }
3407
Adam Cohened307df2013-10-02 09:37:31 -07003408 protected void showWorkspace() {
3409 showWorkspace(true);
3410 }
3411
Adam Cohened66b2b2012-01-23 17:28:51 -08003412 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003413 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003414 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003415 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003416 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003417
Winson Chungc7d2b602012-05-16 17:02:20 -07003418 // Show the search bar (only animate if we were showing the drop target bar in spring
3419 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003420 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003421 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003422 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003423
Michael Jurkab3e22d92011-10-31 15:58:33 -07003424 // Set focus to the AppsCustomize button
3425 if (mAllAppsButton != null) {
3426 mAllAppsButton.requestFocus();
3427 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003428 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003429
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003430 // Change the state *after* we've called all the transition code
3431 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003432
Winson Chung5fb63472011-02-02 17:03:37 -08003433 // Resume the auto-advance of widgets
3434 mUserPresent = true;
3435 updateRunning();
3436
alanv1d4fde62012-10-17 13:15:47 -07003437 // Send an accessibility event to announce the context change
3438 getWindow().getDecorView()
3439 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003440
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003441 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003442 }
3443
Adam Cohened307df2013-10-02 09:37:31 -07003444 void showOverviewMode(boolean animated) {
3445 mWorkspace.setVisibility(View.VISIBLE);
3446 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3447 mState = State.WORKSPACE;
3448 onWorkspaceShown(animated);
3449 }
3450
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003451 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003452 }
3453
Winson Chung82963d52013-10-09 11:20:57 -07003454 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3455 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003456 if (mState != State.WORKSPACE) return;
3457
Winson Chung82963d52013-10-09 11:20:57 -07003458 if (resetPageToZero) {
3459 mAppsCustomizeTabHost.reset();
3460 }
Winson Chungc58497e2013-09-03 17:48:37 -07003461 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003462 mAppsCustomizeTabHost.post(new Runnable() {
3463 @Override
3464 public void run() {
3465 // We post this in-case the all apps view isn't yet constructed.
3466 mAppsCustomizeTabHost.requestFocus();
3467 }
3468 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003469
Michael Jurkab3e22d92011-10-31 15:58:33 -07003470 // Change the state *after* we've called all the transition code
3471 mState = State.APPS_CUSTOMIZE;
3472
3473 // Pause the auto-advance of widgets until we are out of AllApps
3474 mUserPresent = false;
3475 updateRunning();
3476 closeFolder();
3477
3478 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003479 getWindow().getDecorView()
3480 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003481 }
3482
Adam Cohen7777d962011-08-18 18:58:38 -07003483 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003484 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003485 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003486 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003487 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003488 }
Adam Cohen7777d962011-08-18 18:58:38 -07003489
Adam Cohenad4e15c2013-10-17 16:21:35 -07003490 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003491 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003492 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3493
Winson Chunge7a03942011-08-05 15:05:12 -07003494 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003495 @Override
3496 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003497 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003498 // Before we show workspace, hide all apps again because
3499 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3500 // clean up our state transition functions
3501 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003502 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003503 } else {
3504 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003505 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003506 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003507 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003508 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003509
Michael Jurkad3ef3062010-11-23 16:23:58 -08003510 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003511 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003512 final boolean animated = true;
3513 final boolean springLoaded = true;
3514 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003515 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003516 }
3517 // Otherwise, we are not in spring loaded mode, so don't do anything.
3518 }
3519
Michael Jurkab3e22d92011-10-31 15:58:33 -07003520 void lockAllApps() {
3521 // TODO
3522 }
3523
3524 void unlockAllApps() {
3525 // TODO
3526 }
3527
Winson Chungf0ea4d32011-06-06 14:27:16 -07003528 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003529 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003530 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003531 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003532 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003533 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003534 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003535 int duration = 0;
3536 if (mSearchDropTargetBar != null) {
3537 duration = mSearchDropTargetBar.getTransitionInDuration();
3538 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003539 mHotseat.animate().alpha(1f).setDuration(duration);
3540 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003541 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003542 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003543 }
3544 }
3545 }
3546
3547 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003548 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003549 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003550 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003551 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003552 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003553 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003554 int duration = 0;
3555 if (mSearchDropTargetBar != null) {
3556 duration = mSearchDropTargetBar.getTransitionOutDuration();
3557 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003558 mHotseat.animate().alpha(0f).setDuration(duration);
3559 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003560 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003561 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003562 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003563 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003564 }
3565
Patrick Dubroy5f445422011-02-18 14:35:21 -08003566 /**
3567 * Add an item from all apps or customize onto the given workspace screen.
3568 * If layout is null, add to the current screen.
3569 */
3570 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003571 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003572 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003573 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003574 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003575
Winson Chungdff8ebb2011-09-08 17:25:31 -07003576 /** Maps the current orientation to an index for referencing orientation correct global icons */
3577 private int getCurrentOrientationIndexForGlobalIcons() {
3578 // default - 0, landscape - 1
3579 switch (getResources().getConfiguration().orientation) {
3580 case Configuration.ORIENTATION_LANDSCAPE:
3581 return 1;
3582 default:
3583 return 0;
3584 }
3585 }
3586
Michael Jurkaae65ee32012-04-30 11:45:54 -07003587 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003588 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003589 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003590 // Look for the toolbar icon specified in the activity meta-data
3591 Bundle metaData = packageManager.getActivityInfo(
3592 activityName, PackageManager.GET_META_DATA).metaData;
3593 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003594 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003595 if (iconResId != 0) {
3596 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003597 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003598 }
3599 }
3600 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003601 // This can happen if the activity defines an invalid drawable
3602 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3603 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003604 } catch (Resources.NotFoundException nfe) {
3605 // This can happen if the activity defines an invalid drawable
3606 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3607 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003608 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003609 return null;
3610 }
3611
3612 // if successful in getting icon, return it; otherwise, set button to use default drawable
3613 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003614 int buttonId, ComponentName activityName, int fallbackDrawableId,
3615 String toolbarResourceName) {
3616 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003617 Resources r = getResources();
3618 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3619 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003620
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003621 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003622 // If we were unable to find the icon via the meta-data, use a generic one
3623 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003624 toolbarIcon = r.getDrawable(fallbackDrawableId);
3625 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003626 if (button != null) {
3627 button.setCompoundDrawables(toolbarIcon, null, null, null);
3628 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003629 return null;
3630 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003631 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003632 if (button != null) {
3633 button.setCompoundDrawables(toolbarIcon, null, null, null);
3634 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003635 return toolbarIcon.getConstantState();
3636 }
3637 }
3638
3639 // if successful in getting icon, return it; otherwise, set button to use default drawable
3640 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003641 int buttonId, ComponentName activityName, int fallbackDrawableId,
3642 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003643 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003644 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003645
Michael Jurka19e0fc52011-07-22 18:00:21 -07003646 if (button != null) {
3647 // If we were unable to find the icon via the meta-data, use a
3648 // generic one
3649 if (toolbarIcon == null) {
3650 button.setImageResource(fallbackDrawableId);
3651 } else {
3652 button.setImageDrawable(toolbarIcon);
3653 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003654 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003655
3656 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3657
Michael Jurka0423dcf2010-10-05 14:56:18 -07003658 }
3659
Winson Chung1b884092012-06-08 17:13:02 -07003660 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003661 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003662 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003663 }
3664
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003665 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003666 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003667 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003668 }
3669
Winson Chungbb185bd2011-11-21 12:31:42 -08003670 private void invalidatePressedFocusedStates(View container, View button) {
3671 if (container instanceof HolographicLinearLayout) {
3672 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3673 layout.invalidatePressedFocusedStates();
3674 } else if (button instanceof HolographicImageView) {
3675 HolographicImageView view = (HolographicImageView) button;
3676 view.invalidatePressedFocusedStates();
3677 }
3678 }
3679
Cristina Stancu476493b2013-08-07 17:20:14 +01003680 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003681 if (mQsb == null) {
3682 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3683 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003684 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003685 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003686 }
3687
3688 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003689 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003690 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003691 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003692 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003693
Winson Chung1cad91e2011-05-25 17:41:01 -07003694 final SearchManager searchManager =
3695 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3696 ComponentName activityName = searchManager.getGlobalSearchActivity();
3697 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003698 int coi = getCurrentOrientationIndexForGlobalIcons();
3699 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003700 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3701 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3702 if (sGlobalSearchIcon[coi] == null) {
3703 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3704 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3705 TOOLBAR_ICON_METADATA_NAME);
3706 }
3707
Winson Chungc51db6a2011-10-05 11:44:49 -07003708 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3709 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003710 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003711 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003712 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003713 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003714 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3715 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003716 if (searchButton != null) searchButton.setVisibility(View.GONE);
3717 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003718 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003719 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003720 }
3721 }
3722
Cristina Stancu476493b2013-08-07 17:20:14 +01003723 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003724 final View searchButtonContainer = findViewById(R.id.search_button_container);
3725 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003726 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003727 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003728 }
3729
Cristina Stancu476493b2013-08-07 17:20:14 +01003730 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003731 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003732 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003733
Winson Chungc51db6a2011-10-05 11:44:49 -07003734 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003735 final SearchManager searchManager =
3736 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3737 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3738
3739 ComponentName activityName = null;
3740 if (globalSearchActivity != null) {
3741 // Check if the global search activity handles voice search
3742 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3743 intent.setPackage(globalSearchActivity.getPackageName());
3744 activityName = intent.resolveActivity(getPackageManager());
3745 }
3746
3747 if (activityName == null) {
3748 // Fallback: check if an activity other than the global search activity
3749 // resolves this
3750 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3751 activityName = intent.resolveActivity(getPackageManager());
3752 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003753 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003754 int coi = getCurrentOrientationIndexForGlobalIcons();
3755 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003756 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3757 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3758 if (sVoiceSearchIcon[coi] == null) {
3759 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3760 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3761 TOOLBAR_ICON_METADATA_NAME);
3762 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003763 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003764 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003765 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003766 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003767 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003768 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003769 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003770 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003771 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003772 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003773 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003774 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003775
Cristina Stancu476493b2013-08-07 17:20:14 +01003776 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003777 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3778 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003779 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003780 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003781 }
3782
Winson Chung5841efa2013-09-30 18:06:44 -07003783 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003784 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3785 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003786 boolean visible = !forceDisableVoiceButtonProxy &&
3787 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003788 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003789 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003790 }
3791 }
Winson Chung5841efa2013-09-30 18:06:44 -07003792
3793 /**
3794 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3795 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3796 */
3797 public void disableVoiceButtonProxy(boolean disabled) {
3798 updateVoiceButtonProxyVisible(disabled);
3799 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003800 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003801 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003802 */
3803 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003804 if (!DISABLE_MARKET_BUTTON) {
3805 final View marketButton = findViewById(R.id.market_button);
3806 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3807 // Find the app market activity by resolving an intent.
3808 // (If multiple app markets are installed, it will return the ResolverActivity.)
3809 ComponentName activityName = intent.resolveActivity(getPackageManager());
3810 if (activityName != null) {
3811 int coi = getCurrentOrientationIndexForGlobalIcons();
3812 mAppMarketIntent = intent;
3813 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3814 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3815 TOOLBAR_ICON_METADATA_NAME);
3816 marketButton.setVisibility(View.VISIBLE);
3817 } else {
3818 // We should hide and disable the view so that we don't try and restore the visibility
3819 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3820 marketButton.setVisibility(View.GONE);
3821 marketButton.setEnabled(false);
3822 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003823 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003824 }
3825
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003826 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003827 if (!DISABLE_MARKET_BUTTON) {
3828 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3829 Resources r = getResources();
3830 Drawable marketIconDrawable = d.newDrawable(r);
3831 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3832 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3833 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003834
Winson Chungd64a6662013-09-30 11:06:59 -07003835 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3836 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003837 }
3838
Michael Jurkad7c28052012-04-27 15:43:36 -07003839 @Override
3840 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003841 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003842 final List<CharSequence> text = event.getText();
3843 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003844 // Populate event with a fake title based on the current state.
3845 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003846 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003847 } else {
3848 text.add(getString(R.string.all_apps_home_button_label));
3849 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003850 return result;
3851 }
3852
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003853 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003854 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003855 */
3856 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3857 @Override
3858 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003859 closeSystemDialogs();
3860 }
3861 }
3862
3863 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003864 * Receives notifications whenever the appwidgets are reset.
3865 */
3866 private class AppWidgetResetObserver extends ContentObserver {
3867 public AppWidgetResetObserver() {
3868 super(new Handler());
3869 }
3870
3871 @Override
3872 public void onChange(boolean selfChange) {
3873 onAppWidgetReset();
3874 }
3875 }
3876
3877 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003878 * If the activity is currently paused, signal that we need to run the passed Runnable
3879 * in onResume.
3880 *
3881 * This needs to be called from incoming places where resources might have been loaded
3882 * while we are paused. That is becaues the Configuration might be wrong
3883 * when we're not running, and if it comes back to what it was when we
3884 * were paused, we are not restarted.
3885 *
3886 * Implementation of the method from LauncherModel.Callbacks.
3887 *
3888 * @return true if we are currently paused. The caller might be able to
3889 * skip some work in that case since we will come back again.
3890 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003891 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003892 if (mPaused) {
3893 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003894 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003895 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003896 }
3897 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003898 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003899 return true;
3900 } else {
3901 return false;
3902 }
3903 }
3904
Michael Jurkac402cd92013-05-20 15:49:32 +02003905 private boolean waitUntilResume(Runnable run) {
3906 return waitUntilResume(run, false);
3907 }
3908
Michael Jurka1e2f4652013-07-08 18:03:46 -07003909 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003910 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003911 }
3912
Michael Jurka7607c2f2013-04-03 14:33:19 -07003913 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003914 * If the activity is currently paused, signal that we need to re-run the loader
3915 * in onResume.
3916 *
3917 * This needs to be called from incoming places where resources might have been loaded
3918 * while we are paused. That is becaues the Configuration might be wrong
3919 * when we're not running, and if it comes back to what it was when we
3920 * were paused, we are not restarted.
3921 *
3922 * Implementation of the method from LauncherModel.Callbacks.
3923 *
3924 * @return true if we are currently paused. The caller might be able to
3925 * skip some work in that case since we will come back again.
3926 */
3927 public boolean setLoadOnResume() {
3928 if (mPaused) {
3929 Log.i(TAG, "setLoadOnResume");
3930 mOnResumeNeedsLoad = true;
3931 return true;
3932 } else {
3933 return false;
3934 }
3935 }
3936
3937 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003938 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003939 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003940 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003941 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003942 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003943 } else {
3944 return SCREEN_COUNT / 2;
3945 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003946 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003947
Joe Onorato9c1289c2009-08-17 11:03:03 -04003948 /**
3949 * Refreshes the shortcuts shown on the workspace.
3950 *
3951 * Implementation of the method from LauncherModel.Callbacks.
3952 */
3953 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003954 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003955
Michael Jurka7607c2f2013-04-03 14:33:19 -07003956 // If we're starting binding all over again, clear any bind calls we'd postponed in
3957 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3958 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003959 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003960
Winson Chungd64d1762013-08-20 14:37:16 -07003961 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003962 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003963 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003964
Michael Jurka05bf6442011-11-30 20:28:53 -08003965 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003966 if (mHotseat != null) {
3967 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003968 }
3969 }
3970
Adam Cohendcd297f2013-06-18 13:13:40 -07003971 @Override
3972 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003973 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003974
Adam Cohen5084cba2013-09-03 12:01:16 -07003975 // If there are no screens, we need to have an empty screen
3976 if (orderedScreenIds.size() == 0) {
3977 mWorkspace.addExtraEmptyScreen();
3978 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003979
3980 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003981 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003982 if (hasCustomContentToLeft()) {
3983 mWorkspace.createCustomContentContainer();
3984 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003985 }
Winson Chung64359a52013-07-08 17:17:08 -07003986 }
3987
3988 @Override
3989 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003990 // Log to disk
3991 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3992 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3993 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003994 int count = orderedScreenIds.size();
3995 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003996 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003997 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003998 }
3999
Adam Cohen39a06042013-07-19 14:30:12 -07004000 private boolean shouldShowWeightWatcher() {
4001 String spKey = LauncherAppState.getSharedPreferencesKey();
4002 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004003 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004004
4005 return show;
4006 }
4007
4008 private void toggleShowWeightWatcher() {
4009 String spKey = LauncherAppState.getSharedPreferencesKey();
4010 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4011 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4012
4013 show = !show;
4014
4015 SharedPreferences.Editor editor = sp.edit();
4016 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4017 editor.commit();
4018
4019 if (mWeightWatcher != null) {
4020 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4021 }
4022 }
4023
Winson Chungd64d1762013-08-20 14:37:16 -07004024 public void bindAppsAdded(final ArrayList<Long> newScreens,
4025 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004026 final ArrayList<ItemInfo> addAnimated,
4027 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004028 Runnable r = new Runnable() {
4029 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004030 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004031 }
4032 };
4033 if (waitUntilResume(r)) {
4034 return;
4035 }
4036
Winson Chungd64d1762013-08-20 14:37:16 -07004037 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004038 if (newScreens != null) {
4039 bindAddScreens(newScreens);
4040 }
Winson Chungd64d1762013-08-20 14:37:16 -07004041
4042 // We add the items without animation on non-visible pages, and with
4043 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004044 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004045 bindItems(addNotAnimated, 0,
4046 addNotAnimated.size(), false);
4047 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004048 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004049 bindItems(addAnimated, 0,
4050 addAnimated.size(), true);
4051 }
Winson Chungc58497e2013-09-03 17:48:37 -07004052
Winson Chung87412982013-10-03 18:34:14 -07004053 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004054 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004055
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004056 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004057 addedApps != null && mAppsCustomizeContent != null) {
4058 mAppsCustomizeContent.addApps(addedApps);
4059 }
Winson Chungd64d1762013-08-20 14:37:16 -07004060 }
4061
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004062 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004063 * Bind the items start-end from the list.
4064 *
4065 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004066 */
Winson Chung64359a52013-07-08 17:17:08 -07004067 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4068 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004069 Runnable r = new Runnable() {
4070 public void run() {
4071 bindItems(shortcuts, start, end, forceAnimateIcons);
4072 }
4073 };
4074 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004075 return;
4076 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004077
Winson Chungf0c6ae02012-03-21 16:10:31 -07004078 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004079 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4080 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004081 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004082 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004083 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004084 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004085 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004086
4087 // Short circuit if we are loading dock items for a configuration which has no dock
4088 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4089 mHotseat == null) {
4090 continue;
4091 }
4092
Joe Onorato9c1289c2009-08-17 11:03:03 -04004093 switch (item.itemType) {
4094 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4095 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004096 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004097 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004098
Winson Chung64359a52013-07-08 17:17:08 -07004099 /*
4100 * TODO: FIX collision case
4101 */
Winson Chungce376632013-07-11 16:12:41 -07004102 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4103 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4104 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004105 View v = cl.getChildAt(item.cellX, item.cellY);
4106 Object tag = v.getTag();
4107 String desc = "Collision while binding workspace item: " + item
4108 + ". Collides with " + tag;
4109 if (LauncherAppState.isDogfoodBuild()) {
4110 throw (new RuntimeException(desc));
4111 } else {
4112 Log.d(TAG, desc);
4113 }
Winson Chungce376632013-07-11 16:12:41 -07004114 }
Winson Chung64359a52013-07-08 17:17:08 -07004115 }
4116
Adam Cohendcd297f2013-06-18 13:13:40 -07004117 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4118 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004119 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004120 // Animate all the applications up now
4121 shortcut.setAlpha(0f);
4122 shortcut.setScaleX(0f);
4123 shortcut.setScaleY(0f);
4124 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004125 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004126 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004127 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004128 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004129 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004130 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004131 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004132 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4133 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004134 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004135 default:
4136 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004138 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004139
Winson Chung997a9232013-07-24 15:33:46 -07004140 if (animateIcons) {
4141 // Animate to the correct page
4142 if (newShortcutsScreenId > -1) {
4143 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004144 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004145 final Runnable startBounceAnimRunnable = new Runnable() {
4146 public void run() {
4147 anim.playTogether(bounceAnims);
4148 anim.start();
4149 }
4150 };
Winson Chung997a9232013-07-24 15:33:46 -07004151 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004152 // We post the animation slightly delayed to prevent slowdowns
4153 // when we are loading right after we return to launcher.
4154 mWorkspace.postDelayed(new Runnable() {
4155 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004156 if (mWorkspace != null) {
4157 mWorkspace.snapToPage(newScreenIndex);
4158 mWorkspace.postDelayed(startBounceAnimRunnable,
4159 NEW_APPS_ANIMATION_DELAY);
4160 }
Winson Chung94d67682013-09-25 16:29:40 -07004161 }
4162 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004163 } else {
4164 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004165 }
4166 }
Winson Chung64359a52013-07-08 17:17:08 -07004167 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004168 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004169 }
4170
Joe Onorato9c1289c2009-08-17 11:03:03 -04004171 /**
4172 * Implementation of the method from LauncherModel.Callbacks.
4173 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004174 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004175 Runnable r = new Runnable() {
4176 public void run() {
4177 bindFolders(folders);
4178 }
4179 };
4180 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004181 return;
4182 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004183 sFolders.clear();
4184 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004185 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004186
4187 /**
4188 * Add the views for a widget to the workspace.
4189 *
4190 * Implementation of the method from LauncherModel.Callbacks.
4191 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004192 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004193 Runnable r = new Runnable() {
4194 public void run() {
4195 bindAppWidget(item);
4196 }
4197 };
4198 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004199 return;
4200 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004201
Daniel Sandler843e8602010-06-07 14:59:01 -04004202 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4203 if (DEBUG_WIDGETS) {
4204 Log.d(TAG, "bindAppWidget: " + item);
4205 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004206 final Workspace workspace = mWorkspace;
4207
4208 final int appWidgetId = item.appWidgetId;
4209 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004210 if (DEBUG_WIDGETS) {
4211 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4212 }
4213
Joe Onorato9c1289c2009-08-17 11:03:03 -04004214 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4215
Joe Onorato9c1289c2009-08-17 11:03:03 -04004216 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004217 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004218
Adam Cohendcd297f2013-06-18 13:13:40 -07004219 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004220 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004221 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4222
Joe Onorato9c1289c2009-08-17 11:03:03 -04004223 workspace.requestLayout();
4224
Daniel Sandler843e8602010-06-07 14:59:01 -04004225 if (DEBUG_WIDGETS) {
4226 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4227 + (SystemClock.uptimeMillis()-start) + "ms");
4228 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004229 }
4230
Adam Cohen1462de32012-07-24 22:34:36 -07004231 public void onPageBoundSynchronously(int page) {
4232 mSynchronouslyBoundPages.add(page);
4233 }
4234
Joe Onorato9c1289c2009-08-17 11:03:03 -04004235 /**
4236 * Callback saying that there aren't any more items to bind.
4237 *
4238 * Implementation of the method from LauncherModel.Callbacks.
4239 */
Adam Cohene25af792013-06-06 23:08:25 -07004240 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004241 Runnable r = new Runnable() {
4242 public void run() {
4243 finishBindingItems(upgradePath);
4244 }
4245 };
4246 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004247 return;
4248 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004249 if (mSavedState != null) {
4250 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004251 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004252 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004253 mSavedState = null;
4254 }
4255
Adam Cohen1462de32012-07-24 22:34:36 -07004256 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004257
Winson Chungc51db6a2011-10-05 11:44:49 -07004258 // Update the market app icon as necessary (the other icons will be managed in response to
4259 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004260 if (!DISABLE_MARKET_BUTTON) {
4261 updateAppMarketIcon();
4262 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004263
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004264 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004265
4266 // If we received the result of any pending adds while the loader was running (e.g. the
4267 // widget configuration forced an orientation change), process them now.
4268 if (sPendingAddItem != null) {
4269 final long screenId = completeAdd(sPendingAddItem);
4270
4271 // TODO: this moves the user to the page where the pending item was added. Ideally,
4272 // the screen would be guaranteed to exist after bind, and the page would be set through
4273 // the workspace restore process.
4274 mWorkspace.post(new Runnable() {
4275 @Override
4276 public void run() {
4277 mWorkspace.snapToScreenId(screenId);
4278 }
4279 });
4280 sPendingAddItem = null;
4281 }
4282
Adam Cohene25af792013-06-06 23:08:25 -07004283 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004284 mWorkspace.getUniqueComponents(true, null);
4285 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004286 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004287 }
4288
Winson Chunga0b7e862013-09-05 16:03:15 -07004289 public boolean isAllAppsButtonRank(int rank) {
4290 if (mHotseat != null) {
4291 return mHotseat.isAllAppsButtonRank(rank);
4292 }
4293 return false;
4294 }
4295
Winson Chunga2413752012-04-03 14:22:34 -07004296 private boolean canRunNewAppsAnimation() {
4297 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4298 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4299 }
4300
Winson Chungc9168342013-06-26 14:54:55 -07004301 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4302 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4303 PropertyValuesHolder.ofFloat("alpha", 1f),
4304 PropertyValuesHolder.ofFloat("scaleX", 1f),
4305 PropertyValuesHolder.ofFloat("scaleY", 1f));
4306 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4307 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4308 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4309 return bounceAnim;
4310 }
4311
Adam Cohen27772732013-11-11 14:00:56 +00004312 public boolean useVerticalBarLayout() {
4313 return LauncherAppState.getInstance().getDynamicGrid().
4314 getDeviceProfile().isVerticalBarLayout();
4315 }
4316
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004317 protected Rect getSearchBarBounds() {
4318 return LauncherAppState.getInstance().getDynamicGrid().
4319 getDeviceProfile().getSearchBarBounds();
4320 }
4321
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004322 @Override
4323 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004324 boolean searchVisible = updateGlobalSearchIcon();
4325 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004326 if (mSearchDropTargetBar != null) {
4327 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4328 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004329 }
4330
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004331 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004332 * Add the icons for all apps.
4333 *
4334 * Implementation of the method from LauncherModel.Callbacks.
4335 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004336 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004337 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004338 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4339 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4340 getHotseat().addAllAppsFolder(mIconCache, apps,
4341 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4342 }
4343 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004344 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004345 if (mAppsCustomizeContent != null) {
4346 mAppsCustomizeContent.onPackagesUpdated(
4347 LauncherModel.getSortedWidgetsAndShortcuts(this));
4348 }
Winson Chungc58497e2013-09-03 17:48:37 -07004349 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004350 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004351 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004352 mAppsCustomizeContent.onPackagesUpdated(
4353 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004354 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004355 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004356 }
4357
4358 /**
4359 * A package was updated.
4360 *
4361 * Implementation of the method from LauncherModel.Callbacks.
4362 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004363 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004364 Runnable r = new Runnable() {
4365 public void run() {
4366 bindAppsUpdated(apps);
4367 }
4368 };
4369 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004370 return;
4371 }
4372
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004373 if (mWorkspace != null) {
4374 mWorkspace.updateShortcuts(apps);
4375 }
Winson Chungc58497e2013-09-03 17:48:37 -07004376
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004377 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004378 mAppsCustomizeContent != null) {
4379 mAppsCustomizeContent.updateApps(apps);
4380 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004381 }
4382
4383 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004384 * Update the state of a package, typically related to install state.
4385 *
4386 * Implementation of the method from LauncherModel.Callbacks.
4387 */
4388 public void updatePackageState(String pkgName, int state) {
4389 if (mWorkspace != null) {
4390 mWorkspace.updatePackageState(pkgName, state);
4391 }
4392 }
4393
4394 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004395 * A package was uninstalled. We take both the super set of packageNames
4396 * in addition to specific applications to remove, the reason being that
4397 * this can be called when a package is updated as well. In that scenario,
4398 * we only remove specific components from the workspace, where as
4399 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004400 *
4401 * Implementation of the method from LauncherModel.Callbacks.
4402 */
Winson Chung83892cc2013-05-01 16:53:33 -07004403 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01004404 final ArrayList<AppInfo> appInfos, final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004405 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004406 public void run() {
Kenny Guyed131872014-04-30 03:02:21 +01004407 bindComponentsRemoved(packageNames, appInfos, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004408 }
Winson Chungd64d1762013-08-20 14:37:16 -07004409 };
4410 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004411 return;
4412 }
4413
Winson Chungdf95eb12013-10-16 14:57:07 -07004414 if (!packageNames.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004415 mWorkspace.removeItemsByPackageName(packageNames, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07004416 }
4417 if (!appInfos.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004418 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
Joe Onorato36115782010-06-17 13:28:48 -04004419 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004420
Winson Chunga1820962011-10-03 16:31:06 -07004421 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004422 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004423
Winson Chungdf95eb12013-10-16 14:57:07 -07004424 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004425 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004426 mAppsCustomizeContent != null) {
4427 mAppsCustomizeContent.removeApps(appInfos);
4428 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004429 }
Joe Onoratobe386092009-11-17 17:32:16 -08004430
4431 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004432 * A number of packages were updated.
4433 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004434 private ArrayList<Object> mWidgetsAndShortcuts;
4435 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004436 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004437 bindPackagesUpdated(mWidgetsAndShortcuts);
4438 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004439 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004440 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004441 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4442 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4443 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004444 return;
4445 }
4446
Winson Chung64359a52013-07-08 17:17:08 -07004447 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004448 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004449 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004450 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004451 }
4452
Winson Chung400438b2011-01-16 17:53:48 -08004453 private int mapConfigurationOriActivityInfoOri(int configOri) {
4454 final Display d = getWindowManager().getDefaultDisplay();
4455 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4456 switch (d.getRotation()) {
4457 case Surface.ROTATION_0:
4458 case Surface.ROTATION_180:
4459 // We are currently in the same basic orientation as the natural orientation
4460 naturalOri = configOri;
4461 break;
4462 case Surface.ROTATION_90:
4463 case Surface.ROTATION_270:
4464 // We are currently in the other basic orientation to the natural orientation
4465 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4466 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4467 break;
4468 }
4469
4470 int[] oriMap = {
4471 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4472 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4473 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4474 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4475 };
4476 // Since the map starts at portrait, we need to offset if this device's natural orientation
4477 // is landscape.
4478 int indexOffset = 0;
4479 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4480 indexOffset = 1;
4481 }
4482 return oriMap[(d.getRotation() + indexOffset) % 4];
4483 }
Adam Cohen446e9402011-09-15 18:21:21 -07004484
Winson Chung4b919f82012-05-01 10:44:08 -07004485 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004486 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004487 getResources().getBoolean(R.bool.allow_rotation);
4488 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004489 }
Winson Chung4b919f82012-05-01 10:44:08 -07004490 public void lockScreenOrientation() {
4491 if (isRotationEnabled()) {
4492 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4493 .getConfiguration().orientation));
4494 }
4495 }
4496 public void unlockScreenOrientation(boolean immediate) {
4497 if (isRotationEnabled()) {
4498 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004499 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004500 } else {
4501 mHandler.postDelayed(new Runnable() {
4502 public void run() {
4503 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4504 }
4505 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004506 }
Winson Chung4b919f82012-05-01 10:44:08 -07004507 }
Winson Chung400438b2011-01-16 17:53:48 -08004508 }
4509
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004510 /**
4511 * Called when the SearchBar hint should be changed.
4512 *
4513 * @param hint the hint to be displayed in the search bar.
4514 */
4515 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004516 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004517 }
4518
Winson Chunge43a1e72014-01-15 10:33:02 -08004519 protected boolean isLauncherPreinstalled() {
4520 PackageManager pm = getPackageManager();
4521 try {
4522 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4523 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4524 return true;
4525 } else {
4526 return false;
4527 }
4528 } catch (NameNotFoundException e) {
4529 e.printStackTrace();
4530 return false;
4531 }
4532 }
4533
Adam Cohenea90f832014-05-21 15:03:34 -07004534 /**
4535 * This method indicates whether or not we should suggest default wallpaper dimensions
4536 * when our wallpaper cropper was not yet used to set a wallpaper.
4537 */
4538 protected boolean overrideWallpaperDimensions() {
4539 return true;
4540 }
4541
Adam Cohen5eed5d82014-05-19 13:12:13 -07004542 protected boolean shouldClingFocusHotseatApp() {
4543 return false;
4544 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004545 protected String getFirstRunClingSearchBarHint() {
4546 return "";
4547 }
4548 protected String getFirstRunCustomContentHint() {
4549 return "";
4550 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004551 protected int getFirstRunFocusedHotseatAppDrawableId() {
4552 return -1;
4553 }
4554 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4555 return null;
4556 }
4557 protected int getFirstRunFocusedHotseatAppRank() {
4558 return -1;
4559 }
4560 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4561 return "";
4562 }
4563 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4564 return "";
4565 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004566
Winson Chungaf40f202013-09-18 18:26:31 -07004567 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004568 mLauncherClings.dismissFirstRunCling(v);
4569 }
4570 public void dismissMigrationClingCopyApps(View v) {
4571 mLauncherClings.dismissMigrationClingCopyApps(v);
4572 }
4573 public void dismissMigrationClingUseDefault(View v) {
4574 mLauncherClings.dismissMigrationClingUseDefault(v);
4575 }
4576 public void dismissMigrationWorkspaceCling(View v) {
4577 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004578 }
Winson Chung82f55532011-08-09 14:14:23 -07004579 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004580 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004581 }
4582 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004583 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004584 }
Adam Cohen6268f2d2014-05-16 16:42:35 -07004585 public void markFolderClingDismissedIfNecessary() {
4586 mLauncherClings.markFolderClingDismissedIfNecessary();
4587 }
Adam Cohen432609a2014-03-13 17:03:22 -07004588
4589 /**
4590 * To be overridden by subclasses to indicate that there is an activity to launch
4591 * before showing the standard launcher experience.
4592 */
4593 protected boolean hasFirstRunActivity() {
4594 return false;
4595 }
4596
4597 /**
4598 * To be overridden by subclasses to launch any first run activity
4599 */
4600 protected Intent getFirstRunActivity() {
4601 return null;
4602 }
4603
Winson Chunga6945242014-01-08 14:04:34 -08004604 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004605 return !ActivityManager.isRunningInTestHarness() &&
4606 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004607 }
4608
Adam Cohen4a9423d2014-03-28 14:22:31 -07004609 protected boolean hasRunFirstRunActivity() {
4610 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4611 }
4612
Adam Cohen432609a2014-03-13 17:03:22 -07004613 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004614 if (shouldRunFirstRunActivity() &&
4615 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004616 Intent firstRunIntent = getFirstRunActivity();
4617 if (firstRunIntent != null) {
4618 startActivity(firstRunIntent);
4619 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004620 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004621 }
4622 }
Adam Cohen432609a2014-03-13 17:03:22 -07004623 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004624 }
4625
4626 private void markFirstRunActivityShown() {
4627 SharedPreferences.Editor editor = mSharedPrefs.edit();
4628 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4629 editor.apply();
4630 }
4631
Adam Cohen432609a2014-03-13 17:03:22 -07004632 /**
4633 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4634 * screen that must be displayed and dismissed.
4635 */
4636 protected boolean hasDismissableIntroScreen() {
4637 return false;
4638 }
4639
4640 /**
4641 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4642 */
4643 protected View getIntroScreen() {
4644 return null;
4645 }
4646
4647 /**
4648 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4649 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4650 */
4651 private boolean shouldShowIntroScreen() {
4652 return hasDismissableIntroScreen() &&
4653 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4654 }
4655
4656 protected void showIntroScreen() {
4657 View introScreen = getIntroScreen();
4658 changeWallpaperVisiblity(false);
4659 if (introScreen != null) {
4660 mDragLayer.showOverlayView(introScreen);
4661 }
4662 }
4663
4664 public void dismissIntroScreen() {
4665 markIntroScreenDismissed();
4666 if (showFirstRunActivity()) {
4667 // We delay hiding the intro view until the first run activity is showing. This
4668 // avoids a blip.
4669 mWorkspace.postDelayed(new Runnable() {
4670 @Override
4671 public void run() {
4672 mDragLayer.dismissOverlayView();
4673 }
4674 }, ACTIVITY_START_DELAY);
4675 } else {
4676 mDragLayer.dismissOverlayView();
4677 }
4678 changeWallpaperVisiblity(true);
4679 }
4680
4681 private void markIntroScreenDismissed() {
4682 SharedPreferences.Editor editor = mSharedPrefs.edit();
4683 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4684 editor.apply();
4685 }
4686
Winson Chunga6945242014-01-08 14:04:34 -08004687 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004688 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4689 if (mHotseat != null) mHotseat.setAlpha(1f);
4690 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4691 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004692 }
4693
4694 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004695 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4696 if (mHotseat != null) mHotseat.setAlpha(0f);
4697 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4698 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004699 }
4700
Mathew Inwood72fbec12013-11-19 15:45:07 +00004701 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004702 // Called from search suggestion, not supported in other profiles.
4703 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4704 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4705 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4706 myUser);
4707 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004708 return null;
4709 }
Kenny Guyed131872014-04-30 03:02:21 +01004710 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004711 }
4712
4713 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4714 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004715 // Called from search suggestion, not supported in other profiles.
4716 return createShortcutDragInfo(shortcutIntent, caption, icon,
4717 UserHandleCompat.myUserHandle());
4718 }
4719
4720 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4721 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004722 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
4723 String contentDescription = userManager.getBadgedLabelForUser(caption.toString(), user);
4724 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004725 }
4726
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004727 protected void moveWorkspaceToDefaultScreen() {
4728 mWorkspace.moveToDefaultScreen(false);
4729 }
4730
Mathew Inwood72fbec12013-11-19 15:45:07 +00004731 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4732 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004733 mWorkspace.onExternalDragStartedWithItem(dragView);
4734 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004735 }
4736
Anjali Koppalf5d29132014-02-28 13:33:27 -08004737 @Override
4738 public void onPageSwitch(View newPage, int newPageIndex) {
4739 }
4740
Winson Chung80baf5a2010-08-09 16:03:15 -07004741 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004742 * Prints out out state for debugging.
4743 */
4744 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004745 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004746 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004747 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4748 Log.d(TAG, "mRestoring=" + mRestoring);
4749 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4750 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004751 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004752 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004753
Winson Chung785d2eb2011-04-14 16:08:02 -07004754 if (mAppsCustomizeContent != null) {
4755 mAppsCustomizeContent.dumpState();
4756 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004757 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004758 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004759
4760 @Override
4761 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4762 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004763 synchronized (sDumpLogs) {
4764 writer.println(" ");
4765 writer.println("Debug logs: ");
4766 for (int i = 0; i < sDumpLogs.size(); i++) {
4767 writer.println(" " + sDumpLogs.get(i));
4768 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004769 }
4770 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004771
4772 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004773 if (DEBUG_DUMP_LOG) {
4774 synchronized (sDumpLogs) {
4775 Log.d(TAG, "");
4776 Log.d(TAG, "*********************");
4777 Log.d(TAG, "Launcher debug logs: ");
4778 for (int i = 0; i < sDumpLogs.size(); i++) {
4779 Log.d(TAG, " " + sDumpLogs.get(i));
4780 }
4781 Log.d(TAG, "*********************");
4782 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004783 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004784 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004785 }
4786
4787 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004788 addDumpLog(tag, log, null, debugLog);
4789 }
4790
4791 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004792 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004793 if (e != null) {
4794 Log.d(tag, log, e);
4795 } else {
4796 Log.d(tag, log);
4797 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004798 }
Winson Chungede41292013-09-19 16:27:36 -07004799 if (DEBUG_DUMP_LOG) {
4800 sDateStamp.setTime(System.currentTimeMillis());
4801 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004802 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4803 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004804 }
Winson Chungede41292013-09-19 16:27:36 -07004805 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004806 }
4807
Winson Chungede41292013-09-19 16:27:36 -07004808 public void dumpLogsToLocalData() {
4809 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004810 new AsyncTask<Void, Void, Void>() {
4811 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004812 boolean success = false;
4813 sDateStamp.setTime(sRunStart);
4814 String FILENAME = sDateStamp.getMonth() + "-"
4815 + sDateStamp.getDay() + "_"
4816 + sDateStamp.getHours() + "-"
4817 + sDateStamp.getMinutes() + "_"
4818 + sDateStamp.getSeconds() + ".txt";
4819
4820 FileOutputStream fos = null;
4821 File outFile = null;
4822 try {
4823 outFile = new File(getFilesDir(), FILENAME);
4824 outFile.createNewFile();
4825 fos = new FileOutputStream(outFile);
4826 } catch (Exception e) {
4827 e.printStackTrace();
4828 }
4829 if (fos != null) {
4830 PrintWriter writer = new PrintWriter(fos);
4831
4832 writer.println(" ");
4833 writer.println("Debug logs: ");
4834 synchronized (sDumpLogs) {
4835 for (int i = 0; i < sDumpLogs.size(); i++) {
4836 writer.println(" " + sDumpLogs.get(i));
4837 }
4838 }
4839 writer.close();
4840 }
4841 try {
4842 if (fos != null) {
4843 fos.close();
4844 success = true;
4845 }
4846 } catch (IOException e) {
4847 e.printStackTrace();
4848 }
Michael Jurka43467462013-11-14 12:03:58 +01004849 return null;
Winson Chungede41292013-09-19 16:27:36 -07004850 }
Michael Jurka43467462013-11-14 12:03:58 +01004851 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004852 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004854}
Michael Jurka2763be32011-02-24 11:19:57 -08004855
Michael Jurkaabded662011-03-04 12:06:57 -08004856interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004857 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004858 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004859 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004860 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004861 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004862}
Dan Sandlerd5024042014-01-09 15:01:33 -05004863
4864interface DebugIntents {
4865 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4866 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004867}