blob: f03ffc6e8b84dd20ed27bcb422580919891a608b [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;
Romain Guyedcce092010-03-04 13:03:17 -080099
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.io.DataInputStream;
101import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700102import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700103import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700105import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700107import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700108import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700110import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700111import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700113import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000114import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115
Winson Chunga6945242014-01-08 14:04:34 -0800116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100120public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700122 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Daniel Sandlerf061f822013-06-27 13:59:36 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700128 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400129 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700130 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700131
Dan Sandlerd5024042014-01-09 15:01:33 -0500132 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int REQUEST_PICK_APPLICATION = 6;
137 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700138 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700139 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurka8b805b12012-04-18 14:23:14 -0700141 private static final int REQUEST_BIND_APPWIDGET = 11;
142
Mathew Inwood876a8462013-06-14 14:12:41 +0100143 /**
144 * IntentStarter uses request codes starting with this. This must be greater than all activity
145 * request codes used internally.
146 */
147 protected static final int REQUEST_LAST = 100;
148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
150
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800151 static final int SCREEN_COUNT = 5;
152 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Romain Guy98d01652009-06-30 16:21:04 -0700154 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800155 // To turn on these properties, type
156 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
157 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
158 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800159 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Winson Chung2672ff92012-05-04 16:22:30 -0700161 // The Intent extra that defines whether to ignore the launch animation
162 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400163 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: int
166 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700167 // Type: int
168 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700170 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
171 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
173 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: boolean
178 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
179 // Type: long
180 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700181 // Type: int
182 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
183 // Type: int
184 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
185 // Type: parcelable
186 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000187 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800188 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000189 // Type: int[]
190 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
Adam Cohenb54a5982014-01-08 15:21:04 -0800192
193 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
194
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100195 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700196 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100197 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700198 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100199 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700200
Adam Cohen39a06042013-07-19 14:30:12 -0700201 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700202 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700203
Winson Chunga6945242014-01-08 14:04:34 -0800204 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
205
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700206 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700207 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700208 private State mState = State.WORKSPACE;
209 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700210
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700212 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
213 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800216 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000218 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
219 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
220
Winson Chunga2413752012-04-03 14:22:34 -0700221 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700222 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
223 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700224 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700225
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800226 private final BroadcastReceiver mCloseSystemDialogsReceiver
227 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800228 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 private LayoutInflater mInflater;
231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700233 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800234 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800235 private DragLayer mDragLayer;
236 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700237 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800238 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700239
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700240 private AppWidgetManager mAppWidgetManager;
241 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700242
Michael Jurkac9d95c52011-08-29 14:03:34 -0700243 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700244 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800245 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700246
Michael Jurka0280c3b2010-09-17 15:00:07 -0700247 private int[] mTmpAddItemCellCoordinates = new int[2];
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private FolderInfo mFolderInfo;
250
Winson Chung3d503fb2011-07-13 17:25:49 -0700251 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800252 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700253
Michael Jurka838a4ca2011-02-07 13:33:06 -0800254 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700255
Winson Chungc51db6a2011-10-05 11:44:49 -0700256 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700257 private AppsCustomizeTabHost mAppsCustomizeTabHost;
258 private AppsCustomizePagedView mAppsCustomizeContent;
259 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800260 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700263 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
264 // scroll issues (because the workspace may not have been measured yet) and extra work.
265 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
266 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267
268 private SpannableStringBuilder mDefaultKeySsb = null;
269
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270 private boolean mWorkspaceLoading = true;
271
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800272 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 private boolean mRestoring;
274 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700275 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
Michael Jurka1e2f4652013-07-08 18:03:46 -0700277 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700278 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
279
Winson Chung4a2afa32012-07-19 14:53:05 -0700280 // Keep track of whether the user has left launcher
281 private static boolean sPausedFromUserAction = false;
282
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 private Bundle mSavedInstanceState;
284
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800286 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800287 private boolean mUserPresent = true;
288 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700289 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800290 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700292 private static LocaleConfiguration sLocaleConfiguration = null;
293
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700294 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700295
Adam Cohen61f560d2013-09-30 15:58:20 -0700296 private View.OnTouchListener mHapticFeedbackTouchListener;
297
Adam Cohended9f8d2010-11-03 13:25:16 -0700298 // Related to the auto-advancing of widgets
299 private final int ADVANCE_MSG = 1;
300 private final int mAdvanceInterval = 20000;
301 private final int mAdvanceStagger = 250;
302 private long mAutoAdvanceSentTime;
303 private long mAutoAdvanceTimeLeft = -1;
304 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
305 new HashMap<View, AppWidgetProviderInfo>();
306
Winson Chung400438b2011-01-16 17:53:48 -0800307 // Determines how long to wait after a rotation before restoring the screen orientation to
308 // match the sensor state.
309 private final int mRestoreScreenOrientationDelay = 500;
310
Michael Jurka4ef207b2010-11-29 17:05:45 -0800311 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700312 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
313 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
314 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800315
Winson Chungd64a6662013-09-30 11:06:59 -0700316 private Intent mAppMarketIntent = null;
317 private static final boolean DISABLE_MARKET_BUTTON = true;
318
Craig Mautner360310b2012-10-26 15:13:08 -0700319 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700320
Adam Cohen1462de32012-07-24 22:34:36 -0700321 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700322 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700323
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700324 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700325 static Date sDateStamp = new Date();
326 static DateFormat sDateFormat =
327 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
328 static long sRunStart = System.currentTimeMillis();
329 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700330
Winson Chung46353de2012-02-16 14:05:10 -0800331 // We only want to get the SharedPreferences once since it does an FS stat each time we get
332 // it from the context.
333 private SharedPreferences mSharedPrefs;
334
Adam Cohene25af792013-06-06 23:08:25 -0700335 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
336
Winson Chungf0c6ae02012-03-21 16:10:31 -0700337 // Holds the page that we need to animate to, and the icon views that we need to animate up
338 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700339 private ImageView mFolderIconImageView;
340 private Bitmap mFolderIconBitmap;
341 private Canvas mFolderIconCanvas;
342 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700343
Michael Jurkaddd62e92011-02-16 17:49:14 -0800344 private BubbleTextView mWaitingForResume;
345
Michael Jurkac1f5d262011-09-30 19:32:27 -0700346 private Runnable mBuildLayersRunnable = new Runnable() {
347 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700348 if (mWorkspace != null) {
349 mWorkspace.buildPageHardwareLayers();
350 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700351 }
352 };
353
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800354 private static ArrayList<PendingAddArguments> sPendingAddList
355 = new ArrayList<PendingAddArguments>();
356
Michael Jurka7267fa52013-09-26 15:29:57 -0700357 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800358
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800359 private static class PendingAddArguments {
360 int requestCode;
361 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700362 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700363 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800364 int cellX;
365 int cellY;
366 }
367
Daniel Sandlerff02d492013-08-05 02:12:05 -0400368 private Stats mStats;
369
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800370 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800371 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700372 }
373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 @Override
375 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700376 if (DEBUG_STRICT_MODE) {
377 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
378 .detectDiskReads()
379 .detectDiskWrites()
380 .detectNetwork() // or .detectAll() for all detectable problems
381 .penaltyLog()
382 .build());
383 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
384 .detectLeakedSqlLiteObjects()
385 .detectLeakedClosableObjects()
386 .penaltyLog()
387 .penaltyDeath()
388 .build());
389 }
390
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400392
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400393 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400394 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700395
396 // Determine the dynamic grid properties
397 Point smallestSize = new Point();
398 Point largestSize = new Point();
399 Point realSize = new Point();
400 Display display = getWindowManager().getDefaultDisplay();
401 display.getCurrentSizeRange(smallestSize, largestSize);
402 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700403 DisplayMetrics dm = new DisplayMetrics();
404 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700405
Winson Chung5f8afe62013-08-12 16:19:28 -0700406 // Lazy-initialize the dynamic grid
407 DeviceProfile grid = app.initDynamicGrid(this,
408 Math.min(smallestSize.x, smallestSize.y),
409 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700410 realSize.x, realSize.y,
411 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700412
413 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400414 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700415 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800416 mModel = app.setLauncher(this);
417 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700418 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400419 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800420 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700422
Daniel Sandlerff02d492013-08-05 02:12:05 -0400423 mStats = new Stats(this);
424
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700425 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700426
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700427 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
428 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700430 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
431 // this also ensures that any synchronous binding below doesn't re-trigger another
432 // LauncherModel load.
433 mPaused = false;
434
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200436 android.os.Debug.startMethodTracing(
437 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 }
439
Adam Cohen53805212013-10-01 10:39:23 -0700440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100445 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800447 registerContentObservers();
448
Joe Onorato7c312c12009-08-13 21:36:53 -0700449 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700450
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 mSavedState = savedInstanceState;
452 restoreState(mSavedState);
453
454 if (PROFILE_STARTUP) {
455 android.os.Debug.stopMethodTracing();
456 }
457
458 if (!mRestoring) {
Winson Chung6e1c0d32013-10-25 15:24:24 -0700459 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE || sPausedFromUserAction) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700460 // If the user leaves launcher, then we should just load items asynchronously when
461 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500462 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 } else {
464 // We only load the page synchronously if the user rotates (or triggers a
465 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800466 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 }
469
470 // For handling default keys
471 mDefaultKeySsb = new SpannableStringBuilder();
472 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800473
474 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
475 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800476
Adam Cohenf9426d52012-06-04 17:26:21 -0700477 updateGlobalIcons();
478
479 // On large interfaces, we want the screen to auto-rotate based on the current orientation
480 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700481
Winson Chunge43a1e72014-01-15 10:33:02 -0800482 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
483 // on the device, then we always show the first run cling experience (or if there is no
484 // launcher2). Otherwise, we prompt the user upon started for migration
Adam Cohenb54a5982014-01-08 15:21:04 -0800485 showFirstRunActivity();
Winson Chunge43a1e72014-01-15 10:33:02 -0800486 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
487 if (mModel.canMigrateFromOldLauncherDb(this)) {
488 mLauncherClings.showMigrationCling();
489 } else {
490 mLauncherClings.showFirstRunCling();
491 }
Winson Chunga6945242014-01-08 14:04:34 -0800492 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800493 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800494 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700495 }
496
Winson Chung4a2afa32012-07-19 14:53:05 -0700497 protected void onUserLeaveHint() {
498 super.onUserLeaveHint();
499 sPausedFromUserAction = true;
500 }
501
Winson Chung98ca0f72013-07-29 12:58:51 -0700502 /** To be overriden by subclasses to hint to Launcher that we have custom content */
503 protected boolean hasCustomContentToLeft() {
504 return false;
505 }
506
Dave Hawkeya8881582013-09-17 15:55:33 -0600507 /**
508 * To be overridden by subclasses to create the custom content and call
509 * {@link #addToCustomContentPage}. This will only be invoked if
510 * {@link #hasCustomContentToLeft()} is {@code true}.
511 */
512 protected void addCustomContentToLeft() {
513 }
514
515 /**
Adam Cohenb54a5982014-01-08 15:21:04 -0800516 * To be overridden by subclasses to indicate that there is an activity to launch
517 * before showing the standard launcher experience.
518 */
519 protected boolean hasFirstRunActivity() {
520 return false;
521 }
522
523 /**
524 * To be overridden by subclasses to launch any first run activity
525 */
526 protected Intent getFirstRunActivity() {
527 return null;
528 }
529
530 /**
Dave Hawkeya8881582013-09-17 15:55:33 -0600531 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
532 * ensure the custom content page is added or removed if necessary.
533 */
534 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600535 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600536 // Not bound yet, wait for bindScreens to be called.
537 return;
538 }
539
540 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
541 // Create the custom content page and call the subclass to populate it.
542 mWorkspace.createCustomContentPage();
543 addCustomContentToLeft();
544 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
545 mWorkspace.removeCustomContentPage();
546 }
547 }
548
Adam Cohenf9426d52012-06-04 17:26:21 -0700549 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700550 boolean searchVisible = false;
551 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800552 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700553 int coi = getCurrentOrientationIndexForGlobalIcons();
554 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
555 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700556 if (!DISABLE_MARKET_BUTTON) {
557 updateAppMarketIcon();
558 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700559 searchVisible = updateGlobalSearchIcon();
560 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700561 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700562 if (sGlobalSearchIcon[coi] != null) {
563 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700564 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700565 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700566 if (sVoiceSearchIcon[coi] != null) {
567 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700568 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700569 }
Winson Chungd64a6662013-09-30 11:06:59 -0700570 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700571 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800572 }
Winson Chungadf0c182012-08-23 14:59:07 -0700573 if (mSearchDropTargetBar != null) {
574 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
575 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 }
Romain Guycbb89e42009-06-08 15:52:54 -0700577
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700579 if (sLocaleConfiguration == null) {
580 new AsyncTask<Void, Void, LocaleConfiguration>() {
581 @Override
582 protected LocaleConfiguration doInBackground(Void... unused) {
583 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
584 readConfiguration(Launcher.this, localeConfiguration);
585 return localeConfiguration;
586 }
587
588 @Override
589 protected void onPostExecute(LocaleConfiguration result) {
590 sLocaleConfiguration = result;
591 checkForLocaleChange(); // recursive, but now with a locale configuration
592 }
593 }.execute();
594 return;
595 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700596
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 final Configuration configuration = getResources().getConfiguration();
598
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700599 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 final String locale = configuration.locale.toString();
601
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700602 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 final int mcc = configuration.mcc;
604
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700605 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 final int mnc = configuration.mnc;
607
Romain Guy84f296c2009-11-04 15:00:44 -0800608 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609
Romain Guy84f296c2009-11-04 15:00:44 -0800610 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700611 sLocaleConfiguration.locale = locale;
612 sLocaleConfiguration.mcc = mcc;
613 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700614
Joe Onorato0589f0f2010-02-08 13:44:00 -0800615 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700616
617 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100618 new AsyncTask<Void, Void, Void>() {
619 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700620 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100621 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700622 }
Michael Jurka43467462013-11-14 12:03:58 +0100623 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700624 }
625 }
626
627 private static class LocaleConfiguration {
628 public String locale;
629 public int mcc = -1;
630 public int mnc = -1;
631 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700632
Romain Guy98d01652009-06-30 16:21:04 -0700633 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
634 DataInputStream in = null;
635 try {
636 in = new DataInputStream(context.openFileInput(PREFERENCES));
637 configuration.locale = in.readUTF();
638 configuration.mcc = in.readInt();
639 configuration.mnc = in.readInt();
640 } catch (FileNotFoundException e) {
641 // Ignore
642 } catch (IOException e) {
643 // Ignore
644 } finally {
645 if (in != null) {
646 try {
647 in.close();
648 } catch (IOException e) {
649 // Ignore
650 }
651 }
652 }
653 }
654
655 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
656 DataOutputStream out = null;
657 try {
658 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
659 out.writeUTF(configuration.locale);
660 out.writeInt(configuration.mcc);
661 out.writeInt(configuration.mnc);
662 out.flush();
663 } catch (FileNotFoundException e) {
664 // Ignore
665 } catch (IOException e) {
666 //noinspection ResultOfMethodCallIgnored
667 context.getFileStreamPath(PREFERENCES).delete();
668 } finally {
669 if (out != null) {
670 try {
671 out.close();
672 } catch (IOException e) {
673 // Ignore
674 }
675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
677 }
678
Anton Hanssona2f665f2013-09-26 12:18:32 +0100679 public Stats getStats() {
680 return mStats;
681 }
682
Bjorn Bringertc459e522013-06-07 19:36:01 +0100683 public LayoutInflater getInflater() {
684 return mInflater;
685 }
686
Winson Chung36a62fe2012-05-06 18:04:42 -0700687 boolean isDraggingEnabled() {
688 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
689 // that is subsequently removed from the workspace in startBinding().
690 return !mModel.isLoadingWorkspace();
691 }
692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 static int getScreen() {
694 synchronized (sLock) {
695 return sScreen;
696 }
697 }
698
699 static void setScreen(int screen) {
700 synchronized (sLock) {
701 sScreen = screen;
702 }
703 }
704
Winson Chung557d6ed2011-07-08 15:34:52 -0700705 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000706 * Copied from View -- the View version of the method isn't called
707 * anywhere else in our process and only exists for API level 17+,
708 * so it's ok to keep our own version with no API requirement.
709 */
710 public static int generateViewId() {
711 for (;;) {
712 final int result = sNextGeneratedId.get();
713 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
714 int newValue = result + 1;
715 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
716 if (sNextGeneratedId.compareAndSet(result, newValue)) {
717 return result;
718 }
719 }
720 }
721
722 public int getViewIdForItem(ItemInfo info) {
723 // This cast is safe given the > 2B range for int.
724 int itemId = (int) info.id;
725 if (mItemIdToViewId.containsKey(itemId)) {
726 return mItemIdToViewId.get(itemId);
727 }
728 int viewId = generateViewId();
729 mItemIdToViewId.put(itemId, viewId);
730 return viewId;
731 }
732
733 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700734 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
735 * a configuration step, this allows the proper animations to run after other transitions.
736 */
737 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700738 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800739 switch (args.requestCode) {
740 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700741 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700742 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800743 break;
744 case REQUEST_PICK_SHORTCUT:
745 processShortcut(args.intent);
746 break;
747 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700748 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700749 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700750 result = true;
751 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800752 case REQUEST_CREATE_APPWIDGET:
753 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700754 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700755 result = true;
756 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800757 }
Michael Jurka27614d22012-04-02 06:40:22 -0700758 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
759 // if you turned the screen off and then back while in All Apps, Launcher would not
760 // return to the workspace. Clearing mAddInfo.container here fixes this issue
761 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700762 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800763 }
764
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700766 protected void onActivityResult(
767 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700768 // Reset the startActivity waiting flag
769 mWaitingForResult = false;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800770 int pendingAddWidgetId = mPendingAddWidgetId;
771 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700772
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 Runnable exitSpringLoaded = new Runnable() {
774 @Override
775 public void run() {
776 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
777 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
778 }
779 };
780
Michael Jurka8b805b12012-04-18 14:23:14 -0700781 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700782 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700783 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
784 if (resultCode == RESULT_CANCELED) {
785 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700786 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
787 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700788 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700789 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
790 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700791 }
792 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200793 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
794 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
795 mWorkspace.exitOverviewMode(false);
796 }
797 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700798 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200799
Adam Cohened66b2b2012-01-23 17:28:51 -0800800 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
801 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700802
Adam Cohened66b2b2012-01-23 17:28:51 -0800803 // We have special handling for widgets
804 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800805 final int appWidgetId;
806 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
807 : -1;
808 if (widgetId < 0) {
809 appWidgetId = pendingAddWidgetId;
810 } else {
811 appWidgetId = widgetId;
812 }
813
Adam Cohenad4e15c2013-10-17 16:21:35 -0700814 final int result;
815 final Runnable onComplete;
Adam Cohenf0129b12013-11-04 17:57:36 -0800816 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Winson Chung5aaab772012-04-27 15:24:02 -0700817 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
818 "widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700819 result = RESULT_CANCELED;
820 completeTwoStageWidgetDrop(result, appWidgetId);
821 onComplete = new Runnable() {
822 @Override
823 public void run() {
824 exitSpringLoadedDragModeDelayed(false, 0, null);
825 }
826 };
Winson Chung5aaab772012-04-27 15:24:02 -0700827 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700828 result = resultCode;
Adam Cohen917e3882013-10-31 15:03:35 -0700829 final CellLayout dropLayout =
830 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
831 dropLayout.setDropPending(true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700832 onComplete = new Runnable() {
833 @Override
834 public void run() {
835 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohen917e3882013-10-31 15:03:35 -0700836 dropLayout.setDropPending(false);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700837 }
838 };
Winson Chung5aaab772012-04-27 15:24:02 -0700839 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700840 mWorkspace.removeExtraEmptyScreen(true, onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY,
841 false);
Adam Cohened66b2b2012-01-23 17:28:51 -0800842 return;
843 }
844
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 // The pattern used here is that a user PICKs a specific application,
846 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700847
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
849 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700850 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800851 final PendingAddArguments args = new PendingAddArguments();
852 args.requestCode = requestCode;
853 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700854 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700855 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700856 args.cellX = mPendingAddInfo.cellX;
857 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800858 if (isWorkspaceLocked()) {
859 sPendingAddList.add(args);
860 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700861 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 }
Adam Cohenad4e15c2013-10-17 16:21:35 -0700863 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
864 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Adam Cohen00074722013-10-11 17:46:09 -0700865 } else if (resultCode == RESULT_CANCELED) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700866 mWorkspace.removeExtraEmptyScreen(true, exitSpringLoaded,
867 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800870 }
871
872 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700873 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700874 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800875 Runnable onCompleteRunnable = null;
876 int animationType = 0;
877
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700878 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800879 if (resultCode == RESULT_OK) {
880 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
881 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700882 mPendingAddWidgetInfo);
883 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800884 onCompleteRunnable = new Runnable() {
885 @Override
886 public void run() {
887 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700888 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700889 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
890 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800891 }
892 };
893 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800894 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800895 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800896 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700897 if (mDragLayer.getAnimatedView() != null) {
898 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
899 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
900 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700901 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700902 // The animated view may be null in the case of a rotation during widget configuration
903 onCompleteRunnable.run();
904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
906
907 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700908 protected void onStop() {
909 super.onStop();
910 FirstFrameAnimatorHelper.setIsVisible(false);
911 }
912
913 @Override
914 protected void onStart() {
915 super.onStart();
916 FirstFrameAnimatorHelper.setIsVisible(true);
917 }
918
919 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200921 long startTime = 0;
922 if (DEBUG_RESUME_TIME) {
923 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400924 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700927
Winson Chung4a2afa32012-07-19 14:53:05 -0700928 // Restore the previous launcher state
929 if (mOnResumeState == State.WORKSPACE) {
930 showWorkspace(false);
931 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800932 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700933 }
934 mOnResumeState = State.NONE;
935
Craig Mautner360310b2012-10-26 15:13:08 -0700936 // Background was set to gradient in onPause(), restore to black if in all apps.
937 setWorkspaceBackground(mState == State.WORKSPACE);
938
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800939 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700940 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700941 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400942 mWorkspaceLoading = true;
Derek Prothro7aff3992013-12-10 14:00:37 -0500943 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400944 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700945 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700947 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200948 // We might have postponed some bind calls until onResume (see waitUntilResume) --
949 // execute them here
950 long startTimeCallbacks = 0;
951 if (DEBUG_RESUME_TIME) {
952 startTimeCallbacks = System.currentTimeMillis();
953 }
954
955 if (mAppsCustomizeContent != null) {
956 mAppsCustomizeContent.setBulkBind(true);
957 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700958 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
959 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200960 }
961 if (mAppsCustomizeContent != null) {
962 mAppsCustomizeContent.setBulkBind(false);
963 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700964 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200965 if (DEBUG_RESUME_TIME) {
966 Log.d(TAG, "Time spent processing callbacks in onResume: " +
967 (System.currentTimeMillis() - startTimeCallbacks));
968 }
Michael Jurka447bf842013-05-15 14:52:15 +0200969 }
Michael Jurka54554252013-08-01 12:52:23 +0200970 if (mOnResumeCallbacks.size() > 0) {
971 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
972 mOnResumeCallbacks.get(i).run();
973 }
974 mOnResumeCallbacks.clear();
975 }
Winson Chunge4e50662012-01-23 14:45:13 -0800976
977 // Reset the pressed state of icons that were locked in the press state while activities
978 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800979 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800980 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800981 mWaitingForResume.setStayPressed(false);
982 }
Winson Chunge4e50662012-01-23 14:45:13 -0800983 if (mAppsCustomizeContent != null) {
984 // Resets the previous all apps icon press state
985 mAppsCustomizeContent.resetDrawableState();
986 }
Winson Chung82963d52013-10-09 11:20:57 -0700987
Adam Cohen06dff352012-06-01 17:17:08 -0700988 // It is possible that widgets can receive updates while launcher is not in the foreground.
989 // Consequently, the widgets will be inflated in the orientation of the foreground activity
990 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
991 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700992 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700993
Winson Chung780fe592013-09-26 14:48:44 -0700994 // Process any items that were added while Launcher was away.
995 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
996
Winson Chung5841efa2013-09-30 18:06:44 -0700997 // Update the voice search button proxy
998 updateVoiceButtonProxyVisible(false);
999
Adam Cohenf9426d52012-06-04 17:26:21 -07001000 // Again, as with the above scenario, it's possible that one or more of the global icons
1001 // were updated in the wrong orientation.
1002 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001003 if (DEBUG_RESUME_TIME) {
1004 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1005 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001006
1007 if (mWorkspace.getCustomContentCallbacks() != null) {
1008 // If we are resuming and the custom content is the current page, we call onShow().
1009 // It is also poassible that onShow will instead be called slightly after first layout
1010 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1011 if (mWorkspace.isOnOrMovingToCustomContent()) {
1012 mWorkspace.getCustomContentCallbacks().onShow();
1013 }
1014 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001015 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001016 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001017 }
1018
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001020 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001021 // Ensure that items added to Launcher are queued until Launcher returns
1022 InstallShortcutReceiver.enableInstallQueue();
1023
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001024 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001025 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001026 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001027 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001028
1029 // We call onHide() aggressively. The custom content callbacks should be able to
1030 // debounce excess onHide calls.
1031 if (mWorkspace.getCustomContentCallbacks() != null) {
1032 mWorkspace.getCustomContentCallbacks().onHide();
1033 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001034 }
Romain Guycbb89e42009-06-08 15:52:54 -07001035
Adam Cohen66a01fd2013-06-11 12:48:00 -07001036 protected void onFinishBindingItems() {
Adam Cohen82398362013-09-29 12:57:34 -07001037 if (mWorkspace != null && hasCustomContentToLeft() && mWorkspace.hasCustomContent()) {
Dave Hawkeya8881582013-09-17 15:55:33 -06001038 addCustomContentToLeft();
1039 }
Adam Cohen66a01fd2013-06-11 12:48:00 -07001040 }
1041
Adam Cohenbffe7452013-07-22 18:21:45 -07001042 QSBScroller mQsbScroller = new QSBScroller() {
1043 int scrollY = 0;
1044
1045 @Override
1046 public void setScrollY(int scroll) {
1047 scrollY = scroll;
1048
1049 if (mWorkspace.isOnOrMovingToCustomContent()) {
1050 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001051 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001052 }
1053 }
1054 };
1055
1056 public void resetQSBScroll() {
1057 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001058 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001059 }
1060
1061 public interface CustomContentCallbacks {
1062 // Custom content is completely shown
1063 public void onShow();
1064
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 Cohen30bacb22013-08-29 14:25:25 -07001076 protected void startSettings() {
1077 }
1078
Adam Cohenbffe7452013-07-22 18:21:45 -07001079 public interface QSBScroller {
1080 public void setScrollY(int scrollY);
1081 }
1082
Winson Chung98ca0f72013-07-29 12:58:51 -07001083 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001084 CustomContentCallbacks callbacks, String description) {
1085 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001086 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001087 }
1088
Adam Cohenedb40762013-07-18 16:45:45 -07001089 // The custom content needs to offset its content to account for the QSB
1090 public int getTopOffsetForCustomContent() {
1091 return mWorkspace.getPaddingTop();
1092 }
1093
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001094 @Override
1095 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001096 // Flag the loader to stop early before switching
1097 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001098 if (mAppsCustomizeContent != null) {
1099 mAppsCustomizeContent.surrender();
1100 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001101 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001102 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001103
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001104 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001105 @Override
1106 public void onWindowFocusChanged(boolean hasFocus) {
1107 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001108 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001109 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 private boolean acceptFilter() {
1112 final InputMethodManager inputManager = (InputMethodManager)
1113 getSystemService(Context.INPUT_METHOD_SERVICE);
1114 return !inputManager.isFullscreenMode();
1115 }
1116
1117 @Override
1118 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001119 final int uniChar = event.getUnicodeChar();
1120 final boolean handled = super.onKeyDown(keyCode, event);
1121 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1122 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1124 keyCode, event);
1125 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001126 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001127 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001128 // showSearchDialog()
1129 // If there are multiple keystrokes before the search dialog takes focus,
1130 // onSearchRequested() will be called for every keystroke,
1131 // but it is idempotent, so it's fine.
1132 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133 }
1134 }
1135
Joe Onorato8a9625e2010-01-28 15:55:35 -08001136 // Eat the long press event so the keyboard doesn't come up.
1137 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1138 return true;
1139 }
1140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 return handled;
1142 }
1143
Karl Rosaen138a0412009-04-23 19:00:21 -07001144 private String getTypedText() {
1145 return mDefaultKeySsb.toString();
1146 }
1147
1148 private void clearTypedText() {
1149 mDefaultKeySsb.clear();
1150 mDefaultKeySsb.clearSpans();
1151 Selection.setSelection(mDefaultKeySsb, 0);
1152 }
1153
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001155 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1156 * State
1157 */
1158 private static State intToState(int stateOrdinal) {
1159 State state = State.WORKSPACE;
1160 final State[] stateValues = State.values();
1161 for (int i = 0; i < stateValues.length; i++) {
1162 if (stateValues[i].ordinal() == stateOrdinal) {
1163 state = stateValues[i];
1164 break;
1165 }
1166 }
1167 return state;
1168 }
1169
1170 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 * Restores the previous state, if it exists.
1172 *
1173 * @param savedState The previous state.
1174 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001175 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 private void restoreState(Bundle savedState) {
1177 if (savedState == null) {
1178 return;
1179 }
1180
Michael Jurka883f55b2010-10-21 15:47:14 -07001181 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001182 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001183 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001184 }
1185
Adam Cohen21cd0022013-10-09 18:57:02 -07001186 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1187 PagedView.INVALID_RESTORE_PAGE);
1188 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001189 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 }
1191
Winson Chung3d503fb2011-07-13 17:25:49 -07001192 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001193 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001194
Winson Chung3d503fb2011-07-13 17:25:49 -07001195 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1196 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001197 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001198 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1199 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001200 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1201 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1202 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001203 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001204 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 mRestoring = true;
1206 }
1207
1208 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1209 if (renameFolder) {
1210 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001211 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 mRestoring = true;
1213 }
Winson Chunga12a2502010-12-20 14:41:35 -08001214
Winson Chung785d2eb2011-04-14 16:08:02 -07001215 // Restore the AppsCustomize tab
1216 if (mAppsCustomizeTabHost != null) {
1217 String curTab = savedState.getString("apps_customize_currentTab");
1218 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001219 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001220 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001221 mAppsCustomizeContent.loadAssociatedPages(
1222 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001223 }
1224
Winson Chung5afbf7b2011-07-25 11:53:08 -07001225 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1226 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001227 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001228 mItemIdToViewId = (HashMap<Integer, Integer>)
1229 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
1231
1232 /**
1233 * Finds all the views we need and configure them properly.
1234 */
1235 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001236 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001237
Craig Mautner360310b2012-10-26 15:13:08 -07001238 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001239 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1240 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Winson Chunga6945242014-01-08 14:04:34 -08001241 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001242
John Spurlock77e1f472013-09-11 10:09:51 -04001243 mLauncherView.setSystemUiVisibility(
1244 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001245 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246
Winson Chung4c98d922011-05-31 16:50:48 -07001247 // Setup the drag layer
1248 mDragLayer.setup(this, dragController);
1249
Winson Chung3d503fb2011-07-13 17:25:49 -07001250 // Setup the hotseat
1251 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1252 if (mHotseat != null) {
1253 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001254 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001255 }
1256
Jorim Jaggid017f882014-01-14 17:08:48 -08001257 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001258 View widgetButton = findViewById(R.id.widget_button);
1259 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001260 @Override
1261 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001262 if (!mWorkspace.isSwitchingState()) {
1263 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
1264 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001265 }
1266 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001267 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1268
1269 View wallpaperButton = findViewById(R.id.wallpaper_button);
1270 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001271 @Override
1272 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001273 if (!mWorkspace.isSwitchingState()) {
1274 startWallpaper();
1275 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001276 }
1277 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001278 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1279
1280 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001281 if (hasSettings()) {
1282 settingsButton.setOnClickListener(new OnClickListener() {
1283 @Override
1284 public void onClick(View arg0) {
1285 if (!mWorkspace.isSwitchingState()) {
1286 startSettings();
1287 }
1288 }
1289 });
1290 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1291 } else {
1292 settingsButton.setVisibility(View.GONE);
1293 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1294 lp.gravity = Gravity.END | Gravity.TOP;
1295 widgetButton.requestLayout();
1296 }
1297
Adam Cohendbdff6b2013-09-18 19:09:15 -07001298 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001299
Winson Chung4c98d922011-05-31 16:50:48 -07001300 // Setup the workspace
1301 mWorkspace.setHapticFeedbackEnabled(false);
1302 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001303 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001304 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001305
Winson Chungf0ea4d32011-06-06 14:27:16 -07001306 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001307 mSearchDropTargetBar = (SearchDropTargetBar)
1308 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001309
Winson Chungf0ea4d32011-06-06 14:27:16 -07001310 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001311 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001312 mAppsCustomizeContent = (AppsCustomizePagedView)
1313 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1314 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001315
Winson Chung3d503fb2011-07-13 17:25:49 -07001316 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001317 dragController.setDragScoller(mWorkspace);
1318 dragController.setScrollView(mDragLayer);
1319 dragController.setMoveTarget(mWorkspace);
1320 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001321 if (mSearchDropTargetBar != null) {
1322 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001323 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001324
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001325 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001326 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001327 mWeightWatcher = new WeightWatcher(this);
1328 mWeightWatcher.setAlpha(0.5f);
1329 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001330 new FrameLayout.LayoutParams(
1331 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001332 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001333 Gravity.BOTTOM)
1334 );
Adam Cohen39a06042013-07-19 14:30:12 -07001335
1336 boolean show = shouldShowWeightWatcher();
1337 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001338 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 }
1340
1341 /**
1342 * Creates a view representing a shortcut.
1343 *
1344 * @param info The data structure describing the shortcut.
1345 *
1346 * @return A View inflated from R.layout.application.
1347 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001348 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001350 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
1352
1353 /**
1354 * Creates a view representing a shortcut inflated from the specified resource.
1355 *
1356 * @param layoutResId The id of the XML layout used to create the shortcut.
1357 * @param parent The group the shortcut belongs to.
1358 * @param info The data structure describing the shortcut.
1359 *
1360 * @return A View inflated from layoutResId.
1361 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001362 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001363 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1364 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 return favorite;
1367 }
1368
1369 /**
1370 * Add an application shortcut to the workspace.
1371 *
1372 * @param data The intent describing the application.
1373 * @param cellInfo The position on screen where to create the shortcut.
1374 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001375 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001376 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001377 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001378
Adam Cohenfbba09b2011-07-18 21:43:05 -07001379 // First we check if we already know the exact location where we want to add this item.
1380 if (cellX >= 0 && cellY >= 0) {
1381 cellXY[0] = cellX;
1382 cellXY[1] = cellY;
1383 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001384 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001385 return;
1386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001388 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001389
Romain Guy73b979d2009-06-09 12:57:21 -07001390 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001391 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001393 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001394 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001395 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001396 } else {
1397 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 }
1399 }
Romain Guycbb89e42009-06-08 15:52:54 -07001400
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 /**
1402 * Add a shortcut to the workspace.
1403 *
1404 * @param data The intent describing the shortcut.
1405 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001407 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001408 int cellY) {
1409 int[] cellXY = mTmpAddItemCellCoordinates;
1410 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001411 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001412
Michael Jurkad3ef3062010-11-23 16:23:58 -08001413 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001414
Adam Cohen558baaf2011-08-15 15:22:57 -07001415 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001416 if (info == null) {
1417 return;
1418 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001419 final View view = createShortcut(info);
1420
Adam Cohenfbba09b2011-07-18 21:43:05 -07001421 // First we check if we already know the exact location where we want to add this item.
1422 if (cellX >= 0 && cellY >= 0) {
1423 cellXY[0] = cellX;
1424 cellXY[1] = cellY;
1425 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001426
1427 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001428 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001429 true, null,null)) {
1430 return;
1431 }
1432 DragObject dragObject = new DragObject();
1433 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001434 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1435 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001436 return;
1437 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001438 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001439 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001440 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001441 foundCellSpan = (result != null);
1442 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001443 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001444 }
1445
1446 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001447 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001448 return;
1449 }
1450
Adam Cohendcd297f2013-06-18 13:13:40 -07001451 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452
1453 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001454 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001455 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 }
1457 }
1458
Adam Cohen2f093b62012-04-30 18:59:53 -07001459 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1460 int minHeight) {
1461 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001462 // We want to account for the extra amount of padding that we are adding to the widget
1463 // to ensure that it gets the full amount of space that it has requested
1464 int requiredWidth = minWidth + padding.left + padding.right;
1465 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001466 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001467 }
1468
Adam Cohen2f093b62012-04-30 18:59:53 -07001469 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1470 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001471 }
1472
Adam Cohen2f093b62012-04-30 18:59:53 -07001473 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1474 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001475 }
1476
Adam Cohen2f093b62012-04-30 18:59:53 -07001477 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1478 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001479 }
1480
Adam Cohen2f093b62012-04-30 18:59:53 -07001481 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1482 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001483 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001484 }
1485
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001487 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001489 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001490 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001492 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001493 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1494 if (appWidgetInfo == null) {
1495 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1496 }
Romain Guycbb89e42009-06-08 15:52:54 -07001497
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001498 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001499 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001500
Adam Cohen2f093b62012-04-30 18:59:53 -07001501 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1502 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001503
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001505 // We have saved the position to which the widget was dragged-- this really only matters
1506 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001507 int[] cellXY = mTmpAddItemCellCoordinates;
1508 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001509 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001510 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001511 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1512 cellXY[0] = mPendingAddInfo.cellX;
1513 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001514 spanXY[0] = mPendingAddInfo.spanX;
1515 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001516 foundCellSpan = true;
1517 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001518 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001519 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001520 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1521 spanXY[1], cellXY, finalSpan);
1522 spanXY[0] = finalSpan[0];
1523 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001524 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001525 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001526 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001527 }
1528
Michael Jurka0280c3b2010-09-17 15:00:07 -07001529 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001530 if (appWidgetId != -1) {
1531 // Deleting an app widget ID is a void call but writes to disk before returning
1532 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001533 new AsyncTask<Void, Void, Void>() {
1534 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001535 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001536 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001537 }
Michael Jurka43467462013-11-14 12:03:58 +01001538 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001539 }
Winson Chung93eef082012-03-23 15:59:27 -07001540 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001541 return;
1542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001544 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001545 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1546 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 launcherInfo.spanX = spanXY[0];
1548 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001549 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1550 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001551
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001553 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554
1555 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001556 if (hostView == null) {
1557 // Perform actual inflation because we're live
1558 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1559 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1560 } else {
1561 // The AppWidgetHostView has already been inflated and instantiated
1562 launcherInfo.hostView = hostView;
1563 }
Romain Guycbb89e42009-06-08 15:52:54 -07001564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001566 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001567 launcherInfo.notifyWidgetSizeChanged(this);
1568
Adam Cohendcd297f2013-06-18 13:13:40 -07001569 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001570 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001571
1572 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001574 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 }
Romain Guycbb89e42009-06-08 15:52:54 -07001576
Adam Cohended9f8d2010-11-03 13:25:16 -07001577 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1578 @Override
1579 public void onReceive(Context context, Intent intent) {
1580 final String action = intent.getAction();
1581 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1582 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001583 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001584 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001585
Winson Chungc100e8e2011-08-09 16:02:43 -07001586 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001587 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001588 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001589 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001590 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001591 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1592 mUserPresent = true;
1593 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001594 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1595 mModel.resetLoadedState(false, true);
1596 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1597 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1598 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1599 mModel.resetLoadedState(false, true);
1600 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1601 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1602 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001603 }
1604 }
1605 };
1606
1607 @Override
1608 public void onAttachedToWindow() {
1609 super.onAttachedToWindow();
1610
1611 // Listen for broadcasts related to user-presence
1612 final IntentFilter filter = new IntentFilter();
1613 filter.addAction(Intent.ACTION_SCREEN_OFF);
1614 filter.addAction(Intent.ACTION_USER_PRESENT);
Dan Sandlerd5024042014-01-09 15:01:33 -05001615 if (ENABLE_DEBUG_INTENTS) {
1616 filter.addAction(DebugIntents.DELETE_DATABASE);
1617 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1618 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001619 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001620 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001621 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001622 mVisible = true;
1623 }
1624
1625 @Override
1626 public void onDetachedFromWindow() {
1627 super.onDetachedFromWindow();
1628 mVisible = false;
1629
Adam Cohend113e0c2010-11-11 10:48:05 -08001630 if (mAttached) {
1631 unregisterReceiver(mReceiver);
1632 mAttached = false;
1633 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001634 updateRunning();
1635 }
1636
1637 public void onWindowVisibilityChanged(int visibility) {
1638 mVisible = visibility == View.VISIBLE;
1639 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001640 // The following code used to be in onResume, but it turns out onResume is called when
1641 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1642 // is a more appropriate event to handle
1643 if (mVisible) {
1644 mAppsCustomizeTabHost.onWindowVisible();
1645 if (!mWorkspaceLoading) {
1646 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001647 // We want to let Launcher draw itself at least once before we force it to build
1648 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001649 // apps is nice and speedy.
1650 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001651 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001652 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001653 if (mStarted) return;
1654 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001655 // We delay the layer building a bit in order to give
1656 // other message processing a time to run. In particular
1657 // this avoids a delay in hiding the IME if it was
1658 // currently shown, because doing that may involve
1659 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001660 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001661 final ViewTreeObserver.OnDrawListener listener = this;
1662 mWorkspace.post(new Runnable() {
1663 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001664 if (mWorkspace != null &&
1665 mWorkspace.getViewTreeObserver() != null) {
1666 mWorkspace.getViewTreeObserver().
1667 removeOnDrawListener(listener);
1668 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001669 }
1670 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001671 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001672 }
1673 });
1674 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001675 // When Launcher comes back to foreground, a different Activity might be responsible for
1676 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001677 if (!DISABLE_MARKET_BUTTON) {
1678 updateAppMarketIcon();
1679 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001680 clearTypedText();
1681 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001682 }
1683
1684 private void sendAdvanceMessage(long delay) {
1685 mHandler.removeMessages(ADVANCE_MSG);
1686 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1687 mHandler.sendMessageDelayed(msg, delay);
1688 mAutoAdvanceSentTime = System.currentTimeMillis();
1689 }
1690
1691 private void updateRunning() {
1692 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1693 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1694 mAutoAdvanceRunning = autoAdvanceRunning;
1695 if (autoAdvanceRunning) {
1696 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1697 sendAdvanceMessage(delay);
1698 } else {
1699 if (!mWidgetsToAdvance.isEmpty()) {
1700 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1701 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1702 }
1703 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001704 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001705 }
1706 }
1707 }
1708
1709 private final Handler mHandler = new Handler() {
1710 @Override
1711 public void handleMessage(Message msg) {
1712 if (msg.what == ADVANCE_MSG) {
1713 int i = 0;
1714 for (View key: mWidgetsToAdvance.keySet()) {
1715 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1716 final int delay = mAdvanceStagger * i;
1717 if (v instanceof Advanceable) {
1718 postDelayed(new Runnable() {
1719 public void run() {
1720 ((Advanceable) v).advance();
1721 }
1722 }, delay);
1723 }
1724 i++;
1725 }
1726 sendAdvanceMessage(mAdvanceInterval);
1727 }
1728 }
1729 };
1730
1731 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001732 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1734 if (v instanceof Advanceable) {
1735 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001736 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001737 updateRunning();
1738 }
1739 }
1740
1741 void removeWidgetToAutoAdvance(View hostView) {
1742 if (mWidgetsToAdvance.containsKey(hostView)) {
1743 mWidgetsToAdvance.remove(hostView);
1744 updateRunning();
1745 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001746 }
1747
Joe Onorato9c1289c2009-08-17 11:03:03 -04001748 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001749 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001750 launcherInfo.hostView = null;
1751 }
1752
Winson Chung93eef082012-03-23 15:59:27 -07001753 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1754 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1755 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 }
1757
Winson Chunga6945242014-01-08 14:04:34 -08001758 public DragLayer getDragLayer() {
1759 return mDragLayer;
1760 }
1761
1762 public Workspace getWorkspace() {
1763 return mWorkspace;
1764 }
1765
1766 public Hotseat getHotseat() {
1767 return mHotseat;
1768 }
1769
Jorim Jaggid017f882014-01-14 17:08:48 -08001770 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001771 return mOverviewPanel;
1772 }
1773
1774 public SearchDropTargetBar getSearchBar() {
1775 return mSearchDropTargetBar;
1776 }
1777
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001778 public LauncherAppWidgetHost getAppWidgetHost() {
1779 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 }
Romain Guycbb89e42009-06-08 15:52:54 -07001781
Winson Chunga9abd0e2010-10-27 17:18:37 -07001782 public LauncherModel getModel() {
1783 return mModel;
1784 }
1785
Winson Chunga6945242014-01-08 14:04:34 -08001786 public LauncherClings getLauncherClings() {
1787 return mLauncherClings;
1788 }
1789
1790 protected SharedPreferences getSharedPrefs() {
1791 return mSharedPrefs;
1792 }
1793
Bjorn Bringertc459e522013-06-07 19:36:01 +01001794 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001795 getWindow().closeAllPanels();
1796
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001797 // Whatever we were doing is hereby canceled.
1798 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001799 }
1800
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 @Override
1802 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001803 long startTime = 0;
1804 if (DEBUG_RESUME_TIME) {
1805 startTime = System.currentTimeMillis();
1806 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 super.onNewIntent(intent);
1808
1809 // Close the menu
1810 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001811 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001812 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001813
Adam Cohened307df2013-10-02 09:37:31 -07001814 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1815 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1816 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001817
Adam Cohen6fecd412013-10-02 17:41:50 -07001818 if (mWorkspace == null) {
1819 // Can be cases where mWorkspace is null, this prevents a NPE
1820 return;
1821 }
1822 Folder openFolder = mWorkspace.getOpenFolder();
1823 // In all these cases, only animate if we're already on home
1824 mWorkspace.exitWidgetResizeMode();
1825 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001826 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001827 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001828 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001829
Adam Cohen6fecd412013-10-02 17:41:50 -07001830 closeFolder();
1831 exitSpringLoadedDragMode();
1832
1833 // If we are already on home, then just animate back to the workspace,
1834 // otherwise, just wait until onResume to set the state back to Workspace
1835 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001836 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001837 } else {
1838 mOnResumeState = State.WORKSPACE;
1839 }
1840
1841 final View v = getWindow().peekDecorView();
1842 if (v != null && v.getWindowToken() != null) {
1843 InputMethodManager imm = (InputMethodManager)getSystemService(
1844 INPUT_METHOD_SERVICE);
1845 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1846 }
1847
1848 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001849 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001850 mAppsCustomizeTabHost.reset();
1851 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001852
1853 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 }
Adam Cohened307df2013-10-02 09:37:31 -07001855
Michael Jurka447bf842013-05-15 14:52:15 +02001856 if (DEBUG_RESUME_TIME) {
1857 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1858 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 }
1860
Adam Coppa120b8e2013-11-12 16:26:04 +00001861 /**
1862 * Override point for subclasses to prevent movement to the default screen when the home
1863 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1864 */
1865 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1866 return true;
1867 }
1868
1869 /**
1870 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1871 */
1872 protected void onHomeIntent() {
1873 // Do nothing
1874 }
1875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001877 public void onRestoreInstanceState(Bundle state) {
1878 super.onRestoreInstanceState(state);
1879 for (int page: mSynchronouslyBoundPages) {
1880 mWorkspace.restoreInstanceStateForChild(page);
1881 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 }
1883
1884 @Override
1885 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001886 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001887 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1888 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001889 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001890 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891
Michael Jurka883f55b2010-10-21 15:47:14 -07001892 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001893 // We close any open folder since it will not be re-opened, and we need to make sure
1894 // this state is reflected.
1895 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896
Adam Cohendcd297f2013-06-18 13:13:40 -07001897 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001898 mWaitingForResult) {
1899 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001900 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001901 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1902 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001903 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1904 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1905 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001906 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
1908
1909 if (mFolderInfo != null && mWaitingForResult) {
1910 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1911 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1912 }
Michael Jurka946ad472010-07-09 18:05:18 -07001913
Winson Chung785d2eb2011-04-14 16:08:02 -07001914 // Save the current AppsCustomize tab
1915 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001916 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1917 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001918 if (currentTabTag != null) {
1919 outState.putString("apps_customize_currentTab", currentTabTag);
1920 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001921 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1922 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001923 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001924 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 }
1926
1927 @Override
1928 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001930
Winson Chunge7a03942011-08-05 15:05:12 -07001931 // Remove all pending runnables
1932 mHandler.removeMessages(ADVANCE_MSG);
1933 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001934 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001935
Winson Chungcd2b0142011-06-08 16:02:26 -07001936 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001937 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001938 mModel.stopLoader();
1939 app.setLauncher(null);
1940
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001942 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001944 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001946 mAppWidgetHost = null;
1947
1948 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949
1950 TextKeyListener.getInstance().release();
1951
Winson Chung81b52252012-08-27 15:34:29 -07001952 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1953 // to prevent leaking Launcher activities on orientation change.
1954 if (mModel != null) {
1955 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1956 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001957
1958 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001959 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001960
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001961 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001962 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08001963 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001964 mWorkspace = null;
1965 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001966
Michael Jurka2ecf9952012-06-18 12:52:28 -07001967 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 }
1969
Adam Cohena9cf38f2011-05-02 15:36:58 -07001970 public DragController getDragController() {
1971 return mDragController;
1972 }
1973
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 @Override
1975 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001976 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 super.startActivityForResult(intent, requestCode);
1978 }
1979
Winson Chung70d72102011-08-12 11:24:35 -07001980 /**
1981 * Indicates that we want global search for this activity by setting the globalSearch
1982 * argument for {@link #startSearch} to true.
1983 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001985 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001987
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001988 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001989
Karl Rosaen138a0412009-04-23 19:00:21 -07001990 if (initialQuery == null) {
1991 // Use any text typed in the launcher as the initial query
1992 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001993 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 if (appSearchData == null) {
1995 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001996 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
Winson Chungadf0c182012-08-23 14:59:07 -07001998 Rect sourceBounds = new Rect();
1999 if (mSearchDropTargetBar != null) {
2000 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2001 }
Romain Guycbb89e42009-06-08 15:52:54 -07002002
Bjorn Bringertc459e522013-06-07 19:36:01 +01002003 startSearch(initialQuery, selectInitialQuery,
2004 appSearchData, sourceBounds);
2005 }
2006
2007 public void startSearch(String initialQuery,
2008 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002009 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002010 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002011 }
2012
2013 /**
2014 * Starts the global search activity. This code is a copied from SearchManager
2015 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002016 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002017 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002018 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002019 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2020 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2021 if (globalSearchActivity == null) {
2022 Log.w(TAG, "No global search activity found.");
2023 return;
2024 }
2025 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2026 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2027 intent.setComponent(globalSearchActivity);
2028 // Make sure that we have a Bundle to put source in
2029 if (appSearchData == null) {
2030 appSearchData = new Bundle();
2031 } else {
2032 appSearchData = new Bundle(appSearchData);
2033 }
2034 // Set source to package name of app that starts global search, if not set already.
2035 if (!appSearchData.containsKey("source")) {
2036 appSearchData.putString("source", getPackageName());
2037 }
2038 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2039 if (!TextUtils.isEmpty(initialQuery)) {
2040 intent.putExtra(SearchManager.QUERY, initialQuery);
2041 }
2042 if (selectInitialQuery) {
2043 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2044 }
2045 intent.setSourceBounds(sourceBounds);
2046 try {
2047 startActivity(intent);
2048 } catch (ActivityNotFoundException ex) {
2049 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2050 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 }
2052
Winson Chung70d72102011-08-12 11:24:35 -07002053 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002054 public boolean onPrepareOptionsMenu(Menu menu) {
2055 super.onPrepareOptionsMenu(menu);
Winson Chunge0298742014-01-17 12:03:00 -08002056 // Close any open folders
2057 closeFolder();
2058 // Stop resizing any widgets
2059 mWorkspace.exitWidgetResizeMode();
2060 if (!mWorkspace.isInOverviewMode()) {
2061 // Show the overview mode
2062 showOverviewMode(true);
2063 } else {
2064 showWorkspace(true);
2065 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002066 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002069 @Override
2070 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002071 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002072 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002073 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002074 }
2075
Joe Onorato9c1289c2009-08-17 11:03:03 -04002076 public boolean isWorkspaceLocked() {
2077 return mWorkspaceLoading || mWaitingForResult;
2078 }
2079
Michael Jurka0280c3b2010-09-17 15:00:07 -07002080 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002081 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002082 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002083 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2084 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002085 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002086 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002087 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002088
Adam Cohenad4e15c2013-10-17 16:21:35 -07002089 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2090 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2091 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2092 }
2093
2094 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2095 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2096 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002097 if (appWidgetInfo.configure != null) {
2098 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002099 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002100
Bjorn Bringert7984c942009-12-09 15:38:25 +00002101 // Launch over to configure widget, if needed
2102 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002103 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002104 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002105 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002107 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002108 Runnable onComplete = new Runnable() {
2109 @Override
2110 public void run() {
2111 // Exit spring loaded mode if necessary after adding the widget
2112 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2113 null);
2114 }
2115 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002116 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002117 appWidgetInfo);
Adam Cohenad4e15c2013-10-17 16:21:35 -07002118 mWorkspace.removeExtraEmptyScreen(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 }
2120 }
Romain Guycbb89e42009-06-08 15:52:54 -07002121
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002122 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002123 // Close any folders that may be open.
2124 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002125 mWorkspace.moveToCustomContentScreen(animate);
2126 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002127 /**
2128 * Process a shortcut drop.
2129 *
2130 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002131 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002132 * @param cell The cell it should be added to, optional
2133 * @param position The location on the screen where it was dropped, optional
2134 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002135 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002136 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002137 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002138 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002139 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002140 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002141
2142 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002143 mPendingAddInfo.cellX = cell[0];
2144 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002145 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002146
2147 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2148 createShortcutIntent.setComponent(componentName);
2149 processShortcut(createShortcutIntent);
2150 }
2151
Adam Cohenfbba09b2011-07-18 21:43:05 -07002152 /**
2153 * Process a widget drop.
2154 *
2155 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002156 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002157 * @param cell The cell it should be added to, optional
2158 * @param position The location on the screen where it was dropped, optional
2159 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002160 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002161 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002162 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002163 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002164 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002165 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002166 mPendingAddInfo.minSpanX = info.minSpanX;
2167 mPendingAddInfo.minSpanY = info.minSpanY;
2168
Adam Cohenfbba09b2011-07-18 21:43:05 -07002169 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002170 mPendingAddInfo.cellX = cell[0];
2171 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002172 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002173 if (span != null) {
2174 mPendingAddInfo.spanX = span[0];
2175 mPendingAddInfo.spanY = span[1];
2176 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002177
Adam Cohened66b2b2012-01-23 17:28:51 -08002178 AppWidgetHostView hostView = info.boundWidget;
2179 int appWidgetId;
2180 if (hostView != null) {
2181 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002182 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002183 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002184 // In this case, we either need to start an activity to get permission to bind
2185 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002186 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002187 Bundle options = info.bindOptions;
2188
2189 boolean success = false;
2190 if (options != null) {
2191 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2192 info.componentName, options);
2193 } else {
2194 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2195 info.componentName);
2196 }
2197 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002198 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002199 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002200 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002201 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2202 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2203 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002204 // TODO: we need to make sure that this accounts for the options bundle.
2205 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002206 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2207 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002208 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002209 }
2210
Joe Onoratodeb98af2010-02-19 14:59:39 -08002211 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002212 // Handle case where user selected "Applications"
2213 String applicationName = getResources().getString(R.string.group_applications);
2214 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07002215
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002216 if (applicationName != null && applicationName.equals(shortcutName)) {
2217 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2218 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002219
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002220 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2221 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002222 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002223 Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002224 } else {
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002225 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 }
2228
Winson Chung24ab2f12010-09-16 14:10:47 -07002229 void processWallpaper(Intent intent) {
2230 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2231 }
2232
Adam Cohendcd297f2013-06-18 13:13:40 -07002233 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002234 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002235 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 folderInfo.title = getText(R.string.folder_name);
2237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002239 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002240 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002241 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242
2243 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002244 FolderIcon newFolder =
2245 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002246 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002248 // Force measure the new folder icon
2249 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2250 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002251 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002252 }
Romain Guycbb89e42009-06-08 15:52:54 -07002253
Joe Onorato9c1289c2009-08-17 11:03:03 -04002254 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002255 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002256 }
2257
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002258 protected void startWallpaper() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002260 pickWallpaper.setComponent(getWallpaperPickerComponent());
Michael Jurka104c4562013-07-08 18:03:46 -07002261 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 }
2263
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002264 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002265 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002266 }
2267
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002268 /**
2269 * Registers various content observers. The current implementation registers
2270 * only a favorites observer to keep track of the favorites applications.
2271 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002272 private void registerContentObservers() {
2273 ContentResolver resolver = getContentResolver();
2274 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2275 true, mWidgetObserver);
2276 }
2277
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 @Override
2279 public boolean dispatchKeyEvent(KeyEvent event) {
2280 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2281 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002282 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002283 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002284 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002285 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002286 dumpState();
2287 return true;
2288 }
2289 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002290 }
2291 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2292 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002293 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002294 return true;
2295 }
2296 }
2297
2298 return super.dispatchKeyEvent(event);
2299 }
2300
Joe Onorato88ec0992009-11-19 13:16:06 -08002301 @Override
2302 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002303 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002304 if (mAppsCustomizeContent.getContentType() ==
2305 AppsCustomizePagedView.ContentType.Applications) {
2306 showWorkspace(true);
2307 } else {
2308 showOverviewMode(true);
2309 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002310 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002311 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002312 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002313 Folder openFolder = mWorkspace.getOpenFolder();
2314 if (openFolder.isEditingName()) {
2315 openFolder.dismissEditingName();
2316 } else {
2317 closeFolder();
2318 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002319 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002320 mWorkspace.exitWidgetResizeMode();
2321
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002322 // Back button is a no-op here, but give at least some feedback for the button press
2323 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002324 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002325 }
2326
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002328 * Re-listen when widgets are reset.
2329 */
2330 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002331 if (mAppWidgetHost != null) {
2332 mAppWidgetHost.startListening();
2333 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002334 }
2335
2336 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002337 * Launches the intent referred by the clicked shortcut.
2338 *
2339 * @param v The view representing the clicked shortcut.
2340 */
2341 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002342 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2343 // view has detached (it's possible for this to happen if the view is removed mid touch).
2344 if (v.getWindowToken() == null) {
2345 return;
2346 }
2347
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002348 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002349 return;
2350 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002351
Adam Cohen1697b792013-09-17 19:08:21 -07002352 if (v instanceof Workspace) {
2353 if (mWorkspace.isInOverviewMode()) {
2354 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002355 }
2356 return;
2357 }
2358
2359 if (v instanceof CellLayout) {
2360 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002361 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002362 }
2363 }
2364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002366 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002368 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2369 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002370
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002371 // Check for special shortcuts
2372 if (intent.getComponent() != null) {
2373 final String shortcutClass = intent.getComponent().getClassName();
2374
2375 if (shortcutClass.equals(WidgetAdder.class.getName())) {
Winson Chung82963d52013-10-09 11:20:57 -07002376 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002377 return;
2378 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2379 MemoryDumpActivity.startDump(this);
2380 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002381 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2382 toggleShowWeightWatcher();
2383 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002384 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002385 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002386
2387 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002388 int[] pos = new int[2];
2389 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002390 intent.setSourceBounds(new Rect(pos[0], pos[1],
2391 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002392
2393 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002394
Daniel Sandlerff02d492013-08-05 02:12:05 -04002395 mStats.recordLaunch(intent, shortcut);
2396
Michael Jurkaddd62e92011-02-16 17:49:14 -08002397 if (success && v instanceof BubbleTextView) {
2398 mWaitingForResume = (BubbleTextView) v;
2399 mWaitingForResume.setStayPressed(true);
2400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002402 if (v instanceof FolderIcon) {
2403 FolderIcon fi = (FolderIcon) v;
2404 handleFolderClick(fi);
2405 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002406 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002407 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002408 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002409 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002410 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002411 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 }
2413 }
2414
Michael Jurka0e260592010-06-30 17:07:39 -07002415 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002416 return false;
2417 }
2418
Michael Jurkaaf442092010-06-10 17:01:57 -07002419 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002420 * Event handler for the search button
2421 *
2422 * @param v The view that was clicked.
2423 */
2424 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002425 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2426
Amith Yamasania135ba82011-08-09 17:42:01 -07002427 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002428 }
2429
2430 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002431 * Event handler for the voice button
2432 *
2433 * @param v The view that was clicked.
2434 */
2435 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002436 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002437
Bjorn Bringertc459e522013-06-07 19:36:01 +01002438 startVoice();
2439 }
2440
2441 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002442 try {
2443 final SearchManager searchManager =
2444 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2445 ComponentName activityName = searchManager.getGlobalSearchActivity();
2446 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002447 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002448 if (activityName != null) {
2449 intent.setPackage(activityName.getPackageName());
2450 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002451 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002452 } catch (ActivityNotFoundException e) {
2453 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002454 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002455 startActivitySafely(null, intent, "onClickVoiceButton");
2456 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002457 }
2458
2459 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002460 * Event handler for the "grid" button that appears on the home screen, which
2461 * enters all apps mode.
2462 *
2463 * @param v The view that was clicked.
2464 */
2465 public void onClickAllAppsButton(View v) {
Winson Chunge7e97e62013-12-02 17:02:50 -08002466 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
Michael Jurka5130e402011-10-13 04:55:35 -07002467 }
2468
2469 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002470 // Provide the same haptic feedback that the system offers for virtual keys.
2471 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002472 }
2473
Adam Cohen61f560d2013-09-30 15:58:20 -07002474 public void performHapticFeedbackOnTouchDown(View v) {
2475 // Provide the same haptic feedback that the system offers for virtual keys.
2476 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2477 }
2478
2479 public View.OnTouchListener getHapticFeedbackTouchListener() {
2480 if (mHapticFeedbackTouchListener == null) {
2481 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2482 @Override
2483 public boolean onTouch(View v, MotionEvent event) {
2484 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2485 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2486 }
2487 return false;
2488 }
2489 };
2490 }
2491 return mHapticFeedbackTouchListener;
2492 }
2493
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002494 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002495 if (!DISABLE_MARKET_BUTTON) {
2496 if (mAppMarketIntent != null) {
2497 startActivitySafely(v, mAppMarketIntent, "app market");
2498 } else {
2499 Log.e(TAG, "Invalid app market intent.");
2500 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002501 }
2502 }
2503
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002504 /**
2505 * Called when the user stops interacting with the launcher.
2506 * This implies that the user is now on the homescreen and is not doing housekeeping.
2507 */
2508 protected void onInteractionEnd() {}
2509
2510 /**
2511 * Called when the user starts interacting with the launcher.
2512 * The possible interactions are:
2513 * - open all apps
2514 * - reorder an app shortcut, or a widget
2515 * - open the overview mode.
2516 * This is a good time to stop doing things that only make sense
2517 * when the user is on the homescreen and not doing housekeeping.
2518 */
2519 protected void onInteractionBegin() {}
2520
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002521 void startApplicationDetailsActivity(ComponentName componentName) {
2522 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002523 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2524 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002525 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2526 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002527 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002528 }
2529
Michael Jurka1e2f4652013-07-08 18:03:46 -07002530 // returns true if the activity was started
2531 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002532 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002533 // System applications cannot be installed. For now, show a toast explaining that.
2534 // We may give them the option of disabling apps this way.
2535 int messageId = R.string.uninstall_system_app_text;
2536 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002537 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002538 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002539 String packageName = componentName.getPackageName();
2540 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002541 Intent intent = new Intent(
2542 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002543 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2544 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002545 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002546 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002547 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002548 }
2549
Michael Jurka86a720e2012-05-09 11:23:23 -07002550 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002552
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002554 // Only launch using the new animation if the shortcut has not opted out (this is a
2555 // private contract between launcher and may be ignored in the future).
2556 boolean useLaunchAnimation = (v != null) &&
2557 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2558 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002559 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2560 v.getMeasuredWidth(), v.getMeasuredHeight());
2561
2562 startActivity(intent, opts.toBundle());
2563 } else {
2564 startActivity(intent);
2565 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002566 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002567 } catch (SecurityException e) {
2568 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002569 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002570 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002571 "or use the exported attribute for this activity. "
2572 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002573 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002574 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002575 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002576
Michael Jurka86a720e2012-05-09 11:23:23 -07002577 boolean startActivitySafely(View v, Intent intent, Object tag) {
2578 boolean success = false;
2579 try {
2580 success = startActivity(v, intent, tag);
2581 } catch (ActivityNotFoundException e) {
2582 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2583 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2584 }
2585 return success;
2586 }
2587
Adam Cohena9cf38f2011-05-02 15:36:58 -07002588 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002589 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002590 Folder openFolder = mWorkspace.getFolderForTag(info);
2591
2592 // If the folder info reports that the associated folder is open, then verify that
2593 // it is actually opened. There have been a few instances where this gets out of sync.
2594 if (info.opened && openFolder == null) {
2595 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002596 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002597 info.opened = false;
2598 }
2599
Adam Cohenfb91f302012-06-11 15:45:18 -07002600 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002601 // Close any open folder
2602 closeFolder();
2603 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002604 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002605 } else {
2606 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002607 int folderScreen;
2608 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002609 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002610 // .. and close it
2611 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002612 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002613 // Close any folder open on the current screen
2614 closeFolder();
2615 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002616 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002617 }
2618 }
2619 }
2620 }
2621
Adam Cohen268c4752012-06-06 17:47:33 -07002622 /**
2623 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2624 * in the DragLayer in the exact absolute location of the original FolderIcon.
2625 */
2626 private void copyFolderIconToImage(FolderIcon fi) {
2627 final int width = fi.getMeasuredWidth();
2628 final int height = fi.getMeasuredHeight();
2629
2630 // Lazy load ImageView, Bitmap and Canvas
2631 if (mFolderIconImageView == null) {
2632 mFolderIconImageView = new ImageView(this);
2633 }
2634 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2635 mFolderIconBitmap.getHeight() != height) {
2636 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2637 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2638 }
2639
2640 DragLayer.LayoutParams lp;
2641 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2642 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2643 } else {
2644 lp = new DragLayer.LayoutParams(width, height);
2645 }
2646
Adam Cohen307fe232012-08-16 17:55:58 -07002647 // The layout from which the folder is being opened may be scaled, adjust the starting
2648 // view size by this scale factor.
2649 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002650 lp.customPosition = true;
2651 lp.x = mRectForFolderAnimation.left;
2652 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002653 lp.width = (int) (scale * width);
2654 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002655
2656 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2657 fi.draw(mFolderIconCanvas);
2658 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002659 if (fi.getFolder() != null) {
2660 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2661 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002662 }
Adam Cohen268c4752012-06-06 17:47:33 -07002663 // Just in case this image view is still in the drag layer from a previous animation,
2664 // we remove it and re-add it.
2665 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2666 mDragLayer.removeView(mFolderIconImageView);
2667 }
2668 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002669 if (fi.getFolder() != null) {
2670 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002671 }
Adam Cohen268c4752012-06-06 17:47:33 -07002672 }
2673
Adam Cohen2801caf2011-05-13 20:57:39 -07002674 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002675 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002676 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2677 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2678 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2679
Adam Cohenc51934b2011-07-26 21:07:43 -07002680 FolderInfo info = (FolderInfo) fi.getTag();
2681 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2682 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002683 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2684 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002685 }
2686
Adam Cohen268c4752012-06-06 17:47:33 -07002687 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2688 copyFolderIconToImage(fi);
2689 fi.setVisibility(View.INVISIBLE);
2690
Michael Jurka2ecf9952012-06-18 12:52:28 -07002691 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002692 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002693 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2694 oa.start();
2695 }
2696
Adam Cohen268c4752012-06-06 17:47:33 -07002697 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002698 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002699 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2700 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2701 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2702
Adam Cohen268c4752012-06-06 17:47:33 -07002703 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002704
Adam Cohen268c4752012-06-06 17:47:33 -07002705 // We remove and re-draw the FolderIcon in-case it has changed
2706 mDragLayer.removeView(mFolderIconImageView);
2707 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002708 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002709 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002710 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002711 oa.addListener(new AnimatorListenerAdapter() {
2712 @Override
2713 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002714 if (cl != null) {
2715 cl.clearFolderLeaveBehind();
2716 // Remove the ImageView copy of the FolderIcon and make the original visible.
2717 mDragLayer.removeView(mFolderIconImageView);
2718 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002719 }
2720 }
2721 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002722 oa.start();
2723 }
2724
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002725 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002726 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002727 * is animated relative to the specified View. If the View is null, no animation
2728 * is played.
2729 *
2730 * @param folderInfo The FolderInfo describing the folder to open.
2731 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002732 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002733 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002734 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002735
Adam Cohena9cf38f2011-05-02 15:36:58 -07002736 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002737
Adam Cohen4554ee12011-08-03 16:13:21 -07002738 // Just verify that the folder hasn't already been added to the DragLayer.
2739 // There was a one-off crash where the folder had a parent already.
2740 if (folder.getParent() == null) {
2741 mDragLayer.addView(folder);
2742 mDragController.addDropTarget((DropTarget) folder);
2743 } else {
2744 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2745 folder.getParent() + ").");
2746 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002747 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002748 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002749
2750 // Notify the accessibility manager that this folder "window" has appeared and occluded
2751 // the workspace items
2752 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2753 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002754 }
2755
2756 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002757 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002758 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002759 if (folder.isEditingName()) {
2760 folder.dismissEditingName();
2761 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002762 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002763
2764 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002765 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002766 }
2767 }
2768
2769 void closeFolder(Folder folder) {
2770 folder.getInfo().opened = false;
2771
2772 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2773 if (parent != null) {
2774 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2775 shrinkAndFadeInFolderIcon(fi);
2776 }
2777 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002778
2779 // Notify the accessibility manager that this folder "window" has disappeard and no
2780 // longer occludeds the workspace items
2781 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002782 }
2783
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002784 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002785 if (!isDraggingEnabled()) return false;
2786 if (isWorkspaceLocked()) return false;
2787 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002788
Adam Cohen1697b792013-09-17 19:08:21 -07002789 if (v instanceof Workspace) {
2790 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002791 if (mWorkspace.enterOverviewMode()) {
2792 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2793 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2794 return true;
2795 } else {
2796 return false;
2797 }
Adam Cohen1697b792013-09-17 19:08:21 -07002798 }
Adam Cohen1697b792013-09-17 19:08:21 -07002799 }
2800
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002801 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002802 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002803 }
2804
Michael Jurka0280c3b2010-09-17 15:00:07 -07002805 resetAddInfo();
2806 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002807 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002808 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002809 return true;
2810 }
2811
Winson Chung3d503fb2011-07-13 17:25:49 -07002812 // The hotseat touch handling does not go through Workspace, and we always allow long press
2813 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002814 final View itemUnderLongClick = longClickCellInfo.cell;
Dan Sandlere26d0942014-01-13 14:30:14 -05002815 final boolean inHotseat = isHotseatLayout(v);
2816 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07002817 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002818 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07002819 // User long pressed on empty space
2820 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2821 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2822 // Disabling reordering until we sort out some issues.
2823 if (mWorkspace.isInOverviewMode()) {
2824 mWorkspace.startReordering(v);
2825 } else {
2826 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002827 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002828 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05002829 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
2830 mHotseat.getOrderInHotseat(
2831 longClickCellInfo.cellX,
2832 longClickCellInfo.cellY));
2833 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002834 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002835 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836 }
2837 }
2838 }
2839 return true;
2840 }
2841
Winson Chung3d503fb2011-07-13 17:25:49 -07002842 boolean isHotseatLayout(View layout) {
2843 return mHotseat != null && layout != null &&
2844 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2845 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002846
Winson Chung3d503fb2011-07-13 17:25:49 -07002847 /**
2848 * Returns the CellLayout of the specified container at the specified screen.
2849 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002850 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002851 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2852 if (mHotseat != null) {
2853 return mHotseat.getLayout();
2854 } else {
2855 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002856 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002857 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002858 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002859 }
2860 }
2861
Daniel Sandler843e8602010-06-07 14:59:01 -04002862 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002863 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002864 }
2865
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002866 /**
2867 * Helper method for the cameraZoomIn/cameraZoomOut animations
2868 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002869 * @param scaleFactor The scale factor used for the zoom
2870 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002871 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002872 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002873 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002874 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002875
Craig Mautner360310b2012-10-26 15:13:08 -07002876 private void setWorkspaceBackground(boolean workspace) {
2877 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002878 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002879 }
2880
Selim Cinekf3a8e8c2014-01-16 10:38:38 -08002881 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002882 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2883 int curflags = getWindow().getAttributes().flags
2884 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2885 if (wpflags != curflags) {
2886 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2887 }
Craig Mautner360310b2012-10-26 15:13:08 -07002888 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002889 }
2890
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002891 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2892 if (v instanceof LauncherTransitionable) {
2893 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2894 }
2895 }
2896
Michael Jurkabed61d22012-02-14 22:51:29 -08002897 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2898 if (v instanceof LauncherTransitionable) {
2899 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2900 }
Winson Chung70442722012-02-10 15:43:22 -08002901
2902 // Update the workspace transition step as well
2903 dispatchOnLauncherTransitionStep(v, 0f);
2904 }
2905
2906 private void dispatchOnLauncherTransitionStep(View v, float t) {
2907 if (v instanceof LauncherTransitionable) {
2908 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2909 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002910 }
2911
2912 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2913 if (v instanceof LauncherTransitionable) {
2914 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2915 }
Winson Chung70442722012-02-10 15:43:22 -08002916
2917 // Update the workspace transition step as well
2918 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002919 }
2920
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002921 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002922 * Things to test when changing the following seven functions.
2923 * - Home from workspace
2924 * - from center screen
2925 * - from other screens
2926 * - Home from all apps
2927 * - from center screen
2928 * - from other screens
2929 * - Back from all apps
2930 * - from center screen
2931 * - from other screens
2932 * - Launch app from workspace and quit
2933 * - with back
2934 * - with home
2935 * - Launch app from all apps and quit
2936 * - with back
2937 * - with home
2938 * - Go to a screen that's not the default, then all
2939 * apps, and launch and app, and go back
2940 * - with back
2941 * -with home
2942 * - On workspace, long press power and go back
2943 * - with back
2944 * - with home
2945 * - On all apps, long press power and go back
2946 * - with back
2947 * - with home
2948 * - On workspace, power off
2949 * - On all apps, power off
2950 * - Launch an app and turn off the screen while in that app
2951 * - Go back with home key
2952 * - Go back with back key TODO: make this not go to workspace
2953 * - From all apps
2954 * - From workspace
2955 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2956 * - From all apps
2957 * - From the center workspace
2958 * - From another workspace
2959 */
2960
2961 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002962 * Zoom the camera out from the workspace to reveal 'toView'.
2963 * Assumes that the view to show is anchored at either the very top or very bottom
2964 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002965 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002966 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07002967 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
2968 showAppsCustomizeHelper(animated, springLoaded, contentType);
2969 }
2970 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
2971 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002972 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002973 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002974 mStateAnimation.cancel();
2975 mStateAnimation = null;
2976 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002977 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002978
Winson Chungf0ea4d32011-06-06 14:27:16 -07002979 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2980 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2981 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002982 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002983 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002984 final int startDelay =
2985 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002986
Michael Jurkab3e22d92011-10-31 15:58:33 -07002987 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002988
Michael Jurkad74c9842011-07-10 12:44:21 -07002989 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002990 Animator workspaceAnim =
2991 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08002992 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08002993 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
2994 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07002995 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
2996 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002997
2998 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002999 toView.setScaleX(scale);
3000 toView.setScaleY(scale);
3001 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
3002 scaleAnim.
3003 scaleX(1f).scaleY(1f).
3004 setDuration(duration).
3005 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003006
Winson Chungf0ea4d32011-06-06 14:27:16 -07003007 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08003008 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003009 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003010 .ofFloat(toView, "alpha", 0f, 1f)
3011 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003012 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08003013 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3014 @Override
3015 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07003016 if (animation == null) {
3017 throw new RuntimeException("animation is null");
3018 }
Winson Chung70442722012-02-10 15:43:22 -08003019 float t = (Float) animation.getAnimatedValue();
3020 dispatchOnLauncherTransitionStep(fromView, t);
3021 dispatchOnLauncherTransitionStep(toView, t);
3022 }
3023 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003024
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003025 // toView should appear right at the end of the workspace shrink
3026 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07003027 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003028 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08003029 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003030
3031 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003032 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07003033 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003034 // Prepare the position
3035 toView.setTranslationX(0.0f);
3036 toView.setTranslationY(0.0f);
3037 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003038 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003039 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07003040 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003041 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003042 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3043 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07003044
Winson Chungc7d2b602012-05-16 17:02:20 -07003045 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003046 if (mSearchDropTargetBar != null) {
3047 mSearchDropTargetBar.hideSearchBar(false);
3048 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07003049 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003050 });
3051
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003052 if (workspaceAnim != null) {
3053 mStateAnimation.play(workspaceAnim);
3054 }
Michael Jurka1899a362011-11-03 13:50:45 -07003055
3056 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003057
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003058 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3059 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003060
3061 // If any of the objects being animated haven't been measured/laid out
3062 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003063 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003064 (mWorkspace.getMeasuredWidth() == 0) ||
3065 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003066 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003067 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003068
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003069 final AnimatorSet stateAnimation = mStateAnimation;
3070 final Runnable startAnimRunnable = new Runnable() {
3071 public void run() {
3072 // Check that mStateAnimation hasn't changed while
3073 // we waited for a layout/draw pass
3074 if (mStateAnimation != stateAnimation)
3075 return;
3076 setPivotsForZoom(toView, scale);
3077 dispatchOnLauncherTransitionStart(fromView, animated, false);
3078 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003079 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003080 }
3081 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003082 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003083 final ViewTreeObserver observer = toView.getViewTreeObserver();
3084 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3085 public void onGlobalLayout() {
3086 startAnimRunnable.run();
3087 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3088 }
3089 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003090 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003091 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003092 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003093 } else {
3094 toView.setTranslationX(0.0f);
3095 toView.setTranslationY(0.0f);
3096 toView.setScaleX(1.0f);
3097 toView.setScaleY(1.0f);
3098 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003099 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003100
Daniel Sandlere4f98912013-06-25 15:13:26 -04003101 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003102 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003103 if (mSearchDropTargetBar != null) {
3104 mSearchDropTargetBar.hideSearchBar(false);
3105 }
Michael Jurkaabded662011-03-04 12:06:57 -08003106 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003107 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003108 dispatchOnLauncherTransitionStart(fromView, animated, false);
3109 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003110 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003111 dispatchOnLauncherTransitionStart(toView, animated, false);
3112 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003113 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003114 }
3115
3116 /**
3117 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003118 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003119 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003120 */
Adam Cohened307df2013-10-02 09:37:31 -07003121 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003122 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003123
Michael Jurkab3e22d92011-10-31 15:58:33 -07003124 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003125 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003126 mStateAnimation.cancel();
3127 mStateAnimation = null;
3128 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003129 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003130
Winson Chungf0ea4d32011-06-06 14:27:16 -07003131 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08003132 final int fadeOutDuration =
3133 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003134 final float scaleFactor = (float)
3135 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3136 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003137 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003138 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003139 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003140 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3141 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003142 toState, animated, stagger, -1);
3143 } else if (toState == Workspace.State.SPRING_LOADED ||
3144 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003145 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003146 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003147 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003148
Michael Jurkab3e22d92011-10-31 15:58:33 -07003149 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003150 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003151 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08003152 final LauncherViewPropertyAnimator scaleAnim =
3153 new LauncherViewPropertyAnimator(fromView);
3154 scaleAnim.
3155 scaleX(scaleFactor).scaleY(scaleFactor).
3156 setDuration(duration).
3157 setInterpolator(new Workspace.ZoomInInterpolator());
3158
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003159 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08003160 .ofFloat(fromView, "alpha", 1f, 0f)
3161 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07003162 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08003163 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3164 @Override
3165 public void onAnimationUpdate(ValueAnimator animation) {
3166 float t = 1f - (Float) animation.getAnimatedValue();
3167 dispatchOnLauncherTransitionStep(fromView, t);
3168 dispatchOnLauncherTransitionStep(toView, t);
3169 }
3170 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08003171
Michael Jurka2ecf9952012-06-18 12:52:28 -07003172 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08003173
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003174 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3175 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Adam Cohen4fe4c932013-10-28 16:02:34 -07003176 mAppsCustomizeContent.stopScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08003177
3178 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003179 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003180 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003181 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003182 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3183 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003184 if (onCompleteRunnable != null) {
3185 onCompleteRunnable.run();
3186 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003187 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003188 }
3189 });
3190
Winson Chungf0ea4d32011-06-06 14:27:16 -07003191 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003192 if (workspaceAnim != null) {
3193 mStateAnimation.play(workspaceAnim);
3194 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003195 dispatchOnLauncherTransitionStart(fromView, animated, true);
3196 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003197 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003198 } else {
3199 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003200 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003201 dispatchOnLauncherTransitionStart(fromView, animated, true);
3202 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003203 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003204 dispatchOnLauncherTransitionStart(toView, animated, true);
3205 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003206 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003207 }
3208
Michael Jurkae326f182011-11-21 14:05:46 -08003209 @Override
3210 public void onTrimMemory(int level) {
3211 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003212 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003213 mAppsCustomizeTabHost.onTrimMemory();
3214 }
3215 }
3216
Adam Cohened307df2013-10-02 09:37:31 -07003217 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003218 showWorkspace(animated, null);
3219 }
3220
Adam Cohened307df2013-10-02 09:37:31 -07003221 protected void showWorkspace() {
3222 showWorkspace(true);
3223 }
3224
Adam Cohened66b2b2012-01-23 17:28:51 -08003225 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohened307df2013-10-02 09:37:31 -07003226 if (mWorkspace.isInOverviewMode()) {
3227 mWorkspace.exitOverviewMode(animated);
3228 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003229 if (mState != State.WORKSPACE) {
Winson Chung2d75f122013-09-23 16:53:31 -07003230 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003231 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003232 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003233
Winson Chungc7d2b602012-05-16 17:02:20 -07003234 // Show the search bar (only animate if we were showing the drop target bar in spring
3235 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003236 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003237 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003238 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003239
Michael Jurkab3e22d92011-10-31 15:58:33 -07003240 // Set focus to the AppsCustomize button
3241 if (mAllAppsButton != null) {
3242 mAllAppsButton.requestFocus();
3243 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003244 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003245
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003246 // Change the state *after* we've called all the transition code
3247 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003248
Winson Chung5fb63472011-02-02 17:03:37 -08003249 // Resume the auto-advance of widgets
3250 mUserPresent = true;
3251 updateRunning();
3252
alanv1d4fde62012-10-17 13:15:47 -07003253 // Send an accessibility event to announce the context change
3254 getWindow().getDecorView()
3255 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003256
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003257 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003258 }
3259
Adam Cohened307df2013-10-02 09:37:31 -07003260 void showOverviewMode(boolean animated) {
3261 mWorkspace.setVisibility(View.VISIBLE);
3262 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3263 mState = State.WORKSPACE;
3264 onWorkspaceShown(animated);
3265 }
3266
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003267 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003268 }
3269
Winson Chung82963d52013-10-09 11:20:57 -07003270 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3271 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003272 if (mState != State.WORKSPACE) return;
3273
Winson Chung82963d52013-10-09 11:20:57 -07003274 if (resetPageToZero) {
3275 mAppsCustomizeTabHost.reset();
3276 }
Winson Chungc58497e2013-09-03 17:48:37 -07003277 showAppsCustomizeHelper(animated, false, contentType);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003278 mAppsCustomizeTabHost.requestFocus();
3279
Michael Jurkab3e22d92011-10-31 15:58:33 -07003280 // Change the state *after* we've called all the transition code
3281 mState = State.APPS_CUSTOMIZE;
3282
3283 // Pause the auto-advance of widgets until we are out of AllApps
3284 mUserPresent = false;
3285 updateRunning();
3286 closeFolder();
3287
3288 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003289 getWindow().getDecorView()
3290 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003291 }
3292
Adam Cohen7777d962011-08-18 18:58:38 -07003293 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003294 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003295 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003296 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003297 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003298 }
Adam Cohen7777d962011-08-18 18:58:38 -07003299
Adam Cohenad4e15c2013-10-17 16:21:35 -07003300 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003301 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003302 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3303
Winson Chunge7a03942011-08-05 15:05:12 -07003304 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003305 @Override
3306 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003307 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003308 // Before we show workspace, hide all apps again because
3309 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3310 // clean up our state transition functions
3311 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003312 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003313 } else {
3314 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003315 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003316 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003317 }, delay);
3318
Winson Chung557d6ed2011-07-08 15:34:52 -07003319 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003320
Michael Jurkad3ef3062010-11-23 16:23:58 -08003321 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003322 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003323 final boolean animated = true;
3324 final boolean springLoaded = true;
3325 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003326 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003327 }
3328 // Otherwise, we are not in spring loaded mode, so don't do anything.
3329 }
3330
Michael Jurkab3e22d92011-10-31 15:58:33 -07003331 void lockAllApps() {
3332 // TODO
3333 }
3334
3335 void unlockAllApps() {
3336 // TODO
3337 }
3338
Winson Chungf0ea4d32011-06-06 14:27:16 -07003339 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003340 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003341 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003342 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003343 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003344 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003345 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003346 int duration = 0;
3347 if (mSearchDropTargetBar != null) {
3348 duration = mSearchDropTargetBar.getTransitionInDuration();
3349 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003350 mHotseat.animate().alpha(1f).setDuration(duration);
3351 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003352 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003353 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003354 }
3355 }
3356 }
3357
3358 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003359 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003360 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003361 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003362 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003363 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003364 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003365 int duration = 0;
3366 if (mSearchDropTargetBar != null) {
3367 duration = mSearchDropTargetBar.getTransitionOutDuration();
3368 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003369 mHotseat.animate().alpha(0f).setDuration(duration);
3370 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003371 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003372 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003373 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003374 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003375 }
3376
Patrick Dubroy5f445422011-02-18 14:35:21 -08003377 /**
3378 * Add an item from all apps or customize onto the given workspace screen.
3379 * If layout is null, add to the current screen.
3380 */
3381 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003382 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003383 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003384 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003385 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003386
Winson Chungdff8ebb2011-09-08 17:25:31 -07003387 /** Maps the current orientation to an index for referencing orientation correct global icons */
3388 private int getCurrentOrientationIndexForGlobalIcons() {
3389 // default - 0, landscape - 1
3390 switch (getResources().getConfiguration().orientation) {
3391 case Configuration.ORIENTATION_LANDSCAPE:
3392 return 1;
3393 default:
3394 return 0;
3395 }
3396 }
3397
Michael Jurkaae65ee32012-04-30 11:45:54 -07003398 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003399 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003400 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003401 // Look for the toolbar icon specified in the activity meta-data
3402 Bundle metaData = packageManager.getActivityInfo(
3403 activityName, PackageManager.GET_META_DATA).metaData;
3404 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003405 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003406 if (iconResId != 0) {
3407 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003408 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003409 }
3410 }
3411 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003412 // This can happen if the activity defines an invalid drawable
3413 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3414 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003415 } catch (Resources.NotFoundException nfe) {
3416 // This can happen if the activity defines an invalid drawable
3417 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3418 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003419 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003420 return null;
3421 }
3422
3423 // if successful in getting icon, return it; otherwise, set button to use default drawable
3424 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003425 int buttonId, ComponentName activityName, int fallbackDrawableId,
3426 String toolbarResourceName) {
3427 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003428 Resources r = getResources();
3429 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3430 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003431
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003432 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003433 // If we were unable to find the icon via the meta-data, use a generic one
3434 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003435 toolbarIcon = r.getDrawable(fallbackDrawableId);
3436 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003437 if (button != null) {
3438 button.setCompoundDrawables(toolbarIcon, null, null, null);
3439 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003440 return null;
3441 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003442 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003443 if (button != null) {
3444 button.setCompoundDrawables(toolbarIcon, null, null, null);
3445 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003446 return toolbarIcon.getConstantState();
3447 }
3448 }
3449
3450 // if successful in getting icon, return it; otherwise, set button to use default drawable
3451 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003452 int buttonId, ComponentName activityName, int fallbackDrawableId,
3453 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003454 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003455 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003456
Michael Jurka19e0fc52011-07-22 18:00:21 -07003457 if (button != null) {
3458 // If we were unable to find the icon via the meta-data, use a
3459 // generic one
3460 if (toolbarIcon == null) {
3461 button.setImageResource(fallbackDrawableId);
3462 } else {
3463 button.setImageDrawable(toolbarIcon);
3464 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003465 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003466
3467 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3468
Michael Jurka0423dcf2010-10-05 14:56:18 -07003469 }
3470
Winson Chung1b884092012-06-08 17:13:02 -07003471 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003472 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003473 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003474 }
3475
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003476 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003477 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003478 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003479 }
3480
Winson Chungbb185bd2011-11-21 12:31:42 -08003481 private void invalidatePressedFocusedStates(View container, View button) {
3482 if (container instanceof HolographicLinearLayout) {
3483 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3484 layout.invalidatePressedFocusedStates();
3485 } else if (button instanceof HolographicImageView) {
3486 HolographicImageView view = (HolographicImageView) button;
3487 view.invalidatePressedFocusedStates();
3488 }
3489 }
3490
Cristina Stancu476493b2013-08-07 17:20:14 +01003491 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003492 if (mQsb == null) {
3493 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3494 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003495 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003496 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003497 }
3498
3499 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003500 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003501 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003502 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003503 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003504
Winson Chung1cad91e2011-05-25 17:41:01 -07003505 final SearchManager searchManager =
3506 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3507 ComponentName activityName = searchManager.getGlobalSearchActivity();
3508 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003509 int coi = getCurrentOrientationIndexForGlobalIcons();
3510 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003511 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3512 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3513 if (sGlobalSearchIcon[coi] == null) {
3514 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3515 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3516 TOOLBAR_ICON_METADATA_NAME);
3517 }
3518
Winson Chungc51db6a2011-10-05 11:44:49 -07003519 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3520 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003521 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003522 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003523 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003524 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003525 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3526 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003527 if (searchButton != null) searchButton.setVisibility(View.GONE);
3528 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003529 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003530 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003531 }
3532 }
3533
Cristina Stancu476493b2013-08-07 17:20:14 +01003534 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003535 final View searchButtonContainer = findViewById(R.id.search_button_container);
3536 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003537 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003538 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003539 }
3540
Cristina Stancu476493b2013-08-07 17:20:14 +01003541 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003542 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003543 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003544
Winson Chungc51db6a2011-10-05 11:44:49 -07003545 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003546 final SearchManager searchManager =
3547 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3548 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3549
3550 ComponentName activityName = null;
3551 if (globalSearchActivity != null) {
3552 // Check if the global search activity handles voice search
3553 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3554 intent.setPackage(globalSearchActivity.getPackageName());
3555 activityName = intent.resolveActivity(getPackageManager());
3556 }
3557
3558 if (activityName == null) {
3559 // Fallback: check if an activity other than the global search activity
3560 // resolves this
3561 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3562 activityName = intent.resolveActivity(getPackageManager());
3563 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003564 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003565 int coi = getCurrentOrientationIndexForGlobalIcons();
3566 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003567 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3568 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3569 if (sVoiceSearchIcon[coi] == null) {
3570 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3571 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3572 TOOLBAR_ICON_METADATA_NAME);
3573 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003574 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003575 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003576 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003577 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003578 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003579 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003580 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003581 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003582 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003583 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003584 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003585 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003586
Cristina Stancu476493b2013-08-07 17:20:14 +01003587 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003588 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3589 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003590 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003591 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003592 }
3593
Winson Chung5841efa2013-09-30 18:06:44 -07003594 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003595 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3596 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003597 boolean visible = !forceDisableVoiceButtonProxy &&
3598 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003599 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003600 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003601 }
3602 }
Winson Chung5841efa2013-09-30 18:06:44 -07003603
3604 /**
3605 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3606 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3607 */
3608 public void disableVoiceButtonProxy(boolean disabled) {
3609 updateVoiceButtonProxyVisible(disabled);
3610 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003611 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003612 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003613 */
3614 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003615 if (!DISABLE_MARKET_BUTTON) {
3616 final View marketButton = findViewById(R.id.market_button);
3617 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3618 // Find the app market activity by resolving an intent.
3619 // (If multiple app markets are installed, it will return the ResolverActivity.)
3620 ComponentName activityName = intent.resolveActivity(getPackageManager());
3621 if (activityName != null) {
3622 int coi = getCurrentOrientationIndexForGlobalIcons();
3623 mAppMarketIntent = intent;
3624 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3625 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3626 TOOLBAR_ICON_METADATA_NAME);
3627 marketButton.setVisibility(View.VISIBLE);
3628 } else {
3629 // We should hide and disable the view so that we don't try and restore the visibility
3630 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3631 marketButton.setVisibility(View.GONE);
3632 marketButton.setEnabled(false);
3633 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003634 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003635 }
3636
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003637 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07003638 if (!DISABLE_MARKET_BUTTON) {
3639 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3640 Resources r = getResources();
3641 Drawable marketIconDrawable = d.newDrawable(r);
3642 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3643 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3644 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07003645
Winson Chungd64a6662013-09-30 11:06:59 -07003646 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
3647 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08003648 }
3649
Michael Jurkad7c28052012-04-27 15:43:36 -07003650 @Override
3651 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003652 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003653 final List<CharSequence> text = event.getText();
3654 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003655 // Populate event with a fake title based on the current state.
3656 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen4b378872013-10-04 18:58:04 -07003657 text.add(mAppsCustomizeTabHost.getCurrentTabView().getContentDescription());
alanv1d4fde62012-10-17 13:15:47 -07003658 } else {
3659 text.add(getString(R.string.all_apps_home_button_label));
3660 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003661 return result;
3662 }
3663
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003664 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003665 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003666 */
3667 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3668 @Override
3669 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003670 closeSystemDialogs();
3671 }
3672 }
3673
3674 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003675 * Receives notifications whenever the appwidgets are reset.
3676 */
3677 private class AppWidgetResetObserver extends ContentObserver {
3678 public AppWidgetResetObserver() {
3679 super(new Handler());
3680 }
3681
3682 @Override
3683 public void onChange(boolean selfChange) {
3684 onAppWidgetReset();
3685 }
3686 }
3687
3688 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003689 * If the activity is currently paused, signal that we need to run the passed Runnable
3690 * in onResume.
3691 *
3692 * This needs to be called from incoming places where resources might have been loaded
3693 * while we are paused. That is becaues the Configuration might be wrong
3694 * when we're not running, and if it comes back to what it was when we
3695 * were paused, we are not restarted.
3696 *
3697 * Implementation of the method from LauncherModel.Callbacks.
3698 *
3699 * @return true if we are currently paused. The caller might be able to
3700 * skip some work in that case since we will come back again.
3701 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003702 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003703 if (mPaused) {
3704 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003705 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003706 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003707 }
3708 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003709 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003710 return true;
3711 } else {
3712 return false;
3713 }
3714 }
3715
Michael Jurkac402cd92013-05-20 15:49:32 +02003716 private boolean waitUntilResume(Runnable run) {
3717 return waitUntilResume(run, false);
3718 }
3719
Michael Jurka1e2f4652013-07-08 18:03:46 -07003720 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003721 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003722 }
3723
Michael Jurka7607c2f2013-04-03 14:33:19 -07003724 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003725 * If the activity is currently paused, signal that we need to re-run the loader
3726 * in onResume.
3727 *
3728 * This needs to be called from incoming places where resources might have been loaded
3729 * while we are paused. That is becaues the Configuration might be wrong
3730 * when we're not running, and if it comes back to what it was when we
3731 * were paused, we are not restarted.
3732 *
3733 * Implementation of the method from LauncherModel.Callbacks.
3734 *
3735 * @return true if we are currently paused. The caller might be able to
3736 * skip some work in that case since we will come back again.
3737 */
3738 public boolean setLoadOnResume() {
3739 if (mPaused) {
3740 Log.i(TAG, "setLoadOnResume");
3741 mOnResumeNeedsLoad = true;
3742 return true;
3743 } else {
3744 return false;
3745 }
3746 }
3747
3748 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003749 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003750 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003751 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003752 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003753 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003754 } else {
3755 return SCREEN_COUNT / 2;
3756 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003757 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003758
Joe Onorato9c1289c2009-08-17 11:03:03 -04003759 /**
3760 * Refreshes the shortcuts shown on the workspace.
3761 *
3762 * Implementation of the method from LauncherModel.Callbacks.
3763 */
3764 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003765 // If we're starting binding all over again, clear any bind calls we'd postponed in
3766 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3767 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003768 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003769
Winson Chungd64d1762013-08-20 14:37:16 -07003770 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003771 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003772 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003773
Michael Jurka05bf6442011-11-30 20:28:53 -08003774 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003775 if (mHotseat != null) {
3776 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003777 }
3778 }
3779
Adam Cohendcd297f2013-06-18 13:13:40 -07003780 @Override
3781 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003782 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003783
Adam Cohen5084cba2013-09-03 12:01:16 -07003784 // If there are no screens, we need to have an empty screen
3785 if (orderedScreenIds.size() == 0) {
3786 mWorkspace.addExtraEmptyScreen();
3787 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003788
3789 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003790 // setCurrentPage() so ensure that all pages are added before calling this).
3791 // The actual content of the custom page will be added during onFinishBindingItems().
Winson Chung0e6a7132013-08-23 12:55:10 -07003792 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3793 mWorkspace.createCustomContentPage();
3794 }
Winson Chung64359a52013-07-08 17:17:08 -07003795 }
3796
3797 @Override
3798 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003799 // Log to disk
3800 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3801 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3802 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003803 int count = orderedScreenIds.size();
3804 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003805 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003806 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003807 }
3808
Adam Cohen39a06042013-07-19 14:30:12 -07003809 private boolean shouldShowWeightWatcher() {
3810 String spKey = LauncherAppState.getSharedPreferencesKey();
3811 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003812 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003813
3814 return show;
3815 }
3816
3817 private void toggleShowWeightWatcher() {
3818 String spKey = LauncherAppState.getSharedPreferencesKey();
3819 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3820 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3821
3822 show = !show;
3823
3824 SharedPreferences.Editor editor = sp.edit();
3825 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3826 editor.commit();
3827
3828 if (mWeightWatcher != null) {
3829 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3830 }
3831 }
3832
Winson Chungd64d1762013-08-20 14:37:16 -07003833 public void bindAppsAdded(final ArrayList<Long> newScreens,
3834 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003835 final ArrayList<ItemInfo> addAnimated,
3836 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003837 Runnable r = new Runnable() {
3838 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003839 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003840 }
3841 };
3842 if (waitUntilResume(r)) {
3843 return;
3844 }
3845
Winson Chungd64d1762013-08-20 14:37:16 -07003846 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003847 if (newScreens != null) {
3848 bindAddScreens(newScreens);
3849 }
Winson Chungd64d1762013-08-20 14:37:16 -07003850
3851 // We add the items without animation on non-visible pages, and with
3852 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003853 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003854 bindItems(addNotAnimated, 0,
3855 addNotAnimated.size(), false);
3856 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003857 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003858 bindItems(addAnimated, 0,
3859 addAnimated.size(), true);
3860 }
Winson Chungc58497e2013-09-03 17:48:37 -07003861
Winson Chung87412982013-10-03 18:34:14 -07003862 // Remove the extra empty screen
Adam Cohenad4e15c2013-10-17 16:21:35 -07003863 mWorkspace.removeExtraEmptyScreen(false, null);
Winson Chung87412982013-10-03 18:34:14 -07003864
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003865 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07003866 addedApps != null && mAppsCustomizeContent != null) {
3867 mAppsCustomizeContent.addApps(addedApps);
3868 }
Winson Chungd64d1762013-08-20 14:37:16 -07003869 }
3870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003871 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003872 * Bind the items start-end from the list.
3873 *
3874 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003875 */
Winson Chung64359a52013-07-08 17:17:08 -07003876 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3877 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003878 Runnable r = new Runnable() {
3879 public void run() {
3880 bindItems(shortcuts, start, end, forceAnimateIcons);
3881 }
3882 };
3883 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003884 return;
3885 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003886
Winson Chungf0c6ae02012-03-21 16:10:31 -07003887 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003888 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3889 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003890 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003891 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003892 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003893 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003894 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003895
3896 // Short circuit if we are loading dock items for a configuration which has no dock
3897 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3898 mHotseat == null) {
3899 continue;
3900 }
3901
Joe Onorato9c1289c2009-08-17 11:03:03 -04003902 switch (item.itemType) {
3903 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3904 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003905 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003906 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003907
Winson Chung64359a52013-07-08 17:17:08 -07003908 /*
3909 * TODO: FIX collision case
3910 */
Winson Chungce376632013-07-11 16:12:41 -07003911 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3912 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3913 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3914 throw new RuntimeException("OCCUPIED");
3915 }
Winson Chung64359a52013-07-08 17:17:08 -07003916 }
3917
Adam Cohendcd297f2013-06-18 13:13:40 -07003918 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3919 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003920 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003921 // Animate all the applications up now
3922 shortcut.setAlpha(0f);
3923 shortcut.setScaleX(0f);
3924 shortcut.setScaleY(0f);
3925 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003926 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003927 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003928 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003929 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003930 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003931 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003932 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003933 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3934 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003935 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003936 default:
3937 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003939 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003940
Winson Chung997a9232013-07-24 15:33:46 -07003941 if (animateIcons) {
3942 // Animate to the correct page
3943 if (newShortcutsScreenId > -1) {
3944 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003945 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003946 final Runnable startBounceAnimRunnable = new Runnable() {
3947 public void run() {
3948 anim.playTogether(bounceAnims);
3949 anim.start();
3950 }
3951 };
Winson Chung997a9232013-07-24 15:33:46 -07003952 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003953 // We post the animation slightly delayed to prevent slowdowns
3954 // when we are loading right after we return to launcher.
3955 mWorkspace.postDelayed(new Runnable() {
3956 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003957 if (mWorkspace != null) {
3958 mWorkspace.snapToPage(newScreenIndex);
3959 mWorkspace.postDelayed(startBounceAnimRunnable,
3960 NEW_APPS_ANIMATION_DELAY);
3961 }
Winson Chung94d67682013-09-25 16:29:40 -07003962 }
3963 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003964 } else {
3965 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003966 }
3967 }
Winson Chung64359a52013-07-08 17:17:08 -07003968 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003969 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003970 }
3971
Joe Onorato9c1289c2009-08-17 11:03:03 -04003972 /**
3973 * Implementation of the method from LauncherModel.Callbacks.
3974 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003975 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003976 Runnable r = new Runnable() {
3977 public void run() {
3978 bindFolders(folders);
3979 }
3980 };
3981 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003982 return;
3983 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003984 sFolders.clear();
3985 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003986 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003987
3988 /**
3989 * Add the views for a widget to the workspace.
3990 *
3991 * Implementation of the method from LauncherModel.Callbacks.
3992 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003993 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003994 Runnable r = new Runnable() {
3995 public void run() {
3996 bindAppWidget(item);
3997 }
3998 };
3999 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004000 return;
4001 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004002
Daniel Sandler843e8602010-06-07 14:59:01 -04004003 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4004 if (DEBUG_WIDGETS) {
4005 Log.d(TAG, "bindAppWidget: " + item);
4006 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004007 final Workspace workspace = mWorkspace;
4008
4009 final int appWidgetId = item.appWidgetId;
4010 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004011 if (DEBUG_WIDGETS) {
4012 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4013 }
4014
Joe Onorato9c1289c2009-08-17 11:03:03 -04004015 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4016
Joe Onorato9c1289c2009-08-17 11:03:03 -04004017 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004018 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004019
Adam Cohendcd297f2013-06-18 13:13:40 -07004020 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004021 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004022 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4023
Joe Onorato9c1289c2009-08-17 11:03:03 -04004024 workspace.requestLayout();
4025
Daniel Sandler843e8602010-06-07 14:59:01 -04004026 if (DEBUG_WIDGETS) {
4027 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4028 + (SystemClock.uptimeMillis()-start) + "ms");
4029 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004030 }
4031
Adam Cohen1462de32012-07-24 22:34:36 -07004032 public void onPageBoundSynchronously(int page) {
4033 mSynchronouslyBoundPages.add(page);
4034 }
4035
Joe Onorato9c1289c2009-08-17 11:03:03 -04004036 /**
4037 * Callback saying that there aren't any more items to bind.
4038 *
4039 * Implementation of the method from LauncherModel.Callbacks.
4040 */
Adam Cohene25af792013-06-06 23:08:25 -07004041 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004042 Runnable r = new Runnable() {
4043 public void run() {
4044 finishBindingItems(upgradePath);
4045 }
4046 };
4047 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004048 return;
4049 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004050 if (mSavedState != null) {
4051 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004052 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004053 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004054 mSavedState = null;
4055 }
4056
Adam Cohen1462de32012-07-24 22:34:36 -07004057 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004058
Patrick Dubroy002cbf42011-03-03 16:36:21 -08004059 // If we received the result of any pending adds while the loader was running (e.g. the
4060 // widget configuration forced an orientation change), process them now.
4061 for (int i = 0; i < sPendingAddList.size(); i++) {
4062 completeAdd(sPendingAddList.get(i));
4063 }
4064 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004065
Winson Chungc51db6a2011-10-05 11:44:49 -07004066 // Update the market app icon as necessary (the other icons will be managed in response to
4067 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004068 if (!DISABLE_MARKET_BUTTON) {
4069 updateAppMarketIcon();
4070 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004071
Winson Chungf0c6ae02012-03-21 16:10:31 -07004072 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07004073 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004074 mWorkspace.getUniqueComponents(true, null);
4075 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004076 }
Adam Cohen99894d92013-06-14 11:22:59 -07004077
Adam Cohen66a01fd2013-06-11 12:48:00 -07004078 mWorkspace.post(new Runnable() {
4079 @Override
4080 public void run() {
4081 onFinishBindingItems();
4082 }
4083 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07004084 }
4085
Winson Chunga0b7e862013-09-05 16:03:15 -07004086 public boolean isAllAppsButtonRank(int rank) {
4087 if (mHotseat != null) {
4088 return mHotseat.isAllAppsButtonRank(rank);
4089 }
4090 return false;
4091 }
4092
Winson Chunga2413752012-04-03 14:22:34 -07004093 private boolean canRunNewAppsAnimation() {
4094 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4095 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4096 }
4097
Winson Chungc9168342013-06-26 14:54:55 -07004098 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4099 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4100 PropertyValuesHolder.ofFloat("alpha", 1f),
4101 PropertyValuesHolder.ofFloat("scaleX", 1f),
4102 PropertyValuesHolder.ofFloat("scaleY", 1f));
4103 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4104 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4105 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4106 return bounceAnim;
4107 }
4108
Adam Cohen27772732013-11-11 14:00:56 +00004109 public boolean useVerticalBarLayout() {
4110 return LauncherAppState.getInstance().getDynamicGrid().
4111 getDeviceProfile().isVerticalBarLayout();
4112 }
4113
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004114 protected Rect getSearchBarBounds() {
4115 return LauncherAppState.getInstance().getDynamicGrid().
4116 getDeviceProfile().getSearchBarBounds();
4117 }
4118
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004119 @Override
4120 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004121 boolean searchVisible = updateGlobalSearchIcon();
4122 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004123 if (mSearchDropTargetBar != null) {
4124 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4125 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004126 }
4127
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004128 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004129 * Add the icons for all apps.
4130 *
4131 * Implementation of the method from LauncherModel.Callbacks.
4132 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004133 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004134 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004135 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4136 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4137 getHotseat().addAllAppsFolder(mIconCache, apps,
4138 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4139 }
4140 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004141 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004142 if (mAppsCustomizeContent != null) {
4143 mAppsCustomizeContent.onPackagesUpdated(
4144 LauncherModel.getSortedWidgetsAndShortcuts(this));
4145 }
Winson Chungc58497e2013-09-03 17:48:37 -07004146 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004147 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004148 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004149 mAppsCustomizeContent.onPackagesUpdated(
4150 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004151 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004152 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004153 }
4154
4155 /**
4156 * A package was updated.
4157 *
4158 * Implementation of the method from LauncherModel.Callbacks.
4159 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004160 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004161 Runnable r = new Runnable() {
4162 public void run() {
4163 bindAppsUpdated(apps);
4164 }
4165 };
4166 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004167 return;
4168 }
4169
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004170 if (mWorkspace != null) {
4171 mWorkspace.updateShortcuts(apps);
4172 }
Winson Chungc58497e2013-09-03 17:48:37 -07004173
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004174 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004175 mAppsCustomizeContent != null) {
4176 mAppsCustomizeContent.updateApps(apps);
4177 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004178 }
4179
4180 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004181 * A package was uninstalled. We take both the super set of packageNames
4182 * in addition to specific applications to remove, the reason being that
4183 * this can be called when a package is updated as well. In that scenario,
4184 * we only remove specific components from the workspace, where as
4185 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004186 *
4187 * Implementation of the method from LauncherModel.Callbacks.
4188 */
Winson Chung83892cc2013-05-01 16:53:33 -07004189 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Winson Chungdf95eb12013-10-16 14:57:07 -07004190 final ArrayList<AppInfo> appInfos) {
Winson Chungd64d1762013-08-20 14:37:16 -07004191 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004192 public void run() {
Winson Chungdf95eb12013-10-16 14:57:07 -07004193 bindComponentsRemoved(packageNames, appInfos);
Winson Chung83892cc2013-05-01 16:53:33 -07004194 }
Winson Chungd64d1762013-08-20 14:37:16 -07004195 };
4196 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004197 return;
4198 }
4199
Winson Chungdf95eb12013-10-16 14:57:07 -07004200 if (!packageNames.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004201 mWorkspace.removeItemsByPackageName(packageNames);
Winson Chungdf95eb12013-10-16 14:57:07 -07004202 }
4203 if (!appInfos.isEmpty()) {
Winson Chung83892cc2013-05-01 16:53:33 -07004204 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04004205 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004206
Winson Chunga1820962011-10-03 16:31:06 -07004207 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004208 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004209
Winson Chungdf95eb12013-10-16 14:57:07 -07004210 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004211 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004212 mAppsCustomizeContent != null) {
4213 mAppsCustomizeContent.removeApps(appInfos);
4214 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004215 }
Joe Onoratobe386092009-11-17 17:32:16 -08004216
4217 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004218 * A number of packages were updated.
4219 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004220 private ArrayList<Object> mWidgetsAndShortcuts;
4221 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004222 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004223 bindPackagesUpdated(mWidgetsAndShortcuts);
4224 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004225 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004226 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004227 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4228 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4229 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004230 return;
4231 }
4232
Winson Chung64359a52013-07-08 17:17:08 -07004233 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004234 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004235 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004236 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004237 }
4238
Winson Chung400438b2011-01-16 17:53:48 -08004239 private int mapConfigurationOriActivityInfoOri(int configOri) {
4240 final Display d = getWindowManager().getDefaultDisplay();
4241 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4242 switch (d.getRotation()) {
4243 case Surface.ROTATION_0:
4244 case Surface.ROTATION_180:
4245 // We are currently in the same basic orientation as the natural orientation
4246 naturalOri = configOri;
4247 break;
4248 case Surface.ROTATION_90:
4249 case Surface.ROTATION_270:
4250 // We are currently in the other basic orientation to the natural orientation
4251 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4252 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4253 break;
4254 }
4255
4256 int[] oriMap = {
4257 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4258 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4259 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4260 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4261 };
4262 // Since the map starts at portrait, we need to offset if this device's natural orientation
4263 // is landscape.
4264 int indexOffset = 0;
4265 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4266 indexOffset = 1;
4267 }
4268 return oriMap[(d.getRotation() + indexOffset) % 4];
4269 }
Adam Cohen446e9402011-09-15 18:21:21 -07004270
Winson Chung4b919f82012-05-01 10:44:08 -07004271 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004272 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004273 getResources().getBoolean(R.bool.allow_rotation);
4274 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004275 }
Winson Chung4b919f82012-05-01 10:44:08 -07004276 public void lockScreenOrientation() {
4277 if (isRotationEnabled()) {
4278 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4279 .getConfiguration().orientation));
4280 }
4281 }
4282 public void unlockScreenOrientation(boolean immediate) {
4283 if (isRotationEnabled()) {
4284 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004285 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004286 } else {
4287 mHandler.postDelayed(new Runnable() {
4288 public void run() {
4289 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4290 }
4291 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004292 }
Winson Chung4b919f82012-05-01 10:44:08 -07004293 }
Winson Chung400438b2011-01-16 17:53:48 -08004294 }
4295
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004296 /**
4297 * Called when the SearchBar hint should be changed.
4298 *
4299 * @param hint the hint to be displayed in the search bar.
4300 */
4301 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004302 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004303 }
4304
Winson Chunge43a1e72014-01-15 10:33:02 -08004305 protected boolean isLauncherPreinstalled() {
4306 PackageManager pm = getPackageManager();
4307 try {
4308 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4309 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4310 return true;
4311 } else {
4312 return false;
4313 }
4314 } catch (NameNotFoundException e) {
4315 e.printStackTrace();
4316 return false;
4317 }
4318 }
4319
Winson Chunge6eabff2013-09-24 11:01:23 -07004320 protected String getFirstRunClingSearchBarHint() {
4321 return "";
4322 }
4323 protected String getFirstRunCustomContentHint() {
4324 return "";
4325 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004326 protected int getFirstRunFocusedHotseatAppDrawableId() {
4327 return -1;
4328 }
4329 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4330 return null;
4331 }
4332 protected int getFirstRunFocusedHotseatAppRank() {
4333 return -1;
4334 }
4335 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4336 return "";
4337 }
4338 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4339 return "";
4340 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004341
Winson Chungaf40f202013-09-18 18:26:31 -07004342 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004343 mLauncherClings.dismissFirstRunCling(v);
4344 }
4345 public void dismissMigrationClingCopyApps(View v) {
4346 mLauncherClings.dismissMigrationClingCopyApps(v);
4347 }
4348 public void dismissMigrationClingUseDefault(View v) {
4349 mLauncherClings.dismissMigrationClingUseDefault(v);
4350 }
4351 public void dismissMigrationWorkspaceCling(View v) {
4352 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004353 }
Winson Chung82f55532011-08-09 14:14:23 -07004354 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004355 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004356 }
4357 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004358 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004359 }
4360
Winson Chunga6945242014-01-08 14:04:34 -08004361 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004362 return !ActivityManager.isRunningInTestHarness() &&
4363 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004364 }
4365
4366 public void showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004367 if (shouldRunFirstRunActivity() &&
4368 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004369 Intent firstRunIntent = getFirstRunActivity();
4370 if (firstRunIntent != null) {
4371 startActivity(firstRunIntent);
4372 markFirstRunActivityShown();
4373 }
4374 }
4375 }
4376
4377 private void markFirstRunActivityShown() {
4378 SharedPreferences.Editor editor = mSharedPrefs.edit();
4379 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4380 editor.apply();
4381 }
4382
4383 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004384 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4385 if (mHotseat != null) mHotseat.setAlpha(1f);
4386 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4387 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004388 }
4389
4390 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004391 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4392 if (mHotseat != null) mHotseat.setAlpha(0f);
4393 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4394 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004395 }
4396
4397
Mathew Inwood72fbec12013-11-19 15:45:07 +00004398 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
4399 ResolveInfo ri = getPackageManager().resolveActivity(appLaunchIntent, 0);
4400 if (ri == null) {
4401 return null;
4402 }
4403 return new AppInfo(getPackageManager(), ri, mIconCache, null);
4404 }
4405
4406 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4407 Bitmap icon) {
4408 return new ShortcutInfo(shortcutIntent, caption, icon);
4409 }
4410
4411 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4412 dragView.setTag(dragInfo);
4413 mWorkspace.onDragStartedWithItem(dragView);
4414 mWorkspace.beginDragShared(dragView, source);
4415 }
4416
Winson Chung80baf5a2010-08-09 16:03:15 -07004417 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004418 * Prints out out state for debugging.
4419 */
4420 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004421 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004422 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004423 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4424 Log.d(TAG, "mRestoring=" + mRestoring);
4425 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4426 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004427 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004428 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004429
Winson Chung785d2eb2011-04-14 16:08:02 -07004430 if (mAppsCustomizeContent != null) {
4431 mAppsCustomizeContent.dumpState();
4432 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004433 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004434 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004435
4436 @Override
4437 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4438 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004439 synchronized (sDumpLogs) {
4440 writer.println(" ");
4441 writer.println("Debug logs: ");
4442 for (int i = 0; i < sDumpLogs.size(); i++) {
4443 writer.println(" " + sDumpLogs.get(i));
4444 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004445 }
4446 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004447
4448 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004449 if (DEBUG_DUMP_LOG) {
4450 synchronized (sDumpLogs) {
4451 Log.d(TAG, "");
4452 Log.d(TAG, "*********************");
4453 Log.d(TAG, "Launcher debug logs: ");
4454 for (int i = 0; i < sDumpLogs.size(); i++) {
4455 Log.d(TAG, " " + sDumpLogs.get(i));
4456 }
4457 Log.d(TAG, "*********************");
4458 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004459 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004460 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004461 }
4462
4463 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004464 addDumpLog(tag, log, null, debugLog);
4465 }
4466
4467 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004468 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004469 if (e != null) {
4470 Log.d(tag, log, e);
4471 } else {
4472 Log.d(tag, log);
4473 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004474 }
Winson Chungede41292013-09-19 16:27:36 -07004475 if (DEBUG_DUMP_LOG) {
4476 sDateStamp.setTime(System.currentTimeMillis());
4477 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004478 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4479 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004480 }
Winson Chungede41292013-09-19 16:27:36 -07004481 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004482 }
4483
Winson Chungede41292013-09-19 16:27:36 -07004484 public void dumpLogsToLocalData() {
4485 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004486 new AsyncTask<Void, Void, Void>() {
4487 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004488 boolean success = false;
4489 sDateStamp.setTime(sRunStart);
4490 String FILENAME = sDateStamp.getMonth() + "-"
4491 + sDateStamp.getDay() + "_"
4492 + sDateStamp.getHours() + "-"
4493 + sDateStamp.getMinutes() + "_"
4494 + sDateStamp.getSeconds() + ".txt";
4495
4496 FileOutputStream fos = null;
4497 File outFile = null;
4498 try {
4499 outFile = new File(getFilesDir(), FILENAME);
4500 outFile.createNewFile();
4501 fos = new FileOutputStream(outFile);
4502 } catch (Exception e) {
4503 e.printStackTrace();
4504 }
4505 if (fos != null) {
4506 PrintWriter writer = new PrintWriter(fos);
4507
4508 writer.println(" ");
4509 writer.println("Debug logs: ");
4510 synchronized (sDumpLogs) {
4511 for (int i = 0; i < sDumpLogs.size(); i++) {
4512 writer.println(" " + sDumpLogs.get(i));
4513 }
4514 }
4515 writer.close();
4516 }
4517 try {
4518 if (fos != null) {
4519 fos.close();
4520 success = true;
4521 }
4522 } catch (IOException e) {
4523 e.printStackTrace();
4524 }
Michael Jurka43467462013-11-14 12:03:58 +01004525 return null;
Winson Chungede41292013-09-19 16:27:36 -07004526 }
Michael Jurka43467462013-11-14 12:03:58 +01004527 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004528 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004529 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004530}
Michael Jurka2763be32011-02-24 11:19:57 -08004531
Michael Jurkaabded662011-03-04 12:06:57 -08004532interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004533 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004534 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004535 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004536 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004537 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004538}
Dan Sandlerd5024042014-01-09 15:01:33 -05004539
4540interface DebugIntents {
4541 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4542 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004543}