blob: e3b30cb90fb1dc2a7a0bea55a667bdcf1f804e48 [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;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080036import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070041import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070042import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080044import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070046import android.content.pm.PackageManager.NameNotFoundException;
Mathew Inwood72fbec12013-11-19 15:45:07 +000047import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Winson Chung5f8afe62013-08-12 16:19:28 -070053import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070071import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070079import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070082import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080083import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070084import android.view.ViewGroup;
85import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080086import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080088import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070089import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
95import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Daniel Sandler325dc232013-06-05 22:57:57 -040098import com.android.launcher3.DropTarget.DragObject;
Anjali Koppalf5d29132014-02-28 13:33:27 -080099import com.android.launcher3.PagedView.PageSwitchListener;
Romain Guyedcce092010-03-04 13:03:17 -0800100
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101import java.io.DataInputStream;
102import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700103import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700104import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700106import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700108import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700109import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700114import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000115import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116
Winson Chunga6945242014-01-08 14:04:34 -0800117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118/**
119 * Default launcher application.
120 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100121public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700122 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700123 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800124 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700125 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Daniel Sandlerf061f822013-06-27 13:59:36 -0400127 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400128 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700129 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400130 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700131 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700132
Dan Sandlerd5024042014-01-09 15:01:33 -0500133 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
134
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
Mathew Inwood876a8462013-06-14 14:12:41 +0100144 /**
145 * IntentStarter uses request codes starting with this. This must be greater than all activity
146 * request codes used internally.
147 */
148 protected static final int REQUEST_LAST = 100;
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
151
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800152 static final int SCREEN_COUNT = 5;
153 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800156 // To turn on these properties, type
157 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
158 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
159 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800160 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Winson Chung2672ff92012-05-04 16:22:30 -0700162 // The Intent extra that defines whether to ignore the launch animation
163 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400164 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
167 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700168 // Type: int
169 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700171 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
172 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
174 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700175 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700177 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: boolean
179 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
180 // Type: long
181 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700182 // Type: int
183 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
184 // Type: int
185 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
186 // Type: parcelable
187 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000188 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800189 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000190 // Type: int[]
191 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
Adam Cohenb54a5982014-01-08 15:21:04 -0800193
194 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
195
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100196 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700197 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100198 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700199 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100200 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700201
Adam Cohen39a06042013-07-19 14:30:12 -0700202 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700203 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700204
Winson Chunga6945242014-01-08 14:04:34 -0800205 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
206
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700207 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700208 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700209 private State mState = State.WORKSPACE;
210 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700211
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700213 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
Jorim Jaggi55bd9722014-01-16 15:30:42 -0800214 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT_FOLDER_CLOSE = 400;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700215 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800218 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000220 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
221 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
222
Winson Chunga2413752012-04-03 14:22:34 -0700223 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700224 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
225 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700226 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700227
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800228 private final BroadcastReceiver mCloseSystemDialogsReceiver
229 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800230 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 private LayoutInflater mInflater;
233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700235 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800236 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800237 private DragLayer mDragLayer;
238 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700239 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800240 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700241
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700242 private AppWidgetManager mAppWidgetManager;
243 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700244
Michael Jurkac9d95c52011-08-29 14:03:34 -0700245 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700246 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800247 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700248
Michael Jurka0280c3b2010-09-17 15:00:07 -0700249 private int[] mTmpAddItemCellCoordinates = new int[2];
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private FolderInfo mFolderInfo;
252
Winson Chung3d503fb2011-07-13 17:25:49 -0700253 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800254 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700255
Michael Jurka838a4ca2011-02-07 13:33:06 -0800256 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700257
Winson Chungc51db6a2011-10-05 11:44:49 -0700258 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700259 private AppsCustomizeTabHost mAppsCustomizeTabHost;
260 private AppsCustomizePagedView mAppsCustomizeContent;
261 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800262 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700265 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
266 // scroll issues (because the workspace may not have been measured yet) and extra work.
267 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
268 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269
270 private SpannableStringBuilder mDefaultKeySsb = null;
271
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272 private boolean mWorkspaceLoading = true;
273
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800274 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 private boolean mRestoring;
276 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700277 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
Michael Jurka1e2f4652013-07-08 18:03:46 -0700279 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700280 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
281
Winson Chung4a2afa32012-07-19 14:53:05 -0700282 // Keep track of whether the user has left launcher
283 private static boolean sPausedFromUserAction = false;
284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 private Bundle mSavedInstanceState;
286
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800288 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800289 private boolean mUserPresent = true;
290 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700291 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800292 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700294 private static LocaleConfiguration sLocaleConfiguration = null;
295
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700296 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700297
Adam Cohen61f560d2013-09-30 15:58:20 -0700298 private View.OnTouchListener mHapticFeedbackTouchListener;
299
Adam Cohended9f8d2010-11-03 13:25:16 -0700300 // Related to the auto-advancing of widgets
301 private final int ADVANCE_MSG = 1;
302 private final int mAdvanceInterval = 20000;
303 private final int mAdvanceStagger = 250;
304 private long mAutoAdvanceSentTime;
305 private long mAutoAdvanceTimeLeft = -1;
306 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
307 new HashMap<View, AppWidgetProviderInfo>();
308
Winson Chung400438b2011-01-16 17:53:48 -0800309 // Determines how long to wait after a rotation before restoring the screen orientation to
310 // match the sensor state.
311 private final int mRestoreScreenOrientationDelay = 500;
312
Michael Jurka4ef207b2010-11-29 17:05:45 -0800313 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700314 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
315 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
316 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800317
Winson Chungd64a6662013-09-30 11:06:59 -0700318 private Intent mAppMarketIntent = null;
319 private static final boolean DISABLE_MARKET_BUTTON = true;
320
Craig Mautner360310b2012-10-26 15:13:08 -0700321 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700322
Adam Cohen1462de32012-07-24 22:34:36 -0700323 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700324 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700325
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700326 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700327 static Date sDateStamp = new Date();
328 static DateFormat sDateFormat =
329 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
330 static long sRunStart = System.currentTimeMillis();
331 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700332
Winson Chung46353de2012-02-16 14:05:10 -0800333 // We only want to get the SharedPreferences once since it does an FS stat each time we get
334 // it from the context.
335 private SharedPreferences mSharedPrefs;
336
Adam Cohene25af792013-06-06 23:08:25 -0700337 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
338
Winson Chungf0c6ae02012-03-21 16:10:31 -0700339 // Holds the page that we need to animate to, and the icon views that we need to animate up
340 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700341 private ImageView mFolderIconImageView;
342 private Bitmap mFolderIconBitmap;
343 private Canvas mFolderIconCanvas;
344 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700345
Michael Jurkaddd62e92011-02-16 17:49:14 -0800346 private BubbleTextView mWaitingForResume;
347
Michael Jurkac1f5d262011-09-30 19:32:27 -0700348 private Runnable mBuildLayersRunnable = new Runnable() {
349 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700350 if (mWorkspace != null) {
351 mWorkspace.buildPageHardwareLayers();
352 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700353 }
354 };
355
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800356 private static ArrayList<PendingAddArguments> sPendingAddList
357 = new ArrayList<PendingAddArguments>();
358
Michael Jurka7267fa52013-09-26 15:29:57 -0700359 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800360
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800361 private static class PendingAddArguments {
362 int requestCode;
363 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700364 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700365 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800366 int cellX;
367 int cellY;
368 }
369
Daniel Sandlerff02d492013-08-05 02:12:05 -0400370 private Stats mStats;
371
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800372 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800373 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700374 }
375
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 @Override
377 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700378 if (DEBUG_STRICT_MODE) {
379 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
380 .detectDiskReads()
381 .detectDiskWrites()
382 .detectNetwork() // or .detectAll() for all detectable problems
383 .penaltyLog()
384 .build());
385 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
386 .detectLeakedSqlLiteObjects()
387 .detectLeakedClosableObjects()
388 .penaltyLog()
389 .penaltyDeath()
390 .build());
391 }
392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400394
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400395 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400396 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700397 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700398 // Determine the dynamic grid properties
399 Point smallestSize = new Point();
400 Point largestSize = new Point();
401 Point realSize = new Point();
402 Display display = getWindowManager().getDefaultDisplay();
403 display.getCurrentSizeRange(smallestSize, largestSize);
404 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700405 DisplayMetrics dm = new DisplayMetrics();
406 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700407
Winson Chung5f8afe62013-08-12 16:19:28 -0700408 // Lazy-initialize the dynamic grid
409 DeviceProfile grid = app.initDynamicGrid(this,
410 Math.min(smallestSize.x, smallestSize.y),
411 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700412 realSize.x, realSize.y,
413 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700414
415 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400416 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700417 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 mModel = app.setLauncher(this);
419 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700420 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400421 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800422 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700424
Daniel Sandlerff02d492013-08-05 02:12:05 -0400425 mStats = new Stats(this);
426
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700428
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700429 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
430 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700431
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700432 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
433 // this also ensures that any synchronous binding below doesn't re-trigger another
434 // LauncherModel load.
435 mPaused = false;
436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200438 android.os.Debug.startMethodTracing(
439 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 }
441
Adam Cohen53805212013-10-01 10:39:23 -0700442
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800443 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100447 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800449 registerContentObservers();
450
Joe Onorato7c312c12009-08-13 21:36:53 -0700451 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 mSavedState = savedInstanceState;
454 restoreState(mSavedState);
455
456 if (PROFILE_STARTUP) {
457 android.os.Debug.stopMethodTracing();
458 }
459
460 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700461 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 // If the user leaves launcher, then we should just load items asynchronously when
463 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500464 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700465 } else {
466 // We only load the page synchronously if the user rotates (or triggers a
467 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800468 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700469 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 }
471
472 // For handling default keys
473 mDefaultKeySsb = new SpannableStringBuilder();
474 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800475
476 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
477 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800478
Adam Cohenf9426d52012-06-04 17:26:21 -0700479 updateGlobalIcons();
480
481 // On large interfaces, we want the screen to auto-rotate based on the current orientation
482 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700483
Winson Chunge43a1e72014-01-15 10:33:02 -0800484 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
485 // on the device, then we always show the first run cling experience (or if there is no
486 // launcher2). Otherwise, we prompt the user upon started for migration
Adam Cohenb54a5982014-01-08 15:21:04 -0800487 showFirstRunActivity();
Winson Chunge43a1e72014-01-15 10:33:02 -0800488 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
489 if (mModel.canMigrateFromOldLauncherDb(this)) {
490 mLauncherClings.showMigrationCling();
491 } else {
492 mLauncherClings.showFirstRunCling();
493 }
Winson Chunga6945242014-01-08 14:04:34 -0800494 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800495 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800496 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700497 }
498
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700499 @Override
500 public void onLauncherProviderChange() { }
501
Winson Chung4a2afa32012-07-19 14:53:05 -0700502 protected void onUserLeaveHint() {
503 super.onUserLeaveHint();
504 sPausedFromUserAction = true;
505 }
506
Winson Chung98ca0f72013-07-29 12:58:51 -0700507 /** To be overriden by subclasses to hint to Launcher that we have custom content */
508 protected boolean hasCustomContentToLeft() {
509 return false;
510 }
511
Dave Hawkeya8881582013-09-17 15:55:33 -0600512 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500513 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600514 * {@link #addToCustomContentPage}. This will only be invoked if
515 * {@link #hasCustomContentToLeft()} is {@code true}.
516 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500517 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600518 }
519
520 /**
Adam Cohenb54a5982014-01-08 15:21:04 -0800521 * To be overridden by subclasses to indicate that there is an activity to launch
522 * before showing the standard launcher experience.
523 */
524 protected boolean hasFirstRunActivity() {
525 return false;
526 }
527
528 /**
529 * To be overridden by subclasses to launch any first run activity
530 */
531 protected Intent getFirstRunActivity() {
532 return null;
533 }
534
535 /**
Dave Hawkeya8881582013-09-17 15:55:33 -0600536 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
537 * ensure the custom content page is added or removed if necessary.
538 */
539 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600540 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600541 // Not bound yet, wait for bindScreens to be called.
542 return;
543 }
544
545 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
546 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500547 mWorkspace.createCustomContentContainer();
548 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600549 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
550 mWorkspace.removeCustomContentPage();
551 }
552 }
553
Adam Cohenf9426d52012-06-04 17:26:21 -0700554 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700555 boolean searchVisible = false;
556 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800557 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700558 int coi = getCurrentOrientationIndexForGlobalIcons();
559 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
560 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700561 if (!DISABLE_MARKET_BUTTON) {
562 updateAppMarketIcon();
563 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700564 searchVisible = updateGlobalSearchIcon();
565 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700566 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700567 if (sGlobalSearchIcon[coi] != null) {
568 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700569 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700570 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700571 if (sVoiceSearchIcon[coi] != null) {
572 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700573 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700574 }
Winson Chungd64a6662013-09-30 11:06:59 -0700575 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700576 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800577 }
Winson Chungadf0c182012-08-23 14:59:07 -0700578 if (mSearchDropTargetBar != null) {
579 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
580 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 }
Romain Guycbb89e42009-06-08 15:52:54 -0700582
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700584 if (sLocaleConfiguration == null) {
585 new AsyncTask<Void, Void, LocaleConfiguration>() {
586 @Override
587 protected LocaleConfiguration doInBackground(Void... unused) {
588 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
589 readConfiguration(Launcher.this, localeConfiguration);
590 return localeConfiguration;
591 }
592
593 @Override
594 protected void onPostExecute(LocaleConfiguration result) {
595 sLocaleConfiguration = result;
596 checkForLocaleChange(); // recursive, but now with a locale configuration
597 }
598 }.execute();
599 return;
600 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700601
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 final Configuration configuration = getResources().getConfiguration();
603
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700604 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 final String locale = configuration.locale.toString();
606
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700607 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 final int mcc = configuration.mcc;
609
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700610 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 final int mnc = configuration.mnc;
612
Romain Guy84f296c2009-11-04 15:00:44 -0800613 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614
Romain Guy84f296c2009-11-04 15:00:44 -0800615 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700616 sLocaleConfiguration.locale = locale;
617 sLocaleConfiguration.mcc = mcc;
618 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700619
Joe Onorato0589f0f2010-02-08 13:44:00 -0800620 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700621
622 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100623 new AsyncTask<Void, Void, Void>() {
624 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700625 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100626 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700627 }
Michael Jurka43467462013-11-14 12:03:58 +0100628 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700629 }
630 }
631
632 private static class LocaleConfiguration {
633 public String locale;
634 public int mcc = -1;
635 public int mnc = -1;
636 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700637
Romain Guy98d01652009-06-30 16:21:04 -0700638 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
639 DataInputStream in = null;
640 try {
641 in = new DataInputStream(context.openFileInput(PREFERENCES));
642 configuration.locale = in.readUTF();
643 configuration.mcc = in.readInt();
644 configuration.mnc = in.readInt();
645 } catch (FileNotFoundException e) {
646 // Ignore
647 } catch (IOException e) {
648 // Ignore
649 } finally {
650 if (in != null) {
651 try {
652 in.close();
653 } catch (IOException e) {
654 // Ignore
655 }
656 }
657 }
658 }
659
660 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
661 DataOutputStream out = null;
662 try {
663 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
664 out.writeUTF(configuration.locale);
665 out.writeInt(configuration.mcc);
666 out.writeInt(configuration.mnc);
667 out.flush();
668 } catch (FileNotFoundException e) {
669 // Ignore
670 } catch (IOException e) {
671 //noinspection ResultOfMethodCallIgnored
672 context.getFileStreamPath(PREFERENCES).delete();
673 } finally {
674 if (out != null) {
675 try {
676 out.close();
677 } catch (IOException e) {
678 // Ignore
679 }
680 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
682 }
683
Anton Hanssona2f665f2013-09-26 12:18:32 +0100684 public Stats getStats() {
685 return mStats;
686 }
687
Bjorn Bringertc459e522013-06-07 19:36:01 +0100688 public LayoutInflater getInflater() {
689 return mInflater;
690 }
691
Winson Chung36a62fe2012-05-06 18:04:42 -0700692 boolean isDraggingEnabled() {
693 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
694 // that is subsequently removed from the workspace in startBinding().
695 return !mModel.isLoadingWorkspace();
696 }
697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 static int getScreen() {
699 synchronized (sLock) {
700 return sScreen;
701 }
702 }
703
704 static void setScreen(int screen) {
705 synchronized (sLock) {
706 sScreen = screen;
707 }
708 }
709
Winson Chung557d6ed2011-07-08 15:34:52 -0700710 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000711 * Copied from View -- the View version of the method isn't called
712 * anywhere else in our process and only exists for API level 17+,
713 * so it's ok to keep our own version with no API requirement.
714 */
715 public static int generateViewId() {
716 for (;;) {
717 final int result = sNextGeneratedId.get();
718 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
719 int newValue = result + 1;
720 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
721 if (sNextGeneratedId.compareAndSet(result, newValue)) {
722 return result;
723 }
724 }
725 }
726
727 public int getViewIdForItem(ItemInfo info) {
728 // This cast is safe given the > 2B range for int.
729 int itemId = (int) info.id;
730 if (mItemIdToViewId.containsKey(itemId)) {
731 return mItemIdToViewId.get(itemId);
732 }
733 int viewId = generateViewId();
734 mItemIdToViewId.put(itemId, viewId);
735 return viewId;
736 }
737
738 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700739 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
740 * a configuration step, this allows the proper animations to run after other transitions.
741 */
742 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700743 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800744 switch (args.requestCode) {
745 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700746 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700747 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800748 break;
749 case REQUEST_PICK_SHORTCUT:
750 processShortcut(args.intent);
751 break;
752 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700753 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700754 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700755 result = true;
756 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800757 case REQUEST_CREATE_APPWIDGET:
758 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700759 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700760 result = true;
761 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800762 }
Michael Jurka27614d22012-04-02 06:40:22 -0700763 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
764 // if you turned the screen off and then back while in All Apps, Launcher would not
765 // return to the workspace. Clearing mAddInfo.container here fixes this issue
766 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700767 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800768 }
769
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700771 protected void onActivityResult(
772 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700773 // Reset the startActivity waiting flag
774 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800775 int pendingAddWidgetId = mPendingAddWidgetId;
776 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700777
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 Runnable exitSpringLoaded = new Runnable() {
779 @Override
780 public void run() {
781 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
782 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
783 }
784 };
785
Michael Jurka8b805b12012-04-18 14:23:14 -0700786 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700787 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700788 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
789 if (resultCode == RESULT_CANCELED) {
790 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700791 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
792 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700793 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700794 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
795 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700796 }
797 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200798 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
799 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
800 mWorkspace.exitOverviewMode(false);
801 }
802 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700803 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200804
Adam Cohened66b2b2012-01-23 17:28:51 -0800805 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
806 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700807
Adam Cohened66b2b2012-01-23 17:28:51 -0800808 // We have special handling for widgets
809 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800810 final int appWidgetId;
811 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
812 : -1;
813 if (widgetId < 0) {
814 appWidgetId = pendingAddWidgetId;
815 } else {
816 appWidgetId = widgetId;
817 }
818
Adam Cohenad4e15c2013-10-17 16:21:35 -0700819 final int result;
820 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800821 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700822 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
823 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700824 result = RESULT_CANCELED;
825 completeTwoStageWidgetDrop(result, appWidgetId);
826 onComplete = new Runnable() {
827 @Override
828 public void run() {
829 exitSpringLoadedDragModeDelayed(false, 0, null);
830 }
831 };
Winson Chung5aaab772012-04-27 15:24:02 -0700832 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700833 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700834 final CellLayout dropLayout =
835 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
836 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700837 onComplete = new Runnable() {
838 @Override
839 public void run() {
840 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700841 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700842 }
843 };
Winson Chung5aaab772012-04-27 15:24:02 -0700844 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700845 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
846 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800847 return;
848 }
849
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 // The pattern used here is that a user PICKs a specific application,
851 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
854 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700855 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800856 final PendingAddArguments args = new PendingAddArguments();
857 args.requestCode = requestCode;
858 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700859 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700860 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700861 args.cellX = mPendingAddInfo.cellX;
862 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800863 if (isWorkspaceLocked()) {
864 sPendingAddList.add(args);
865 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700866 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700868 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
869 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700870 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700871 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
872 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800874 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 }
876
877 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700878 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700879 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800880 Runnable onCompleteRunnable = null;
881 int animationType = 0;
882
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700883 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800884 if (resultCode == RESULT_OK) {
885 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
886 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700887 mPendingAddWidgetInfo);
888 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800889 onCompleteRunnable = new Runnable() {
890 @Override
891 public void run() {
892 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700893 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700894 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
895 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800896 }
897 };
898 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800899 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800900 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800901 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700902 if (mDragLayer.getAnimatedView() != null) {
903 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
904 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
905 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700906 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700907 // The animated view may be null in the case of a rotation during widget configuration
908 onCompleteRunnable.run();
909 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 }
911
912 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700913 protected void onStop() {
914 super.onStop();
915 FirstFrameAnimatorHelper.setIsVisible(false);
916 }
917
918 @Override
919 protected void onStart() {
920 super.onStart();
921 FirstFrameAnimatorHelper.setIsVisible(true);
922 }
923
924 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200926 long startTime = 0;
927 if (DEBUG_RESUME_TIME) {
928 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400929 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700932
Winson Chung4a2afa32012-07-19 14:53:05 -0700933 // Restore the previous launcher state
934 if (mOnResumeState == State.WORKSPACE) {
935 showWorkspace(false);
936 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c62013-11-06 15:49:51 -0800937 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700938 }
939 mOnResumeState = State.NONE;
940
Craig Mautner360310b2012-10-26 15:13:08 -0700941 // Background was set to gradient in onPause(), restore to black if in all apps.
942 setWorkspaceBackground(mState == State.WORKSPACE);
943
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800944 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700945 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700946 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400947 mWorkspaceLoading = true;
Derek Prothro7aff3992013-12-10 14:00:37 -0500948 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700950 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700952 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200953 // We might have postponed some bind calls until onResume (see waitUntilResume) --
954 // execute them here
955 long startTimeCallbacks = 0;
956 if (DEBUG_RESUME_TIME) {
957 startTimeCallbacks = System.currentTimeMillis();
958 }
959
960 if (mAppsCustomizeContent != null) {
961 mAppsCustomizeContent.setBulkBind(true);
962 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700963 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
964 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200965 }
966 if (mAppsCustomizeContent != null) {
967 mAppsCustomizeContent.setBulkBind(false);
968 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700969 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200970 if (DEBUG_RESUME_TIME) {
971 Log.d(TAG, "Time spent processing callbacks in onResume: " +
972 (System.currentTimeMillis() - startTimeCallbacks));
973 }
Michael Jurka447bf842013-05-15 14:52:15 +0200974 }
Michael Jurka54554252013-08-01 12:52:23 +0200975 if (mOnResumeCallbacks.size() > 0) {
976 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
977 mOnResumeCallbacks.get(i).run();
978 }
979 mOnResumeCallbacks.clear();
980 }
Winson Chunge4e50662012-01-23 14:45:13 -0800981
982 // Reset the pressed state of icons that were locked in the press state while activities
983 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800984 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800985 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800986 mWaitingForResume.setStayPressed(false);
987 }
Winson Chunge4e50662012-01-23 14:45:13 -0800988 if (mAppsCustomizeContent != null) {
989 // Resets the previous all apps icon press state
990 mAppsCustomizeContent.resetDrawableState();
991 }
Winson Chung82963d52013-10-09 11:20:57 -0700992
Adam Cohen06dff352012-06-01 17:17:08 -0700993 // It is possible that widgets can receive updates while launcher is not in the foreground.
994 // Consequently, the widgets will be inflated in the orientation of the foreground activity
995 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
996 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700997 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700998
Winson Chung780fe592013-09-26 14:48:44 -0700999 // Process any items that were added while Launcher was away.
1000 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1001
Winson Chung5841efa2013-09-30 18:06:44 -07001002 // Update the voice search button proxy
1003 updateVoiceButtonProxyVisible(false);
1004
Adam Cohenf9426d52012-06-04 17:26:21 -07001005 // Again, as with the above scenario, it's possible that one or more of the global icons
1006 // were updated in the wrong orientation.
1007 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001008 if (DEBUG_RESUME_TIME) {
1009 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1010 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001011
1012 if (mWorkspace.getCustomContentCallbacks() != null) {
1013 // If we are resuming and the custom content is the current page, we call onShow().
1014 // It is also poassible that onShow will instead be called slightly after first layout
1015 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1016 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001017 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001018 }
1019 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001020 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001021 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001022 }
1023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001025 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001026 // Ensure that items added to Launcher are queued until Launcher returns
1027 InstallShortcutReceiver.enableInstallQueue();
1028
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001029 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001030 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001031 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001032 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001033
1034 // We call onHide() aggressively. The custom content callbacks should be able to
1035 // debounce excess onHide calls.
1036 if (mWorkspace.getCustomContentCallbacks() != null) {
1037 mWorkspace.getCustomContentCallbacks().onHide();
1038 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001039 }
Romain Guycbb89e42009-06-08 15:52:54 -07001040
Adam Cohenbffe7452013-07-22 18:21:45 -07001041 QSBScroller mQsbScroller = new QSBScroller() {
1042 int scrollY = 0;
1043
1044 @Override
1045 public void setScrollY(int scroll) {
1046 scrollY = scroll;
1047
1048 if (mWorkspace.isOnOrMovingToCustomContent()) {
1049 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001050 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001051 }
1052 }
1053 };
1054
1055 public void resetQSBScroll() {
1056 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001057 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001058 }
1059
1060 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001061 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1062 // by a onResume or by scrolling otherwise.
1063 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001064
1065 // Custom content is completely hidden
1066 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001067
1068 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1069 public void onScrollProgressChanged(float progress);
Adam Cohenbffe7452013-07-22 18:21:45 -07001070 }
1071
Jorim Jaggid017f882014-01-14 17:08:48 -08001072 protected boolean hasSettings() {
1073 return false;
1074 }
1075
Adam Cohenbffe7452013-07-22 18:21:45 -07001076 public interface QSBScroller {
1077 public void setScrollY(int scrollY);
1078 }
1079
Winson Chung98ca0f72013-07-29 12:58:51 -07001080 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001081 CustomContentCallbacks callbacks, String description) {
1082 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001083 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001084 }
1085
Adam Cohenedb40762013-07-18 16:45:45 -07001086 // The custom content needs to offset its content to account for the QSB
1087 public int getTopOffsetForCustomContent() {
1088 return mWorkspace.getPaddingTop();
1089 }
1090
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001091 @Override
1092 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001093 // Flag the loader to stop early before switching
1094 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001095 if (mAppsCustomizeContent != null) {
1096 mAppsCustomizeContent.surrender();
1097 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001098 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001099 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001100
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001101 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001102 @Override
1103 public void onWindowFocusChanged(boolean hasFocus) {
1104 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001105 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001106 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 private boolean acceptFilter() {
1109 final InputMethodManager inputManager = (InputMethodManager)
1110 getSystemService(Context.INPUT_METHOD_SERVICE);
1111 return !inputManager.isFullscreenMode();
1112 }
1113
1114 @Override
1115 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001116 final int uniChar = event.getUnicodeChar();
1117 final boolean handled = super.onKeyDown(keyCode, event);
1118 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1119 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1121 keyCode, event);
1122 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001123 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001124 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001125 // showSearchDialog()
1126 // If there are multiple keystrokes before the search dialog takes focus,
1127 // onSearchRequested() will be called for every keystroke,
1128 // but it is idempotent, so it's fine.
1129 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 }
1131 }
1132
Joe Onorato8a9625e2010-01-28 15:55:35 -08001133 // Eat the long press event so the keyboard doesn't come up.
1134 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1135 return true;
1136 }
1137
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 return handled;
1139 }
1140
Karl Rosaen138a0412009-04-23 19:00:21 -07001141 private String getTypedText() {
1142 return mDefaultKeySsb.toString();
1143 }
1144
1145 private void clearTypedText() {
1146 mDefaultKeySsb.clear();
1147 mDefaultKeySsb.clearSpans();
1148 Selection.setSelection(mDefaultKeySsb, 0);
1149 }
1150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001152 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1153 * State
1154 */
1155 private static State intToState(int stateOrdinal) {
1156 State state = State.WORKSPACE;
1157 final State[] stateValues = State.values();
1158 for (int i = 0; i < stateValues.length; i++) {
1159 if (stateValues[i].ordinal() == stateOrdinal) {
1160 state = stateValues[i];
1161 break;
1162 }
1163 }
1164 return state;
1165 }
1166
1167 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 * Restores the previous state, if it exists.
1169 *
1170 * @param savedState The previous state.
1171 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001172 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 private void restoreState(Bundle savedState) {
1174 if (savedState == null) {
1175 return;
1176 }
1177
Michael Jurka883f55b2010-10-21 15:47:14 -07001178 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001179 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001180 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001181 }
1182
Adam Cohen21cd0022013-10-09 18:57:02 -07001183 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1184 PagedView.INVALID_RESTORE_PAGE);
1185 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001186 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 }
1188
Winson Chung3d503fb2011-07-13 17:25:49 -07001189 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001190 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001191
Winson Chung3d503fb2011-07-13 17:25:49 -07001192 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1193 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001194 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001195 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1196 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001197 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1198 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1199 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001200 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001201 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 mRestoring = true;
1203 }
1204
1205 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1206 if (renameFolder) {
1207 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001208 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 mRestoring = true;
1210 }
Winson Chunga12a2502010-12-20 14:41:35 -08001211
Winson Chung785d2eb2011-04-14 16:08:02 -07001212 // Restore the AppsCustomize tab
1213 if (mAppsCustomizeTabHost != null) {
1214 String curTab = savedState.getString("apps_customize_currentTab");
1215 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001216 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001217 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001218 mAppsCustomizeContent.loadAssociatedPages(
1219 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001220 }
1221
Winson Chung5afbf7b2011-07-25 11:53:08 -07001222 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1223 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001224 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001225 mItemIdToViewId = (HashMap<Integer, Integer>)
1226 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228
1229 /**
1230 * Finds all the views we need and configure them properly.
1231 */
1232 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001233 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001234
Craig Mautner360310b2012-10-26 15:13:08 -07001235 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001236 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1237 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001238 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001239 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001240
John Spurlock77e1f472013-09-11 10:09:51 -04001241 mLauncherView.setSystemUiVisibility(
1242 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001243 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244
Winson Chung4c98d922011-05-31 16:50:48 -07001245 // Setup the drag layer
1246 mDragLayer.setup(this, dragController);
1247
Winson Chung3d503fb2011-07-13 17:25:49 -07001248 // Setup the hotseat
1249 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1250 if (mHotseat != null) {
1251 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001252 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001253 }
1254
Jorim Jaggid017f882014-01-14 17:08:48 -08001255 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001256 View widgetButton = findViewById(R.id.widget_button);
1257 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001258 @Override
1259 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001260 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001261 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001262 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001263 }
1264 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001265 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1266
1267 View wallpaperButton = findViewById(R.id.wallpaper_button);
1268 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001269 @Override
1270 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001271 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001272 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001273 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001274 }
1275 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001276 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1277
1278 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001279 if (hasSettings()) {
1280 settingsButton.setOnClickListener(new OnClickListener() {
1281 @Override
1282 public void onClick(View arg0) {
1283 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001284 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001285 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001286 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001287 });
1288 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1289 } else {
1290 settingsButton.setVisibility(View.GONE);
1291 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1292 lp.gravity = Gravity.END | Gravity.TOP;
1293 widgetButton.requestLayout();
1294 }
1295
Adam Cohendbdff6b2013-09-18 19:09:15 -07001296 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001297
Winson Chung4c98d922011-05-31 16:50:48 -07001298 // Setup the workspace
1299 mWorkspace.setHapticFeedbackEnabled(false);
1300 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001301 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001302 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001303
Winson Chungf0ea4d32011-06-06 14:27:16 -07001304 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001305 mSearchDropTargetBar = (SearchDropTargetBar)
1306 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001307
Winson Chungf0ea4d32011-06-06 14:27:16 -07001308 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001309 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001310 mAppsCustomizeContent = (AppsCustomizePagedView)
1311 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1312 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001313
Winson Chung3d503fb2011-07-13 17:25:49 -07001314 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001315 dragController.setDragScoller(mWorkspace);
1316 dragController.setScrollView(mDragLayer);
1317 dragController.setMoveTarget(mWorkspace);
1318 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001319 if (mSearchDropTargetBar != null) {
1320 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001321 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001322
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001323 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001324 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001325 mWeightWatcher = new WeightWatcher(this);
1326 mWeightWatcher.setAlpha(0.5f);
1327 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001328 new FrameLayout.LayoutParams(
1329 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001330 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001331 Gravity.BOTTOM)
1332 );
Adam Cohen39a06042013-07-19 14:30:12 -07001333
1334 boolean show = shouldShowWeightWatcher();
1335 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
1339 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001340 * Sets the all apps button. This method is called from {@link Hotseat}.
1341 */
1342 public void setAllAppsButton(View allAppsButton) {
1343 mAllAppsButton = allAppsButton;
1344 }
1345
1346 public View getAllAppsButton() {
1347 return mAllAppsButton;
1348 }
1349
1350 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 * Creates a view representing a shortcut.
1352 *
1353 * @param info The data structure describing the shortcut.
1354 *
1355 * @return A View inflated from R.layout.application.
1356 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001357 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001359 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361
1362 /**
1363 * Creates a view representing a shortcut inflated from the specified resource.
1364 *
1365 * @param layoutResId The id of the XML layout used to create the shortcut.
1366 * @param parent The group the shortcut belongs to.
1367 * @param info The data structure describing the shortcut.
1368 *
1369 * @return A View inflated from layoutResId.
1370 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001371 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001372 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1373 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 return favorite;
1376 }
1377
1378 /**
1379 * Add an application shortcut to the workspace.
1380 *
1381 * @param data The intent describing the application.
1382 * @param cellInfo The position on screen where to create the shortcut.
1383 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001384 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001385 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001386 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001387
Adam Cohenfbba09b2011-07-18 21:43:05 -07001388 // First we check if we already know the exact location where we want to add this item.
1389 if (cellX >= 0 && cellY >= 0) {
1390 cellXY[0] = cellX;
1391 cellXY[1] = cellY;
1392 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001393 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001394 return;
1395 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001397 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001398
Romain Guy73b979d2009-06-09 12:57:21 -07001399 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001400 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001402 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001403 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001404 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001405 } else {
1406 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
1408 }
Romain Guycbb89e42009-06-08 15:52:54 -07001409
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 /**
1411 * Add a shortcut to the workspace.
1412 *
1413 * @param data The intent describing the shortcut.
1414 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001416 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001417 int cellY) {
1418 int[] cellXY = mTmpAddItemCellCoordinates;
1419 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001420 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001421
Michael Jurkad3ef3062010-11-23 16:23:58 -08001422 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001423
Adam Cohen558baaf2011-08-15 15:22:57 -07001424 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001425 if (info == null) {
1426 return;
1427 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001428 final View view = createShortcut(info);
1429
Adam Cohenfbba09b2011-07-18 21:43:05 -07001430 // First we check if we already know the exact location where we want to add this item.
1431 if (cellX >= 0 && cellY >= 0) {
1432 cellXY[0] = cellX;
1433 cellXY[1] = cellY;
1434 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001435
1436 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001437 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001438 true, null,null)) {
1439 return;
1440 }
1441 DragObject dragObject = new DragObject();
1442 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001443 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1444 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001445 return;
1446 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001447 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001448 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001449 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001450 foundCellSpan = (result != null);
1451 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001452 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001453 }
1454
1455 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001456 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001457 return;
1458 }
1459
Adam Cohendcd297f2013-06-18 13:13:40 -07001460 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461
1462 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001463 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001464 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 }
1466 }
1467
Adam Cohen2f093b62012-04-30 18:59:53 -07001468 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1469 int minHeight) {
1470 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001471 // We want to account for the extra amount of padding that we are adding to the widget
1472 // to ensure that it gets the full amount of space that it has requested
1473 int requiredWidth = minWidth + padding.left + padding.right;
1474 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001475 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001476 }
1477
Adam Cohen2f093b62012-04-30 18:59:53 -07001478 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1479 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001480 }
1481
Adam Cohen2f093b62012-04-30 18:59:53 -07001482 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1483 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001484 }
1485
Adam Cohen2f093b62012-04-30 18:59:53 -07001486 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1487 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001488 }
1489
Adam Cohen2f093b62012-04-30 18:59:53 -07001490 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1491 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001492 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001493 }
1494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001496 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001498 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001499 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001501 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001502 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1503 if (appWidgetInfo == null) {
1504 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1505 }
Romain Guycbb89e42009-06-08 15:52:54 -07001506
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001507 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001508 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001509
Adam Cohen2f093b62012-04-30 18:59:53 -07001510 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1511 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001514 // We have saved the position to which the widget was dragged-- this really only matters
1515 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001516 int[] cellXY = mTmpAddItemCellCoordinates;
1517 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001518 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001519 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001520 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1521 cellXY[0] = mPendingAddInfo.cellX;
1522 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001523 spanXY[0] = mPendingAddInfo.spanX;
1524 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001525 foundCellSpan = true;
1526 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001527 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001528 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001529 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1530 spanXY[1], cellXY, finalSpan);
1531 spanXY[0] = finalSpan[0];
1532 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001533 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001534 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001535 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001536 }
1537
Michael Jurka0280c3b2010-09-17 15:00:07 -07001538 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001539 if (appWidgetId != -1) {
1540 // Deleting an app widget ID is a void call but writes to disk before returning
1541 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001542 new AsyncTask<Void, Void, Void>() {
1543 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001544 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001545 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001546 }
Michael Jurka43467462013-11-14 12:03:58 +01001547 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001548 }
Winson Chung93eef082012-03-23 15:59:27 -07001549 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001550 return;
1551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001553 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001554 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1555 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001556 launcherInfo.spanX = spanXY[0];
1557 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001558 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1559 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001562 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563
1564 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001565 if (hostView == null) {
1566 // Perform actual inflation because we're live
1567 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1568 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1569 } else {
1570 // The AppWidgetHostView has already been inflated and instantiated
1571 launcherInfo.hostView = hostView;
1572 }
Romain Guycbb89e42009-06-08 15:52:54 -07001573
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001575 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001576 launcherInfo.notifyWidgetSizeChanged(this);
1577
Adam Cohendcd297f2013-06-18 13:13:40 -07001578 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001579 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001580
1581 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001583 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
Romain Guycbb89e42009-06-08 15:52:54 -07001585
Adam Cohended9f8d2010-11-03 13:25:16 -07001586 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1587 @Override
1588 public void onReceive(Context context, Intent intent) {
1589 final String action = intent.getAction();
1590 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1591 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001592 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001593 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001594
Winson Chungc100e8e2011-08-09 16:02:43 -07001595 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001596 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001597 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001598 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001599 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001600 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1601 mUserPresent = true;
1602 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001603 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1604 mModel.resetLoadedState(false, true);
1605 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1606 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1607 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1608 mModel.resetLoadedState(false, true);
1609 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1610 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1611 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001612 }
1613 }
1614 };
1615
1616 @Override
1617 public void onAttachedToWindow() {
1618 super.onAttachedToWindow();
1619
1620 // Listen for broadcasts related to user-presence
1621 final IntentFilter filter = new IntentFilter();
1622 filter.addAction(Intent.ACTION_SCREEN_OFF);
1623 filter.addAction(Intent.ACTION_USER_PRESENT);
Dan Sandlerd5024042014-01-09 15:01:33 -05001624 if (ENABLE_DEBUG_INTENTS) {
1625 filter.addAction(DebugIntents.DELETE_DATABASE);
1626 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1627 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001628 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001629 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001630 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001631 mVisible = true;
1632 }
1633
1634 @Override
1635 public void onDetachedFromWindow() {
1636 super.onDetachedFromWindow();
1637 mVisible = false;
1638
Adam Cohend113e0c2010-11-11 10:48:05 -08001639 if (mAttached) {
1640 unregisterReceiver(mReceiver);
1641 mAttached = false;
1642 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001643 updateRunning();
1644 }
1645
1646 public void onWindowVisibilityChanged(int visibility) {
1647 mVisible = visibility == View.VISIBLE;
1648 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001649 // The following code used to be in onResume, but it turns out onResume is called when
1650 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1651 // is a more appropriate event to handle
1652 if (mVisible) {
1653 mAppsCustomizeTabHost.onWindowVisible();
1654 if (!mWorkspaceLoading) {
1655 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001656 // We want to let Launcher draw itself at least once before we force it to build
1657 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001658 // apps is nice and speedy.
1659 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001660 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001661 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001662 if (mStarted) return;
1663 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001664 // We delay the layer building a bit in order to give
1665 // other message processing a time to run. In particular
1666 // this avoids a delay in hiding the IME if it was
1667 // currently shown, because doing that may involve
1668 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001669 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001670 final ViewTreeObserver.OnDrawListener listener = this;
1671 mWorkspace.post(new Runnable() {
1672 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001673 if (mWorkspace != null &&
1674 mWorkspace.getViewTreeObserver() != null) {
1675 mWorkspace.getViewTreeObserver().
1676 removeOnDrawListener(listener);
1677 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001678 }
1679 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001680 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001681 }
1682 });
1683 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001684 // When Launcher comes back to foreground, a different Activity might be responsible for
1685 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001686 if (!DISABLE_MARKET_BUTTON) {
1687 updateAppMarketIcon();
1688 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001689 clearTypedText();
1690 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001691 }
1692
1693 private void sendAdvanceMessage(long delay) {
1694 mHandler.removeMessages(ADVANCE_MSG);
1695 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1696 mHandler.sendMessageDelayed(msg, delay);
1697 mAutoAdvanceSentTime = System.currentTimeMillis();
1698 }
1699
1700 private void updateRunning() {
1701 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1702 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1703 mAutoAdvanceRunning = autoAdvanceRunning;
1704 if (autoAdvanceRunning) {
1705 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1706 sendAdvanceMessage(delay);
1707 } else {
1708 if (!mWidgetsToAdvance.isEmpty()) {
1709 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1710 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1711 }
1712 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001713 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001714 }
1715 }
1716 }
1717
1718 private final Handler mHandler = new Handler() {
1719 @Override
1720 public void handleMessage(Message msg) {
1721 if (msg.what == ADVANCE_MSG) {
1722 int i = 0;
1723 for (View key: mWidgetsToAdvance.keySet()) {
1724 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1725 final int delay = mAdvanceStagger * i;
1726 if (v instanceof Advanceable) {
1727 postDelayed(new Runnable() {
1728 public void run() {
1729 ((Advanceable) v).advance();
1730 }
1731 }, delay);
1732 }
1733 i++;
1734 }
1735 sendAdvanceMessage(mAdvanceInterval);
1736 }
1737 }
1738 };
1739
1740 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001741 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1743 if (v instanceof Advanceable) {
1744 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001745 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001746 updateRunning();
1747 }
1748 }
1749
1750 void removeWidgetToAutoAdvance(View hostView) {
1751 if (mWidgetsToAdvance.containsKey(hostView)) {
1752 mWidgetsToAdvance.remove(hostView);
1753 updateRunning();
1754 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001755 }
1756
Joe Onorato9c1289c2009-08-17 11:03:03 -04001757 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001758 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001759 launcherInfo.hostView = null;
1760 }
1761
Winson Chung93eef082012-03-23 15:59:27 -07001762 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1763 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1764 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 }
1766
Winson Chunga6945242014-01-08 14:04:34 -08001767 public DragLayer getDragLayer() {
1768 return mDragLayer;
1769 }
1770
1771 public Workspace getWorkspace() {
1772 return mWorkspace;
1773 }
1774
1775 public Hotseat getHotseat() {
1776 return mHotseat;
1777 }
1778
Jorim Jaggid017f882014-01-14 17:08:48 -08001779 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001780 return mOverviewPanel;
1781 }
1782
1783 public SearchDropTargetBar getSearchBar() {
1784 return mSearchDropTargetBar;
1785 }
1786
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001787 public LauncherAppWidgetHost getAppWidgetHost() {
1788 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 }
Romain Guycbb89e42009-06-08 15:52:54 -07001790
Winson Chunga9abd0e2010-10-27 17:18:37 -07001791 public LauncherModel getModel() {
1792 return mModel;
1793 }
1794
Winson Chunga6945242014-01-08 14:04:34 -08001795 public LauncherClings getLauncherClings() {
1796 return mLauncherClings;
1797 }
1798
1799 protected SharedPreferences getSharedPrefs() {
1800 return mSharedPrefs;
1801 }
1802
Bjorn Bringertc459e522013-06-07 19:36:01 +01001803 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001804 getWindow().closeAllPanels();
1805
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001806 // Whatever we were doing is hereby canceled.
1807 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001808 }
1809
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 @Override
1811 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001812 long startTime = 0;
1813 if (DEBUG_RESUME_TIME) {
1814 startTime = System.currentTimeMillis();
1815 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 super.onNewIntent(intent);
1817
1818 // Close the menu
1819 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001820 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001821 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001822
Adam Cohened307df2013-10-02 09:37:31 -07001823 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1824 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1825 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001826
Adam Cohen6fecd412013-10-02 17:41:50 -07001827 if (mWorkspace == null) {
1828 // Can be cases where mWorkspace is null, this prevents a NPE
1829 return;
1830 }
1831 Folder openFolder = mWorkspace.getOpenFolder();
1832 // In all these cases, only animate if we're already on home
1833 mWorkspace.exitWidgetResizeMode();
1834 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001835 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001836 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001837 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001838
Adam Cohen6fecd412013-10-02 17:41:50 -07001839 closeFolder();
1840 exitSpringLoadedDragMode();
1841
1842 // If we are already on home, then just animate back to the workspace,
1843 // otherwise, just wait until onResume to set the state back to Workspace
1844 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001845 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001846 } else {
1847 mOnResumeState = State.WORKSPACE;
1848 }
1849
1850 final View v = getWindow().peekDecorView();
1851 if (v != null && v.getWindowToken() != null) {
1852 InputMethodManager imm = (InputMethodManager)getSystemService(
1853 INPUT_METHOD_SERVICE);
1854 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1855 }
1856
1857 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001858 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001859 mAppsCustomizeTabHost.reset();
1860 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001861
1862 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 }
Adam Cohened307df2013-10-02 09:37:31 -07001864
Michael Jurka447bf842013-05-15 14:52:15 +02001865 if (DEBUG_RESUME_TIME) {
1866 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1867 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 }
1869
Adam Coppa120b8e2013-11-12 16:26:04 +00001870 /**
1871 * Override point for subclasses to prevent movement to the default screen when the home
1872 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1873 */
1874 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1875 return true;
1876 }
1877
1878 /**
1879 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1880 */
1881 protected void onHomeIntent() {
1882 // Do nothing
1883 }
1884
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001886 public void onRestoreInstanceState(Bundle state) {
1887 super.onRestoreInstanceState(state);
1888 for (int page: mSynchronouslyBoundPages) {
1889 mWorkspace.restoreInstanceStateForChild(page);
1890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 }
1892
1893 @Override
1894 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001895 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001896 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1897 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001898 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001899 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900
Michael Jurka883f55b2010-10-21 15:47:14 -07001901 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001902 // We close any open folder since it will not be re-opened, and we need to make sure
1903 // this state is reflected.
1904 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905
Adam Cohendcd297f2013-06-18 13:13:40 -07001906 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001907 mWaitingForResult) {
1908 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001909 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001910 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1911 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001912 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1913 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1914 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001915 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
1917
1918 if (mFolderInfo != null && mWaitingForResult) {
1919 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1920 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1921 }
Michael Jurka946ad472010-07-09 18:05:18 -07001922
Winson Chung785d2eb2011-04-14 16:08:02 -07001923 // Save the current AppsCustomize tab
1924 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08001925 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1926 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001927 if (currentTabTag != null) {
1928 outState.putString("apps_customize_currentTab", currentTabTag);
1929 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001930 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1931 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001932 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001933 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 }
1935
1936 @Override
1937 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001939
Winson Chunge7a03942011-08-05 15:05:12 -07001940 // Remove all pending runnables
1941 mHandler.removeMessages(ADVANCE_MSG);
1942 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001943 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001944
Winson Chungcd2b0142011-06-08 16:02:26 -07001945 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001946 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001947 mModel.stopLoader();
1948 app.setLauncher(null);
1949
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001951 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001953 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001955 mAppWidgetHost = null;
1956
1957 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958
1959 TextKeyListener.getInstance().release();
1960
Winson Chung81b52252012-08-27 15:34:29 -07001961 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1962 // to prevent leaking Launcher activities on orientation change.
1963 if (mModel != null) {
1964 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1965 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001966
1967 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001968 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001969
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001970 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001971 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001972 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001973 mWorkspace = null;
1974 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001975
Michael Jurka2ecf9952012-06-18 12:52:28 -07001976 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
1978
Adam Cohena9cf38f2011-05-02 15:36:58 -07001979 public DragController getDragController() {
1980 return mDragController;
1981 }
1982
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 @Override
1984 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001985 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 super.startActivityForResult(intent, requestCode);
1987 }
1988
Winson Chung70d72102011-08-12 11:24:35 -07001989 /**
1990 * Indicates that we want global search for this activity by setting the globalSearch
1991 * argument for {@link #startSearch} to true.
1992 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001994 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001996
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001997 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001998
Karl Rosaen138a0412009-04-23 19:00:21 -07001999 if (initialQuery == null) {
2000 // Use any text typed in the launcher as the initial query
2001 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 if (appSearchData == null) {
2004 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002005 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 }
Winson Chungadf0c182012-08-23 14:59:07 -07002007 Rect sourceBounds = new Rect();
2008 if (mSearchDropTargetBar != null) {
2009 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2010 }
Romain Guycbb89e42009-06-08 15:52:54 -07002011
Bjorn Bringertc459e522013-06-07 19:36:01 +01002012 startSearch(initialQuery, selectInitialQuery,
2013 appSearchData, sourceBounds);
2014 }
2015
2016 public void startSearch(String initialQuery,
2017 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002018 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002019 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002020 }
2021
2022 /**
2023 * Starts the global search activity. This code is a copied from SearchManager
2024 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002025 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002026 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002027 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002028 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2029 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2030 if (globalSearchActivity == null) {
2031 Log.w(TAG, "No global search activity found.");
2032 return;
2033 }
2034 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2035 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2036 intent.setComponent(globalSearchActivity);
2037 // Make sure that we have a Bundle to put source in
2038 if (appSearchData == null) {
2039 appSearchData = new Bundle();
2040 } else {
2041 appSearchData = new Bundle(appSearchData);
2042 }
2043 // Set source to package name of app that starts global search, if not set already.
2044 if (!appSearchData.containsKey("source")) {
2045 appSearchData.putString("source", getPackageName());
2046 }
2047 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2048 if (!TextUtils.isEmpty(initialQuery)) {
2049 intent.putExtra(SearchManager.QUERY, initialQuery);
2050 }
2051 if (selectInitialQuery) {
2052 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2053 }
2054 intent.setSourceBounds(sourceBounds);
2055 try {
2056 startActivity(intent);
2057 } catch (ActivityNotFoundException ex) {
2058 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2059 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 }
2061
Yura4f93ec62014-02-04 14:15:21 +00002062 public boolean isOnCustomContent() {
2063 return mWorkspace.isOnOrMovingToCustomContent();
2064 }
2065
Winson Chung70d72102011-08-12 11:24:35 -07002066 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002067 public boolean onPrepareOptionsMenu(Menu menu) {
2068 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002069 if (!isOnCustomContent()) {
2070 // Close any open folders
2071 closeFolder();
2072 // Stop resizing any widgets
2073 mWorkspace.exitWidgetResizeMode();
2074 if (!mWorkspace.isInOverviewMode()) {
2075 // Show the overview mode
2076 showOverviewMode(true);
2077 } else {
2078 showWorkspace(true);
2079 }
Winson Chunge0298742014-01-17 12:03:00 -08002080 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002081 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002084 @Override
2085 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002086 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002087 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002088 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002089 }
2090
Joe Onorato9c1289c2009-08-17 11:03:03 -04002091 public boolean isWorkspaceLocked() {
2092 return mWorkspaceLoading || mWaitingForResult;
2093 }
2094
Adam Cohen517a7f52014-03-01 12:12:59 -08002095 public boolean isWorkspaceLoading() {
2096 return mWorkspaceLoading;
2097 }
2098
Michael Jurka0280c3b2010-09-17 15:00:07 -07002099 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002100 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002101 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002102 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2103 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002104 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002105 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002106 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002107
Adam Cohenad4e15c2013-10-17 16:21:35 -07002108 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2109 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2110 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2111 }
2112
2113 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2114 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2115 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002116 if (appWidgetInfo.configure != null) {
2117 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002118 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002119
Bjorn Bringert7984c942009-12-09 15:38:25 +00002120 // Launch over to configure widget, if needed
2121 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002122 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002123 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002124 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002126 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002127 Runnable onComplete = new Runnable() {
2128 @Override
2129 public void run() {
2130 // Exit spring loaded mode if necessary after adding the widget
2131 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2132 null);
2133 }
2134 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002135 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002136 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002137 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 }
2139 }
Romain Guycbb89e42009-06-08 15:52:54 -07002140
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002141 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002142 // Close any folders that may be open.
2143 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002144 mWorkspace.moveToCustomContentScreen(animate);
2145 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002146 /**
2147 * Process a shortcut drop.
2148 *
2149 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002150 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002151 * @param cell The cell it should be added to, optional
2152 * @param position The location on the screen where it was dropped, optional
2153 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002154 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002155 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002156 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002157 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002158 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002159 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002160
2161 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002162 mPendingAddInfo.cellX = cell[0];
2163 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002164 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002165
2166 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2167 createShortcutIntent.setComponent(componentName);
2168 processShortcut(createShortcutIntent);
2169 }
2170
Adam Cohenfbba09b2011-07-18 21:43:05 -07002171 /**
2172 * Process a widget drop.
2173 *
2174 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002175 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002176 * @param cell The cell it should be added to, optional
2177 * @param position The location on the screen where it was dropped, optional
2178 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002179 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002180 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002181 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002182 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002183 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002184 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002185 mPendingAddInfo.minSpanX = info.minSpanX;
2186 mPendingAddInfo.minSpanY = info.minSpanY;
2187
Adam Cohenfbba09b2011-07-18 21:43:05 -07002188 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002189 mPendingAddInfo.cellX = cell[0];
2190 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002191 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002192 if (span != null) {
2193 mPendingAddInfo.spanX = span[0];
2194 mPendingAddInfo.spanY = span[1];
2195 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002196
Adam Cohened66b2b2012-01-23 17:28:51 -08002197 AppWidgetHostView hostView = info.boundWidget;
2198 int appWidgetId;
2199 if (hostView != null) {
2200 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002201 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002202 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002203 // In this case, we either need to start an activity to get permission to bind
2204 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002205 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002206 Bundle options = info.bindOptions;
2207
2208 boolean success = false;
2209 if (options != null) {
2210 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2211 info.componentName, options);
2212 } else {
2213 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2214 info.componentName);
2215 }
2216 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002217 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002218 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002219 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002220 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2221 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2222 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002223 // TODO: we need to make sure that this accounts for the options bundle.
2224 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002225 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2226 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002227 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002228 }
2229
Joe Onoratodeb98af2010-02-19 14:59:39 -08002230 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002231 // Handle case where user selected "Applications"
2232 String applicationName = getResources().getString(R.string.group_applications);
2233 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002234
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002235 if (applicationName != null && applicationName.equals(shortcutName)) {
2236 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2237 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002238
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002239 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2240 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002241 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002242 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002243 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002244 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002245 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 }
2247
Winson Chung24ab2f12010-09-16 14:10:47 -07002248 void processWallpaper(Intent intent) {
2249 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2250 }
2251
Adam Cohendcd297f2013-06-18 13:13:40 -07002252 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002253 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002254 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002255 folderInfo.title = getText(R.string.folder_name);
2256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002258 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002259 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002260 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261
2262 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002263 FolderIcon newFolder =
2264 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002265 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002266 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002267 // Force measure the new folder icon
2268 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2269 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002270 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 }
Romain Guycbb89e42009-06-08 15:52:54 -07002272
Joe Onorato9c1289c2009-08-17 11:03:03 -04002273 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002274 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002275 }
2276
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002277 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002278 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002279 }
2280
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002281 /**
2282 * Registers various content observers. The current implementation registers
2283 * only a favorites observer to keep track of the favorites applications.
2284 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002285 private void registerContentObservers() {
2286 ContentResolver resolver = getContentResolver();
2287 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2288 true, mWidgetObserver);
2289 }
2290
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002291 @Override
2292 public boolean dispatchKeyEvent(KeyEvent event) {
2293 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2294 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002295 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002296 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002297 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002298 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002299 dumpState();
2300 return true;
2301 }
2302 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002303 }
2304 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2305 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002306 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002307 return true;
2308 }
2309 }
2310
2311 return super.dispatchKeyEvent(event);
2312 }
2313
Joe Onorato88ec0992009-11-19 13:16:06 -08002314 @Override
2315 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002316 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002317 if (mAppsCustomizeContent.getContentType() ==
2318 AppsCustomizePagedView.ContentType.Applications) {
2319 showWorkspace(true);
2320 } else {
2321 showOverviewMode(true);
2322 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002323 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002324 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002325 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002326 Folder openFolder = mWorkspace.getOpenFolder();
2327 if (openFolder.isEditingName()) {
2328 openFolder.dismissEditingName();
2329 } else {
2330 closeFolder();
2331 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002332 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002333 mWorkspace.exitWidgetResizeMode();
2334
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002335 // Back button is a no-op here, but give at least some feedback for the button press
2336 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002337 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002338 }
2339
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002340 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002341 * Re-listen when widgets are reset.
2342 */
2343 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002344 if (mAppWidgetHost != null) {
2345 mAppWidgetHost.startListening();
2346 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002347 }
2348
2349 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 * Launches the intent referred by the clicked shortcut.
2351 *
2352 * @param v The view representing the clicked shortcut.
2353 */
2354 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002355 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2356 // view has detached (it's possible for this to happen if the view is removed mid touch).
2357 if (v.getWindowToken() == null) {
2358 return;
2359 }
2360
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002361 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002362 return;
2363 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002364
Adam Cohen1697b792013-09-17 19:08:21 -07002365 if (v instanceof Workspace) {
2366 if (mWorkspace.isInOverviewMode()) {
2367 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002368 }
2369 return;
2370 }
2371
2372 if (v instanceof CellLayout) {
2373 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002374 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002375 }
2376 }
2377
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002379 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002380 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002382 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002383 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002384 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002385 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002386 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002387 }
2388 }
2389
Michael Jurka0e260592010-06-30 17:07:39 -07002390 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002391 return false;
2392 }
2393
Michael Jurkaaf442092010-06-10 17:01:57 -07002394 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002395 * Event handler for the search button
2396 *
2397 * @param v The view that was clicked.
2398 */
2399 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002400 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2401
Amith Yamasania135ba82011-08-09 17:42:01 -07002402 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002403 }
2404
2405 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002406 * Event handler for the voice button
2407 *
2408 * @param v The view that was clicked.
2409 */
2410 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002411 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002412
Bjorn Bringertc459e522013-06-07 19:36:01 +01002413 startVoice();
2414 }
2415
2416 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002417 try {
2418 final SearchManager searchManager =
2419 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2420 ComponentName activityName = searchManager.getGlobalSearchActivity();
2421 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002422 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002423 if (activityName != null) {
2424 intent.setPackage(activityName.getPackageName());
2425 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002426 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002427 } catch (ActivityNotFoundException e) {
2428 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002429 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002430 startActivitySafely(null, intent, "onClickVoiceButton");
2431 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002432 }
2433
2434 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002435 * Event handler for the "grid" button that appears on the home screen, which
2436 * enters all apps mode.
2437 *
2438 * @param v The view that was clicked.
2439 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002440 protected void onClickAllAppsButton(View v) {
2441 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2442 if (isAllAppsVisible()) {
2443 showWorkspace(true);
2444 } else {
2445 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2446 }
2447 }
2448
2449 /**
2450 * Event handler for an app shortcut click.
2451 *
2452 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2453 */
2454 protected void onClickAppShortcut(View v) {
2455 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2456 Object tag = v.getTag();
2457 if (!(tag instanceof ShortcutInfo)) {
2458 throw new IllegalArgumentException("Input must be a Shortcut");
2459 }
2460
2461 // Open shortcut
2462 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2463 final Intent intent = shortcut.intent;
2464
2465 // Check for special shortcuts
2466 if (intent.getComponent() != null) {
2467 final String shortcutClass = intent.getComponent().getClassName();
2468
2469 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2470 MemoryDumpActivity.startDump(this);
2471 return;
2472 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2473 toggleShowWeightWatcher();
2474 return;
2475 }
2476 }
2477
2478 // Start activities
2479 int[] pos = new int[2];
2480 v.getLocationOnScreen(pos);
2481 intent.setSourceBounds(new Rect(pos[0], pos[1],
2482 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2483
2484 boolean success = startActivitySafely(v, intent, tag);
2485
2486 mStats.recordLaunch(intent, shortcut);
2487
2488 if (success && v instanceof BubbleTextView) {
2489 mWaitingForResume = (BubbleTextView) v;
2490 mWaitingForResume.setStayPressed(true);
2491 }
2492 }
2493
2494 /**
2495 * Event handler for a folder icon click.
2496 *
2497 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2498 */
2499 protected void onClickFolderIcon(View v) {
2500 if (LOGD) Log.d(TAG, "onClickFolder");
2501 if (!(v instanceof FolderIcon)){
2502 throw new IllegalArgumentException("Input must be a FolderIcon");
2503 }
2504
2505 FolderIcon folderIcon = (FolderIcon) v;
2506 final FolderInfo info = folderIcon.getFolderInfo();
2507 Folder openFolder = mWorkspace.getFolderForTag(info);
2508
2509 // If the folder info reports that the associated folder is open, then verify that
2510 // it is actually opened. There have been a few instances where this gets out of sync.
2511 if (info.opened && openFolder == null) {
2512 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2513 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2514 info.opened = false;
2515 }
2516
2517 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2518 // Close any open folder
2519 closeFolder();
2520 // Open the requested folder
2521 openFolder(folderIcon);
2522 } else {
2523 // Find the open folder...
2524 int folderScreen;
2525 if (openFolder != null) {
2526 folderScreen = mWorkspace.getPageForView(openFolder);
2527 // .. and close it
2528 closeFolder(openFolder);
2529 if (folderScreen != mWorkspace.getCurrentPage()) {
2530 // Close any folder open on the current screen
2531 closeFolder();
2532 // Pull the folder onto this screen
2533 openFolder(folderIcon);
2534 }
2535 }
2536 }
Michael Jurka5130e402011-10-13 04:55:35 -07002537 }
2538
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002539 /**
2540 * Event handler for the (Add) Widgets button that appears after a long press
2541 * on the home screen.
2542 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002543 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002544 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002545 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2546 }
2547
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002548 /**
2549 * Event handler for the wallpaper picker button that appears after a long press
2550 * on the home screen.
2551 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002552 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002553 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2554 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2555 pickWallpaper.setComponent(getWallpaperPickerComponent());
2556 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2557 }
2558
2559 /**
2560 * Event handler for a click on the settings button that appears after a long press
2561 * on the home screen.
2562 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002563 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002564 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2565 }
2566
Michael Jurka5130e402011-10-13 04:55:35 -07002567 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002568 // Provide the same haptic feedback that the system offers for virtual keys.
2569 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002570 }
2571
Adam Cohen61f560d2013-09-30 15:58:20 -07002572 public void performHapticFeedbackOnTouchDown(View v) {
2573 // Provide the same haptic feedback that the system offers for virtual keys.
2574 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2575 }
2576
2577 public View.OnTouchListener getHapticFeedbackTouchListener() {
2578 if (mHapticFeedbackTouchListener == null) {
2579 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2580 @Override
2581 public boolean onTouch(View v, MotionEvent event) {
2582 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2583 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2584 }
2585 return false;
2586 }
2587 };
2588 }
2589 return mHapticFeedbackTouchListener;
2590 }
2591
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002592 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002593 if (!DISABLE_MARKET_BUTTON) {
2594 if (mAppMarketIntent != null) {
2595 startActivitySafely(v, mAppMarketIntent, "app market");
2596 } else {
2597 Log.e(TAG, "Invalid app market intent.");
2598 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002599 }
2600 }
2601
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002602 /**
2603 * Called when the user stops interacting with the launcher.
2604 * This implies that the user is now on the homescreen and is not doing housekeeping.
2605 */
2606 protected void onInteractionEnd() {}
2607
2608 /**
2609 * Called when the user starts interacting with the launcher.
2610 * The possible interactions are:
2611 * - open all apps
2612 * - reorder an app shortcut, or a widget
2613 * - open the overview mode.
2614 * This is a good time to stop doing things that only make sense
2615 * when the user is on the homescreen and not doing housekeeping.
2616 */
2617 protected void onInteractionBegin() {}
2618
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002619 void startApplicationDetailsActivity(ComponentName componentName) {
2620 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002621 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2622 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002623 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2624 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002625 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002626 }
2627
Michael Jurka1e2f4652013-07-08 18:03:46 -07002628 // returns true if the activity was started
2629 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002630 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002631 // System applications cannot be installed. For now, show a toast explaining that.
2632 // We may give them the option of disabling apps this way.
2633 int messageId = R.string.uninstall_system_app_text;
2634 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002635 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002636 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002637 String packageName = componentName.getPackageName();
2638 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002639 Intent intent = new Intent(
2640 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002641 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2642 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002643 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002644 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002645 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002646 }
2647
Michael Jurka86a720e2012-05-09 11:23:23 -07002648 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002649 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002650
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002651 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002652 // Only launch using the new animation if the shortcut has not opted out (this is a
2653 // private contract between launcher and may be ignored in the future).
2654 boolean useLaunchAnimation = (v != null) &&
2655 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2656 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002657 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2658 v.getMeasuredWidth(), v.getMeasuredHeight());
2659
2660 startActivity(intent, opts.toBundle());
2661 } else {
2662 startActivity(intent);
2663 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002664 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002665 } catch (SecurityException e) {
2666 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002667 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002668 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002669 "or use the exported attribute for this activity. "
2670 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002671 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002672 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002673 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002674
Michael Jurka86a720e2012-05-09 11:23:23 -07002675 boolean startActivitySafely(View v, Intent intent, Object tag) {
2676 boolean success = false;
2677 try {
2678 success = startActivity(v, intent, tag);
2679 } catch (ActivityNotFoundException e) {
2680 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2681 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2682 }
2683 return success;
2684 }
2685
Adam Cohen268c4752012-06-06 17:47:33 -07002686 /**
2687 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2688 * in the DragLayer in the exact absolute location of the original FolderIcon.
2689 */
2690 private void copyFolderIconToImage(FolderIcon fi) {
2691 final int width = fi.getMeasuredWidth();
2692 final int height = fi.getMeasuredHeight();
2693
2694 // Lazy load ImageView, Bitmap and Canvas
2695 if (mFolderIconImageView == null) {
2696 mFolderIconImageView = new ImageView(this);
2697 }
2698 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2699 mFolderIconBitmap.getHeight() != height) {
2700 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2701 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2702 }
2703
2704 DragLayer.LayoutParams lp;
2705 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2706 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2707 } else {
2708 lp = new DragLayer.LayoutParams(width, height);
2709 }
2710
Adam Cohen307fe232012-08-16 17:55:58 -07002711 // The layout from which the folder is being opened may be scaled, adjust the starting
2712 // view size by this scale factor.
2713 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002714 lp.customPosition = true;
2715 lp.x = mRectForFolderAnimation.left;
2716 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002717 lp.width = (int) (scale * width);
2718 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002719
2720 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2721 fi.draw(mFolderIconCanvas);
2722 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002723 if (fi.getFolder() != null) {
2724 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2725 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002726 }
Adam Cohen268c4752012-06-06 17:47:33 -07002727 // Just in case this image view is still in the drag layer from a previous animation,
2728 // we remove it and re-add it.
2729 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2730 mDragLayer.removeView(mFolderIconImageView);
2731 }
2732 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002733 if (fi.getFolder() != null) {
2734 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002735 }
Adam Cohen268c4752012-06-06 17:47:33 -07002736 }
2737
Adam Cohen2801caf2011-05-13 20:57:39 -07002738 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002739 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002740 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2741 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2742 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2743
Adam Cohenc51934b2011-07-26 21:07:43 -07002744 FolderInfo info = (FolderInfo) fi.getTag();
2745 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2746 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002747 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2748 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002749 }
2750
Adam Cohen268c4752012-06-06 17:47:33 -07002751 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2752 copyFolderIconToImage(fi);
2753 fi.setVisibility(View.INVISIBLE);
2754
Michael Jurka2ecf9952012-06-18 12:52:28 -07002755 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002756 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002757 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2758 oa.start();
2759 }
2760
Adam Cohen268c4752012-06-06 17:47:33 -07002761 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002762 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002763 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2764 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2765 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2766
Adam Cohen268c4752012-06-06 17:47:33 -07002767 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002768
Adam Cohen268c4752012-06-06 17:47:33 -07002769 // We remove and re-draw the FolderIcon in-case it has changed
2770 mDragLayer.removeView(mFolderIconImageView);
2771 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002772 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002773 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002774 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002775 oa.addListener(new AnimatorListenerAdapter() {
2776 @Override
2777 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002778 if (cl != null) {
2779 cl.clearFolderLeaveBehind();
2780 // Remove the ImageView copy of the FolderIcon and make the original visible.
2781 mDragLayer.removeView(mFolderIconImageView);
2782 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002783 }
2784 }
2785 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002786 oa.start();
2787 }
2788
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002790 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002791 * is animated relative to the specified View. If the View is null, no animation
2792 * is played.
2793 *
2794 * @param folderInfo The FolderInfo describing the folder to open.
2795 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002796 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002797 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002798 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002799
Adam Cohena9cf38f2011-05-02 15:36:58 -07002800 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002801
Adam Cohen4554ee12011-08-03 16:13:21 -07002802 // Just verify that the folder hasn't already been added to the DragLayer.
2803 // There was a one-off crash where the folder had a parent already.
2804 if (folder.getParent() == null) {
2805 mDragLayer.addView(folder);
2806 mDragController.addDropTarget((DropTarget) folder);
2807 } else {
2808 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2809 folder.getParent() + ").");
2810 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002811 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002812 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002813
2814 // Notify the accessibility manager that this folder "window" has appeared and occluded
2815 // the workspace items
2816 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2817 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002818 }
2819
2820 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002821 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002822 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002823 if (folder.isEditingName()) {
2824 folder.dismissEditingName();
2825 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002826 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002827
2828 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002829 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002830 }
2831 }
2832
2833 void closeFolder(Folder folder) {
2834 folder.getInfo().opened = false;
2835
2836 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2837 if (parent != null) {
2838 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2839 shrinkAndFadeInFolderIcon(fi);
2840 }
2841 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002842
2843 // Notify the accessibility manager that this folder "window" has disappeard and no
2844 // longer occludeds the workspace items
2845 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002846 }
2847
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002848 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002849 if (!isDraggingEnabled()) return false;
2850 if (isWorkspaceLocked()) return false;
2851 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002852
Adam Cohen1697b792013-09-17 19:08:21 -07002853 if (v instanceof Workspace) {
2854 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002855 if (mWorkspace.enterOverviewMode()) {
2856 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2857 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2858 return true;
2859 } else {
2860 return false;
2861 }
Adam Cohen1697b792013-09-17 19:08:21 -07002862 }
Adam Cohen1697b792013-09-17 19:08:21 -07002863 }
2864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002865 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002866 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867 }
2868
Michael Jurka0280c3b2010-09-17 15:00:07 -07002869 resetAddInfo();
2870 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002871 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002872 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002873 return true;
2874 }
2875
Winson Chung3d503fb2011-07-13 17:25:49 -07002876 // The hotseat touch handling does not go through Workspace, and we always allow long press
2877 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002878 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002879 final boolean inHotseat = isHotseatLayout(v);
2880 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002881 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002882 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002883 // User long pressed on empty space
2884 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2885 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2886 // Disabling reordering until we sort out some issues.
2887 if (mWorkspace.isInOverviewMode()) {
2888 mWorkspace.startReordering(v);
2889 } else {
2890 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002891 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002892 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002893 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2894 mHotseat.getOrderInHotseat(
2895 longClickCellInfo.cellX,
2896 longClickCellInfo.cellY));
2897 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002898 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002899 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002900 }
2901 }
2902 }
2903 return true;
2904 }
2905
Winson Chung3d503fb2011-07-13 17:25:49 -07002906 boolean isHotseatLayout(View layout) {
2907 return mHotseat != null && layout != null &&
2908 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2909 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002910
Winson Chung3d503fb2011-07-13 17:25:49 -07002911 /**
2912 * Returns the CellLayout of the specified container at the specified screen.
2913 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002914 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002915 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2916 if (mHotseat != null) {
2917 return mHotseat.getLayout();
2918 } else {
2919 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002920 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002921 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002922 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002923 }
2924 }
2925
Daniel Sandler843e8602010-06-07 14:59:01 -04002926 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002927 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002928 }
2929
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002930 /**
2931 * Helper method for the cameraZoomIn/cameraZoomOut animations
2932 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002933 * @param scaleFactor The scale factor used for the zoom
2934 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002935 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002936 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002937 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002938 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002939
Craig Mautner360310b2012-10-26 15:13:08 -07002940 private void setWorkspaceBackground(boolean workspace) {
2941 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002942 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002943 }
2944
Selim Cinek3a8a8f72014-01-16 10:38:38 -08002945 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002946 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2947 int curflags = getWindow().getAttributes().flags
2948 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2949 if (wpflags != curflags) {
2950 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2951 }
Craig Mautner360310b2012-10-26 15:13:08 -07002952 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002953 }
2954
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002955 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2956 if (v instanceof LauncherTransitionable) {
2957 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2958 }
2959 }
2960
Michael Jurkabed61d22012-02-14 22:51:29 -08002961 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2962 if (v instanceof LauncherTransitionable) {
2963 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2964 }
Winson Chung70442722012-02-10 15:43:22 -08002965
2966 // Update the workspace transition step as well
2967 dispatchOnLauncherTransitionStep(v, 0f);
2968 }
2969
2970 private void dispatchOnLauncherTransitionStep(View v, float t) {
2971 if (v instanceof LauncherTransitionable) {
2972 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2973 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002974 }
2975
2976 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2977 if (v instanceof LauncherTransitionable) {
2978 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2979 }
Winson Chung70442722012-02-10 15:43:22 -08002980
2981 // Update the workspace transition step as well
2982 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002983 }
2984
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002985 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002986 * Things to test when changing the following seven functions.
2987 * - Home from workspace
2988 * - from center screen
2989 * - from other screens
2990 * - Home from all apps
2991 * - from center screen
2992 * - from other screens
2993 * - Back from all apps
2994 * - from center screen
2995 * - from other screens
2996 * - Launch app from workspace and quit
2997 * - with back
2998 * - with home
2999 * - Launch app from all apps and quit
3000 * - with back
3001 * - with home
3002 * - Go to a screen that's not the default, then all
3003 * apps, and launch and app, and go back
3004 * - with back
3005 * -with home
3006 * - On workspace, long press power and go back
3007 * - with back
3008 * - with home
3009 * - On all apps, long press power and go back
3010 * - with back
3011 * - with home
3012 * - On workspace, power off
3013 * - On all apps, power off
3014 * - Launch an app and turn off the screen while in that app
3015 * - Go back with home key
3016 * - Go back with back key TODO: make this not go to workspace
3017 * - From all apps
3018 * - From workspace
3019 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3020 * - From all apps
3021 * - From the center workspace
3022 * - From another workspace
3023 */
3024
3025 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003026 * Zoom the camera out from the workspace to reveal 'toView'.
3027 * Assumes that the view to show is anchored at either the very top or very bottom
3028 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003029 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003030 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003031 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3032 showAppsCustomizeHelper(animated, springLoaded, contentType);
3033 }
3034 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3035 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003036 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003037 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003038 mStateAnimation.cancel();
3039 mStateAnimation = null;
3040 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003041 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003042
Winson Chungf0ea4d32011-06-06 14:27:16 -07003043 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3044 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
3045 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003046 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003047 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003048 final int startDelay =
3049 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003050
Michael Jurkab3e22d92011-10-31 15:58:33 -07003051 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003052
Michael Jurkad74c9842011-07-10 12:44:21 -07003053 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003054 Animator workspaceAnim =
3055 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003056 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003057 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3058 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003059 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3060 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003061
3062 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003063 toView.setScaleX(scale);
3064 toView.setScaleY(scale);
3065 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3066 scaleAnim.
3067 scaleX(1f).scaleY(1f).
3068 setDuration(duration).
3069 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003070
Winson Chungf0ea4d32011-06-06 14:27:16 -07003071 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003072 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003073 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003074 .ofFloat(toView, "alpha", 0f, 1f)
3075 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003076 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003077 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3078 @Override
3079 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003080 if (animation == null) {
3081 throw new RuntimeException("animation is null");
3082 }
Winson Chung70442722012-02-10 15:43:22 -08003083 float t = (Float) animation.getAnimatedValue();
3084 dispatchOnLauncherTransitionStep(fromView, t);
3085 dispatchOnLauncherTransitionStep(toView, t);
3086 }
3087 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003088
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003089 // toView should appear right at the end of the workspace shrink
3090 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003091 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003092 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003093 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003094
3095 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003096 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003097 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003098 // Prepare the position
3099 toView.setTranslationX(0.0f);
3100 toView.setTranslationY(0.0f);
3101 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003102 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003103 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003104 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003105 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003106 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3107 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003108
Winson Chungc7d2b602012-05-16 17:02:20 -07003109 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003110 if (mSearchDropTargetBar != null) {
3111 mSearchDropTargetBar.hideSearchBar(false);
3112 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003113 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003114 });
3115
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003116 if (workspaceAnim != null) {
3117 mStateAnimation.play(workspaceAnim);
3118 }
Michael Jurka1899a362011-11-03 13:50:45 -07003119
3120 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003121
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003122 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3123 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003124
3125 // If any of the objects being animated haven't been measured/laid out
3126 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003127 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003128 (mWorkspace.getMeasuredWidth() == 0) ||
3129 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003130 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003131 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003132
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003133 final AnimatorSet stateAnimation = mStateAnimation;
3134 final Runnable startAnimRunnable = new Runnable() {
3135 public void run() {
3136 // Check that mStateAnimation hasn't changed while
3137 // we waited for a layout/draw pass
3138 if (mStateAnimation != stateAnimation)
3139 return;
3140 setPivotsForZoom(toView, scale);
3141 dispatchOnLauncherTransitionStart(fromView, animated, false);
3142 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003143 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003144 }
3145 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003146 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003147 final ViewTreeObserver observer = toView.getViewTreeObserver();
3148 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3149 public void onGlobalLayout() {
3150 startAnimRunnable.run();
3151 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3152 }
3153 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003154 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003155 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003156 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003157 } else {
3158 toView.setTranslationX(0.0f);
3159 toView.setTranslationY(0.0f);
3160 toView.setScaleX(1.0f);
3161 toView.setScaleY(1.0f);
3162 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003163 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003164
Daniel Sandlere4f98912013-06-25 15:13:26 -04003165 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003166 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003167 if (mSearchDropTargetBar != null) {
3168 mSearchDropTargetBar.hideSearchBar(false);
3169 }
Michael Jurkaabded662011-03-04 12:06:57 -08003170 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003171 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003172 dispatchOnLauncherTransitionStart(fromView, animated, false);
3173 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003174 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003175 dispatchOnLauncherTransitionStart(toView, animated, false);
3176 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003177 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003178 }
3179
3180 /**
3181 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003182 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003183 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003184 */
Adam Cohened307df2013-10-02 09:37:31 -07003185 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003186 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003187
Michael Jurkab3e22d92011-10-31 15:58:33 -07003188 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003189 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003190 mStateAnimation.cancel();
3191 mStateAnimation = null;
3192 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003193 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003194
Winson Chungf0ea4d32011-06-06 14:27:16 -07003195 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003196 final int fadeOutDuration =
3197 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003198 final float scaleFactor = (float)
3199 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3200 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003201 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003202 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003203 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003204 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3205 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003206 toState, animated, stagger, -1);
3207 } else if (toState == Workspace.State.SPRING_LOADED ||
3208 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003209 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003210 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003211 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003212
Michael Jurkab3e22d92011-10-31 15:58:33 -07003213 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003214 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003215 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003216 final LauncherViewPropertyAnimator scaleAnim =
3217 new LauncherViewPropertyAnimator(fromView);
3218 scaleAnim.
3219 scaleX(scaleFactor).scaleY(scaleFactor).
3220 setDuration(duration).
3221 setInterpolator(new Workspace.ZoomInInterpolator());
3222
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003223 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003224 .ofFloat(fromView, "alpha", 1f, 0f)
3225 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003226 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003227 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3228 @Override
3229 public void onAnimationUpdate(ValueAnimator animation) {
3230 float t = 1f - (Float) animation.getAnimatedValue();
3231 dispatchOnLauncherTransitionStep(fromView, t);
3232 dispatchOnLauncherTransitionStep(toView, t);
3233 }
3234 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003235
Michael Jurka2ecf9952012-06-18 12:52:28 -07003236 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003237
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003238 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3239 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003240 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003241
3242 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003243 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003244 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003245 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003246 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3247 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003248 if (onCompleteRunnable != null) {
3249 onCompleteRunnable.run();
3250 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003251 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003252 }
3253 });
3254
Winson Chungf0ea4d32011-06-06 14:27:16 -07003255 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003256 if (workspaceAnim != null) {
3257 mStateAnimation.play(workspaceAnim);
3258 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003259 dispatchOnLauncherTransitionStart(fromView, animated, true);
3260 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003261 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003262 } else {
3263 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003264 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003265 dispatchOnLauncherTransitionStart(fromView, animated, true);
3266 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003267 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003268 dispatchOnLauncherTransitionStart(toView, animated, true);
3269 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003270 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003271 }
3272
Michael Jurkae326f182011-11-21 14:05:46 -08003273 @Override
3274 public void onTrimMemory(int level) {
3275 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003276 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003277 mAppsCustomizeTabHost.onTrimMemory();
3278 }
3279 }
3280
Adam Cohened307df2013-10-02 09:37:31 -07003281 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003282 showWorkspace(animated, null);
3283 }
3284
Adam Cohened307df2013-10-02 09:37:31 -07003285 protected void showWorkspace() {
3286 showWorkspace(true);
3287 }
3288
Adam Cohened66b2b2012-01-23 17:28:51 -08003289 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003290 if (mWorkspace.isInOverviewMode()) {
3291 mWorkspace.exitOverviewMode(animated);
3292 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003293 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003294 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003295 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003296 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003297
Winson Chungc7d2b602012-05-16 17:02:20 -07003298 // Show the search bar (only animate if we were showing the drop target bar in spring
3299 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003300 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003301 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003302 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003303
Michael Jurkab3e22d92011-10-31 15:58:33 -07003304 // Set focus to the AppsCustomize button
3305 if (mAllAppsButton != null) {
3306 mAllAppsButton.requestFocus();
3307 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003308 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003309
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003310 // Change the state *after* we've called all the transition code
3311 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003312
Winson Chung5fb63472011-02-02 17:03:37 -08003313 // Resume the auto-advance of widgets
3314 mUserPresent = true;
3315 updateRunning();
3316
alanv1d4fde62012-10-17 13:15:47 -07003317 // Send an accessibility event to announce the context change
3318 getWindow().getDecorView()
3319 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003320
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003321 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003322 }
3323
Adam Cohened307df2013-10-02 09:37:31 -07003324 void showOverviewMode(boolean animated) {
3325 mWorkspace.setVisibility(View.VISIBLE);
3326 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3327 mState = State.WORKSPACE;
3328 onWorkspaceShown(animated);
3329 }
3330
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003331 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003332 }
3333
Winson Chung82963d52013-10-09 11:20:57 -07003334 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3335 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003336 if (mState != State.WORKSPACE) return;
3337
Winson Chung82963d52013-10-09 11:20:57 -07003338 if (resetPageToZero) {
3339 mAppsCustomizeTabHost.reset();
3340 }
Winson Chungc58497e2013-09-03 17:48:37 -07003341 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003342 mAppsCustomizeTabHost.requestFocus();
3343
Michael Jurkab3e22d92011-10-31 15:58:33 -07003344 // Change the state *after* we've called all the transition code
3345 mState = State.APPS_CUSTOMIZE;
3346
3347 // Pause the auto-advance of widgets until we are out of AllApps
3348 mUserPresent = false;
3349 updateRunning();
3350 closeFolder();
3351
3352 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003353 getWindow().getDecorView()
3354 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003355 }
3356
Adam Cohen7777d962011-08-18 18:58:38 -07003357 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003358 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003359 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003360 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003361 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003362 }
Adam Cohen7777d962011-08-18 18:58:38 -07003363
Adam Cohenad4e15c2013-10-17 16:21:35 -07003364 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003365 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003366 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3367
Winson Chunge7a03942011-08-05 15:05:12 -07003368 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003369 @Override
3370 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003371 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003372 // Before we show workspace, hide all apps again because
3373 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3374 // clean up our state transition functions
3375 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003376 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003377 } else {
3378 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003379 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003380 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003381 }, delay);
3382
Winson Chung557d6ed2011-07-08 15:34:52 -07003383 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003384
Michael Jurkad3ef3062010-11-23 16:23:58 -08003385 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003386 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003387 final boolean animated = true;
3388 final boolean springLoaded = true;
3389 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003390 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003391 }
3392 // Otherwise, we are not in spring loaded mode, so don't do anything.
3393 }
3394
Michael Jurkab3e22d92011-10-31 15:58:33 -07003395 void lockAllApps() {
3396 // TODO
3397 }
3398
3399 void unlockAllApps() {
3400 // TODO
3401 }
3402
Winson Chungf0ea4d32011-06-06 14:27:16 -07003403 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003404 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003405 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003406 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003407 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003408 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003409 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003410 int duration = 0;
3411 if (mSearchDropTargetBar != null) {
3412 duration = mSearchDropTargetBar.getTransitionInDuration();
3413 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003414 mHotseat.animate().alpha(1f).setDuration(duration);
3415 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003416 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003417 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003418 }
3419 }
3420 }
3421
3422 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003423 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003424 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003425 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003426 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003427 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003428 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003429 int duration = 0;
3430 if (mSearchDropTargetBar != null) {
3431 duration = mSearchDropTargetBar.getTransitionOutDuration();
3432 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003433 mHotseat.animate().alpha(0f).setDuration(duration);
3434 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003435 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003436 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003437 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003438 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003439 }
3440
Patrick Dubroy5f445422011-02-18 14:35:21 -08003441 /**
3442 * Add an item from all apps or customize onto the given workspace screen.
3443 * If layout is null, add to the current screen.
3444 */
3445 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003446 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003447 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003448 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003449 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003450
Winson Chungdff8ebb2011-09-08 17:25:31 -07003451 /** Maps the current orientation to an index for referencing orientation correct global icons */
3452 private int getCurrentOrientationIndexForGlobalIcons() {
3453 // default - 0, landscape - 1
3454 switch (getResources().getConfiguration().orientation) {
3455 case Configuration.ORIENTATION_LANDSCAPE:
3456 return 1;
3457 default:
3458 return 0;
3459 }
3460 }
3461
Michael Jurkaae65ee32012-04-30 11:45:54 -07003462 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003463 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003464 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003465 // Look for the toolbar icon specified in the activity meta-data
3466 Bundle metaData = packageManager.getActivityInfo(
3467 activityName, PackageManager.GET_META_DATA).metaData;
3468 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003469 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003470 if (iconResId != 0) {
3471 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003472 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003473 }
3474 }
3475 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003476 // This can happen if the activity defines an invalid drawable
3477 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3478 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003479 } catch (Resources.NotFoundException nfe) {
3480 // This can happen if the activity defines an invalid drawable
3481 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3482 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003483 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003484 return null;
3485 }
3486
3487 // if successful in getting icon, return it; otherwise, set button to use default drawable
3488 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003489 int buttonId, ComponentName activityName, int fallbackDrawableId,
3490 String toolbarResourceName) {
3491 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003492 Resources r = getResources();
3493 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3494 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003495
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003496 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003497 // If we were unable to find the icon via the meta-data, use a generic one
3498 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003499 toolbarIcon = r.getDrawable(fallbackDrawableId);
3500 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003501 if (button != null) {
3502 button.setCompoundDrawables(toolbarIcon, null, null, null);
3503 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003504 return null;
3505 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003506 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003507 if (button != null) {
3508 button.setCompoundDrawables(toolbarIcon, null, null, null);
3509 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003510 return toolbarIcon.getConstantState();
3511 }
3512 }
3513
3514 // if successful in getting icon, return it; otherwise, set button to use default drawable
3515 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003516 int buttonId, ComponentName activityName, int fallbackDrawableId,
3517 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003518 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003519 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003520
Michael Jurka19e0fc52011-07-22 18:00:21 -07003521 if (button != null) {
3522 // If we were unable to find the icon via the meta-data, use a
3523 // generic one
3524 if (toolbarIcon == null) {
3525 button.setImageResource(fallbackDrawableId);
3526 } else {
3527 button.setImageDrawable(toolbarIcon);
3528 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003529 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003530
3531 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3532
Michael Jurka0423dcf2010-10-05 14:56:18 -07003533 }
3534
Winson Chung1b884092012-06-08 17:13:02 -07003535 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003536 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003537 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003538 }
3539
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003540 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003541 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003542 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003543 }
3544
Winson Chungbb185bd2011-11-21 12:31:42 -08003545 private void invalidatePressedFocusedStates(View container, View button) {
3546 if (container instanceof HolographicLinearLayout) {
3547 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3548 layout.invalidatePressedFocusedStates();
3549 } else if (button instanceof HolographicImageView) {
3550 HolographicImageView view = (HolographicImageView) button;
3551 view.invalidatePressedFocusedStates();
3552 }
3553 }
3554
Cristina Stancu476493b2013-08-07 17:20:14 +01003555 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003556 if (mQsb == null) {
3557 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3558 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003559 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003560 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003561 }
3562
3563 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003564 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003565 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003566 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003567 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003568
Winson Chung1cad91e2011-05-25 17:41:01 -07003569 final SearchManager searchManager =
3570 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3571 ComponentName activityName = searchManager.getGlobalSearchActivity();
3572 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003573 int coi = getCurrentOrientationIndexForGlobalIcons();
3574 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003575 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3576 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3577 if (sGlobalSearchIcon[coi] == null) {
3578 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3579 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3580 TOOLBAR_ICON_METADATA_NAME);
3581 }
3582
Winson Chungc51db6a2011-10-05 11:44:49 -07003583 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3584 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003585 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003586 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003587 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003588 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003589 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3590 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003591 if (searchButton != null) searchButton.setVisibility(View.GONE);
3592 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003593 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003594 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003595 }
3596 }
3597
Cristina Stancu476493b2013-08-07 17:20:14 +01003598 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003599 final View searchButtonContainer = findViewById(R.id.search_button_container);
3600 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003601 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003602 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003603 }
3604
Cristina Stancu476493b2013-08-07 17:20:14 +01003605 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003606 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003607 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003608
Winson Chungc51db6a2011-10-05 11:44:49 -07003609 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003610 final SearchManager searchManager =
3611 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3612 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3613
3614 ComponentName activityName = null;
3615 if (globalSearchActivity != null) {
3616 // Check if the global search activity handles voice search
3617 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3618 intent.setPackage(globalSearchActivity.getPackageName());
3619 activityName = intent.resolveActivity(getPackageManager());
3620 }
3621
3622 if (activityName == null) {
3623 // Fallback: check if an activity other than the global search activity
3624 // resolves this
3625 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3626 activityName = intent.resolveActivity(getPackageManager());
3627 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003628 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003629 int coi = getCurrentOrientationIndexForGlobalIcons();
3630 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003631 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3632 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3633 if (sVoiceSearchIcon[coi] == null) {
3634 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3635 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3636 TOOLBAR_ICON_METADATA_NAME);
3637 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003638 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003639 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003640 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003641 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003642 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003643 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003644 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003645 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003646 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003647 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003648 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003649 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003650
Cristina Stancu476493b2013-08-07 17:20:14 +01003651 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003652 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3653 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003654 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003655 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003656 }
3657
Winson Chung5841efa2013-09-30 18:06:44 -07003658 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003659 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3660 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003661 boolean visible = !forceDisableVoiceButtonProxy &&
3662 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003663 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003664 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003665 }
3666 }
Winson Chung5841efa2013-09-30 18:06:44 -07003667
3668 /**
3669 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3670 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3671 */
3672 public void disableVoiceButtonProxy(boolean disabled) {
3673 updateVoiceButtonProxyVisible(disabled);
3674 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003675 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003676 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003677 */
3678 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003679 if (!DISABLE_MARKET_BUTTON) {
3680 final View marketButton = findViewById(R.id.market_button);
3681 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3682 // Find the app market activity by resolving an intent.
3683 // (If multiple app markets are installed, it will return the ResolverActivity.)
3684 ComponentName activityName = intent.resolveActivity(getPackageManager());
3685 if (activityName != null) {
3686 int coi = getCurrentOrientationIndexForGlobalIcons();
3687 mAppMarketIntent = intent;
3688 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3689 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3690 TOOLBAR_ICON_METADATA_NAME);
3691 marketButton.setVisibility(View.VISIBLE);
3692 } else {
3693 // We should hide and disable the view so that we don't try and restore the visibility
3694 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3695 marketButton.setVisibility(View.GONE);
3696 marketButton.setEnabled(false);
3697 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003698 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003699 }
3700
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003701 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003702 if (!DISABLE_MARKET_BUTTON) {
3703 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3704 Resources r = getResources();
3705 Drawable marketIconDrawable = d.newDrawable(r);
3706 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3707 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3708 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003709
Winson Chungd64a6662013-09-30 11:06:59 -07003710 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3711 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003712 }
3713
Michael Jurkad7c28052012-04-27 15:43:36 -07003714 @Override
3715 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003716 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003717 final List<CharSequence> text = event.getText();
3718 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003719 // Populate event with a fake title based on the current state.
3720 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003721 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003722 } else {
3723 text.add(getString(R.string.all_apps_home_button_label));
3724 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003725 return result;
3726 }
3727
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003728 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003729 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003730 */
3731 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3732 @Override
3733 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003734 closeSystemDialogs();
3735 }
3736 }
3737
3738 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003739 * Receives notifications whenever the appwidgets are reset.
3740 */
3741 private class AppWidgetResetObserver extends ContentObserver {
3742 public AppWidgetResetObserver() {
3743 super(new Handler());
3744 }
3745
3746 @Override
3747 public void onChange(boolean selfChange) {
3748 onAppWidgetReset();
3749 }
3750 }
3751
3752 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003753 * If the activity is currently paused, signal that we need to run the passed Runnable
3754 * in onResume.
3755 *
3756 * This needs to be called from incoming places where resources might have been loaded
3757 * while we are paused. That is becaues the Configuration might be wrong
3758 * when we're not running, and if it comes back to what it was when we
3759 * were paused, we are not restarted.
3760 *
3761 * Implementation of the method from LauncherModel.Callbacks.
3762 *
3763 * @return true if we are currently paused. The caller might be able to
3764 * skip some work in that case since we will come back again.
3765 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003766 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003767 if (mPaused) {
3768 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003769 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003770 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003771 }
3772 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003773 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003774 return true;
3775 } else {
3776 return false;
3777 }
3778 }
3779
Michael Jurkac402cd92013-05-20 15:49:32 +02003780 private boolean waitUntilResume(Runnable run) {
3781 return waitUntilResume(run, false);
3782 }
3783
Michael Jurka1e2f4652013-07-08 18:03:46 -07003784 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003785 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003786 }
3787
Michael Jurka7607c2f2013-04-03 14:33:19 -07003788 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003789 * If the activity is currently paused, signal that we need to re-run the loader
3790 * in onResume.
3791 *
3792 * This needs to be called from incoming places where resources might have been loaded
3793 * while we are paused. That is becaues the Configuration might be wrong
3794 * when we're not running, and if it comes back to what it was when we
3795 * were paused, we are not restarted.
3796 *
3797 * Implementation of the method from LauncherModel.Callbacks.
3798 *
3799 * @return true if we are currently paused. The caller might be able to
3800 * skip some work in that case since we will come back again.
3801 */
3802 public boolean setLoadOnResume() {
3803 if (mPaused) {
3804 Log.i(TAG, "setLoadOnResume");
3805 mOnResumeNeedsLoad = true;
3806 return true;
3807 } else {
3808 return false;
3809 }
3810 }
3811
3812 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003813 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003814 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003815 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003816 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003817 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003818 } else {
3819 return SCREEN_COUNT / 2;
3820 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003821 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003822
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 /**
3824 * Refreshes the shortcuts shown on the workspace.
3825 *
3826 * Implementation of the method from LauncherModel.Callbacks.
3827 */
3828 public void startBinding() {
Adam Cohen517a7f52014-03-01 12:12:59 -08003829 mWorkspaceLoading = true;
3830
Michael Jurka7607c2f2013-04-03 14:33:19 -07003831 // If we're starting binding all over again, clear any bind calls we'd postponed in
3832 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3833 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003834 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003835
Winson Chungd64d1762013-08-20 14:37:16 -07003836 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003837 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003838 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003839
Michael Jurka05bf644e2011-11-30 20:28:53 -08003840 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003841 if (mHotseat != null) {
3842 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003843 }
3844 }
3845
Adam Cohendcd297f2013-06-18 13:13:40 -07003846 @Override
3847 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003848 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003849
Adam Cohen5084cba2013-09-03 12:01:16 -07003850 // If there are no screens, we need to have an empty screen
3851 if (orderedScreenIds.size() == 0) {
3852 mWorkspace.addExtraEmptyScreen();
3853 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003854
3855 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003856 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003857 if (hasCustomContentToLeft()) {
3858 mWorkspace.createCustomContentContainer();
3859 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003860 }
Winson Chung64359a52013-07-08 17:17:08 -07003861 }
3862
3863 @Override
3864 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003865 // Log to disk
3866 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3867 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3868 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003869 int count = orderedScreenIds.size();
3870 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003871 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003872 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003873 }
3874
Adam Cohen39a06042013-07-19 14:30:12 -07003875 private boolean shouldShowWeightWatcher() {
3876 String spKey = LauncherAppState.getSharedPreferencesKey();
3877 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003878 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003879
3880 return show;
3881 }
3882
3883 private void toggleShowWeightWatcher() {
3884 String spKey = LauncherAppState.getSharedPreferencesKey();
3885 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3886 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3887
3888 show = !show;
3889
3890 SharedPreferences.Editor editor = sp.edit();
3891 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3892 editor.commit();
3893
3894 if (mWeightWatcher != null) {
3895 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3896 }
3897 }
3898
Winson Chungd64d1762013-08-20 14:37:16 -07003899 public void bindAppsAdded(final ArrayList<Long> newScreens,
3900 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003901 final ArrayList<ItemInfo> addAnimated,
3902 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003903 Runnable r = new Runnable() {
3904 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003905 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003906 }
3907 };
3908 if (waitUntilResume(r)) {
3909 return;
3910 }
3911
Winson Chungd64d1762013-08-20 14:37:16 -07003912 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003913 if (newScreens != null) {
3914 bindAddScreens(newScreens);
3915 }
Winson Chungd64d1762013-08-20 14:37:16 -07003916
3917 // We add the items without animation on non-visible pages, and with
3918 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003919 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003920 bindItems(addNotAnimated, 0,
3921 addNotAnimated.size(), false);
3922 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003923 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003924 bindItems(addAnimated, 0,
3925 addAnimated.size(), true);
3926 }
Winson Chungc58497e2013-09-03 17:48:37 -07003927
Winson Chung87412982013-10-03 18:34:14 -07003928 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003929 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003930
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003931 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003932 addedApps != null && mAppsCustomizeContent != null) {
3933 mAppsCustomizeContent.addApps(addedApps);
3934 }
Winson Chungd64d1762013-08-20 14:37:16 -07003935 }
3936
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003937 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003938 * Bind the items start-end from the list.
3939 *
3940 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003941 */
Winson Chung64359a52013-07-08 17:17:08 -07003942 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3943 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003944 Runnable r = new Runnable() {
3945 public void run() {
3946 bindItems(shortcuts, start, end, forceAnimateIcons);
3947 }
3948 };
3949 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003950 return;
3951 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003952
Winson Chungf0c6ae02012-03-21 16:10:31 -07003953 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003954 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3955 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003956 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003957 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003958 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003959 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003960 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003961
3962 // Short circuit if we are loading dock items for a configuration which has no dock
3963 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3964 mHotseat == null) {
3965 continue;
3966 }
3967
Joe Onorato9c1289c2009-08-17 11:03:03 -04003968 switch (item.itemType) {
3969 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3970 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003971 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003972 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003973
Winson Chung64359a52013-07-08 17:17:08 -07003974 /*
3975 * TODO: FIX collision case
3976 */
Winson Chungce376632013-07-11 16:12:41 -07003977 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3978 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3979 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3980 throw new RuntimeException("OCCUPIED");
3981 }
Winson Chung64359a52013-07-08 17:17:08 -07003982 }
3983
Adam Cohendcd297f2013-06-18 13:13:40 -07003984 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3985 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003986 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003987 // Animate all the applications up now
3988 shortcut.setAlpha(0f);
3989 shortcut.setScaleX(0f);
3990 shortcut.setScaleY(0f);
3991 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003992 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003993 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003994 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003995 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003996 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003997 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003998 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003999 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4000 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004001 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004002 default:
4003 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004004 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004005 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004006
Winson Chung997a9232013-07-24 15:33:46 -07004007 if (animateIcons) {
4008 // Animate to the correct page
4009 if (newShortcutsScreenId > -1) {
4010 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004011 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004012 final Runnable startBounceAnimRunnable = new Runnable() {
4013 public void run() {
4014 anim.playTogether(bounceAnims);
4015 anim.start();
4016 }
4017 };
Winson Chung997a9232013-07-24 15:33:46 -07004018 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004019 // We post the animation slightly delayed to prevent slowdowns
4020 // when we are loading right after we return to launcher.
4021 mWorkspace.postDelayed(new Runnable() {
4022 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004023 if (mWorkspace != null) {
4024 mWorkspace.snapToPage(newScreenIndex);
4025 mWorkspace.postDelayed(startBounceAnimRunnable,
4026 NEW_APPS_ANIMATION_DELAY);
4027 }
Winson Chung94d67682013-09-25 16:29:40 -07004028 }
4029 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004030 } else {
4031 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004032 }
4033 }
Winson Chung64359a52013-07-08 17:17:08 -07004034 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004035 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004036 }
4037
Joe Onorato9c1289c2009-08-17 11:03:03 -04004038 /**
4039 * Implementation of the method from LauncherModel.Callbacks.
4040 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004041 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004042 Runnable r = new Runnable() {
4043 public void run() {
4044 bindFolders(folders);
4045 }
4046 };
4047 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004048 return;
4049 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004050 sFolders.clear();
4051 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004052 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004053
4054 /**
4055 * Add the views for a widget to the workspace.
4056 *
4057 * Implementation of the method from LauncherModel.Callbacks.
4058 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004059 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004060 Runnable r = new Runnable() {
4061 public void run() {
4062 bindAppWidget(item);
4063 }
4064 };
4065 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004066 return;
4067 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004068
Daniel Sandler843e8602010-06-07 14:59:01 -04004069 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4070 if (DEBUG_WIDGETS) {
4071 Log.d(TAG, "bindAppWidget: " + item);
4072 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004073 final Workspace workspace = mWorkspace;
4074
4075 final int appWidgetId = item.appWidgetId;
4076 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004077 if (DEBUG_WIDGETS) {
4078 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4079 }
4080
Joe Onorato9c1289c2009-08-17 11:03:03 -04004081 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4082
Joe Onorato9c1289c2009-08-17 11:03:03 -04004083 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004084 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004085
Adam Cohendcd297f2013-06-18 13:13:40 -07004086 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004087 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004088 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4089
Joe Onorato9c1289c2009-08-17 11:03:03 -04004090 workspace.requestLayout();
4091
Daniel Sandler843e8602010-06-07 14:59:01 -04004092 if (DEBUG_WIDGETS) {
4093 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4094 + (SystemClock.uptimeMillis()-start) + "ms");
4095 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004096 }
4097
Adam Cohen1462de32012-07-24 22:34:36 -07004098 public void onPageBoundSynchronously(int page) {
4099 mSynchronouslyBoundPages.add(page);
4100 }
4101
Joe Onorato9c1289c2009-08-17 11:03:03 -04004102 /**
4103 * Callback saying that there aren't any more items to bind.
4104 *
4105 * Implementation of the method from LauncherModel.Callbacks.
4106 */
Adam Cohene25af792013-06-06 23:08:25 -07004107 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004108 Runnable r = new Runnable() {
4109 public void run() {
4110 finishBindingItems(upgradePath);
4111 }
4112 };
4113 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004114 return;
4115 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004116 if (mSavedState != null) {
4117 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004118 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004119 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004120 mSavedState = null;
4121 }
4122
Adam Cohen1462de32012-07-24 22:34:36 -07004123 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004124
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004125 // If we received the result of any pending adds while the loader was running (e.g. the
4126 // widget configuration forced an orientation change), process them now.
4127 for (int i = 0; i < sPendingAddList.size(); i++) {
4128 completeAdd(sPendingAddList.get(i));
4129 }
4130 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004131
Winson Chungc51db6a2011-10-05 11:44:49 -07004132 // Update the market app icon as necessary (the other icons will be managed in response to
4133 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004134 if (!DISABLE_MARKET_BUTTON) {
4135 updateAppMarketIcon();
4136 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004137
Winson Chungf0c6ae02012-03-21 16:10:31 -07004138 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004139 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004140 mWorkspace.getUniqueComponents(true, null);
4141 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004142 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004143 }
4144
Winson Chunga0b7e862013-09-05 16:03:15 -07004145 public boolean isAllAppsButtonRank(int rank) {
4146 if (mHotseat != null) {
4147 return mHotseat.isAllAppsButtonRank(rank);
4148 }
4149 return false;
4150 }
4151
Winson Chunga2413752012-04-03 14:22:34 -07004152 private boolean canRunNewAppsAnimation() {
4153 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4154 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4155 }
4156
Winson Chungc9168342013-06-26 14:54:55 -07004157 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4158 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4159 PropertyValuesHolder.ofFloat("alpha", 1f),
4160 PropertyValuesHolder.ofFloat("scaleX", 1f),
4161 PropertyValuesHolder.ofFloat("scaleY", 1f));
4162 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4163 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4164 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4165 return bounceAnim;
4166 }
4167
Adam Cohen27772732013-11-11 14:00:56 +00004168 public boolean useVerticalBarLayout() {
4169 return LauncherAppState.getInstance().getDynamicGrid().
4170 getDeviceProfile().isVerticalBarLayout();
4171 }
4172
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004173 protected Rect getSearchBarBounds() {
4174 return LauncherAppState.getInstance().getDynamicGrid().
4175 getDeviceProfile().getSearchBarBounds();
4176 }
4177
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004178 @Override
4179 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004180 boolean searchVisible = updateGlobalSearchIcon();
4181 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004182 if (mSearchDropTargetBar != null) {
4183 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4184 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004185 }
4186
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004187 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004188 * Add the icons for all apps.
4189 *
4190 * Implementation of the method from LauncherModel.Callbacks.
4191 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004192 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004193 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004194 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4195 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4196 getHotseat().addAllAppsFolder(mIconCache, apps,
4197 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4198 }
4199 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004200 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004201 if (mAppsCustomizeContent != null) {
4202 mAppsCustomizeContent.onPackagesUpdated(
4203 LauncherModel.getSortedWidgetsAndShortcuts(this));
4204 }
Winson Chungc58497e2013-09-03 17:48:37 -07004205 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004206 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004207 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004208 mAppsCustomizeContent.onPackagesUpdated(
4209 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004210 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004211 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004212 }
4213
4214 /**
4215 * A package was updated.
4216 *
4217 * Implementation of the method from LauncherModel.Callbacks.
4218 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004219 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004220 Runnable r = new Runnable() {
4221 public void run() {
4222 bindAppsUpdated(apps);
4223 }
4224 };
4225 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004226 return;
4227 }
4228
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004229 if (mWorkspace != null) {
4230 mWorkspace.updateShortcuts(apps);
4231 }
Winson Chungc58497e2013-09-03 17:48:37 -07004232
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004233 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004234 mAppsCustomizeContent != null) {
4235 mAppsCustomizeContent.updateApps(apps);
4236 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004237 }
4238
4239 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004240 * Update the state of a package, typically related to install state.
4241 *
4242 * Implementation of the method from LauncherModel.Callbacks.
4243 */
4244 public void updatePackageState(String pkgName, int state) {
4245 if (mWorkspace != null) {
4246 mWorkspace.updatePackageState(pkgName, state);
4247 }
4248 }
4249
4250 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004251 * A package was uninstalled. We take both the super set of packageNames
4252 * in addition to specific applications to remove, the reason being that
4253 * this can be called when a package is updated as well. In that scenario,
4254 * we only remove specific components from the workspace, where as
4255 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004256 *
4257 * Implementation of the method from LauncherModel.Callbacks.
4258 */
Winson Chung83892cc2013-05-01 16:53:33 -07004259 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004260 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004261 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004262 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004263 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004264 }
Winson Chungd64d1762013-08-20 14:37:16 -07004265 };
4266 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004267 return;
4268 }
4269
Winson Chungdf95eb12013-10-16 14:57:07 -07004270 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004271 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004272 }
4273 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004274 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004275 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004276
Winson Chunga1820962011-10-03 16:31:06 -07004277 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004278 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004279
Winson Chungdf95eb12013-10-16 14:57:07 -07004280 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004281 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004282 mAppsCustomizeContent != null) {
4283 mAppsCustomizeContent.removeApps(appInfos);
4284 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004285 }
Joe Onoratobe386092009-11-17 17:32:16 -08004286
4287 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004288 * A number of packages were updated.
4289 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004290 private ArrayList<Object> mWidgetsAndShortcuts;
4291 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004292 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004293 bindPackagesUpdated(mWidgetsAndShortcuts);
4294 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004295 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004296 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004297 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4298 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4299 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004300 return;
4301 }
4302
Winson Chung64359a52013-07-08 17:17:08 -07004303 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004304 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004305 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004306 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004307 }
4308
Winson Chung400438b2011-01-16 17:53:48 -08004309 private int mapConfigurationOriActivityInfoOri(int configOri) {
4310 final Display d = getWindowManager().getDefaultDisplay();
4311 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4312 switch (d.getRotation()) {
4313 case Surface.ROTATION_0:
4314 case Surface.ROTATION_180:
4315 // We are currently in the same basic orientation as the natural orientation
4316 naturalOri = configOri;
4317 break;
4318 case Surface.ROTATION_90:
4319 case Surface.ROTATION_270:
4320 // We are currently in the other basic orientation to the natural orientation
4321 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4322 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4323 break;
4324 }
4325
4326 int[] oriMap = {
4327 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4328 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4329 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4330 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4331 };
4332 // Since the map starts at portrait, we need to offset if this device's natural orientation
4333 // is landscape.
4334 int indexOffset = 0;
4335 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4336 indexOffset = 1;
4337 }
4338 return oriMap[(d.getRotation() + indexOffset) % 4];
4339 }
Adam Cohen446e9402011-09-15 18:21:21 -07004340
Winson Chung4b919f82012-05-01 10:44:08 -07004341 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004342 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004343 getResources().getBoolean(R.bool.allow_rotation);
4344 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004345 }
Winson Chung4b919f82012-05-01 10:44:08 -07004346 public void lockScreenOrientation() {
4347 if (isRotationEnabled()) {
4348 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4349 .getConfiguration().orientation));
4350 }
4351 }
4352 public void unlockScreenOrientation(boolean immediate) {
4353 if (isRotationEnabled()) {
4354 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004355 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004356 } else {
4357 mHandler.postDelayed(new Runnable() {
4358 public void run() {
4359 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4360 }
4361 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004362 }
Winson Chung4b919f82012-05-01 10:44:08 -07004363 }
Winson Chung400438b2011-01-16 17:53:48 -08004364 }
4365
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004366 /**
4367 * Called when the SearchBar hint should be changed.
4368 *
4369 * @param hint the hint to be displayed in the search bar.
4370 */
4371 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004372 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004373 }
4374
Winson Chunge43a1e72014-01-15 10:33:02 -08004375 protected boolean isLauncherPreinstalled() {
4376 PackageManager pm = getPackageManager();
4377 try {
4378 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4379 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4380 return true;
4381 } else {
4382 return false;
4383 }
4384 } catch (NameNotFoundException e) {
4385 e.printStackTrace();
4386 return false;
4387 }
4388 }
4389
Winson Chunge6eabff2013-09-24 11:01:23 -07004390 protected String getFirstRunClingSearchBarHint() {
4391 return "";
4392 }
4393 protected String getFirstRunCustomContentHint() {
4394 return "";
4395 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004396 protected int getFirstRunFocusedHotseatAppDrawableId() {
4397 return -1;
4398 }
4399 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4400 return null;
4401 }
4402 protected int getFirstRunFocusedHotseatAppRank() {
4403 return -1;
4404 }
4405 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4406 return "";
4407 }
4408 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4409 return "";
4410 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004411
Winson Chungaf40f202013-09-18 18:26:31 -07004412 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004413 mLauncherClings.dismissFirstRunCling(v);
4414 }
4415 public void dismissMigrationClingCopyApps(View v) {
4416 mLauncherClings.dismissMigrationClingCopyApps(v);
4417 }
4418 public void dismissMigrationClingUseDefault(View v) {
4419 mLauncherClings.dismissMigrationClingUseDefault(v);
4420 }
4421 public void dismissMigrationWorkspaceCling(View v) {
4422 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004423 }
Winson Chung82f55532011-08-09 14:14:23 -07004424 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004425 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004426 }
4427 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004428 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004429 }
4430
Winson Chunga6945242014-01-08 14:04:34 -08004431 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004432 return !ActivityManager.isRunningInTestHarness() &&
4433 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004434 }
4435
4436 public void showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004437 if (shouldRunFirstRunActivity() &&
4438 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004439 Intent firstRunIntent = getFirstRunActivity();
4440 if (firstRunIntent != null) {
4441 startActivity(firstRunIntent);
4442 markFirstRunActivityShown();
4443 }
4444 }
4445 }
4446
4447 private void markFirstRunActivityShown() {
4448 SharedPreferences.Editor editor = mSharedPrefs.edit();
4449 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4450 editor.apply();
4451 }
4452
4453 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004454 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4455 if (mHotseat != null) mHotseat.setAlpha(1f);
4456 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4457 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004458 }
4459
4460 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004461 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4462 if (mHotseat != null) mHotseat.setAlpha(0f);
4463 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4464 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004465 }
4466
4467
Mathew Inwood72fbec12013-11-19 15:45:07 +00004468 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4469 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4470 if (ri == null) {
4471 return null;
4472 }
4473 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4474 }
4475
4476 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4477 Bitmap icon) {
4478 return new ShortcutInfo(shortcutIntent, caption, icon);
4479 }
4480
4481 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4482 dragView.setTag(dragInfo);
4483 mWorkspace.onDragStartedWithItem(dragView);
4484 mWorkspace.beginDragShared(dragView, source);
4485 }
4486
Anjali Koppalf5d29132014-02-28 13:33:27 -08004487 @Override
4488 public void onPageSwitch(View newPage, int newPageIndex) {
4489 }
4490
Winson Chung80baf5a2010-08-09 16:03:15 -07004491 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004492 * Prints out out state for debugging.
4493 */
4494 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004495 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004496 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004497 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4498 Log.d(TAG, "mRestoring=" + mRestoring);
4499 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4500 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004501 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004502 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004503
Winson Chung785d2eb2011-04-14 16:08:02 -07004504 if (mAppsCustomizeContent != null) {
4505 mAppsCustomizeContent.dumpState();
4506 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004507 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004508 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004509
4510 @Override
4511 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4512 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004513 synchronized (sDumpLogs) {
4514 writer.println(" ");
4515 writer.println("Debug logs: ");
4516 for (int i = 0; i < sDumpLogs.size(); i++) {
4517 writer.println(" " + sDumpLogs.get(i));
4518 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004519 }
4520 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004521
4522 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004523 if (DEBUG_DUMP_LOG) {
4524 synchronized (sDumpLogs) {
4525 Log.d(TAG, "");
4526 Log.d(TAG, "*********************");
4527 Log.d(TAG, "Launcher debug logs: ");
4528 for (int i = 0; i < sDumpLogs.size(); i++) {
4529 Log.d(TAG, " " + sDumpLogs.get(i));
4530 }
4531 Log.d(TAG, "*********************");
4532 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004533 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004534 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004535 }
4536
4537 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004538 addDumpLog(tag, log, null, debugLog);
4539 }
4540
4541 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004542 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004543 if (e != null) {
4544 Log.d(tag, log, e);
4545 } else {
4546 Log.d(tag, log);
4547 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004548 }
Winson Chungede41292013-09-19 16:27:36 -07004549 if (DEBUG_DUMP_LOG) {
4550 sDateStamp.setTime(System.currentTimeMillis());
4551 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004552 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4553 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004554 }
Winson Chungede41292013-09-19 16:27:36 -07004555 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004556 }
4557
Winson Chungede41292013-09-19 16:27:36 -07004558 public void dumpLogsToLocalData() {
4559 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004560 new AsyncTask<Void, Void, Void>() {
4561 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004562 boolean success = false;
4563 sDateStamp.setTime(sRunStart);
4564 String FILENAME = sDateStamp.getMonth() + "-"
4565 + sDateStamp.getDay() + "_"
4566 + sDateStamp.getHours() + "-"
4567 + sDateStamp.getMinutes() + "_"
4568 + sDateStamp.getSeconds() + ".txt";
4569
4570 FileOutputStream fos = null;
4571 File outFile = null;
4572 try {
4573 outFile = new File(getFilesDir(), FILENAME);
4574 outFile.createNewFile();
4575 fos = new FileOutputStream(outFile);
4576 } catch (Exception e) {
4577 e.printStackTrace();
4578 }
4579 if (fos != null) {
4580 PrintWriter writer = new PrintWriter(fos);
4581
4582 writer.println(" ");
4583 writer.println("Debug logs: ");
4584 synchronized (sDumpLogs) {
4585 for (int i = 0; i < sDumpLogs.size(); i++) {
4586 writer.println(" " + sDumpLogs.get(i));
4587 }
4588 }
4589 writer.close();
4590 }
4591 try {
4592 if (fos != null) {
4593 fos.close();
4594 success = true;
4595 }
4596 } catch (IOException e) {
4597 e.printStackTrace();
4598 }
Michael Jurka43467462013-11-14 12:03:58 +01004599 return null;
Winson Chungede41292013-09-19 16:27:36 -07004600 }
Michael Jurka43467462013-11-14 12:03:58 +01004601 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004602 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004603 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004604}
Michael Jurka2763be32011-02-24 11:19:57 -08004605
Michael Jurkaabded662011-03-04 12:06:57 -08004606interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004607 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004608 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004609 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004610 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004611 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004612}
Dan Sandlerd5024042014-01-09 15:01:33 -05004613
4614interface DebugIntents {
4615 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4616 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004617}