blob: 05be4ae67873be7a92b6206c1077b6866ecb936a [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 /**
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -07002450 * Event handler for a paged view icon click.
2451 * @param v The view that was clicked.
2452 * @param appInfo The {link AppInfo} of the view.
2453 */
2454 public void onClickPagedViewIcon(View v, AppInfo appInfo) {
2455 if (LOGD) Log.d(TAG, "onClickPagedViewIcon");
2456 startActivitySafely(v, appInfo.intent, appInfo);
2457 getStats().recordLaunch(appInfo.intent);
2458 }
2459
2460 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002461 * Event handler for an app shortcut click.
2462 *
2463 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2464 */
2465 protected void onClickAppShortcut(View v) {
2466 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2467 Object tag = v.getTag();
2468 if (!(tag instanceof ShortcutInfo)) {
2469 throw new IllegalArgumentException("Input must be a Shortcut");
2470 }
2471
2472 // Open shortcut
2473 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2474 final Intent intent = shortcut.intent;
2475
2476 // Check for special shortcuts
2477 if (intent.getComponent() != null) {
2478 final String shortcutClass = intent.getComponent().getClassName();
2479
2480 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2481 MemoryDumpActivity.startDump(this);
2482 return;
2483 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2484 toggleShowWeightWatcher();
2485 return;
2486 }
2487 }
2488
2489 // Start activities
2490 int[] pos = new int[2];
2491 v.getLocationOnScreen(pos);
2492 intent.setSourceBounds(new Rect(pos[0], pos[1],
2493 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2494
2495 boolean success = startActivitySafely(v, intent, tag);
2496
2497 mStats.recordLaunch(intent, shortcut);
2498
2499 if (success && v instanceof BubbleTextView) {
2500 mWaitingForResume = (BubbleTextView) v;
2501 mWaitingForResume.setStayPressed(true);
2502 }
2503 }
2504
2505 /**
2506 * Event handler for a folder icon click.
2507 *
2508 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2509 */
2510 protected void onClickFolderIcon(View v) {
2511 if (LOGD) Log.d(TAG, "onClickFolder");
2512 if (!(v instanceof FolderIcon)){
2513 throw new IllegalArgumentException("Input must be a FolderIcon");
2514 }
2515
2516 FolderIcon folderIcon = (FolderIcon) v;
2517 final FolderInfo info = folderIcon.getFolderInfo();
2518 Folder openFolder = mWorkspace.getFolderForTag(info);
2519
2520 // If the folder info reports that the associated folder is open, then verify that
2521 // it is actually opened. There have been a few instances where this gets out of sync.
2522 if (info.opened && openFolder == null) {
2523 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2524 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2525 info.opened = false;
2526 }
2527
2528 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2529 // Close any open folder
2530 closeFolder();
2531 // Open the requested folder
2532 openFolder(folderIcon);
2533 } else {
2534 // Find the open folder...
2535 int folderScreen;
2536 if (openFolder != null) {
2537 folderScreen = mWorkspace.getPageForView(openFolder);
2538 // .. and close it
2539 closeFolder(openFolder);
2540 if (folderScreen != mWorkspace.getCurrentPage()) {
2541 // Close any folder open on the current screen
2542 closeFolder();
2543 // Pull the folder onto this screen
2544 openFolder(folderIcon);
2545 }
2546 }
2547 }
Michael Jurka5130e402011-10-13 04:55:35 -07002548 }
2549
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002550 /**
2551 * Event handler for the (Add) Widgets button that appears after a long press
2552 * on the home screen.
2553 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002554 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002555 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002556 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2557 }
2558
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002559 /**
2560 * Event handler for the wallpaper picker button that appears after a long press
2561 * on the home screen.
2562 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002563 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002564 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2565 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2566 pickWallpaper.setComponent(getWallpaperPickerComponent());
2567 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2568 }
2569
2570 /**
2571 * Event handler for a click on the settings button that appears after a long press
2572 * on the home screen.
2573 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002574 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002575 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2576 }
2577
Michael Jurka5130e402011-10-13 04:55:35 -07002578 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002579 // Provide the same haptic feedback that the system offers for virtual keys.
2580 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002581 }
2582
Adam Cohen61f560d2013-09-30 15:58:20 -07002583 public void performHapticFeedbackOnTouchDown(View v) {
2584 // Provide the same haptic feedback that the system offers for virtual keys.
2585 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2586 }
2587
2588 public View.OnTouchListener getHapticFeedbackTouchListener() {
2589 if (mHapticFeedbackTouchListener == null) {
2590 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2591 @Override
2592 public boolean onTouch(View v, MotionEvent event) {
2593 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2594 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2595 }
2596 return false;
2597 }
2598 };
2599 }
2600 return mHapticFeedbackTouchListener;
2601 }
2602
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002603 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002604 if (!DISABLE_MARKET_BUTTON) {
2605 if (mAppMarketIntent != null) {
2606 startActivitySafely(v, mAppMarketIntent, "app market");
2607 } else {
2608 Log.e(TAG, "Invalid app market intent.");
2609 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002610 }
2611 }
2612
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002613 public void onDragStarted(View view) {}
2614
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002615 /**
2616 * Called when the user stops interacting with the launcher.
2617 * This implies that the user is now on the homescreen and is not doing housekeeping.
2618 */
2619 protected void onInteractionEnd() {}
2620
2621 /**
2622 * Called when the user starts interacting with the launcher.
2623 * The possible interactions are:
2624 * - open all apps
2625 * - reorder an app shortcut, or a widget
2626 * - open the overview mode.
2627 * This is a good time to stop doing things that only make sense
2628 * when the user is on the homescreen and not doing housekeeping.
2629 */
2630 protected void onInteractionBegin() {}
2631
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002632 void startApplicationDetailsActivity(ComponentName componentName) {
2633 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002634 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2635 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002636 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2637 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002638 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002639 }
2640
Michael Jurka1e2f4652013-07-08 18:03:46 -07002641 // returns true if the activity was started
2642 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002643 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002644 // System applications cannot be installed. For now, show a toast explaining that.
2645 // We may give them the option of disabling apps this way.
2646 int messageId = R.string.uninstall_system_app_text;
2647 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002648 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002649 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002650 String packageName = componentName.getPackageName();
2651 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002652 Intent intent = new Intent(
2653 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002654 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2655 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002656 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002657 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002658 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002659 }
2660
Michael Jurka86a720e2012-05-09 11:23:23 -07002661 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002662 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002664 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002665 // Only launch using the new animation if the shortcut has not opted out (this is a
2666 // private contract between launcher and may be ignored in the future).
2667 boolean useLaunchAnimation = (v != null) &&
2668 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2669 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002670 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2671 v.getMeasuredWidth(), v.getMeasuredHeight());
2672
2673 startActivity(intent, opts.toBundle());
2674 } else {
2675 startActivity(intent);
2676 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002677 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002678 } catch (SecurityException e) {
2679 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002680 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002682 "or use the exported attribute for this activity. "
2683 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002684 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002685 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002686 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002687
Michael Jurka86a720e2012-05-09 11:23:23 -07002688 boolean startActivitySafely(View v, Intent intent, Object tag) {
2689 boolean success = false;
2690 try {
2691 success = startActivity(v, intent, tag);
2692 } catch (ActivityNotFoundException e) {
2693 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2694 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2695 }
2696 return success;
2697 }
2698
Adam Cohen268c4752012-06-06 17:47:33 -07002699 /**
2700 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2701 * in the DragLayer in the exact absolute location of the original FolderIcon.
2702 */
2703 private void copyFolderIconToImage(FolderIcon fi) {
2704 final int width = fi.getMeasuredWidth();
2705 final int height = fi.getMeasuredHeight();
2706
2707 // Lazy load ImageView, Bitmap and Canvas
2708 if (mFolderIconImageView == null) {
2709 mFolderIconImageView = new ImageView(this);
2710 }
2711 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2712 mFolderIconBitmap.getHeight() != height) {
2713 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2714 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2715 }
2716
2717 DragLayer.LayoutParams lp;
2718 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2719 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2720 } else {
2721 lp = new DragLayer.LayoutParams(width, height);
2722 }
2723
Adam Cohen307fe232012-08-16 17:55:58 -07002724 // The layout from which the folder is being opened may be scaled, adjust the starting
2725 // view size by this scale factor.
2726 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002727 lp.customPosition = true;
2728 lp.x = mRectForFolderAnimation.left;
2729 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002730 lp.width = (int) (scale * width);
2731 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002732
2733 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2734 fi.draw(mFolderIconCanvas);
2735 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002736 if (fi.getFolder() != null) {
2737 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2738 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002739 }
Adam Cohen268c4752012-06-06 17:47:33 -07002740 // Just in case this image view is still in the drag layer from a previous animation,
2741 // we remove it and re-add it.
2742 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2743 mDragLayer.removeView(mFolderIconImageView);
2744 }
2745 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002746 if (fi.getFolder() != null) {
2747 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002748 }
Adam Cohen268c4752012-06-06 17:47:33 -07002749 }
2750
Adam Cohen2801caf2011-05-13 20:57:39 -07002751 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002752 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002753 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2754 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2755 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2756
Adam Cohenc51934b2011-07-26 21:07:43 -07002757 FolderInfo info = (FolderInfo) fi.getTag();
2758 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2759 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002760 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2761 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002762 }
2763
Adam Cohen268c4752012-06-06 17:47:33 -07002764 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2765 copyFolderIconToImage(fi);
2766 fi.setVisibility(View.INVISIBLE);
2767
Michael Jurka2ecf9952012-06-18 12:52:28 -07002768 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002769 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002770 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2771 oa.start();
2772 }
2773
Adam Cohen268c4752012-06-06 17:47:33 -07002774 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002775 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002776 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2777 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2778 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2779
Adam Cohen268c4752012-06-06 17:47:33 -07002780 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002781
Adam Cohen268c4752012-06-06 17:47:33 -07002782 // We remove and re-draw the FolderIcon in-case it has changed
2783 mDragLayer.removeView(mFolderIconImageView);
2784 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002785 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002786 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002787 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002788 oa.addListener(new AnimatorListenerAdapter() {
2789 @Override
2790 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002791 if (cl != null) {
2792 cl.clearFolderLeaveBehind();
2793 // Remove the ImageView copy of the FolderIcon and make the original visible.
2794 mDragLayer.removeView(mFolderIconImageView);
2795 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002796 }
2797 }
2798 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002799 oa.start();
2800 }
2801
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002802 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002803 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002804 * is animated relative to the specified View. If the View is null, no animation
2805 * is played.
2806 *
2807 * @param folderInfo The FolderInfo describing the folder to open.
2808 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002809 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002810 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002811 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002812
Adam Cohena9cf38f2011-05-02 15:36:58 -07002813 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002814
Adam Cohen4554ee12011-08-03 16:13:21 -07002815 // Just verify that the folder hasn't already been added to the DragLayer.
2816 // There was a one-off crash where the folder had a parent already.
2817 if (folder.getParent() == null) {
2818 mDragLayer.addView(folder);
2819 mDragController.addDropTarget((DropTarget) folder);
2820 } else {
2821 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2822 folder.getParent() + ").");
2823 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002824 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002825 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002826
2827 // Notify the accessibility manager that this folder "window" has appeared and occluded
2828 // the workspace items
2829 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2830 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002831 }
2832
2833 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002834 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002835 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002836 if (folder.isEditingName()) {
2837 folder.dismissEditingName();
2838 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002839 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002840
2841 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002842 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002843 }
2844 }
2845
2846 void closeFolder(Folder folder) {
2847 folder.getInfo().opened = false;
2848
2849 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2850 if (parent != null) {
2851 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2852 shrinkAndFadeInFolderIcon(fi);
2853 }
2854 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002855
2856 // Notify the accessibility manager that this folder "window" has disappeard and no
2857 // longer occludeds the workspace items
2858 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859 }
2860
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002861 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002862 if (!isDraggingEnabled()) return false;
2863 if (isWorkspaceLocked()) return false;
2864 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002865
Adam Cohen1697b792013-09-17 19:08:21 -07002866 if (v instanceof Workspace) {
2867 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002868 if (mWorkspace.enterOverviewMode()) {
2869 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2870 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2871 return true;
2872 } else {
2873 return false;
2874 }
Adam Cohen1697b792013-09-17 19:08:21 -07002875 }
Adam Cohen1697b792013-09-17 19:08:21 -07002876 }
2877
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002878 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002879 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002880 }
2881
Michael Jurka0280c3b2010-09-17 15:00:07 -07002882 resetAddInfo();
2883 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002884 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002885 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886 return true;
2887 }
2888
Winson Chung3d503fb2011-07-13 17:25:49 -07002889 // The hotseat touch handling does not go through Workspace, and we always allow long press
2890 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002891 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002892 final boolean inHotseat = isHotseatLayout(v);
2893 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002894 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002895 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002896 // User long pressed on empty space
2897 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2898 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2899 // Disabling reordering until we sort out some issues.
2900 if (mWorkspace.isInOverviewMode()) {
2901 mWorkspace.startReordering(v);
2902 } else {
2903 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002905 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002906 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2907 mHotseat.getOrderInHotseat(
2908 longClickCellInfo.cellX,
2909 longClickCellInfo.cellY));
2910 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002911 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002912 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002913 }
2914 }
2915 }
2916 return true;
2917 }
2918
Winson Chung3d503fb2011-07-13 17:25:49 -07002919 boolean isHotseatLayout(View layout) {
2920 return mHotseat != null && layout != null &&
2921 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2922 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002923
Winson Chung3d503fb2011-07-13 17:25:49 -07002924 /**
2925 * Returns the CellLayout of the specified container at the specified screen.
2926 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002927 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002928 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2929 if (mHotseat != null) {
2930 return mHotseat.getLayout();
2931 } else {
2932 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002933 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002934 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002935 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002936 }
2937 }
2938
Daniel Sandler843e8602010-06-07 14:59:01 -04002939 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002940 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002941 }
2942
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002943 /**
2944 * Helper method for the cameraZoomIn/cameraZoomOut animations
2945 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002946 * @param scaleFactor The scale factor used for the zoom
2947 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002948 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002949 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002950 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002951 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002952
Craig Mautner360310b2012-10-26 15:13:08 -07002953 private void setWorkspaceBackground(boolean workspace) {
2954 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002955 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002956 }
2957
Selim Cinek3a8a8f72014-01-16 10:38:38 -08002958 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002959 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2960 int curflags = getWindow().getAttributes().flags
2961 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2962 if (wpflags != curflags) {
2963 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2964 }
Craig Mautner360310b2012-10-26 15:13:08 -07002965 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002966 }
2967
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002968 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2969 if (v instanceof LauncherTransitionable) {
2970 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2971 }
2972 }
2973
Michael Jurkabed61d22012-02-14 22:51:29 -08002974 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2975 if (v instanceof LauncherTransitionable) {
2976 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2977 }
Winson Chung70442722012-02-10 15:43:22 -08002978
2979 // Update the workspace transition step as well
2980 dispatchOnLauncherTransitionStep(v, 0f);
2981 }
2982
2983 private void dispatchOnLauncherTransitionStep(View v, float t) {
2984 if (v instanceof LauncherTransitionable) {
2985 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2986 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002987 }
2988
2989 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2990 if (v instanceof LauncherTransitionable) {
2991 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2992 }
Winson Chung70442722012-02-10 15:43:22 -08002993
2994 // Update the workspace transition step as well
2995 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002996 }
2997
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002998 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002999 * Things to test when changing the following seven functions.
3000 * - Home from workspace
3001 * - from center screen
3002 * - from other screens
3003 * - Home from all apps
3004 * - from center screen
3005 * - from other screens
3006 * - Back from all apps
3007 * - from center screen
3008 * - from other screens
3009 * - Launch app from workspace and quit
3010 * - with back
3011 * - with home
3012 * - Launch app from all apps and quit
3013 * - with back
3014 * - with home
3015 * - Go to a screen that's not the default, then all
3016 * apps, and launch and app, and go back
3017 * - with back
3018 * -with home
3019 * - On workspace, long press power and go back
3020 * - with back
3021 * - with home
3022 * - On all apps, long press power and go back
3023 * - with back
3024 * - with home
3025 * - On workspace, power off
3026 * - On all apps, power off
3027 * - Launch an app and turn off the screen while in that app
3028 * - Go back with home key
3029 * - Go back with back key TODO: make this not go to workspace
3030 * - From all apps
3031 * - From workspace
3032 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3033 * - From all apps
3034 * - From the center workspace
3035 * - From another workspace
3036 */
3037
3038 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003039 * Zoom the camera out from the workspace to reveal 'toView'.
3040 * Assumes that the view to show is anchored at either the very top or very bottom
3041 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003042 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003043 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003044 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3045 showAppsCustomizeHelper(animated, springLoaded, contentType);
3046 }
3047 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3048 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003049 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003050 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003051 mStateAnimation.cancel();
3052 mStateAnimation = null;
3053 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003054 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003055
Winson Chungf0ea4d32011-06-06 14:27:16 -07003056 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3057 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
3058 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003059 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003060 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003061 final int startDelay =
3062 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003063
Michael Jurkab3e22d92011-10-31 15:58:33 -07003064 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003065
Michael Jurkad74c9842011-07-10 12:44:21 -07003066 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003067 Animator workspaceAnim =
3068 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003069 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003070 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3071 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003072 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3073 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003074
3075 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003076 toView.setScaleX(scale);
3077 toView.setScaleY(scale);
3078 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3079 scaleAnim.
3080 scaleX(1f).scaleY(1f).
3081 setDuration(duration).
3082 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003083
Winson Chungf0ea4d32011-06-06 14:27:16 -07003084 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003085 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003086 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003087 .ofFloat(toView, "alpha", 0f, 1f)
3088 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003089 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003090 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3091 @Override
3092 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003093 if (animation == null) {
3094 throw new RuntimeException("animation is null");
3095 }
Winson Chung70442722012-02-10 15:43:22 -08003096 float t = (Float) animation.getAnimatedValue();
3097 dispatchOnLauncherTransitionStep(fromView, t);
3098 dispatchOnLauncherTransitionStep(toView, t);
3099 }
3100 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003101
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003102 // toView should appear right at the end of the workspace shrink
3103 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003104 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003105 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003106 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003107
3108 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003109 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003110 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003111 // Prepare the position
3112 toView.setTranslationX(0.0f);
3113 toView.setTranslationY(0.0f);
3114 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003115 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003116 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003117 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003118 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003119 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3120 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003121
Winson Chungc7d2b602012-05-16 17:02:20 -07003122 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003123 if (mSearchDropTargetBar != null) {
3124 mSearchDropTargetBar.hideSearchBar(false);
3125 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003126 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003127 });
3128
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003129 if (workspaceAnim != null) {
3130 mStateAnimation.play(workspaceAnim);
3131 }
Michael Jurka1899a362011-11-03 13:50:45 -07003132
3133 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003134
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003135 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3136 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003137
3138 // If any of the objects being animated haven't been measured/laid out
3139 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003140 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003141 (mWorkspace.getMeasuredWidth() == 0) ||
3142 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003143 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003144 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003145
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003146 final AnimatorSet stateAnimation = mStateAnimation;
3147 final Runnable startAnimRunnable = new Runnable() {
3148 public void run() {
3149 // Check that mStateAnimation hasn't changed while
3150 // we waited for a layout/draw pass
3151 if (mStateAnimation != stateAnimation)
3152 return;
3153 setPivotsForZoom(toView, scale);
3154 dispatchOnLauncherTransitionStart(fromView, animated, false);
3155 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003156 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003157 }
3158 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003159 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003160 final ViewTreeObserver observer = toView.getViewTreeObserver();
3161 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3162 public void onGlobalLayout() {
3163 startAnimRunnable.run();
3164 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3165 }
3166 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003167 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003168 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003169 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003170 } else {
3171 toView.setTranslationX(0.0f);
3172 toView.setTranslationY(0.0f);
3173 toView.setScaleX(1.0f);
3174 toView.setScaleY(1.0f);
3175 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003176 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003177
Daniel Sandlere4f98912013-06-25 15:13:26 -04003178 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003179 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003180 if (mSearchDropTargetBar != null) {
3181 mSearchDropTargetBar.hideSearchBar(false);
3182 }
Michael Jurkaabded662011-03-04 12:06:57 -08003183 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003184 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003185 dispatchOnLauncherTransitionStart(fromView, animated, false);
3186 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003187 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003188 dispatchOnLauncherTransitionStart(toView, animated, false);
3189 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003190 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003191 }
3192
3193 /**
3194 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003195 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003196 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003197 */
Adam Cohened307df2013-10-02 09:37:31 -07003198 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003199 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003200
Michael Jurkab3e22d92011-10-31 15:58:33 -07003201 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003202 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003203 mStateAnimation.cancel();
3204 mStateAnimation = null;
3205 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003206 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003207
Winson Chungf0ea4d32011-06-06 14:27:16 -07003208 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003209 final int fadeOutDuration =
3210 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003211 final float scaleFactor = (float)
3212 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3213 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003214 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003215 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003216 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003217 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3218 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003219 toState, animated, stagger, -1);
3220 } else if (toState == Workspace.State.SPRING_LOADED ||
3221 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003222 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003223 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003224 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003225
Michael Jurkab3e22d92011-10-31 15:58:33 -07003226 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003227 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003228 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003229 final LauncherViewPropertyAnimator scaleAnim =
3230 new LauncherViewPropertyAnimator(fromView);
3231 scaleAnim.
3232 scaleX(scaleFactor).scaleY(scaleFactor).
3233 setDuration(duration).
3234 setInterpolator(new Workspace.ZoomInInterpolator());
3235
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003236 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003237 .ofFloat(fromView, "alpha", 1f, 0f)
3238 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003239 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003240 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3241 @Override
3242 public void onAnimationUpdate(ValueAnimator animation) {
3243 float t = 1f - (Float) animation.getAnimatedValue();
3244 dispatchOnLauncherTransitionStep(fromView, t);
3245 dispatchOnLauncherTransitionStep(toView, t);
3246 }
3247 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003248
Michael Jurka2ecf9952012-06-18 12:52:28 -07003249 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003250
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003251 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3252 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003253 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003254
3255 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003256 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003257 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003258 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003259 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3260 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003261 if (onCompleteRunnable != null) {
3262 onCompleteRunnable.run();
3263 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003264 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003265 }
3266 });
3267
Winson Chungf0ea4d32011-06-06 14:27:16 -07003268 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003269 if (workspaceAnim != null) {
3270 mStateAnimation.play(workspaceAnim);
3271 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003272 dispatchOnLauncherTransitionStart(fromView, animated, true);
3273 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003274 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003275 } else {
3276 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003277 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003278 dispatchOnLauncherTransitionStart(fromView, animated, true);
3279 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003280 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003281 dispatchOnLauncherTransitionStart(toView, animated, true);
3282 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003283 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003284 }
3285
Michael Jurkae326f182011-11-21 14:05:46 -08003286 @Override
3287 public void onTrimMemory(int level) {
3288 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003289 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003290 mAppsCustomizeTabHost.onTrimMemory();
3291 }
3292 }
3293
Adam Cohened307df2013-10-02 09:37:31 -07003294 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003295 showWorkspace(animated, null);
3296 }
3297
Adam Cohened307df2013-10-02 09:37:31 -07003298 protected void showWorkspace() {
3299 showWorkspace(true);
3300 }
3301
Adam Cohened66b2b2012-01-23 17:28:51 -08003302 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003303 if (mWorkspace.isInOverviewMode()) {
3304 mWorkspace.exitOverviewMode(animated);
3305 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003306 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003307 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003308 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003309 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003310
Winson Chungc7d2b602012-05-16 17:02:20 -07003311 // Show the search bar (only animate if we were showing the drop target bar in spring
3312 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003313 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003314 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003315 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003316
Michael Jurkab3e22d92011-10-31 15:58:33 -07003317 // Set focus to the AppsCustomize button
3318 if (mAllAppsButton != null) {
3319 mAllAppsButton.requestFocus();
3320 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003321 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003322
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003323 // Change the state *after* we've called all the transition code
3324 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003325
Winson Chung5fb63472011-02-02 17:03:37 -08003326 // Resume the auto-advance of widgets
3327 mUserPresent = true;
3328 updateRunning();
3329
alanv1d4fde62012-10-17 13:15:47 -07003330 // Send an accessibility event to announce the context change
3331 getWindow().getDecorView()
3332 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003333
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003334 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003335 }
3336
Adam Cohened307df2013-10-02 09:37:31 -07003337 void showOverviewMode(boolean animated) {
3338 mWorkspace.setVisibility(View.VISIBLE);
3339 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3340 mState = State.WORKSPACE;
3341 onWorkspaceShown(animated);
3342 }
3343
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003344 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003345 }
3346
Winson Chung82963d52013-10-09 11:20:57 -07003347 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3348 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003349 if (mState != State.WORKSPACE) return;
3350
Winson Chung82963d52013-10-09 11:20:57 -07003351 if (resetPageToZero) {
3352 mAppsCustomizeTabHost.reset();
3353 }
Winson Chungc58497e2013-09-03 17:48:37 -07003354 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003355 mAppsCustomizeTabHost.requestFocus();
3356
Michael Jurkab3e22d92011-10-31 15:58:33 -07003357 // Change the state *after* we've called all the transition code
3358 mState = State.APPS_CUSTOMIZE;
3359
3360 // Pause the auto-advance of widgets until we are out of AllApps
3361 mUserPresent = false;
3362 updateRunning();
3363 closeFolder();
3364
3365 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003366 getWindow().getDecorView()
3367 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003368 }
3369
Adam Cohen7777d962011-08-18 18:58:38 -07003370 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003371 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003372 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003373 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003374 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003375 }
Adam Cohen7777d962011-08-18 18:58:38 -07003376
Adam Cohenad4e15c2013-10-17 16:21:35 -07003377 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003378 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003379 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3380
Winson Chunge7a03942011-08-05 15:05:12 -07003381 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003382 @Override
3383 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003384 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003385 // Before we show workspace, hide all apps again because
3386 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3387 // clean up our state transition functions
3388 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003389 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003390 } else {
3391 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003392 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003393 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003394 }, delay);
3395
Winson Chung557d6ed2011-07-08 15:34:52 -07003396 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003397
Michael Jurkad3ef3062010-11-23 16:23:58 -08003398 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003399 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003400 final boolean animated = true;
3401 final boolean springLoaded = true;
3402 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003403 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003404 }
3405 // Otherwise, we are not in spring loaded mode, so don't do anything.
3406 }
3407
Michael Jurkab3e22d92011-10-31 15:58:33 -07003408 void lockAllApps() {
3409 // TODO
3410 }
3411
3412 void unlockAllApps() {
3413 // TODO
3414 }
3415
Winson Chungf0ea4d32011-06-06 14:27:16 -07003416 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003417 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003418 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003419 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003420 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003421 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003422 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003423 int duration = 0;
3424 if (mSearchDropTargetBar != null) {
3425 duration = mSearchDropTargetBar.getTransitionInDuration();
3426 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003427 mHotseat.animate().alpha(1f).setDuration(duration);
3428 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003429 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003430 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003431 }
3432 }
3433 }
3434
3435 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003436 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003437 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003438 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003439 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003440 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003441 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003442 int duration = 0;
3443 if (mSearchDropTargetBar != null) {
3444 duration = mSearchDropTargetBar.getTransitionOutDuration();
3445 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003446 mHotseat.animate().alpha(0f).setDuration(duration);
3447 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003448 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003449 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003450 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003451 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003452 }
3453
Patrick Dubroy5f445422011-02-18 14:35:21 -08003454 /**
3455 * Add an item from all apps or customize onto the given workspace screen.
3456 * If layout is null, add to the current screen.
3457 */
3458 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003459 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003460 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003461 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003462 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003463
Winson Chungdff8ebb2011-09-08 17:25:31 -07003464 /** Maps the current orientation to an index for referencing orientation correct global icons */
3465 private int getCurrentOrientationIndexForGlobalIcons() {
3466 // default - 0, landscape - 1
3467 switch (getResources().getConfiguration().orientation) {
3468 case Configuration.ORIENTATION_LANDSCAPE:
3469 return 1;
3470 default:
3471 return 0;
3472 }
3473 }
3474
Michael Jurkaae65ee32012-04-30 11:45:54 -07003475 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003476 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003477 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003478 // Look for the toolbar icon specified in the activity meta-data
3479 Bundle metaData = packageManager.getActivityInfo(
3480 activityName, PackageManager.GET_META_DATA).metaData;
3481 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003482 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003483 if (iconResId != 0) {
3484 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003485 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003486 }
3487 }
3488 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003489 // This can happen if the activity defines an invalid drawable
3490 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3491 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003492 } catch (Resources.NotFoundException nfe) {
3493 // This can happen if the activity defines an invalid drawable
3494 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3495 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003496 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003497 return null;
3498 }
3499
3500 // if successful in getting icon, return it; otherwise, set button to use default drawable
3501 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003502 int buttonId, ComponentName activityName, int fallbackDrawableId,
3503 String toolbarResourceName) {
3504 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003505 Resources r = getResources();
3506 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3507 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003508
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003509 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003510 // If we were unable to find the icon via the meta-data, use a generic one
3511 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003512 toolbarIcon = r.getDrawable(fallbackDrawableId);
3513 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003514 if (button != null) {
3515 button.setCompoundDrawables(toolbarIcon, null, null, null);
3516 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003517 return null;
3518 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003519 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003520 if (button != null) {
3521 button.setCompoundDrawables(toolbarIcon, null, null, null);
3522 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003523 return toolbarIcon.getConstantState();
3524 }
3525 }
3526
3527 // if successful in getting icon, return it; otherwise, set button to use default drawable
3528 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003529 int buttonId, ComponentName activityName, int fallbackDrawableId,
3530 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003531 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003532 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003533
Michael Jurka19e0fc52011-07-22 18:00:21 -07003534 if (button != null) {
3535 // If we were unable to find the icon via the meta-data, use a
3536 // generic one
3537 if (toolbarIcon == null) {
3538 button.setImageResource(fallbackDrawableId);
3539 } else {
3540 button.setImageDrawable(toolbarIcon);
3541 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003542 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003543
3544 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3545
Michael Jurka0423dcf2010-10-05 14:56:18 -07003546 }
3547
Winson Chung1b884092012-06-08 17:13:02 -07003548 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003549 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003550 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003551 }
3552
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003553 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003554 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003555 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003556 }
3557
Winson Chungbb185bd2011-11-21 12:31:42 -08003558 private void invalidatePressedFocusedStates(View container, View button) {
3559 if (container instanceof HolographicLinearLayout) {
3560 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3561 layout.invalidatePressedFocusedStates();
3562 } else if (button instanceof HolographicImageView) {
3563 HolographicImageView view = (HolographicImageView) button;
3564 view.invalidatePressedFocusedStates();
3565 }
3566 }
3567
Cristina Stancu476493b2013-08-07 17:20:14 +01003568 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003569 if (mQsb == null) {
3570 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3571 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003572 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003573 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003574 }
3575
3576 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003577 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003578 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003579 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003580 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003581
Winson Chung1cad91e2011-05-25 17:41:01 -07003582 final SearchManager searchManager =
3583 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3584 ComponentName activityName = searchManager.getGlobalSearchActivity();
3585 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003586 int coi = getCurrentOrientationIndexForGlobalIcons();
3587 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003588 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3589 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3590 if (sGlobalSearchIcon[coi] == null) {
3591 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3592 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3593 TOOLBAR_ICON_METADATA_NAME);
3594 }
3595
Winson Chungc51db6a2011-10-05 11:44:49 -07003596 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3597 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003598 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003599 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003600 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003601 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003602 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3603 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003604 if (searchButton != null) searchButton.setVisibility(View.GONE);
3605 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003606 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003607 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003608 }
3609 }
3610
Cristina Stancu476493b2013-08-07 17:20:14 +01003611 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003612 final View searchButtonContainer = findViewById(R.id.search_button_container);
3613 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003614 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003615 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003616 }
3617
Cristina Stancu476493b2013-08-07 17:20:14 +01003618 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003619 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003620 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003621
Winson Chungc51db6a2011-10-05 11:44:49 -07003622 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003623 final SearchManager searchManager =
3624 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3625 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3626
3627 ComponentName activityName = null;
3628 if (globalSearchActivity != null) {
3629 // Check if the global search activity handles voice search
3630 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3631 intent.setPackage(globalSearchActivity.getPackageName());
3632 activityName = intent.resolveActivity(getPackageManager());
3633 }
3634
3635 if (activityName == null) {
3636 // Fallback: check if an activity other than the global search activity
3637 // resolves this
3638 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3639 activityName = intent.resolveActivity(getPackageManager());
3640 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003641 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003642 int coi = getCurrentOrientationIndexForGlobalIcons();
3643 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003644 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3645 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3646 if (sVoiceSearchIcon[coi] == null) {
3647 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3648 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3649 TOOLBAR_ICON_METADATA_NAME);
3650 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003651 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003652 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003653 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003654 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003655 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003656 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003657 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003658 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003659 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003660 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003661 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003662 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003663
Cristina Stancu476493b2013-08-07 17:20:14 +01003664 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003665 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3666 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003667 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003668 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003669 }
3670
Winson Chung5841efa2013-09-30 18:06:44 -07003671 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003672 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3673 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003674 boolean visible = !forceDisableVoiceButtonProxy &&
3675 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003676 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003677 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003678 }
3679 }
Winson Chung5841efa2013-09-30 18:06:44 -07003680
3681 /**
3682 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3683 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3684 */
3685 public void disableVoiceButtonProxy(boolean disabled) {
3686 updateVoiceButtonProxyVisible(disabled);
3687 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003688 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003689 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003690 */
3691 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003692 if (!DISABLE_MARKET_BUTTON) {
3693 final View marketButton = findViewById(R.id.market_button);
3694 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3695 // Find the app market activity by resolving an intent.
3696 // (If multiple app markets are installed, it will return the ResolverActivity.)
3697 ComponentName activityName = intent.resolveActivity(getPackageManager());
3698 if (activityName != null) {
3699 int coi = getCurrentOrientationIndexForGlobalIcons();
3700 mAppMarketIntent = intent;
3701 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3702 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3703 TOOLBAR_ICON_METADATA_NAME);
3704 marketButton.setVisibility(View.VISIBLE);
3705 } else {
3706 // We should hide and disable the view so that we don't try and restore the visibility
3707 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3708 marketButton.setVisibility(View.GONE);
3709 marketButton.setEnabled(false);
3710 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003711 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003712 }
3713
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003714 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003715 if (!DISABLE_MARKET_BUTTON) {
3716 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3717 Resources r = getResources();
3718 Drawable marketIconDrawable = d.newDrawable(r);
3719 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3720 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3721 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003722
Winson Chungd64a6662013-09-30 11:06:59 -07003723 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3724 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003725 }
3726
Michael Jurkad7c28052012-04-27 15:43:36 -07003727 @Override
3728 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003729 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003730 final List<CharSequence> text = event.getText();
3731 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003732 // Populate event with a fake title based on the current state.
3733 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003734 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003735 } else {
3736 text.add(getString(R.string.all_apps_home_button_label));
3737 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003738 return result;
3739 }
3740
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003741 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003742 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003743 */
3744 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3745 @Override
3746 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003747 closeSystemDialogs();
3748 }
3749 }
3750
3751 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003752 * Receives notifications whenever the appwidgets are reset.
3753 */
3754 private class AppWidgetResetObserver extends ContentObserver {
3755 public AppWidgetResetObserver() {
3756 super(new Handler());
3757 }
3758
3759 @Override
3760 public void onChange(boolean selfChange) {
3761 onAppWidgetReset();
3762 }
3763 }
3764
3765 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003766 * If the activity is currently paused, signal that we need to run the passed Runnable
3767 * in onResume.
3768 *
3769 * This needs to be called from incoming places where resources might have been loaded
3770 * while we are paused. That is becaues the Configuration might be wrong
3771 * when we're not running, and if it comes back to what it was when we
3772 * were paused, we are not restarted.
3773 *
3774 * Implementation of the method from LauncherModel.Callbacks.
3775 *
3776 * @return true if we are currently paused. The caller might be able to
3777 * skip some work in that case since we will come back again.
3778 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003779 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003780 if (mPaused) {
3781 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003782 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003783 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003784 }
3785 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003786 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003787 return true;
3788 } else {
3789 return false;
3790 }
3791 }
3792
Michael Jurkac402cd92013-05-20 15:49:32 +02003793 private boolean waitUntilResume(Runnable run) {
3794 return waitUntilResume(run, false);
3795 }
3796
Michael Jurka1e2f4652013-07-08 18:03:46 -07003797 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003798 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003799 }
3800
Michael Jurka7607c2f2013-04-03 14:33:19 -07003801 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003802 * If the activity is currently paused, signal that we need to re-run the loader
3803 * in onResume.
3804 *
3805 * This needs to be called from incoming places where resources might have been loaded
3806 * while we are paused. That is becaues the Configuration might be wrong
3807 * when we're not running, and if it comes back to what it was when we
3808 * were paused, we are not restarted.
3809 *
3810 * Implementation of the method from LauncherModel.Callbacks.
3811 *
3812 * @return true if we are currently paused. The caller might be able to
3813 * skip some work in that case since we will come back again.
3814 */
3815 public boolean setLoadOnResume() {
3816 if (mPaused) {
3817 Log.i(TAG, "setLoadOnResume");
3818 mOnResumeNeedsLoad = true;
3819 return true;
3820 } else {
3821 return false;
3822 }
3823 }
3824
3825 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003826 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003827 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003828 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003829 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003830 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003831 } else {
3832 return SCREEN_COUNT / 2;
3833 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003834 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003835
Joe Onorato9c1289c2009-08-17 11:03:03 -04003836 /**
3837 * Refreshes the shortcuts shown on the workspace.
3838 *
3839 * Implementation of the method from LauncherModel.Callbacks.
3840 */
3841 public void startBinding() {
Adam Cohen517a7f52014-03-01 12:12:59 -08003842 mWorkspaceLoading = true;
3843
Michael Jurka7607c2f2013-04-03 14:33:19 -07003844 // If we're starting binding all over again, clear any bind calls we'd postponed in
3845 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3846 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003847 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003848
Winson Chungd64d1762013-08-20 14:37:16 -07003849 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003850 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003851 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003852
Michael Jurka05bf644e2011-11-30 20:28:53 -08003853 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003854 if (mHotseat != null) {
3855 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003856 }
3857 }
3858
Adam Cohendcd297f2013-06-18 13:13:40 -07003859 @Override
3860 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003861 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003862
Adam Cohen5084cba2013-09-03 12:01:16 -07003863 // If there are no screens, we need to have an empty screen
3864 if (orderedScreenIds.size() == 0) {
3865 mWorkspace.addExtraEmptyScreen();
3866 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003867
3868 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003869 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003870 if (hasCustomContentToLeft()) {
3871 mWorkspace.createCustomContentContainer();
3872 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003873 }
Winson Chung64359a52013-07-08 17:17:08 -07003874 }
3875
3876 @Override
3877 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003878 // Log to disk
3879 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3880 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3881 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003882 int count = orderedScreenIds.size();
3883 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003884 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003885 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003886 }
3887
Adam Cohen39a06042013-07-19 14:30:12 -07003888 private boolean shouldShowWeightWatcher() {
3889 String spKey = LauncherAppState.getSharedPreferencesKey();
3890 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003891 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003892
3893 return show;
3894 }
3895
3896 private void toggleShowWeightWatcher() {
3897 String spKey = LauncherAppState.getSharedPreferencesKey();
3898 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3899 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3900
3901 show = !show;
3902
3903 SharedPreferences.Editor editor = sp.edit();
3904 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3905 editor.commit();
3906
3907 if (mWeightWatcher != null) {
3908 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3909 }
3910 }
3911
Winson Chungd64d1762013-08-20 14:37:16 -07003912 public void bindAppsAdded(final ArrayList<Long> newScreens,
3913 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003914 final ArrayList<ItemInfo> addAnimated,
3915 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003916 Runnable r = new Runnable() {
3917 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003918 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003919 }
3920 };
3921 if (waitUntilResume(r)) {
3922 return;
3923 }
3924
Winson Chungd64d1762013-08-20 14:37:16 -07003925 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003926 if (newScreens != null) {
3927 bindAddScreens(newScreens);
3928 }
Winson Chungd64d1762013-08-20 14:37:16 -07003929
3930 // We add the items without animation on non-visible pages, and with
3931 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003932 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003933 bindItems(addNotAnimated, 0,
3934 addNotAnimated.size(), false);
3935 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003936 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003937 bindItems(addAnimated, 0,
3938 addAnimated.size(), true);
3939 }
Winson Chungc58497e2013-09-03 17:48:37 -07003940
Winson Chung87412982013-10-03 18:34:14 -07003941 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003942 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003943
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003944 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003945 addedApps != null && mAppsCustomizeContent != null) {
3946 mAppsCustomizeContent.addApps(addedApps);
3947 }
Winson Chungd64d1762013-08-20 14:37:16 -07003948 }
3949
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003950 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003951 * Bind the items start-end from the list.
3952 *
3953 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003954 */
Winson Chung64359a52013-07-08 17:17:08 -07003955 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3956 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003957 Runnable r = new Runnable() {
3958 public void run() {
3959 bindItems(shortcuts, start, end, forceAnimateIcons);
3960 }
3961 };
3962 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003963 return;
3964 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003965
Winson Chungf0c6ae02012-03-21 16:10:31 -07003966 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003967 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3968 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003969 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003970 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003971 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003972 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003973 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003974
3975 // Short circuit if we are loading dock items for a configuration which has no dock
3976 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3977 mHotseat == null) {
3978 continue;
3979 }
3980
Joe Onorato9c1289c2009-08-17 11:03:03 -04003981 switch (item.itemType) {
3982 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3983 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003984 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003985 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003986
Winson Chung64359a52013-07-08 17:17:08 -07003987 /*
3988 * TODO: FIX collision case
3989 */
Winson Chungce376632013-07-11 16:12:41 -07003990 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3991 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3992 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3993 throw new RuntimeException("OCCUPIED");
3994 }
Winson Chung64359a52013-07-08 17:17:08 -07003995 }
3996
Adam Cohendcd297f2013-06-18 13:13:40 -07003997 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3998 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003999 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004000 // Animate all the applications up now
4001 shortcut.setAlpha(0f);
4002 shortcut.setScaleX(0f);
4003 shortcut.setScaleY(0f);
4004 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004005 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004006 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004007 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004008 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004009 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004010 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004011 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004012 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4013 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004014 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004015 default:
4016 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004017 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004018 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004019
Winson Chung997a9232013-07-24 15:33:46 -07004020 if (animateIcons) {
4021 // Animate to the correct page
4022 if (newShortcutsScreenId > -1) {
4023 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004024 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004025 final Runnable startBounceAnimRunnable = new Runnable() {
4026 public void run() {
4027 anim.playTogether(bounceAnims);
4028 anim.start();
4029 }
4030 };
Winson Chung997a9232013-07-24 15:33:46 -07004031 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004032 // We post the animation slightly delayed to prevent slowdowns
4033 // when we are loading right after we return to launcher.
4034 mWorkspace.postDelayed(new Runnable() {
4035 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004036 if (mWorkspace != null) {
4037 mWorkspace.snapToPage(newScreenIndex);
4038 mWorkspace.postDelayed(startBounceAnimRunnable,
4039 NEW_APPS_ANIMATION_DELAY);
4040 }
Winson Chung94d67682013-09-25 16:29:40 -07004041 }
4042 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004043 } else {
4044 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004045 }
4046 }
Winson Chung64359a52013-07-08 17:17:08 -07004047 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004048 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004049 }
4050
Joe Onorato9c1289c2009-08-17 11:03:03 -04004051 /**
4052 * Implementation of the method from LauncherModel.Callbacks.
4053 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004054 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004055 Runnable r = new Runnable() {
4056 public void run() {
4057 bindFolders(folders);
4058 }
4059 };
4060 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004061 return;
4062 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004063 sFolders.clear();
4064 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004065 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004066
4067 /**
4068 * Add the views for a widget to the workspace.
4069 *
4070 * Implementation of the method from LauncherModel.Callbacks.
4071 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004072 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004073 Runnable r = new Runnable() {
4074 public void run() {
4075 bindAppWidget(item);
4076 }
4077 };
4078 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004079 return;
4080 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004081
Daniel Sandler843e8602010-06-07 14:59:01 -04004082 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4083 if (DEBUG_WIDGETS) {
4084 Log.d(TAG, "bindAppWidget: " + item);
4085 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004086 final Workspace workspace = mWorkspace;
4087
4088 final int appWidgetId = item.appWidgetId;
4089 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004090 if (DEBUG_WIDGETS) {
4091 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4092 }
4093
Joe Onorato9c1289c2009-08-17 11:03:03 -04004094 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4095
Joe Onorato9c1289c2009-08-17 11:03:03 -04004096 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004097 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004098
Adam Cohendcd297f2013-06-18 13:13:40 -07004099 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004100 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004101 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4102
Joe Onorato9c1289c2009-08-17 11:03:03 -04004103 workspace.requestLayout();
4104
Daniel Sandler843e8602010-06-07 14:59:01 -04004105 if (DEBUG_WIDGETS) {
4106 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4107 + (SystemClock.uptimeMillis()-start) + "ms");
4108 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004109 }
4110
Adam Cohen1462de32012-07-24 22:34:36 -07004111 public void onPageBoundSynchronously(int page) {
4112 mSynchronouslyBoundPages.add(page);
4113 }
4114
Joe Onorato9c1289c2009-08-17 11:03:03 -04004115 /**
4116 * Callback saying that there aren't any more items to bind.
4117 *
4118 * Implementation of the method from LauncherModel.Callbacks.
4119 */
Adam Cohene25af792013-06-06 23:08:25 -07004120 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004121 Runnable r = new Runnable() {
4122 public void run() {
4123 finishBindingItems(upgradePath);
4124 }
4125 };
4126 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004127 return;
4128 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004129 if (mSavedState != null) {
4130 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004131 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004132 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004133 mSavedState = null;
4134 }
4135
Adam Cohen1462de32012-07-24 22:34:36 -07004136 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004137
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004138 // If we received the result of any pending adds while the loader was running (e.g. the
4139 // widget configuration forced an orientation change), process them now.
4140 for (int i = 0; i < sPendingAddList.size(); i++) {
4141 completeAdd(sPendingAddList.get(i));
4142 }
4143 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004144
Winson Chungc51db6a2011-10-05 11:44:49 -07004145 // Update the market app icon as necessary (the other icons will be managed in response to
4146 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004147 if (!DISABLE_MARKET_BUTTON) {
4148 updateAppMarketIcon();
4149 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004150
Winson Chungf0c6ae02012-03-21 16:10:31 -07004151 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004152 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004153 mWorkspace.getUniqueComponents(true, null);
4154 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004155 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004156 }
4157
Winson Chunga0b7e862013-09-05 16:03:15 -07004158 public boolean isAllAppsButtonRank(int rank) {
4159 if (mHotseat != null) {
4160 return mHotseat.isAllAppsButtonRank(rank);
4161 }
4162 return false;
4163 }
4164
Winson Chunga2413752012-04-03 14:22:34 -07004165 private boolean canRunNewAppsAnimation() {
4166 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4167 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4168 }
4169
Winson Chungc9168342013-06-26 14:54:55 -07004170 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4171 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4172 PropertyValuesHolder.ofFloat("alpha", 1f),
4173 PropertyValuesHolder.ofFloat("scaleX", 1f),
4174 PropertyValuesHolder.ofFloat("scaleY", 1f));
4175 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4176 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4177 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4178 return bounceAnim;
4179 }
4180
Adam Cohen27772732013-11-11 14:00:56 +00004181 public boolean useVerticalBarLayout() {
4182 return LauncherAppState.getInstance().getDynamicGrid().
4183 getDeviceProfile().isVerticalBarLayout();
4184 }
4185
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004186 protected Rect getSearchBarBounds() {
4187 return LauncherAppState.getInstance().getDynamicGrid().
4188 getDeviceProfile().getSearchBarBounds();
4189 }
4190
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004191 @Override
4192 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004193 boolean searchVisible = updateGlobalSearchIcon();
4194 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004195 if (mSearchDropTargetBar != null) {
4196 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4197 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004198 }
4199
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004200 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004201 * Add the icons for all apps.
4202 *
4203 * Implementation of the method from LauncherModel.Callbacks.
4204 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004205 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004206 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004207 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4208 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4209 getHotseat().addAllAppsFolder(mIconCache, apps,
4210 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4211 }
4212 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004213 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004214 if (mAppsCustomizeContent != null) {
4215 mAppsCustomizeContent.onPackagesUpdated(
4216 LauncherModel.getSortedWidgetsAndShortcuts(this));
4217 }
Winson Chungc58497e2013-09-03 17:48:37 -07004218 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004219 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004220 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004221 mAppsCustomizeContent.onPackagesUpdated(
4222 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004223 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004224 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004225 }
4226
4227 /**
4228 * A package was updated.
4229 *
4230 * Implementation of the method from LauncherModel.Callbacks.
4231 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004232 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004233 Runnable r = new Runnable() {
4234 public void run() {
4235 bindAppsUpdated(apps);
4236 }
4237 };
4238 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004239 return;
4240 }
4241
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004242 if (mWorkspace != null) {
4243 mWorkspace.updateShortcuts(apps);
4244 }
Winson Chungc58497e2013-09-03 17:48:37 -07004245
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004246 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004247 mAppsCustomizeContent != null) {
4248 mAppsCustomizeContent.updateApps(apps);
4249 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004250 }
4251
4252 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004253 * Update the state of a package, typically related to install state.
4254 *
4255 * Implementation of the method from LauncherModel.Callbacks.
4256 */
4257 public void updatePackageState(String pkgName, int state) {
4258 if (mWorkspace != null) {
4259 mWorkspace.updatePackageState(pkgName, state);
4260 }
4261 }
4262
4263 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004264 * A package was uninstalled. We take both the super set of packageNames
4265 * in addition to specific applications to remove, the reason being that
4266 * this can be called when a package is updated as well. In that scenario,
4267 * we only remove specific components from the workspace, where as
4268 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004269 *
4270 * Implementation of the method from LauncherModel.Callbacks.
4271 */
Winson Chung83892cc2013-05-01 16:53:33 -07004272 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004273 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004274 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004275 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004276 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004277 }
Winson Chungd64d1762013-08-20 14:37:16 -07004278 };
4279 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004280 return;
4281 }
4282
Winson Chungdf95eb12013-10-16 14:57:07 -07004283 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004284 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004285 }
4286 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004287 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004288 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004289
Winson Chunga1820962011-10-03 16:31:06 -07004290 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004291 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004292
Winson Chungdf95eb12013-10-16 14:57:07 -07004293 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004294 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004295 mAppsCustomizeContent != null) {
4296 mAppsCustomizeContent.removeApps(appInfos);
4297 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004298 }
Joe Onoratobe386092009-11-17 17:32:16 -08004299
4300 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004301 * A number of packages were updated.
4302 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004303 private ArrayList<Object> mWidgetsAndShortcuts;
4304 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004305 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004306 bindPackagesUpdated(mWidgetsAndShortcuts);
4307 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004308 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004309 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004310 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4311 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4312 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004313 return;
4314 }
4315
Winson Chung64359a52013-07-08 17:17:08 -07004316 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004317 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004318 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004319 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004320 }
4321
Winson Chung400438b2011-01-16 17:53:48 -08004322 private int mapConfigurationOriActivityInfoOri(int configOri) {
4323 final Display d = getWindowManager().getDefaultDisplay();
4324 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4325 switch (d.getRotation()) {
4326 case Surface.ROTATION_0:
4327 case Surface.ROTATION_180:
4328 // We are currently in the same basic orientation as the natural orientation
4329 naturalOri = configOri;
4330 break;
4331 case Surface.ROTATION_90:
4332 case Surface.ROTATION_270:
4333 // We are currently in the other basic orientation to the natural orientation
4334 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4335 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4336 break;
4337 }
4338
4339 int[] oriMap = {
4340 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4341 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4342 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4343 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4344 };
4345 // Since the map starts at portrait, we need to offset if this device's natural orientation
4346 // is landscape.
4347 int indexOffset = 0;
4348 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4349 indexOffset = 1;
4350 }
4351 return oriMap[(d.getRotation() + indexOffset) % 4];
4352 }
Adam Cohen446e9402011-09-15 18:21:21 -07004353
Winson Chung4b919f82012-05-01 10:44:08 -07004354 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004355 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004356 getResources().getBoolean(R.bool.allow_rotation);
4357 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004358 }
Winson Chung4b919f82012-05-01 10:44:08 -07004359 public void lockScreenOrientation() {
4360 if (isRotationEnabled()) {
4361 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4362 .getConfiguration().orientation));
4363 }
4364 }
4365 public void unlockScreenOrientation(boolean immediate) {
4366 if (isRotationEnabled()) {
4367 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004368 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004369 } else {
4370 mHandler.postDelayed(new Runnable() {
4371 public void run() {
4372 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4373 }
4374 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004375 }
Winson Chung4b919f82012-05-01 10:44:08 -07004376 }
Winson Chung400438b2011-01-16 17:53:48 -08004377 }
4378
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004379 /**
4380 * Called when the SearchBar hint should be changed.
4381 *
4382 * @param hint the hint to be displayed in the search bar.
4383 */
4384 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004385 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004386 }
4387
Winson Chunge43a1e72014-01-15 10:33:02 -08004388 protected boolean isLauncherPreinstalled() {
4389 PackageManager pm = getPackageManager();
4390 try {
4391 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4392 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4393 return true;
4394 } else {
4395 return false;
4396 }
4397 } catch (NameNotFoundException e) {
4398 e.printStackTrace();
4399 return false;
4400 }
4401 }
4402
Winson Chunge6eabff2013-09-24 11:01:23 -07004403 protected String getFirstRunClingSearchBarHint() {
4404 return "";
4405 }
4406 protected String getFirstRunCustomContentHint() {
4407 return "";
4408 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004409 protected int getFirstRunFocusedHotseatAppDrawableId() {
4410 return -1;
4411 }
4412 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4413 return null;
4414 }
4415 protected int getFirstRunFocusedHotseatAppRank() {
4416 return -1;
4417 }
4418 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4419 return "";
4420 }
4421 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4422 return "";
4423 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004424
Winson Chungaf40f202013-09-18 18:26:31 -07004425 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004426 mLauncherClings.dismissFirstRunCling(v);
4427 }
4428 public void dismissMigrationClingCopyApps(View v) {
4429 mLauncherClings.dismissMigrationClingCopyApps(v);
4430 }
4431 public void dismissMigrationClingUseDefault(View v) {
4432 mLauncherClings.dismissMigrationClingUseDefault(v);
4433 }
4434 public void dismissMigrationWorkspaceCling(View v) {
4435 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004436 }
Winson Chung82f55532011-08-09 14:14:23 -07004437 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004438 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004439 }
4440 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004441 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004442 }
4443
Winson Chunga6945242014-01-08 14:04:34 -08004444 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004445 return !ActivityManager.isRunningInTestHarness() &&
4446 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004447 }
4448
4449 public void showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004450 if (shouldRunFirstRunActivity() &&
4451 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004452 Intent firstRunIntent = getFirstRunActivity();
4453 if (firstRunIntent != null) {
4454 startActivity(firstRunIntent);
4455 markFirstRunActivityShown();
4456 }
4457 }
4458 }
4459
4460 private void markFirstRunActivityShown() {
4461 SharedPreferences.Editor editor = mSharedPrefs.edit();
4462 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4463 editor.apply();
4464 }
4465
4466 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004467 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4468 if (mHotseat != null) mHotseat.setAlpha(1f);
4469 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4470 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004471 }
4472
4473 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004474 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4475 if (mHotseat != null) mHotseat.setAlpha(0f);
4476 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4477 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004478 }
4479
4480
Mathew Inwood72fbec12013-11-19 15:45:07 +00004481 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4482 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4483 if (ri == null) {
4484 return null;
4485 }
4486 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4487 }
4488
4489 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4490 Bitmap icon) {
4491 return new ShortcutInfo(shortcutIntent, caption, icon);
4492 }
4493
4494 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4495 dragView.setTag(dragInfo);
4496 mWorkspace.onDragStartedWithItem(dragView);
4497 mWorkspace.beginDragShared(dragView, source);
4498 }
4499
Anjali Koppalf5d29132014-02-28 13:33:27 -08004500 @Override
4501 public void onPageSwitch(View newPage, int newPageIndex) {
4502 }
4503
Winson Chung80baf5a2010-08-09 16:03:15 -07004504 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004505 * Prints out out state for debugging.
4506 */
4507 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004508 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004509 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004510 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4511 Log.d(TAG, "mRestoring=" + mRestoring);
4512 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4513 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004514 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004515 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004516
Winson Chung785d2eb2011-04-14 16:08:02 -07004517 if (mAppsCustomizeContent != null) {
4518 mAppsCustomizeContent.dumpState();
4519 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004520 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004521 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004522
4523 @Override
4524 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4525 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004526 synchronized (sDumpLogs) {
4527 writer.println(" ");
4528 writer.println("Debug logs: ");
4529 for (int i = 0; i < sDumpLogs.size(); i++) {
4530 writer.println(" " + sDumpLogs.get(i));
4531 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004532 }
4533 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004534
4535 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004536 if (DEBUG_DUMP_LOG) {
4537 synchronized (sDumpLogs) {
4538 Log.d(TAG, "");
4539 Log.d(TAG, "*********************");
4540 Log.d(TAG, "Launcher debug logs: ");
4541 for (int i = 0; i < sDumpLogs.size(); i++) {
4542 Log.d(TAG, " " + sDumpLogs.get(i));
4543 }
4544 Log.d(TAG, "*********************");
4545 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004546 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004547 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004548 }
4549
4550 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004551 addDumpLog(tag, log, null, debugLog);
4552 }
4553
4554 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004555 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004556 if (e != null) {
4557 Log.d(tag, log, e);
4558 } else {
4559 Log.d(tag, log);
4560 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004561 }
Winson Chungede41292013-09-19 16:27:36 -07004562 if (DEBUG_DUMP_LOG) {
4563 sDateStamp.setTime(System.currentTimeMillis());
4564 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004565 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4566 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004567 }
Winson Chungede41292013-09-19 16:27:36 -07004568 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004569 }
4570
Winson Chungede41292013-09-19 16:27:36 -07004571 public void dumpLogsToLocalData() {
4572 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004573 new AsyncTask<Void, Void, Void>() {
4574 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004575 boolean success = false;
4576 sDateStamp.setTime(sRunStart);
4577 String FILENAME = sDateStamp.getMonth() + "-"
4578 + sDateStamp.getDay() + "_"
4579 + sDateStamp.getHours() + "-"
4580 + sDateStamp.getMinutes() + "_"
4581 + sDateStamp.getSeconds() + ".txt";
4582
4583 FileOutputStream fos = null;
4584 File outFile = null;
4585 try {
4586 outFile = new File(getFilesDir(), FILENAME);
4587 outFile.createNewFile();
4588 fos = new FileOutputStream(outFile);
4589 } catch (Exception e) {
4590 e.printStackTrace();
4591 }
4592 if (fos != null) {
4593 PrintWriter writer = new PrintWriter(fos);
4594
4595 writer.println(" ");
4596 writer.println("Debug logs: ");
4597 synchronized (sDumpLogs) {
4598 for (int i = 0; i < sDumpLogs.size(); i++) {
4599 writer.println(" " + sDumpLogs.get(i));
4600 }
4601 }
4602 writer.close();
4603 }
4604 try {
4605 if (fos != null) {
4606 fos.close();
4607 success = true;
4608 }
4609 } catch (IOException e) {
4610 e.printStackTrace();
4611 }
Michael Jurka43467462013-11-14 12:03:58 +01004612 return null;
Winson Chungede41292013-09-19 16:27:36 -07004613 }
Michael Jurka43467462013-11-14 12:03:58 +01004614 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004615 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004616 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004617}
Michael Jurka2763be32011-02-24 11:19:57 -08004618
Michael Jurkaabded662011-03-04 12:06:57 -08004619interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004620 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004621 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004622 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004623 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004624 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004625}
Dan Sandlerd5024042014-01-09 15:01:33 -05004626
4627interface DebugIntents {
4628 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4629 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004630}